jszip icon indicating copy to clipboard operation
jszip copied to clipboard

Uncompressed data size mismatch (nodejs) still there in 3.7.1 - 32 Bit Issue with <<

Open JMS-1 opened this issue 4 years ago • 2 comments
trafficstars

I got a ZIP which includes a single file with more than 2GB in (decompressed) size. On decompression the error from the title is thrown. It clearly is some 32Bit signed/unsigned issue:

worker.on("end", function () {
            if (this.streamInfo['data_length'] !== that.uncompressedSize) {
                throw new Error("Bug : uncompressed data size mismatch");
            }
        });
        
this.streamInfo['data_length']
	4011341711 (0xEF18378F)
that.uncompressedSize
	-283625585 (0xEF18378F)

So my question is: is there a size limit of 2GB for individual files inside the ZIP (which by the way has been created with jszip 3.7.1 as well)?

Thanks

Jochen

JMS-1 avatar Aug 20 '21 07:08 JMS-1

Problem is that << only reproduces 32 Bit Value, so

readInt: function(size) {
        var result = 0,
            i;
        this.checkOffset(size);
        for (i = this.index + size - 1; i >= this.index; i--) {
            result = (result << 8) + this.byteAt(i);
        }
        this.index += size;
        return result;
    },

will mess with the sign if the MSB is larger than 127. Better use multiplication

readInt: function(size) {
        var result = 0,
            i;
        this.checkOffset(size);
        for (i = this.index + size - 1; i >= this.index; i--) {
            result = (result * 256) + this.byteAt(i);
        }
        this.index += size;
        return result;
    },

FYI: * 256 will be safe up to 53 Bit (MAX_SAFE_INTEGER) which is around 9 PetaByte.

JMS-1 avatar Aug 20 '21 10:08 JMS-1

We are also affected.

RubenGarcia avatar Sep 22 '21 13:09 RubenGarcia