Browse code

hwp: code review fixs, make decompress_and_callback function consistent

Kevin Lin authored on 2016/02/04 07:21:57
Showing 1 changed files
... ...
@@ -81,7 +81,7 @@ static int decompress_and_callback(cli_ctx *ctx, fmap_t *input, off_t at, size_t
81 81
 {
82 82
     int zret, ofd, ret = CL_SUCCESS;
83 83
     off_t off_in = at;
84
-    size_t count, remain = 1, outsize = 0;
84
+    size_t in, count, remain = 1, outsize = 0;
85 85
     z_stream zstrm;
86 86
     char *tmpname;
87 87
     unsigned char inbuf[FILEBUFF], outbuf[FILEBUFF];
... ...
@@ -119,22 +119,22 @@ static int decompress_and_callback(cli_ctx *ctx, fmap_t *input, off_t at, size_t
119 119
     do {
120 120
         if (zstrm.avail_in == 0) {
121 121
             zstrm.next_in = inbuf;
122
-            ret = fmap_readn(input, inbuf, off_in, FILEBUFF);
123
-            if (ret < 0) {
122
+            in = fmap_readn(input, inbuf, off_in, FILEBUFF);
123
+            if (in < 0) {
124 124
                 cli_errmsg("%s: Error reading stream\n", parent);
125 125
                 ret = CL_EUNPACK;
126 126
                 goto dc_end;
127 127
             }
128
-            if (!ret)
128
+            if (!in)
129 129
                 break;
130 130
 
131 131
             if (len) {
132
-                if (remain < ret)
133
-                    ret = remain;
134
-                remain -= ret;
132
+                if (remain < in)
133
+                    in = remain;
134
+                remain -= in;
135 135
             }
136
-            zstrm.avail_in = ret;
137
-            off_in += ret;
136
+            zstrm.avail_in = in;
137
+            off_in += in;
138 138
         }
139 139
         zret = inflate(&zstrm, Z_SYNC_FLUSH);
140 140
         count = FILEBUFF - zstrm.avail_out;
... ...
@@ -153,6 +153,8 @@ static int decompress_and_callback(cli_ctx *ctx, fmap_t *input, off_t at, size_t
153 153
         zstrm.avail_out = FILEBUFF;
154 154
     } while(zret == Z_OK && remain);
155 155
 
156
+    cli_dbgmsg("%s: Decompressed %llu bytes to %s\n", parent, (long long unsigned)outsize, tmpname);
157
+
156 158
     /* post inflation checks */
157 159
     if (zret != Z_STREAM_END && zret != Z_OK) {
158 160
         if (outsize == 0) {
... ...
@@ -164,13 +166,11 @@ static int decompress_and_callback(cli_ctx *ctx, fmap_t *input, off_t at, size_t
164 164
         cli_infomsg(ctx, "%s: Error decompressing stream. Scanning what was decompressed.\n", parent);
165 165
     }
166 166
 
167
-    /* check for limits exceeded */
168
-    if (ret == CL_SUCCESS) {
167
+    /* check for limits exceeded or zlib failure */
168
+    if (ret == CL_SUCCESS && (zret == Z_STREAM_END || zret == Z_OK)) {
169 169
         if (len && remain > 0)
170 170
             cli_infomsg(ctx, "%s: Error decompressing stream. Not all requested input was converted\n", parent);
171 171
 
172
-        cli_dbgmsg("%s: Decompressed %llu bytes to %s\n", parent, (long long unsigned)outsize, tmpname);
173
-
174 172
         /* scanning inflated stream */
175 173
         ret = cb(cbdata, ofd, ctx);
176 174
     } else {
... ...
@@ -181,8 +181,11 @@ static int decompress_and_callback(cli_ctx *ctx, fmap_t *input, off_t at, size_t
181 181
     /* clean-up */
182 182
  dc_end:
183 183
     zret = inflateEnd(&zstrm);
184
-    if (zret != Z_OK)
185
-        ret = CL_EUNPACK;
184
+    if (zret != Z_OK) {
185
+        cli_errmsg("%s: Error closing zlib inflation stream\n", parent);
186
+        if (ret == CL_SUCCESS)
187
+            ret = CL_EUNPACK;
188
+    }
186 189
     close(ofd);
187 190
     if (!ctx->engine->keeptmp)
188 191
         if (cli_unlink(tmpname))
... ...
@@ -278,8 +281,8 @@ int cli_scanhwpole2(cli_ctx *ctx)
278 278
     else
279 279
         cli_dbgmsg("HWPOLE2: Matched uncompressed prefix and size: %u == %u\n", usize, asize);
280 280
 
281
-    return cli_map_scandesc(map, 4, map->len, ctx, CL_TYPE_ANY);
282
-    //return cli_map_scandesc(map, 4, map->len, ctx, CL_TYPE_OLE2);
281
+    return cli_map_scandesc(map, 4, 0, ctx, CL_TYPE_ANY);
282
+    //return cli_map_scandesc(map, 4, 0, ctx, CL_TYPE_OLE2);
283 283
 }
284 284
 
285 285
 /*** HWP5 ***/
... ...
@@ -357,8 +360,6 @@ int cli_hwp5header(cli_ctx *ctx, hwp5_header_t *hwp5)
357 357
 
358 358
 static int hwp5_cb(void *cbdata, int fd, cli_ctx *ctx)
359 359
 {
360
-    int ret;
361
-
362 360
     if (fd < 0 || !ctx)
363 361
         return CL_ENULLARG;
364 362
 
... ...
@@ -1905,6 +1906,9 @@ static size_t num_hwpml_keys = sizeof(hwpml_keys) / sizeof(struct key_entry);
1905 1905
 /* binary streams needs to be base64-decoded then decompressed if fields are set */
1906 1906
 static int hwpml_scan_cb(void *cbdata, int fd, cli_ctx *ctx)
1907 1907
 {
1908
+    if (fd < 0 || !ctx)
1909
+        return CL_ENULLARG;
1910
+
1908 1911
     return cli_magic_scandesc(fd, ctx);
1909 1912
 }
1910 1913