# HG changeset patch # User Matti Hamalainen # Date 1425171533 -7200 # Node ID cd0e0270e1cef8a6cb3bae66a4ec0d27beaf85d5 # Parent acda8cfc91195fa8f95026300273f62c0a00f3c7 Cosmetics. diff -r acda8cfc9119 -r cd0e0270e1ce src/dmzlib.c --- a/src/dmzlib.c Sun Mar 01 02:40:21 2015 +0200 +++ b/src/dmzlib.c Sun Mar 01 02:58:53 2015 +0200 @@ -116,7 +116,7 @@ k += (1 << s); } } - ++next_code[s]; + next_code[s]++; } } @@ -425,8 +425,9 @@ Uint8 header[4]; int len, nlen, k, ret; + // "Any bits of input up to the next byte boundary are ignored." if (ctx->numBits & 7) - dmZReceive(ctx, ctx->numBits & 7); // discard + dmZReceive(ctx, ctx->numBits & 7); // drain the bit-packed data into header k = 0; @@ -462,7 +463,7 @@ (ret = dmZLibExpand(ctx, ctx->zout, len)) != DMERR_OK) { return dmError(DMERR_DATA_ERROR, - "Failed to decompress enough data: %d, %s\n", + "Could not expand output buffer: %d, %s\n", ret, dmErrorStr(ret)); } @@ -474,37 +475,6 @@ } -int dmZLibParseHeader(DMZLibContext * ctx, BOOL checkPNG) -{ - int cmf = dmZGet8(ctx); - int flags = dmZGet8(ctx); - int cm = cmf & 15; - // cinfo = cmf >> 4; - // window = 1 << (8 + cinfo)... but who cares, we fully buffer output - - if ((cmf * 256 + flags) % 31 != 0) - { - return dmError(DMERR_INVALID_DATA, - "Bad zlib header."); - } - - if (checkPNG && (flags & 32)) - { - // preset dictionary not allowed in png - return dmError(DMERR_NOT_SUPPORTED, - "Preset dictionary not allowed in PNG.\n"); - } - - if (checkPNG && cm != 8) - { - return dmError(DMERR_INVALID_DATA, - "Bad or unsupported compression type.\n"); - } - - return DMERR_OK; -} - - int dmZLibDecode(DMZLibContext * ctx) { int final, type, ret; @@ -546,3 +516,40 @@ return DMERR_OK; } + + +int dmZLibParseHeader(DMZLibContext * ctx, BOOL checkPNG) +{ + // See http://tools.ietf.org/html/rfc1950 + int cmf = dmZGet8(ctx); // Compression method and flags + int flags = dmZGet8(ctx); // Flags + int cmethod = (cmf & 15); + int cinfo = (cmf >> 4) & 15; + ctx->window = 1 << (8 + cinfo); // Window size (not used at the moment) + + // "The FCHECK value must be such that CMF and FLG, when viewed as + // a 16-bit unsigned integer stored in MSB order (CMF*256 + FLG), + // is a multiple of 31." + if ((cmf * 256 + flags) % 31 != 0) + { + return dmError(DMERR_INVALID_DATA, + "Bad zlib header."); + } + + // We only support compression method 8 + if (cmethod != 8) + { + return dmError(DMERR_INVALID_DATA, + "Bad or unsupported zlib compression method %d.\n", + cmethod); + } + + if (checkPNG && (flags & 32)) + { + // preset dictionary not allowed in png + return dmError(DMERR_NOT_SUPPORTED, + "Preset dictionary not allowed in PNG.\n"); + } + return DMERR_OK; +} + diff -r acda8cfc9119 -r cd0e0270e1ce src/dmzlib.h --- a/src/dmzlib.h Sun Mar 01 02:40:21 2015 +0200 +++ b/src/dmzlib.h Sun Mar 01 02:58:53 2015 +0200 @@ -44,9 +44,9 @@ } DMZLibContext; -void dmZLibInit(); -int dmZLibDecode(DMZLibContext * ctx); -int dmZLibParseHeader(DMZLibContext * ctx, BOOL checkPNG); +void dmZLibInit(); +int dmZLibParseHeader(DMZLibContext * ctx, BOOL checkPNG); +int dmZLibDecode(DMZLibContext * ctx); #ifdef __cplusplus