Skip to content
Snippets Groups Projects
image.c 27.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • 		image->sections = NULL;
    	}
    
    ntfreak's avatar
    ntfreak committed
    
    int image_calculate_checksum(u8* buffer, u32 nbytes, u32* checksum)
    {
    	u32 crc = 0xffffffff;
    
    oharboe's avatar
    oharboe committed
    	LOG_DEBUG("Calculating checksum");
    
    	u32 crc32_table[256];
    
    oharboe's avatar
    oharboe committed
    	/* Initialize the CRC table and the decoding table.  */
    	int i, j;
    	unsigned int c;
    	for (i = 0; i < 256; i++)
    
    ntfreak's avatar
    ntfreak committed
    	{
    
    oharboe's avatar
    oharboe committed
    		/* as per gdb */
    		for (c = i << 24, j = 8; j > 0; --j)
    			c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
    		crc32_table[i] = c;
    
    ntfreak's avatar
    ntfreak committed
    	}
    
    oharboe's avatar
    oharboe committed
    	while (nbytes>0)
    
    ntfreak's avatar
    ntfreak committed
    	{
    
    oharboe's avatar
    oharboe committed
    		int run=nbytes;
    		if (run>32768)
    
    oharboe's avatar
    oharboe committed
    			run=32768;
    
    oharboe's avatar
    oharboe committed
    		nbytes-=run;
    		while (run--)
    		{
    			/* as per gdb */
    			crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buffer++) & 255];
    		}
    		keep_alive();
    
    ntfreak's avatar
    ntfreak committed
    	}
    
    oharboe's avatar
    oharboe committed
    	LOG_DEBUG("Calculating checksum done");
    
    
    ntfreak's avatar
    ntfreak committed
    	*checksum = crc;
    	return ERROR_OK;
    }