[tor-commits] [stegotorus/master] Switch zpack.h API from plain char* to uint8_t*

zwol at torproject.org zwol at torproject.org
Fri Jul 20 23:17:07 UTC 2012


commit f2883a1507f67e8b0b34fc479d3fbeb52f310afc
Author: Zack Weinberg <zackw at cmu.edu>
Date:   Mon Apr 30 11:54:10 2012 -0700

    Switch zpack.h API from plain char* to uint8_t*
---
 src/steg/jsSteg.cc  |    8 +++--
 src/steg/swfSteg.cc |    7 +++-
 src/zpack.cc        |   72 ++++++++++++++++++++++++--------------------------
 src/zpack.h         |   12 +++++---
 4 files changed, 53 insertions(+), 46 deletions(-)

diff --git a/src/steg/jsSteg.cc b/src/steg/jsSteg.cc
index 7cfd056..8fee7cb 100644
--- a/src/steg/jsSteg.cc
+++ b/src/steg/jsSteg.cc
@@ -724,7 +724,7 @@ http_server_JS_transmit (payloads& pl, struct evbuffer *source, conn_t *conn,
   char newHdr[MAX_RESP_HDR_SIZE];
   unsigned int datalen = 0, cnt = 0, mjs = 0;
   int r, i, mode, jsLen, hLen, cLen, newHdrLen = 0, outbuf2len;
-  
+
   int gzipMode = JS_GZIP_RESP;
 
 
@@ -828,7 +828,8 @@ http_server_JS_transmit (payloads& pl, struct evbuffer *source, conn_t *conn,
     // sizeof outbuf2 = cLen + 10-byte for gzip header + 8-byte for crc 
     outbuf2 = (char *)xmalloc(cLen+18);  
 
-    outbuf2len = gzDeflate(outbuf, cLen, outbuf2, cLen+18, time(NULL));
+    outbuf2len = gzDeflate((const uint8_t *)outbuf, cLen,
+                           (uint8_t *)outbuf2, cLen+18, time(NULL));
 
     if (outbuf2len <= 0) {
       log_warn("gzDeflate for outbuf fails");
@@ -991,7 +992,8 @@ http_handle_client_JS_receive(steg_t *, conn_t *conn, struct evbuffer *dest, str
   gzipMode = isGzipContent(respMsg);
   if (gzipMode) {
     log_debug("gzip content encoding detected");
-    buf2len = gzInflate(httpBody, httpBodyLen, buf2, HTTP_MSG_BUF_SIZE);
+    buf2len = gzInflate((const uint8_t *)httpBody, httpBodyLen,
+                        (uint8_t *)buf2, HTTP_MSG_BUF_SIZE);
     if (buf2len <= 0) {
       log_warn("gzInflate for httpBody fails");
       fprintf(stderr, "gzInflate for httpBody fails");
diff --git a/src/steg/swfSteg.cc b/src/steg/swfSteg.cc
index 4358fe3..77c425b 100644
--- a/src/steg/swfSteg.cc
+++ b/src/steg/swfSteg.cc
@@ -62,7 +62,9 @@ swf_wrap(payloads& pl, char* inbuf, int in_len, char* outbuf, int out_sz) {
   memcpy(tmp_buf, swf+8, SWF_SAVE_HEADER_LEN);
   memcpy(tmp_buf+SWF_SAVE_HEADER_LEN, inbuf, in_len);
   memcpy(tmp_buf+SWF_SAVE_HEADER_LEN+in_len, swf + in_swf_len - SWF_SAVE_FOOTER_LEN, SWF_SAVE_FOOTER_LEN);
-  out_swf_len = def(tmp_buf, SWF_SAVE_HEADER_LEN + in_len + SWF_SAVE_FOOTER_LEN, tmp_buf2+8, 
+  out_swf_len = def((const uint8_t *)tmp_buf,
+                    SWF_SAVE_HEADER_LEN + in_len + SWF_SAVE_FOOTER_LEN,
+                    (uint8_t *)tmp_buf2+8, 
 		    in_len + SWF_SAVE_HEADER_LEN + SWF_SAVE_FOOTER_LEN + 512-8, 
 		    Z_DEFAULT_COMPRESSION);
 
@@ -98,7 +100,8 @@ swf_unwrap(char* inbuf, int in_len, char* outbuf, int out_sz) {
 
   tmp_buf = (char *)xmalloc(in_len * 8);
 
-  inf_len = inf(inbuf + 8, in_len - 8, tmp_buf, in_len * 8); 
+  inf_len = inf((const uint8_t *)inbuf + 8, in_len - 8,
+                (uint8_t *)tmp_buf, in_len * 8); 
 
   //  fprintf(stderr, "in_swf_len = %d\n", in_len -8 );
 
diff --git a/src/zpack.cc b/src/zpack.cc
index 2a7adfc..24c64fd 100644
--- a/src/zpack.cc
+++ b/src/zpack.cc
@@ -17,13 +17,13 @@
    an error reading or writing the files. */
 
 ssize_t
-def(const char *source, size_t slen, char *dest, size_t dlen, int level)
+def(const uint8_t *source, size_t slen, uint8_t *dest, size_t dlen, int level)
 {
   int ret, flush;
   size_t have;
   z_stream strm;
-  unsigned char in[CHUNK];
-  unsigned char out[CHUNK];
+  uint8_t in[CHUNK];
+  uint8_t out[CHUNK];
   size_t dlen_orig = dlen;
 
   if (slen > SIZE_T_CEILING || dlen > SIZE_T_CEILING)
@@ -89,13 +89,13 @@ def(const char *source, size_t slen, char *dest, size_t dlen, int level)
    is an error reading or writing the files. */
 
 ssize_t
-inf(const char *source, size_t slen, char *dest, size_t dlen)
+inf(const uint8_t *source, size_t slen, uint8_t *dest, size_t dlen)
 {
   int ret;
   size_t have;
   z_stream strm;
-  unsigned char in[CHUNK];
-  unsigned char out[CHUNK];
+  uint8_t in[CHUNK];
+  uint8_t out[CHUNK];
   size_t dlen_orig = dlen;
 
   if (slen > SIZE_T_CEILING || dlen > SIZE_T_CEILING)
@@ -167,13 +167,13 @@ inf(const char *source, size_t slen, char *dest, size_t dlen)
 /* assumes that we know there is exactly 10 bytes of gzip header */
 
 ssize_t
-gzInflate(const char *source, size_t slen, char *dest, size_t dlen)
+gzInflate(const uint8_t *source, size_t slen, uint8_t *dest, size_t dlen)
 {
   int ret;
   size_t have;
   z_stream strm;
-  unsigned char in[CHUNK];
-  unsigned char out[CHUNK];
+  uint8_t in[CHUNK];
+  uint8_t out[CHUNK];
   size_t dlen_orig = dlen;
 
   if (slen > SIZE_T_CEILING || dlen > SIZE_T_CEILING)
@@ -248,11 +248,10 @@ 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,
+gzDeflate(const uint8_t *source, size_t slen, uint8_t *dest, size_t dlen,
           time_t mtime)
 {
-  unsigned char *c;
-  unsigned long crc;
+  uint32_t crc;
   z_stream z;
 
   if (slen > SIZE_T_CEILING || dlen > SIZE_T_CEILING)
@@ -270,25 +269,24 @@ gzDeflate(const char *source, size_t slen, char *dest, size_t dlen,
                            Z_DEFAULT_STRATEGY))
     return -1;
 
-  z.next_in = (Bytef *)source;
+  z.next_in = const_cast<uint8_t*>(source);
   z.avail_in = slen;
   z.total_in = 0;
 
   /* write gzip header */
 
-  c = (unsigned char *)dest;
-  c[0] = 0x1f;
-  c[1] = 0x8b;
-  c[2] = Z_DEFLATED;
-  c[3] = 0; /* options */
-  c[4] = (mtime >>  0) & 0xff;
-  c[5] = (mtime >>  8) & 0xff;
-  c[6] = (mtime >> 16) & 0xff;
-  c[7] = (mtime >> 24) & 0xff;
-  c[8] = 0x00; /* extra flags */
-  c[9] = 0x03; /* UNIX */
-
-  z.next_out = c + 10;
+  dest[0] = 0x1f;
+  dest[1] = 0x8b;
+  dest[2] = Z_DEFLATED;
+  dest[3] = 0; /* options */
+  dest[4] = (mtime >>  0) & 0xff;
+  dest[5] = (mtime >>  8) & 0xff;
+  dest[6] = (mtime >> 16) & 0xff;
+  dest[7] = (mtime >> 24) & 0xff;
+  dest[8] = 0x00; /* extra flags */
+  dest[9] = 0x03; /* UNIX */
+
+  z.next_out = dest + 10;
   z.avail_out = dlen - 10 - 8;
   z.total_out = 0;
 
@@ -297,17 +295,17 @@ gzDeflate(const char *source, size_t slen, char *dest, size_t dlen,
     return -1;
   }
 
-  crc = generate_crc32c((const uint8_t *)source, slen);
-
-  c = (unsigned char *)dest + 10 + z.total_out;
-  c[0] = (crc >>  0) & 0xff;
-  c[1] = (crc >>  8) & 0xff;
-  c[2] = (crc >> 16) & 0xff;
-  c[3] = (crc >> 24) & 0xff;
-  c[4] = (z.total_in >>  0) & 0xff;
-  c[5] = (z.total_in >>  8) & 0xff;
-  c[6] = (z.total_in >> 16) & 0xff;
-  c[7] = (z.total_in >> 24) & 0xff;
+  crc = generate_crc32c(source, slen);
+
+  dest = dest + 10 + z.total_out;
+  dest[0] = (crc >>  0) & 0xff;
+  dest[1] = (crc >>  8) & 0xff;
+  dest[2] = (crc >> 16) & 0xff;
+  dest[3] = (crc >> 24) & 0xff;
+  dest[4] = (z.total_in >>  0) & 0xff;
+  dest[5] = (z.total_in >>  8) & 0xff;
+  dest[6] = (z.total_in >> 16) & 0xff;
+  dest[7] = (z.total_in >> 24) & 0xff;
 
   if (deflateEnd(&z) != Z_OK)
     return -1;
diff --git a/src/zpack.h b/src/zpack.h
index e1e1c2c..438c9dc 100644
--- a/src/zpack.h
+++ b/src/zpack.h
@@ -4,12 +4,16 @@
 #ifndef _ZPACK_H
 #define _ZPACK_H
 
-ssize_t def(const char *source, size_t slen, char *dest, size_t dlen,
+ssize_t def(const uint8_t *source, size_t slen,
+            uint8_t *dest, size_t dlen,
             int level);
-ssize_t inf(const char *source, size_t slen, char *dest, size_t dlen);
+ssize_t inf(const uint8_t *source, size_t slen,
+            uint8_t *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,
+ssize_t gzInflate(const uint8_t *source, size_t slen,
+                  uint8_t *dest, size_t dlen);
+ssize_t gzDeflate(const uint8_t *source, size_t slen,
+                  uint8_t *dest, size_t dlen,
                   time_t mtime);
 
 uint32_t generate_crc32c(const uint8_t *string, size_t length);





More information about the tor-commits mailing list