File: NMR_PortableZIPWriter.cpp

package info (click to toggle)
lib3mf 1.8.1%2Bds-6
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 12,652 kB
  • sloc: cpp: 34,988; ansic: 4,255; sh: 109; makefile: 12
file content (343 lines) | stat: -rw-r--r-- 13,595 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*++

Copyright (C) 2018 3MF Consortium

All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Abstract:

NMR_PortableZIPWriter.cpp implements a portable and fast writer of ZIP files

--*/

#include "Common/Platform/NMR_PortableZIPWriter.h"
#include "Common/Platform/NMR_ExportStream_ZIP.h"
#include "Common/NMR_Exception.h"
#include "Common/NMR_StringUtils.h"

namespace NMR {

	CPortableZIPWriter::CPortableZIPWriter(_In_ PExportStream pExportStream, _In_ nfBool bWriteZIP64)
	{
		if (pExportStream.get() == nullptr)
			throw CNMRException(NMR_ERROR_INVALIDPARAM);

		m_pExportStream = pExportStream;
		m_nCurrentEntryKey = 0;
		m_nNextEntryKey = 1;
		m_pCurrentEntry = nullptr;
		m_bIsFinished = false;
		m_bWriteZIP64 = bWriteZIP64;

		if (m_bWriteZIP64) {
			m_nVersionMade = ZIPFILEVERSIONNEEDEDZIP64;
			m_nVersionNeeded = ZIPFILEVERSIONNEEDEDZIP64;
		}
		else {
			m_nVersionMade = ZIPFILEVERSIONNEEDED;
			m_nVersionNeeded = ZIPFILEVERSIONNEEDED;
		}

		if (m_pExportStream->getPosition() != 0)
			throw CNMRException(NMR_ERROR_EXPORTSTREAMNOTEMPTY);

	}

	CPortableZIPWriter::~CPortableZIPWriter()
	{
		if (!m_bIsFinished)
			writeDirectory();
	}

	PExportStream CPortableZIPWriter::createEntry(_In_ const std::string sName, _In_ nfTimeStamp nUnixTimeStamp)
	{
		if (m_bIsFinished)
			throw CNMRException(NMR_ERROR_ZIPALREADYFINISHED);
		// Finish old entry state
		closeEntry();

		// Initialize new entry state
		m_nCurrentEntryKey = m_nNextEntryKey;
		m_nNextEntryKey++;
		if (m_nNextEntryKey >= ZIPFILEMAXENTRIES)
			throw CNMRException(NMR_ERROR_ZIPENTRYOVERFLOW);

		// Convert Name to UTF8
		std::string sFilteredName = fnRemoveLeadingPathDelimiter(sName);
		std::string sUTF8Name = sFilteredName;
		if (sUTF8Name.length() > ZIPFILEMAXFILENAMELENGTH)
			throw CNMRException(NMR_ERRORINVALIDZIPNAME);
		nfUint32 nNameLength = (nfUint32)sUTF8Name.length();

		// Convert Timestamp to File Date
		nfUint32 nFileDate = 0;
		nfUint16 nLastModTime = nFileDate % 65536;
		nfUint16 nLastModDate = nFileDate / 65536;

		// Write local file header
		ZIPLOCALFILEHEADER LocalHeader;
		LocalHeader.m_nSignature = ZIPFILEHEADERSIGNATURE;
		LocalHeader.m_nVersion = m_nVersionNeeded;
		LocalHeader.m_nGeneralPurposeFlags = 0;
		LocalHeader.m_nCompressionMethod = ZIPFILECOMPRESSION_DEFLATED;
		LocalHeader.m_nLastModTime = nLastModTime;
		LocalHeader.m_nLastModDate = nLastModDate;
		LocalHeader.m_nCRC32 = 0;
		LocalHeader.m_nCompressedSize = 0;
		LocalHeader.m_nUnCompressedSize = 0;
		LocalHeader.m_nFileNameLength = nNameLength;
		LocalHeader.m_nExtraFieldLength = 0;// sizeof(ZIPLOCALFILEADDITIONALHEADER) + sizeof(ZIPLOCALFILEEXTENDEDINFORMATIONFIELD);

		if (m_bWriteZIP64) {
			LocalHeader.m_nExtraFieldLength += sizeof(ZIP64EXTRAINFORMATIONFIELD);
		}

		ZIP64EXTRAINFORMATIONFIELD zip64ExtraInformation;
		zip64ExtraInformation.m_nTag = ZIPFILEDATAZIP64EXTENDEDINFORMATIONEXTRAFIELD;
		zip64ExtraInformation.m_nFieldSize = sizeof(ZIP64EXTRAINFORMATIONFIELD) - 4;
		zip64ExtraInformation.m_nCompressedSize = 0;
		zip64ExtraInformation.m_nUncompressedSize = 0;

		// Write data to ZIP stream
		nfUint64 nFilePosition = m_pExportStream->getPosition();
		m_pExportStream->writeBuffer(&LocalHeader, sizeof(LocalHeader));
		m_pExportStream->writeBuffer(sUTF8Name.c_str(), nNameLength);
		nfUint64 nExtInfoPosition = m_pExportStream->getPosition();
		if (m_bWriteZIP64)
			m_pExportStream->writeBuffer(&zip64ExtraInformation, sizeof(zip64ExtraInformation));

		nfUint64 nDataPosition = m_pExportStream->getPosition();

		// create list entry
		m_pCurrentEntry = std::make_shared<CPortableZIPWriterEntry>(sUTF8Name, nLastModTime, nLastModDate, nFilePosition, nExtInfoPosition, nDataPosition);
		m_Entries.push_back(m_pCurrentEntry);

		// Return new ZIP Entry stream
		m_pCurrentStream = std::make_shared<CExportStream_ZIP>(this, m_nCurrentEntryKey);
		return m_pCurrentStream;
	}

