commit 343d961f4055f69a61bd9749d498e61234339bfc Author: Zack Weinberg zackw@cmu.edu Date: Mon Apr 23 15:40:28 2012 -0700
Const- and size_t-correctness, minor other cleanups for crc32 and zpack. --- src/steg/crc32.cc | 7 +- src/steg/crc32.h | 18 +---- src/steg/zpack.cc | 235 ++++++++++++++++------------------------------------- src/steg/zpack.h | 13 ++-- 4 files changed, 83 insertions(+), 190 deletions(-)
diff --git a/src/steg/crc32.cc b/src/steg/crc32.cc index 7fdc847..1b18544 100644 --- a/src/steg/crc32.cc +++ b/src/steg/crc32.cc @@ -1,3 +1,4 @@ +#include "util.h" #include "crc32.h"
#define CRC32C(c,d) (c=(c>>8)^crc_c[(c^(d))&0xFF]) @@ -69,8 +70,9 @@ static const unsigned int crc_c[256] = { 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d, };
- -unsigned int generate_crc32c(char *buffer, size_t length) { +unsigned int +generate_crc32c(const char *buffer, size_t length) +{ size_t i; unsigned int crc32 = ~0L;
@@ -79,4 +81,3 @@ unsigned int generate_crc32c(char *buffer, size_t length) { } return ~crc32; } - diff --git a/src/steg/crc32.h b/src/steg/crc32.h index 780e7bd..0a1ee03 100644 --- a/src/steg/crc32.h +++ b/src/steg/crc32.h @@ -1,18 +1,6 @@ -#ifndef __crc32cr_table_h__ -#define __crc32cr_table_h__ +#ifndef _CRC32_H +#define _CRC32_H
-#ifdef HAVE_CONFIG_H -# include "config.h" -#endif - -#include <sys/types.h> - -#if defined HAVE_STDINT_H -# include <stdint.h> -#elif defined HAVE_INTTYPES_H -# include <inttypes.h> -#endif - -unsigned int generate_crc32c(char *string, size_t length); +unsigned int generate_crc32c(const char *string, size_t length);
#endif diff --git a/src/steg/zpack.cc b/src/steg/zpack.cc index 63aa412..c3c650f 100644 --- a/src/steg/zpack.cc +++ b/src/steg/zpack.cc @@ -1,19 +1,7 @@ -#include <stdio.h> -#include <string.h> -#include <assert.h> -#include <time.h> -#include <stdlib.h> -#include "zlib.h" +#include "util.h" #include "zpack.h" - - -#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__) -# include <fcntl.h> -# include <io.h> -# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) -#else -# define SET_BINARY_MODE(file) -#endif +#include "zlib.h" +#include "crc32.h"
#define CHUNK 16384
@@ -24,15 +12,18 @@ version of the library linked do not match, or Z_ERRNO if there is an error reading or writing the files. */
- -int def(char *source, int slen, char *dest, int dlen, int level) +ssize_t +def(const char *source, size_t slen, char *dest, size_t dlen, int level) { int ret, flush; - unsigned have; + size_t have; z_stream strm; unsigned char in[CHUNK]; unsigned char out[CHUNK]; - int dlen_orig = dlen; + size_t dlen_orig = dlen; + + if (slen > SIZE_T_CEILING || dlen > SIZE_T_CEILING) + return -1;
/* allocate deflate state */ strm.zalloc = Z_NULL; @@ -44,7 +35,6 @@ int def(char *source, int slen, char *dest, int dlen, int level)
/* compress until end of file */ do { - if (slen > CHUNK) strm.avail_in = CHUNK; else @@ -63,30 +53,28 @@ int def(char *source, int slen, char *dest, int dlen, int level) strm.avail_out = CHUNK; strm.next_out = out; ret = deflate(&strm, flush); /* no bad return value */ - assert(ret != Z_STREAM_ERROR); /* state not clobbered */ + log_assert(ret != Z_STREAM_ERROR); /* state not clobbered */ have = CHUNK - strm.avail_out;
- if ((unsigned int) dlen < have) { - fprintf(stderr, "dest buf too small!\n"); - return Z_ERRNO; + if (dlen < have) { + log_warn("dest buf too small - have %lu, need %lu", + (unsigned long)dlen, (unsigned long)have); + return Z_ERRNO; }
memcpy(dest, out, have); dest += have; dlen = dlen - have; } while (strm.avail_out == 0); - assert(strm.avail_in == 0); /* all input will be used */ + log_assert(strm.avail_in == 0); /* all input will be used */
/* done when last data in file processed */ } while (flush != Z_FINISH); - assert(ret == Z_STREAM_END); /* stream will be complete */ + log_assert(ret == Z_STREAM_END); /* stream will be complete */
/* clean up and return */ - (void)deflateEnd(&strm); - - printf("hello here...\n"); + deflateEnd(&strm); return (dlen_orig - dlen); - // return Z_OK; }
/* Decompress from file source to file dest until stream ends or EOF. @@ -96,18 +84,18 @@ int def(char *source, int slen, char *dest, int dlen, int level) the version of the library linked do not match, or Z_ERRNO if there is an error reading or writing the files. */
- - - -int inf(char *source, int slen, char *dest, int dlen) +ssize_t +inf(const char *source, size_t slen, char *dest, size_t dlen) { int ret; - unsigned have; + size_t have; z_stream strm; unsigned char in[CHUNK]; unsigned char out[CHUNK]; - int dlen_orig = dlen; + size_t dlen_orig = dlen;
+ if (slen > SIZE_T_CEILING || dlen > SIZE_T_CEILING) + return -1;
/* allocate inflate state */ strm.zalloc = Z_NULL; @@ -121,10 +109,9 @@ int inf(char *source, int slen, char *dest, int dlen)
/* decompress until deflate stream ends or end of file */ do { - if (slen == 0) break; - + if (slen > CHUNK) strm.avail_in = CHUNK; else @@ -133,9 +120,6 @@ int inf(char *source, int slen, char *dest, int dlen) memcpy(in, source, strm.avail_in); slen = slen - strm.avail_in; source = source + strm.avail_in; - - - strm.next_in = in;
/* run inflate() on input until output buffer not full */ @@ -143,21 +127,20 @@ int inf(char *source, int slen, char *dest, int dlen) strm.avail_out = CHUNK; strm.next_out = out; ret = inflate(&strm, Z_NO_FLUSH); - assert(ret != Z_STREAM_ERROR); /* state not clobbered */ + log_assert(ret != Z_STREAM_ERROR); /* state not clobbered */ switch (ret) { case Z_NEED_DICT: - ret = Z_DATA_ERROR; /* and fall through */ case Z_DATA_ERROR: case Z_MEM_ERROR: - (void)inflateEnd(&strm); - return ret; + inflateEnd(&strm); + return ret; } have = CHUNK - strm.avail_out;
- - if ((unsigned int) dlen < have) { - fprintf(stderr, "dest buf too small!\n"); - return Z_ERRNO; + if (dlen < have) { + log_warn("dest buf too small - have %lu, need %lu", + (unsigned long)dlen, (unsigned long)have); + return Z_ERRNO; }
memcpy(dest, out, have); @@ -170,57 +153,27 @@ int inf(char *source, int slen, char *dest, int dlen) } while (ret != Z_STREAM_END);
/* clean up and return */ - (void)inflateEnd(&strm); + inflateEnd(&strm);
if (ret == Z_STREAM_END) return dlen_orig - dlen; return Z_DATA_ERROR; }
-/* report a zlib or i/o error */ -void zerr(int ret) - -{ - fputs("zpipe: ", stderr); - switch (ret) { - case Z_ERRNO: - if (ferror(stdin)) - fputs("error reading stdin\n", stderr); - if (ferror(stdout)) - fputs("error writing stdout\n", stderr); - break; - case Z_STREAM_ERROR: - fputs("invalid compression level\n", stderr); - break; - case Z_DATA_ERROR: - fputs("invalid or incomplete deflate data\n", stderr); - break; - case Z_MEM_ERROR: - fputs("out of memory\n", stderr); - break; - case Z_VERSION_ERROR: - fputs("zlib version mismatch!\n", stderr); - } -} - - - - - - - - /* assumes that we know there is exactly 10 bytes of gzip header */
-int gzInflate(char *source, int slen, char *dest, int dlen) +ssize_t +gzInflate(const char *source, size_t slen, char *dest, size_t dlen) { int ret; - unsigned have; + size_t have; z_stream strm; unsigned char in[CHUNK]; unsigned char out[CHUNK]; - int dlen_orig = dlen; + size_t dlen_orig = dlen;
+ if (slen > SIZE_T_CEILING || dlen > SIZE_T_CEILING) + return -1;
/* allocate inflate state */ strm.zalloc = Z_NULL; @@ -229,7 +182,6 @@ int gzInflate(char *source, int slen, char *dest, int dlen) strm.avail_in = 0; strm.next_in = Z_NULL;
- ret = inflateInit2(&strm, -MAX_WBITS); if (ret != Z_OK) return ret; @@ -239,10 +191,9 @@ int gzInflate(char *source, int slen, char *dest, int dlen)
/* decompress until deflate stream ends or end of file */ do { - if (slen == 0) break; - + if (slen > CHUNK) strm.avail_in = CHUNK; else @@ -251,9 +202,6 @@ int gzInflate(char *source, int slen, char *dest, int dlen) memcpy(in, source, strm.avail_in); slen = slen - strm.avail_in; source = source + strm.avail_in; - - - strm.next_in = in;
/* run inflate() on input until output buffer not full */ @@ -261,20 +209,21 @@ int gzInflate(char *source, int slen, char *dest, int dlen) strm.avail_out = CHUNK; strm.next_out = out; ret = inflate(&strm, Z_NO_FLUSH); - assert(ret != Z_STREAM_ERROR); /* state not clobbered */ + log_assert(ret != Z_STREAM_ERROR); /* state not clobbered */ switch (ret) { case Z_NEED_DICT: - ret = Z_DATA_ERROR; /* and fall through */ + ret = Z_DATA_ERROR; /* and fall through */ case Z_DATA_ERROR: case Z_MEM_ERROR: - (void)inflateEnd(&strm); - return ret; + inflateEnd(&strm); + return ret; } have = CHUNK - strm.avail_out;
- if ((unsigned int) dlen < have) { - fprintf(stderr, "dest buf too small!\n"); - return Z_ERRNO; + if (dlen < have) { + log_warn("dest buf too small - have %lu, need %lu", + (unsigned long)dlen, (unsigned long)have); + return Z_ERRNO; }
memcpy(dest, out, have); @@ -287,45 +236,43 @@ int gzInflate(char *source, int slen, char *dest, int dlen) } while (ret != Z_STREAM_END);
/* clean up and return */ - (void)inflateEnd(&strm); + inflateEnd(&strm);
if (ret == Z_STREAM_END) return dlen_orig - dlen; return Z_DATA_ERROR; }
- - - - - - -int gzDeflate(char* start, off_t insz, char *buf, off_t outsz, time_t mtime) { +ssize_t +gzDeflate(const char *source, size_t slen, char *dest, size_t dlen, + time_t mtime) +{ unsigned char *c; unsigned long crc; z_stream z;
+ if (slen > SIZE_T_CEILING || dlen > SIZE_T_CEILING) + return -1; + z.zalloc = Z_NULL; z.zfree = Z_NULL; z.opaque = Z_NULL;
if (Z_OK != deflateInit2(&z, - Z_DEFAULT_COMPRESSION, - Z_DEFLATED, - -MAX_WBITS, /* supress zlib-header */ - 8, - Z_DEFAULT_STRATEGY)) { + Z_DEFAULT_COMPRESSION, + Z_DEFLATED, + -MAX_WBITS, /* supress zlib-header */ + 8, + Z_DEFAULT_STRATEGY)) return -1; - }
- z.next_in = (unsigned char *)start; - z.avail_in = insz; + z.next_in = (Bytef *)source; + z.avail_in = slen; z.total_in = 0;
- /* write gzip header */
- c = (unsigned char *) buf; + c = (unsigned char *)dest; c[0] = 0x1f; c[1] = 0x8b; c[2] = Z_DEFLATED; @@ -338,19 +285,17 @@ int gzDeflate(char* start, off_t insz, char *buf, off_t outsz, time_t mtime) { c[9] = 0x03; /* UNIX */
z.next_out = c + 10; - z.avail_out = outsz - 10 - 8; + z.avail_out = dlen - 10 - 8; z.total_out = 0;
- if (Z_STREAM_END != deflate(&z, Z_FINISH)) { + if (deflate(&z, Z_FINISH) != Z_STREAM_END) { deflateEnd(&z); return -1; }
+ crc = generate_crc32c(source, slen);
- crc = generate_crc32c(start, insz); - - c = (unsigned char *)buf + 10 + z.total_out; - + c = (unsigned char *)dest + 10 + z.total_out; c[0] = (crc >> 0) & 0xff; c[1] = (crc >> 8) & 0xff; c[2] = (crc >> 16) & 0xff; @@ -360,49 +305,7 @@ int gzDeflate(char* start, off_t insz, char *buf, off_t outsz, time_t mtime) { c[6] = (z.total_in >> 16) & 0xff; c[7] = (z.total_in >> 24) & 0xff;
- - - if (Z_OK != deflateEnd(&z)) { + if (deflateEnd(&z) != Z_OK) return -1; - } - return 10 + z.total_out + 8; - } - - - - - -/* compress or decompress from stdin to stdout */ -/* int main(int argc, char **argv) */ -/* { */ -/* int ret; */ -/* char buf1[32] = "abcasdfadfadfadf23fasdfa23sdfsdf"; */ -/* char buf2[100]; */ -/* char buf3[100]; */ -/* int i; */ - -/* bzero(buf2, sizeof(buf2)); */ -/* bzero(buf3, sizeof(buf3)); */ - - -/* // ret = def(buf1, 3, buf2, 100, Z_DEFAULT_COMPRESSION); */ -/* ret = gzDeflate(buf1, sizeof(buf1), buf2, sizeof(buf2), time(NULL)); */ -/* if (ret <= 0) */ -/* zerr(ret); */ - -/* /* for (i=0; i < ret; i++) */ -/* putc(buf2[i], stdout); */ -/* */ */ - - -/* // printf("len = %d\n", ret); */ - -/* ret = gzInflate(buf2, ret, buf3, 100); */ -/* if (ret <= 0) */ -/* zerr(ret); */ -/* printf("hello %s\n", buf3); */ - - -/* } */ diff --git a/src/steg/zpack.h b/src/steg/zpack.h index d0e5cb2..5d3f82f 100644 --- a/src/steg/zpack.h +++ b/src/steg/zpack.h @@ -1,11 +1,12 @@ #ifndef _ZPACK_H #define _ZPACK_H
-int def(char *source, int slen, char *dest, int dlen, int level); -int inf(char *source, int slen, char *dest, int dlen); -void zerr(int ret); -int gzInflate(char *source, int slen, char *dest, int dlen); -int gzDeflate(char* start, off_t insz, char *buf, off_t outsz, time_t mtime); -unsigned int generate_crc32c(char *buffer, size_t length); +ssize_t def(const char *source, size_t slen, char *dest, size_t dlen, + int level); +ssize_t inf(const char *source, size_t slen, char *dest, size_t dlen); + +ssize_t gzInflate(const char *source, size_t slen, char *dest, size_t dlen); +ssize_t gzDeflate(const char *source, size_t slen, char *dest, size_t dlen, + time_t mtime);
#endif
tor-commits@lists.torproject.org