+ image->sections = NULL;
+ }
+}
+
+int image_calculate_checksum(uint8_t* buffer, uint32_t nbytes, uint32_t* checksum)
+{
+ uint32_t crc = 0xffffffff;
+ LOG_DEBUG("Calculating checksum");
+
+ static uint32_t crc32_table[256];
+
+ static bool first_init = false;
+ if (!first_init)
+ {
+ /* Initialize the CRC table and the decoding table. */
+ int i, j;
+ unsigned int c;
+ for (i = 0; i < 256; i++)
+ {
+ /* as per gdb */
+ for (c = i << 24, j = 8; j > 0; --j)
+ c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
+ crc32_table[i] = c;
+ }
+
+ first_init = true;
+ }
+
+ while (nbytes > 0)
+ {
+ int run = nbytes;
+ if (run > 32768)
+ {
+ run = 32768;
+ }
+ nbytes -= run;
+ while (run--)
+ {
+ /* as per gdb */
+ crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buffer++) & 255];
+ }
+ keep_alive();
+ }
+
+ LOG_DEBUG("Calculating checksum done");
+
+ *checksum = crc;