	void CPortableZIPWriter::closeEntry()
	{
		if (m_bIsFinished)
			throw CNMRException(NMR_ERROR_ZIPALREADYFINISHED);

		if (m_pCurrentEntry.get() != nullptr) {
			// finish current stream writing
			CExportStream_ZIP * pZipStream = dynamic_cast<CExportStream_ZIP *>(m_pCurrentStream.get());
			if (pZipStream == nullptr)
				throw CNMRException(NMR_ERROR_NOEXPORTSTREAM);
			pZipStream->flushZIPStream();

			// Write CRC and Size
			ZIPLOCALFILEDESCRIPTOR FileDescriptor;
			FileDescriptor.m_nCRC32 = m_pCurrentEntry->getCRC32();
			if (m_bWriteZIP64) {
				FileDescriptor.m_nCompressedSize =0xFFFFFFFF;
				FileDescriptor.m_nUnCompressedSize = 0xFFFFFFFF;
			}
			else {
				if ((m_pCurrentEntry->getCompressedSize() > ZIPFILEMAXIMUMSIZENON64) ||
					(m_pCurrentEntry->getUncompressedSize() > ZIPFILEMAXIMUMSIZENON64))
					throw CNMRException(NMR_ERROR_ZIPENTRYNON64_TOOLARGE);
				FileDescriptor.m_nCompressedSize = (nfUint32)m_pCurrentEntry->getCompressedSize();
				FileDescriptor.m_nUnCompressedSize = (nfUint32)m_pCurrentEntry->getUncompressedSize();
			}

			ZIP64EXTRAINFORMATIONFIELD zip64ExtraInformation;
			zip64ExtraInformation.m_nTag = ZIPFILEDATAZIP64EXTENDEDINFORMATIONEXTRAFIELD;
			zip64ExtraInformation.m_nFieldSize = sizeof(ZIP64EXTRAINFORMATIONFIELD) - 4;
			zip64ExtraInformation.m_nCompressedSize = m_pCurrentEntry->getCompressedSize();
			zip64ExtraInformation.m_nUncompressedSize = m_pCurrentEntry->getUncompressedSize();
			
			// Write File Descriptor to file
			m_pExportStream->seekPosition(m_pCurrentEntry->getFilePosition() + ZIPFILEDESCRIPTOROFFSET, true);
			m_pExportStream->writeBuffer(&FileDescriptor, sizeof(FileDescriptor));

			if (m_bWriteZIP64) {
				// Write Extra Information to file
				m_pExportStream->seekPosition(m_pCurrentEntry->getExtInfoPosition(), true);
				m_pExportStream->writeBuffer(&zip64ExtraInformation, sizeof(zip64ExtraInformation));
			}

			// Reset file pointer
			m_pExportStream->seekFromEnd(0, true);
		}

		m_pCurrentStream = nullptr;
		m_pCurrentEntry = nullptr;
		m_nCurrentEntryKey = 0;
	}

