2 * Copyright 2018 Nikolay Sivov for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "opc_private.h"
30 #include "wine/debug.h"
31 #include "wine/heap.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(msopc
);
36 struct local_file_header
44 DWORD compressed_size
;
45 DWORD uncompressed_size
;
50 struct data_descriptor
54 DWORD compressed_size
;
55 DWORD uncompressed_size
;
58 struct central_directory_header
67 DWORD compressed_size
;
68 DWORD uncompressed_size
;
73 WORD internal_attributes
;
74 DWORD external_attributes
;
75 DWORD local_file_offset
;
78 struct central_directory_end
86 DWORD directory_offset
;
91 #define CENTRAL_DIR_SIGNATURE 0x02014b50
92 #define LOCAL_HEADER_SIGNATURE 0x04034b50
93 #define DIRECTORY_END_SIGNATURE 0x06054b50
94 #define DATA_DESCRIPTOR_SIGNATURE 0x08074b50
99 USE_DATA_DESCRIPTOR
= 0x8,
104 struct central_directory_header
**files
;
111 HRESULT write_result
;
113 unsigned char input_buffer
[0x8000];
114 unsigned char output_buffer
[0x8000];
117 HRESULT
compress_create_archive(IStream
*output
, struct zip_archive
**out
)
119 struct zip_archive
*archive
;
123 if (!(archive
= heap_alloc(sizeof(*archive
))))
124 return E_OUTOFMEMORY
;
126 archive
->files
= NULL
;
127 archive
->file_size
= 0;
128 archive
->file_count
= 0;
129 archive
->write_result
= S_OK
;
131 archive
->output
= output
;
132 IStream_AddRef(archive
->output
);
133 archive
->position
= 0;
135 GetSystemTimeAsFileTime(&ft
);
136 FileTimeToDosDateTime(&ft
, &date
, &time
);
137 archive
->mtime
= date
<< 16 | time
;
144 static void compress_write(struct zip_archive
*archive
, void *data
, ULONG size
)
148 archive
->write_result
= IStream_Write(archive
->output
, data
, size
, &written
);
150 archive
->write_result
= E_FAIL
;
152 archive
->position
+= written
;
154 if (FAILED(archive
->write_result
))
155 WARN("Failed to write output %p, size %u, written %u, hr %#x.\n", data
, size
, written
, archive
->write_result
);
158 void compress_finalize_archive(struct zip_archive
*archive
)
160 struct central_directory_end dir_end
= { 0 };
163 dir_end
.directory_offset
= archive
->position
;
164 dir_end
.records_num
= archive
->file_count
;
165 dir_end
.records_total
= archive
->file_count
;
167 /* Directory entries */
168 for (i
= 0; i
< archive
->file_count
; ++i
)
170 compress_write(archive
, archive
->files
[i
], sizeof(*archive
->files
[i
]));
171 compress_write(archive
, archive
->files
[i
] + 1, archive
->files
[i
]->name_length
);
172 dir_end
.directory_size
+= archive
->files
[i
]->name_length
+ sizeof(*archive
->files
[i
]);
176 dir_end
.signature
= DIRECTORY_END_SIGNATURE
;
177 compress_write(archive
, &dir_end
, sizeof(dir_end
));
179 IStream_Release(archive
->output
);
181 for (i
= 0; i
< archive
->file_count
; i
++)
182 heap_free(archive
->files
[i
]);
183 heap_free(archive
->files
);
187 static void compress_write_content(struct zip_archive
*archive
, IStream
*content
,
188 OPC_COMPRESSION_OPTIONS options
, struct data_descriptor
*data_desc
)
196 data_desc
->crc32
= RtlComputeCrc32(0, NULL
, 0);
198 IStream_Seek(content
, move
, STREAM_SEEK_SET
, NULL
);
202 case OPC_COMPRESSION_NONE
:
203 level
= Z_NO_COMPRESSION
;
205 case OPC_COMPRESSION_NORMAL
:
206 level
= Z_DEFAULT_COMPRESSION
;
208 case OPC_COMPRESSION_MAXIMUM
:
209 level
= Z_BEST_COMPRESSION
;
211 case OPC_COMPRESSION_FAST
:
214 case OPC_COMPRESSION_SUPERFAST
:
215 level
= Z_BEST_SPEED
;
218 WARN("Unsupported compression options %d.\n", options
);
219 level
= Z_DEFAULT_COMPRESSION
;
222 memset(&z_str
, 0, sizeof(z_str
));
223 deflateInit2(&z_str
, level
, Z_DEFLATED
, -MAX_WBITS
, MAX_MEM_LEVEL
, Z_DEFAULT_STRATEGY
);
229 if (FAILED(hr
= IStream_Read(content
, archive
->input_buffer
, sizeof(archive
->input_buffer
), &num_read
)))
231 archive
->write_result
= hr
;
235 z_str
.avail_in
= num_read
;
236 z_str
.next_in
= archive
->input_buffer
;
237 data_desc
->crc32
= RtlComputeCrc32(data_desc
->crc32
, archive
->input_buffer
, num_read
);
239 flush
= sizeof(archive
->input_buffer
) > num_read
? Z_FINISH
: Z_NO_FLUSH
;
245 z_str
.avail_out
= sizeof(archive
->output_buffer
);
246 z_str
.next_out
= archive
->output_buffer
;
248 if ((ret
= deflate(&z_str
, flush
)))
249 WARN("Failed to deflate, ret %d.\n", ret
);
250 have
= sizeof(archive
->output_buffer
) - z_str
.avail_out
;
251 compress_write(archive
, archive
->output_buffer
, have
);
252 } while (z_str
.avail_out
== 0);
253 } while (flush
!= Z_FINISH
);
257 data_desc
->compressed_size
= z_str
.total_out
;
258 data_desc
->uncompressed_size
= z_str
.total_in
;
261 HRESULT
compress_add_file(struct zip_archive
*archive
, const WCHAR
*path
,
262 IStream
*content
, OPC_COMPRESSION_OPTIONS options
)
264 struct central_directory_header
*entry
;
265 struct local_file_header local_header
;
266 struct data_descriptor data_desc
;
267 DWORD local_header_pos
;
271 len
= WideCharToMultiByte(CP_ACP
, 0, path
, -1, NULL
, 0, NULL
, NULL
);
272 if (!(name
= heap_alloc(len
)))
273 return E_OUTOFMEMORY
;
274 WideCharToMultiByte(CP_ACP
, 0, path
, -1, name
, len
, NULL
, NULL
);
277 local_header
.signature
= LOCAL_HEADER_SIGNATURE
;
278 local_header
.version
= VERSION
;
279 local_header
.flags
= USE_DATA_DESCRIPTOR
;
280 local_header
.method
= 8; /* Z_DEFLATED */
281 local_header
.mtime
= archive
->mtime
;
282 local_header
.crc32
= 0;
283 local_header
.compressed_size
= 0;
284 local_header
.uncompressed_size
= 0;
285 local_header
.name_length
= len
- 1;
286 local_header
.extra_length
= 0;
288 local_header_pos
= archive
->position
;
290 compress_write(archive
, &local_header
, sizeof(local_header
));
291 compress_write(archive
, name
, local_header
.name_length
);
294 compress_write_content(archive
, content
, options
, &data_desc
);
296 /* Data descriptor */
297 data_desc
.signature
= DATA_DESCRIPTOR_SIGNATURE
;
298 compress_write(archive
, &data_desc
, sizeof(data_desc
));
300 if (FAILED(archive
->write_result
))
301 return archive
->write_result
;
303 /* Set directory entry */
304 if (!(entry
= heap_alloc_zero(sizeof(*entry
) + local_header
.name_length
)))
307 return E_OUTOFMEMORY
;
310 entry
->signature
= CENTRAL_DIR_SIGNATURE
;
311 entry
->version
= local_header
.version
;
312 entry
->min_version
= local_header
.version
;
313 entry
->flags
= local_header
.flags
;
314 entry
->method
= local_header
.method
;
315 entry
->mtime
= local_header
.mtime
;
316 entry
->crc32
= data_desc
.crc32
;
317 entry
->compressed_size
= data_desc
.compressed_size
;
318 entry
->uncompressed_size
= data_desc
.uncompressed_size
;
319 entry
->name_length
= local_header
.name_length
;
320 entry
->local_file_offset
= local_header_pos
;
321 memcpy(entry
+ 1, name
, entry
->name_length
);
324 if (!opc_array_reserve((void **)&archive
->files
, &archive
->file_size
, archive
->file_count
+ 1,
325 sizeof(*archive
->files
)))
328 return E_OUTOFMEMORY
;
331 archive
->files
[archive
->file_count
++] = entry
;