Search code examples
javasizegzipcompression

How to I find out the size of a GZIP section embedded in firmware?


I am currently analyzing firmware images which contain many different sections, one of which is a GZIP section.

I am able to know the location of the start of the GZIP section using magic number and the GZIPInputStream in Java.

However, I need to know the compressed size of the gzip section. GZIPInputStream would return me the uncompressed file size.

Is there anybody who has an idea?


Solution

  • You can count the number of byte read using a custom InputStream. You would need to force the stream to read one byte at a time to ensure you don't read more than you need.


    You can wrap your current InputStream in this

    class CountingInputStream extends InputStream {
        final InputStream is;
        int counter = 0;
    
        public CountingInputStream(InputStream is) {
            this.is = is;
        }
    
        public int read() throws IOException {
            int read = is.read();
            if (read >= 0) counter++;
            return read;
        }
    }
    

    and then wrap it in a GZIPInputStream. The field counter will hold the number of bytes read.

    To use this with BufferedInputStream you can do

     InputStream is = new BufferedInputStream(new FileInputStream(filename));
     // read some data or skip to where you want to start.
    
     CountingInputStream cis = new CountingInputStream(is);
     GZIPInputStream gzis = new GZIPInputStream(cis);
     // read some compressed data
     cis.read(...);
    
     int dataRead = cis.counter;