	void CPortableZIPWriter::calculateChecksum(_In_ nfUint32 nEntryKey, _In_ const void * pBuffer, _In_ nfUint32 cbUncompressedBytes)
	{
		if (m_pCurrentEntry.get() == nullptr)
			throw CNMRException(NMR_ERROR_INVALIDZIPENTRY);

		if (pBuffer == nullptr)
			throw CNMRException(NMR_ERROR_INVALIDPARAM);

		if (nEntryKey != m_nCurrentEntryKey)
			throw CNMRException(NMR_ERROR_INVALIDZIPENTRYKEY);

		if (cbUncompressedBytes > 0) {
			m_pCurrentEntry->calculateChecksum(pBuffer, cbUncompressedBytes);
			m_pCurrentEntry->increaseUncompressedSize(cbUncompressedBytes);
		}
	}


	void CPortableZIPWriter::writeDeflatedBuffer(_In_ nfUint32 nEntryKey, _In_ const void * pBuffer, _In_ nfUint32 cbCompressedBytes)
	{
		if (m_pCurrentEntry.get() == nullptr)
			throw CNMRException(NMR_ERROR_INVALIDZIPENTRY);

		if (pBuffer == nullptr)
			throw CNMRException(NMR_ERROR_INVALIDPARAM);

		if (nEntryKey != m_nCurrentEntryKey)
			throw CNMRException(NMR_ERROR_INVALIDZIPENTRYKEY);

		if (cbCompressedBytes > 0) {
			m_pExportStream->writeBuffer(pBuffer, cbCompressedBytes);
			m_pCurrentEntry->increaseCompressedSize(cbCompressedBytes);
		}
	}

	nfUint64 CPortableZIPWriter::getCurrentSize(_In_ nfUint32 nEntryKey)
	{
		if (m_pCurrentEntry.get() == nullptr)
			throw CNMRException(NMR_ERROR_INVALIDZIPENTRY);

		if (nEntryKey != m_nCurrentEntryKey)
			throw CNMRException(NMR_ERROR_INVALIDZIPENTRYKEY);
		return m_pCurrentEntry->getUncompressedSize();
	}


