view src/win32/7zip/7z/CPP/7zip/Archive/GZip/GZipIn.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line source
1 // Archive/GZipIn.cpp
3 #include "StdAfx.h"
5 #include "GZipIn.h"
7 #include "Common/Defs.h"
8 #include "Common/MyCom.h"
9 #include "Windows/Defs.h"
11 #include "../../Common/StreamUtils.h"
13 extern "C"
14 {
15 #include "../../../../C/7zCrc.h"
16 }
18 namespace NArchive {
19 namespace NGZip {
21 HRESULT CInArchive::ReadBytes(ISequentialInStream *inStream, void *data, UInt32 size)
22 {
23 RINOK(ReadStream_FALSE(inStream, data, size));
24 m_Position += size;
25 return S_OK;
26 }
28 HRESULT CInArchive::ReadByte(ISequentialInStream *inStream, Byte &value, UInt32 &crc)
29 {
30 RINOK(ReadBytes(inStream, &value, 1));
31 crc = CRC_UPDATE_BYTE(crc, value);
32 return S_OK;
33 }
35 HRESULT CInArchive::ReadUInt16(ISequentialInStream *inStream, UInt16 &value, UInt32 &crc)
36 {
37 value = 0;
38 for (int i = 0; i < 2; i++)
39 {
40 Byte b;
41 RINOK(ReadByte(inStream, b, crc));
42 value |= (UInt16(b) << (8 * i));
43 }
44 return S_OK;
45 }
47 HRESULT CInArchive::ReadUInt32(ISequentialInStream *inStream, UInt32 &value, UInt32 &crc)
48 {
49 value = 0;
50 for (int i = 0; i < 4; i++)
51 {
52 Byte b;
53 RINOK(ReadByte(inStream, b, crc));
54 value |= (UInt32(b) << (8 * i));
55 }
56 return S_OK;
57 }
59 HRESULT CInArchive::ReadZeroTerminatedString(ISequentialInStream *inStream, AString &resString, UInt32 &crc)
60 {
61 resString.Empty();
62 for (;;)
63 {
64 Byte c;
65 RINOK(ReadByte(inStream, c, crc));
66 if (c == 0)
67 return S_OK;
68 resString += char(c);
69 }
70 }
72 HRESULT CInArchive::ReadHeader(ISequentialInStream *inStream, CItem &item)
73 {
74 item.Clear();
75 m_Position = 0;
77 UInt16 signature;
78 UInt32 crc = CRC_INIT_VAL;;
79 RINOK(ReadUInt16(inStream, signature, crc));
80 if (signature != kSignature)
81 return S_FALSE;
83 RINOK(ReadByte(inStream, item.CompressionMethod, crc));
84 RINOK(ReadByte(inStream, item.Flags, crc));
85 RINOK(ReadUInt32(inStream, item.Time, crc));
86 RINOK(ReadByte(inStream, item.ExtraFlags, crc));
87 RINOK(ReadByte(inStream, item.HostOS, crc));
89 if (item.ExtraFieldIsPresent())
90 {
91 UInt16 extraSize;
92 RINOK(ReadUInt16(inStream, extraSize, crc));
93 item.Extra.SetCapacity(extraSize);
94 RINOK(ReadBytes(inStream, item.Extra, extraSize));
95 crc = CrcUpdate(crc, item.Extra, extraSize);
96 }
97 if (item.NameIsPresent())
98 RINOK(ReadZeroTerminatedString(inStream, item.Name, crc));
99 if (item.CommentIsPresent())
100 RINOK(ReadZeroTerminatedString(inStream, item.Comment, crc));
101 if (item.HeaderCRCIsPresent())
102 {
103 UInt16 headerCRC;
104 UInt32 dummy = 0;
105 RINOK(ReadUInt16(inStream, headerCRC, dummy));
106 if ((UInt16)CRC_GET_DIGEST(crc) != headerCRC)
107 return S_FALSE;
108 }
109 return S_OK;
110 }
112 HRESULT CInArchive::ReadPostHeader(ISequentialInStream *inStream, CItem &item)
113 {
114 UInt32 dummy = 0;
115 RINOK(ReadUInt32(inStream, item.FileCRC, dummy));
116 return ReadUInt32(inStream, item.UnPackSize32, dummy);
117 }
119 }}