ubyte32 HashFunctions::crc32( const void *data, int size, ubyte32 running_crc/*=0*/ ) { ubyte32 crc = running_crc; if (crc == 0) crc = mz_crc32(0L, 0, 0); return mz_crc32(running_crc, (const unsigned char*)data, size);; }
uint32_t HashFunctions::crc32(const void *data, int size, uint32_t running_crc/*=0*/) { uint32_t crc = running_crc; if (crc == 0) crc = mz_crc32(0L, nullptr, 0); return mz_crc32(running_crc, (const unsigned char*)data, size);; }
static int Lcrc32(lua_State *L) { size_t len; const char *s = luaL_optlstring(L, 1, NULL, &len); mz_ulong init; if (!lua_isnoneornil(L, 2)) init = (mz_ulong)luaL_checkinteger(L, 2); else init = mz_crc32(0, NULL, 0); if (s == NULL) { lua_pushinteger(L, init); return 1; } lua_pushinteger(L, (lua_Integer)mz_crc32(init, (const unsigned char*)s, len)); return 1; }
static int lmz_crc32(lua_State* L) { mz_ulong crc32 = luaL_optint(L, 1, 0); size_t buf_len = 0; const unsigned char* ptr = (const unsigned char*)luaL_optlstring(L, 2, NULL, &buf_len); crc32 = mz_crc32(crc32, ptr, buf_len); lua_pushinteger(L, crc32); return 1; }
static bool checkCrc32(const unsigned id) { int bytesRead; U8 tempBuffer[1024]; U32 expectedCrc32, calculatedCrc32 = MZ_CRC32_INIT; file_t fp = sysfile_open(resourceFiles[id]); if (fp == NULL) { sys_error("(resources) unable to open \"%s\"", resourceFiles[id]); return false; } bytesRead = sysfile_read(fp, tempBuffer, sizeof(U32), 1); /* prepare beginning of buffer for the following loop */ if (bytesRead != 1) { sys_error("(resources) not enough data for \"%s\"", resourceFiles[id]); sysfile_close(fp); return false; } do { bytesRead = sysfile_read(fp, tempBuffer + sizeof(U32), sizeof(U8), sizeof(tempBuffer) - sizeof(U32)); calculatedCrc32 = mz_crc32(calculatedCrc32, tempBuffer, bytesRead); memcpy(tempBuffer, tempBuffer + bytesRead, sizeof(U32)); } while (bytesRead == sizeof(tempBuffer) - sizeof(U32)); sysfile_close(fp); memcpy(&expectedCrc32, tempBuffer, sizeof(U32)); expectedCrc32 = letoh32(expectedCrc32); if (expectedCrc32 != calculatedCrc32) { sys_error("(resources) crc check failed for \"%s\"", resourceFiles[id]); return false; } return true; }
static int process_file_data(struct update_context *ctx, int ignore_data) { uint32_t bytes_to_write = ctx->file_info.file_size - ctx->file_info.file_received_bytes; bytes_to_write = bytes_to_write < ctx->data_len ? bytes_to_write : ctx->data_len; LOG(LL_DEBUG, ("File size: %u, received: %u to_write: %u", ctx->file_info.file_size, ctx->file_info.file_received_bytes, bytes_to_write)); /* * if ignore_data=1 we have to skip all received data * (usually - extra files in archive) * if ignore_data=0 we have to write data to flash */ if (!ignore_data) { if (prepare_flash(ctx, bytes_to_write) < 0) { ctx->status_msg = "Failed to erase flash"; return -1; } /* Write buffer size must be aligned to 4 */ uint32_t bytes_to_write_aligned = bytes_to_write & -4; LOG(LL_DEBUG, ("Aligned size=%u", bytes_to_write_aligned)); ctx->file_info.crc_current = mz_crc32(ctx->file_info.crc_current, ctx->data, bytes_to_write_aligned); LOG(LL_DEBUG, ("Writing %u bytes @%X", bytes_to_write_aligned, ctx->current_write_address)); if (spi_flash_write(ctx->current_write_address, (uint32_t *) ctx->data, bytes_to_write_aligned) != 0) { LOG(LL_ERROR, ("Failed to write to flash")); ctx->status_msg = "Failed to write to flash"; return -1; } ctx->current_write_address += bytes_to_write_aligned; ctx->file_info.file_received_bytes += bytes_to_write_aligned; context_remove_data(ctx, bytes_to_write_aligned); uint32_t rest = ctx->file_info.file_size - ctx->file_info.file_received_bytes; LOG(LL_DEBUG, ("Rest=%u", rest)); if (rest != 0 && rest < 4 && ctx->data_len >= 4) { /* File size is not aligned to 4, using align buf to write it */ uint8_t align_buf[4] = {0xFF, 0xFF, 0xFF, 0xFF}; memcpy(align_buf, ctx->data, rest); LOG(LL_DEBUG, ("Writing 4 bytes @%X", ctx->current_write_address)); if (spi_flash_write(ctx->current_write_address, (uint32_t *) align_buf, 4) != 0) { LOG(LL_ERROR, ("Failed to write to flash")); ctx->status_msg = "Failed to write to flash"; return -1; } ctx->file_info.crc_current = mz_crc32(ctx->file_info.crc_current, ctx->data, rest); ctx->file_info.file_received_bytes += rest; context_remove_data(ctx, rest); } if (ctx->file_info.file_received_bytes == ctx->file_info.file_size) { LOG(LL_INFO, ("Wrote %s (%u bytes)", ctx->file_info.file_name, ctx->file_info.file_size)) } } else {
size_t Gz::Leanify(size_t size_leanified /*= 0*/) { // written according to this specification // http://www.gzip.org/zlib/rfc-gzip.html if (size <= 18) { std::cerr << "Not a valid GZ file." << std::endl; return Format::Leanify(size_leanified); } depth++; char flags = *(fp + 3); // set the flags to 0, remove all unnecessary section *(fp + 3 - size_leanified) = 0; char *p_read = fp + 10; char *p_write = p_read - size_leanified; *(p_write - 2) = 2; // XFL if (flags & (1 << 2)) // FEXTRA { p_read += *(uint16_t *)p_read + 2; } std::string filename; if (flags & (1 << 3)) // FNAME { for (int i = 1; i < depth; i++) { std::cout << "-> "; } std::cout << p_read << std::endl; filename = std::string(p_read); while (p_read < fp + size && *p_read++) { // skip string } } if (flags & (1 << 4)) // FCOMMENT { while (p_read < fp + size && *p_read++) { // skip string } } if (flags & (1 << 1)) // FHCRC { p_read += 2; } if (p_read >= fp + size) { return Format::Leanify(size_leanified); } if (size_leanified) { memmove(fp - size_leanified, fp, 10); } if (is_fast) { memmove(p_write, p_read, fp + size - p_read); return size - (p_read - p_write); } uint32_t uncompressed_size = *(uint32_t *)(fp + size - 4); uint32_t crc = *(uint32_t *)(fp + size - 8); size_t original_size = fp + size - 8 - p_read; size_t s = 0; unsigned char *buffer = (unsigned char *)tinfl_decompress_mem_to_heap(p_read, original_size, &s, 0); if (!buffer || s != uncompressed_size || crc != mz_crc32(0, buffer, uncompressed_size)) { std::cerr << "GZ corrupted!" << std::endl; mz_free(buffer); memmove(p_write, p_read, original_size + 8); return size - (p_read - p_write); } uncompressed_size = LeanifyFile(buffer, uncompressed_size, 0, filename); ZopfliOptions options; ZopfliInitOptions(&options); options.numiterations = iterations; unsigned char bp = 0, *out = NULL; size_t outsize = 0; ZopfliDeflate(&options, 2, 1, buffer, uncompressed_size, &bp, &out, &outsize); if (outsize < original_size) { memcpy(p_write, out, outsize); p_write += outsize; *(uint32_t *)p_write = mz_crc32(0, buffer, uncompressed_size); *(uint32_t *)(p_write + 4) = uncompressed_size; } else { memmove(p_write, p_read, original_size + 8); p_write += original_size; } mz_free(buffer); delete[] out; depth--; fp -= size_leanified; return p_write + 8 - fp; }