	void CPortableZIPWriter::writeDirectory()
	{
		closeEntry();

		nfUint64 nCentralDirStartPos = m_pExportStream->getPosition();
		auto iIterator = m_Entries.begin();
		while (iIterator != m_Entries.end()) {
			PPortableZIPWriterEntry pEntry = *iIterator;
			std::string sUTF8Name = pEntry->getUTF8Name();
			nfUint32 nNameLength = (nfUint32)sUTF8Name.length();
			
			ZIPCENTRALDIRECTORYFILEHEADER DirectoryHeader;
			DirectoryHeader.m_nSignature = ZIPFILECENTRALHEADERSIGNATURE;
			DirectoryHeader.m_nVersionMade = m_nVersionMade;
			DirectoryHeader.m_nVersionNeeded = m_nVersionNeeded;
			DirectoryHeader.m_nGeneralPurposeFlags = 0;
			DirectoryHeader.m_nCompressionMethod = ZIPFILECOMPRESSION_DEFLATED;
			DirectoryHeader.m_nLastModTime = pEntry->getLastModTime();
			DirectoryHeader.m_nLastModDate = pEntry->getLastModDate();
			DirectoryHeader.m_nCRC32 = pEntry->getCRC32();
			DirectoryHeader.m_nCompressedSize = 0;
			DirectoryHeader.m_nUnCompressedSize = 0;
			DirectoryHeader.m_nFileNameLength = nNameLength;
			DirectoryHeader.m_nExtraFieldLength = 0;
			DirectoryHeader.m_nFileCommentLength = 0;
			DirectoryHeader.m_nDiskNumberStart = 0;
			DirectoryHeader.m_nInternalFileAttributes = 0;
			DirectoryHeader.m_nExternalFileAttributes = ZIPFILEEXTERNALFILEATTRIBUTES;
			DirectoryHeader.m_nRelativeOffsetOfLocalHeader = (nfUint32) pEntry->getFilePosition();

			nfUint64 nRelativeOffsetOfLocalHeader = pEntry->getFilePosition();
			if (m_bWriteZIP64) {
				DirectoryHeader.m_nCompressedSize = 0xFFFFFFFF;
				DirectoryHeader.m_nUnCompressedSize = 0xFFFFFFFF;
				DirectoryHeader.m_nExtraFieldLength += sizeof(ZIP64EXTRAINFORMATIONFIELD) + sizeof(nRelativeOffsetOfLocalHeader);
				DirectoryHeader.m_nRelativeOffsetOfLocalHeader = 0xFFFFFFFF;
			}
			else {
				if ((m_pCurrentEntry->getCompressedSize() > ZIPFILEMAXIMUMSIZENON64) ||
					(m_pCurrentEntry->getUncompressedSize() > ZIPFILEMAXIMUMSIZENON64))
					throw CNMRException(NMR_ERROR_ZIPENTRYNON64_TOOLARGE);
				DirectoryHeader.m_nCompressedSize = (nfUint32)pEntry->getCompressedSize();
				DirectoryHeader.m_nUnCompressedSize = (nfUint32)pEntry->getUncompressedSize();
			}

			m_pExportStream->writeBuffer(&DirectoryHeader, (nfUint64) sizeof(DirectoryHeader));
			m_pExportStream->writeBuffer(sUTF8Name.c_str(), nNameLength);
			
			if (m_bWriteZIP64) {
				ZIP64EXTRAINFORMATIONFIELD zip64ExtraInformation;
				zip64ExtraInformation.m_nTag = ZIPFILEDATAZIP64EXTENDEDINFORMATIONEXTRAFIELD;
				zip64ExtraInformation.m_nFieldSize = sizeof(ZIP64EXTRAINFORMATIONFIELD) - 4 + sizeof(nRelativeOffsetOfLocalHeader);
				zip64ExtraInformation.m_nCompressedSize = pEntry->getCompressedSize();
				zip64ExtraInformation.m_nUncompressedSize = pEntry->getUncompressedSize();

				m_pExportStream->writeBuffer(&zip64ExtraInformation, sizeof(zip64ExtraInformation));
				m_pExportStream->writeBuffer(&nRelativeOffsetOfLocalHeader, sizeof(nRelativeOffsetOfLocalHeader));
			}
			
			iIterator++;
		}

		nfUint64 nEndOfCentralDir64StartPos = m_pExportStream->getPosition();

		// [ZIP64 end of central directory record]
		ZIP64ENDOFCENTRALDIRHEADER EndHeader64;
		EndHeader64.m_nSignature = ZIP64FILEENDOFCENTRALDIRRECORDSIGNATURE;
		EndHeader64.m_nEndOfCentralDirHeaderRecord = (nfUint64)(sizeof(ZIP64ENDOFCENTRALDIRHEADER) - 12);
		EndHeader64.m_nVersionMade = m_nVersionMade;
		EndHeader64.m_nVersionNeeded = m_nVersionNeeded;
		EndHeader64.m_nNumberOfDisk = 0;
		EndHeader64.m_nNumberOfDiskOfCentralDirectory = 0;
		EndHeader64.m_nTotalNumberOfEntriesOnThisDisk = m_Entries.size();
		EndHeader64.m_nTotalNumberOfEntriesInCentralDirectory = m_Entries.size();
		EndHeader64.m_nSizeOfCentralDirectory = (nfUint64)(m_pExportStream->getPosition() - nCentralDirStartPos);
		EndHeader64.m_nOffsetOfCentralDirectoryWithRespectToDisk = nCentralDirStartPos;
		
		// [ZIP64 end of central directory locator]
		ZIP64ENDOFCENTRALDIRLOCATOR EndLocator64;
		EndLocator64.m_nSignature = ZIP64FILEENDOFCENTRALDIRLOCATORSIGNATURE;
		EndLocator64.m_nNumberOfDiskWithStartOfZIP64EOCentralDir = 0;
		EndLocator64.m_nTotalNumberOfDisk = 1;
		EndLocator64.m_nRelativeOffset = nEndOfCentralDir64StartPos;
		
		ZIPENDOFCENTRALDIRHEADER EndHeader;
		EndHeader.m_nSignature = ZIPFILEENDOFCENTRALDIRSIGNATURE;
		EndHeader.m_nNumberOfDisk = 0;
		EndHeader.m_nRelativeNumberOfDisk = 0;
		EndHeader.m_nNumberOfEntriesOfDisk = (nfUint16) m_Entries.size();
		EndHeader.m_nNumberOfEntriesOfDirectory = (nfUint16)m_Entries.size();
		EndHeader.m_nSizeOfCentralDirectory = (nfUint32)(m_pExportStream->getPosition() - nCentralDirStartPos);
		EndHeader.m_nOffsetOfCentralDirectory = (nfUint32)nCentralDirStartPos;
		EndHeader.m_nCommentLength = 0;

		if (m_bWriteZIP64) {
			EndHeader.m_nOffsetOfCentralDirectory = 0xFFFFFFFF;
			m_pExportStream->writeBuffer(&EndHeader64, sizeof(EndHeader64));
			m_pExportStream->writeBuffer(&EndLocator64, sizeof(EndLocator64));
		}
		m_pExportStream->writeBuffer(&EndHeader, (nfUint64) sizeof(EndHeader));

		m_bIsFinished = true;
	}


}