PERFORCE change 154398 for review
Anselm Strauss
strauss at FreeBSD.org
Tue Dec 9 08:00:59 PST 2008
http://perforce.freebsd.org/chv.cgi?CH=154398
Change 154398 by strauss at strauss_silversurfer on 2008/12/09 16:00:15
Added deflate compression, still compile problems (linking)
Affected files ...
.. //depot/projects/soc2008/strauss_libarchive/libarchive/archive_write_set_format_zip.c#42 edit
Differences ...
==== //depot/projects/soc2008/strauss_libarchive/libarchive/archive_write_set_format_zip.c#42 (text+ko) ====
@@ -76,6 +76,11 @@
#define ZIP_VERSION_BY 0x0314 /* Made by UNIX, using ZIP version 2.0. */
#define ZIP_FLAGS 0x08 /* Flagging bit 3 (count from 0) for using data descriptor. */
+enum compression {
+ COMPRESSION_STORE = 0,
+ COMPRESSION_DEFLATE = 6
+};
+
static ssize_t archive_write_zip_data(struct archive_write *, const void *buff, size_t s);
static int archive_write_zip_finish(struct archive_write *);
static int archive_write_zip_destroy(struct archive_write *);
@@ -177,11 +182,6 @@
char comment_length[2];
};
-static enum compression {
- COMPRESSION_STORE = 0,
- COMPRESSION_DEFLATE = 6
-};
-
int
archive_write_set_format_zip(struct archive *_a)
{
@@ -294,7 +294,7 @@
zip_encode(path_length(entry), &h.filename_length, sizeof(h.filename_length));
zip_encode(sizeof(e), &h.extra_length, sizeof(h.extra_length));
- if (zip->compression = COMPRESSION_STORE) {
+ if (zip->compression == COMPRESSION_STORE) {
/* Setting compressed and uncompressed sizes even when specification says
* to set to zero when using data descriptors. Otherwise the end of the
* data for an entry is rather difficult to find. */
@@ -342,35 +342,62 @@
struct zip *zip = a->format_data;
struct zip_file_header_link *l = zip->central_directory_end;
z_stream stream;
+ size_t chunk = sizeof(*buff);
+ unsigned char buff_out[chunk];
if (s > zip->remaining_data_bytes)
s = zip->remaining_data_bytes;
+
+ if (s == 0) return 0;
switch (zip->compression) {
case COMPRESSION_STORE:
ret = (a->compressor.write)(a, buff, s);
+ if (ret < 0) return (ret);
+ zip->written_bytes += s;
+ zip->remaining_data_bytes -= s;
+ l->crc32 = crc32(l->crc32, buff, s);
+ return (ret);
case COMPRESSION_DEFLATE:
- deflateInit2(
- stream,
-
- );
+ stream.zalloc = Z_NULL;
+ stream.zfree = Z_NULL;
+ stream.opaque = Z_NULL;
+ ret = deflateInit(&stream, Z_DEFAULT_COMPRESSION);
+ if (ret != Z_OK) return (ARCHIVE_FATAL);
+ stream.next_in = (unsigned char*) buff;
+ stream.avail_in = s;
+ do {
+ stream.next_out = buff_out;
+ stream.avail_out = chunk;
+ ret = deflate(&stream, Z_FINISH);
+ if (ret == Z_STREAM_ERROR) {
+ deflateEnd(&stream);
+ return (ARCHIVE_FATAL);
+ }
+ ret = (a->compressor.write)(a, buff_out, stream.avail_out);
+ if (ret < 0) {
+ deflateEnd(&stream);
+ return (ret);
+ }
+ zip->written_bytes += ret;
+ } while (stream.avail_out == 0);
+ zip->remaining_data_bytes -= s;
+ l->crc32 = crc32(l->crc32, buff, s);
+ deflateEnd(&stream);
+ return (s);
+ default:
+
+ archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "Invalid ZIP compression type");
+ return ARCHIVE_FATAL;
}
/* TODO: set compressed size in data descriptor and local file header link */
- if (ret >= 0) {
- zip->written_bytes += s;
- zip->remaining_data_bytes -= s;
- l->crc32 = crc32(l->crc32, buff, s);
- return (s);
- } else {
- return (ret);
- }
}
static int
@@ -434,7 +461,7 @@
zip_encode(dos_time(archive_entry_mtime(l->entry)), &h.timedate, sizeof(h.timedate));
zip_encode(l->crc32, &h.crc32, sizeof(h.crc32));
/* TODO: write compressed size */
- zip_encode(archive_entry_size(entry), &h.uncompressed_size, sizeof(h.uncompressed_size));
+ zip_encode(archive_entry_size(l->entry), &h.uncompressed_size, sizeof(h.uncompressed_size));
zip_encode(path_length(l->entry), &h.filename_length, sizeof(h.filename_length));
zip_encode(sizeof(e), &h.extra_length, sizeof(h.extra_length));
mode = archive_entry_mode(l->entry);
@@ -588,7 +615,7 @@
set_compression(struct archive_write *a, enum compression compression)
{
/* TODO: check archive state, should not switch between header and data */
- /* TODO: check if valid compression? */
- a->format_data->compression = compression;
+ struct zip *zip = a->format_data;
+ zip->compression = compression;
}
More information about the p4-projects
mailing list