void operator()(const tbb::blocked_range<uint64>& range) const { HANDLE rDataFileHandle = INVALID_HANDLE_VALUE; IOHandleGuard rIOHandleGuard(rDataFileHandle); rDataFileHandle = IO::fopen(m_rDataFilePath.c_str(), IO::IO_READ_ONLY, IO::IO_DIRECT); if(rDataFileHandle == INVALID_HANDLE_VALUE) { Log.Error(__FUNCTION__, "Cannot open data file."); return; } //log progress Log.Debug("Crc32 check", "Starting range from: " I64FMTD " to: " I64FMTD, range.begin(), range.end()); for(uint64 i = range.begin();i != range.end();++i) { const WriteInfo &rWriteInfo = m_rWriteInfoVec[i]; //get from map RecordIndexMap::accessor rWriteAccessor; if(m_rRecordIndexMap.find(rWriteAccessor, rWriteInfo.m_key)) { //preallocate uint16 blockCount = rWriteAccessor->second.m_blockCount; int64 blocksSize = blockCount * BLOCK_SIZE; void* pBlocks = _ALIGNED_MALLOC(blocksSize, g_DataFileMallocAlignment); //read all blocks in one IO IO::fseek(rDataFileHandle, rWriteAccessor->second.m_recordStart, IO::IO_SEEK_SET); IO::fread(pBlocks, blocksSize, rDataFileHandle); //calc crc32 size_t crc32ArraySize = sizeof(uint32) * blockCount; uint32 *crc32Array = (uint32*)alloca(crc32ArraySize); for(uint16 i = 0;i < blockCount;++i) { uint8 *pBlock = ((uint8*)pBlocks + (BLOCK_SIZE * i)); crc32Array[i] = crc32_compute(pBlock, BLOCK_SIZE); } // uint32 crc32 = crc32_compute((BYTE*)crc32Array, crc32ArraySize); //check crc32 if(rWriteAccessor->second.m_crc32 != crc32) { Log.Error("Crc32 check", "CRC32 error - x: " I64FMTD " saved crc: %u loaded crc: %u", rWriteAccessor->first, rWriteAccessor->second.m_crc32, crc32); } //release memory _ALIGNED_FREE(pBlocks); } } //log progress Log.Debug("Crc32 check", "Finished range from: " I64FMTD " to: " I64FMTD, range.begin(), range.end()); }
/** * Calculates number of hashes from stdin or from files * * Expected input ./gethash [-md5] [-c16] [-c32] [-xor] [-hex] [-f file] */ int main( int argc, char *argv[] ) { unsigned char hash = 0; FILE *file = stdin; parse_args(argc, argv, &hash, &file); unsigned int length; unsigned char *data = read_file(file, &length); printf("Length: %i bytes\n", length); if(hash & XOR){ printf("XOR: "); if(hash & HEX) printf("0x%02x\n", xor_compute(data, length)); else printf("%u\n", xor_compute(data, length)); } if(hash & C16){ printf("CRC-16: "); if(hash & HEX) printf("0x%04x\n", crc16_compute(data, length)); else printf("%u\n", crc16_compute(data, length)); } if(hash & C32){ printf("CRC-32: "); if(hash & HEX) printf("0x%08x\n", crc32_compute(data, length)); else printf("%u\n", crc32_compute(data, length)); } if(hash & MD5){ unsigned char result[16]; md5_compute(data, length, result); printf("MD5: "); for (int i = 0; i < 16; i++){ printf("%02x", result[i]); } printf("\n"); } free(data); fclose(file); return 0; }
bool nrf_dfu_app_is_valid(void) { NRF_LOG_INFO("Enter nrf_dfu_app_is_valid\r\n"); if (s_dfu_settings.bank_0.bank_code != NRF_DFU_BANK_VALID_APP) { // Bank 0 has no valid app. Nothing to boot NRF_LOG_INFO("Return false in valid app check\r\n"); return false; } // If CRC == 0, this means CRC check is skipped. if (s_dfu_settings.bank_0.image_crc != 0) { uint32_t crc = crc32_compute((uint8_t*) CODE_REGION_1_START, s_dfu_settings.bank_0.image_size, NULL); if (crc != s_dfu_settings.bank_0.image_crc) { // CRC does not match with what is stored. NRF_LOG_INFO("Return false in CRC\r\n"); return false; } } NRF_LOG_INFO("Return true. App was valid\r\n"); return true; }
/* * Generate ID for FIDO messages without ^AMSGID, using date and CRC over * From, To and Subject. */ char *s_msgid_default(Message *msg) { /* * Compute CRC for strings from, to, subject */ crc32_init(); crc32_compute(msg->name_from, strlen(msg->name_from)); crc32_compute(msg->name_to , strlen(msg->name_to )); crc32_compute(msg->subject , strlen(msg->subject )); return s_printf("<NOMSGID_%d=3A%d=2F%d.%d_%s_%08lx@%s>", msg->node_orig.zone, msg->node_orig.net, msg->node_orig.node, msg->node_orig.point, date("%y%m%d_%H%M%S", &msg->date), crc32_value(), msgid_domain(msg->node_orig.zone)); }
static uint32_t s_hal_crc32_sw_compute(hal_crc_info_t *info, const void *data, uint32_t size) { uint8_t tailsize = size%4; uint32_t size4 = size-tailsize; info->initialvalue = crc32_compute(info->cfg.crctblram, info->initialvalue, (uint8_t*)data, size4); if(0 != (tailsize)) { uint8_t tail[4]; memset(tail, 0, 4); memcpy(tail, ((uint8_t*)data) + size-tailsize, tailsize); info->initialvalue = crc32_compute(info->cfg.crctblram, info->initialvalue, tail, 4); } return(info->initialvalue); }
uint32 blman_get_blocks_crc32(blman *self) { //C99 - VLA #if !defined(WIN32) && !defined(__STDC_NO_VLA__) uint32 crc32Array[self->blockCount]; #else uint32 *crc32Array = alloca(sizeof(uint32) * self->blockCount); #endif for (uint16 i = 0; i < self->blockCount; ++i) { uint8 *pBlock = blman_get_block(self, i); crc32Array[i] = crc32_compute(pBlock, BLOCK_SIZE); } uint32 crc32 = crc32_compute((BYTE*)crc32Array, self->blockCount * sizeof(uint32)); return crc32; }
unsigned int hash(hashmap *h, const char *key) { unsigned long index = crc32_compute((void*)(key), strlen(key)); #if defined(HASHMAP_WIN_DEBUG) && defined(DEBUG_HASHing_FUNCTION) char hashval[256]; sprintf(hashval, "'%s' %d", key, index); MessageBox(NULL, hashval, "Hashed Key", MB_ICONINFORMATION | MB_OK); #endif /* HASHMAP_WIN_DEBUG */ return index % h->num_buckets; }
uint32_t nrf_dfu_settings_calculate_crc(void) { // the crc is calculated from the s_dfu_settings struct, except the crc itself and the init command return crc32_compute((uint8_t*)&s_dfu_settings + 4, sizeof(nrf_dfu_settings_t) - 4 - sizeof(s_dfu_settings.init_command), NULL); }
static uint32_t nrf_dfu_settings_crc_get(void) { // The crc is calculated from the s_dfu_settings struct, except the crc itself and the init command return crc32_compute((uint8_t*)&s_dfu_settings + 4, DFU_SETTINGS_INIT_COMMAND_OFFSET - 4, NULL); }
/** @brief Function to continue App update * * @details This function will be called after reset if there is a valid application in Bank1 * required to be copied down to bank0. * * @param[in] src_addr Source address of the application to copy from Bank1 to Bank0. * * @retval NRF_SUCCESS Continuation was successful. * @retval NRF_ERROR_NULL Invalid data during compare. * @retval FS_ERR_UNALIGNED_ADDR A call to fstorage was not aligned to a page boundary or the address was not word aliged. * @retval FS_ERR_INVALID_ADDR The destination of a call to fstorage does not point to * the start of a flash page or the operation would * go beyond the flash memory boundary. * @retval FS_ERR_NOT_INITIALIZED The fstorage module is not initialized. * @retval FS_ERR_INVALID_CFG The initialization of the fstorage module is invalid. * @retval FS_ERR_NULL_ARG A call to fstorage had an invalid NULL argument. * @retval FS_ERR_INVALID_ARG A call to fstorage had invalid arguments. * @retval FS_ERR_QUEUE_FULL If the internal operation queue of the fstorage module is full. * @retval FS_ERR_FAILURE_SINCE_LAST If an error occurred in another transaction and fstorage cannot continue before * the event has been dealt with. */ static uint32_t nrf_dfu_app_continue(uint32_t src_addr) { // This function only in use when new app is present in bank 1 uint32_t const image_size = s_dfu_settings.bank_1.image_size; uint32_t const split_size = CODE_PAGE_SIZE; // Arbitrary number that must be page aligned uint32_t ret_val = NRF_SUCCESS; uint32_t target_addr = MAIN_APPLICATION_START_ADDR + s_dfu_settings.write_offset; uint32_t length_left = (image_size - s_dfu_settings.write_offset); uint32_t cur_len; uint32_t crc; NRF_LOG_INFO("Enter nrf_dfu_app_continue\r\n"); // Copy the application down safely do { cur_len = (length_left > split_size) ? split_size : length_left; // Erase the target page ret_val = nrf_dfu_flash_erase((uint32_t*) target_addr, split_size / CODE_PAGE_SIZE, NULL); if (ret_val != NRF_SUCCESS) { return ret_val; } // Flash one page ret_val = nrf_dfu_flash_store((uint32_t*)target_addr, (uint32_t*)src_addr, cur_len, NULL); if (ret_val != NRF_SUCCESS) { return ret_val; } ret_val = nrf_dfu_mbr_compare((uint32_t*)target_addr, (uint32_t*)src_addr, cur_len); if (ret_val != NRF_SUCCESS) { // We will not retry the copy NRF_LOG_INFO("Invalid data during compare: target: 0x%08x, src: 0x%08x\r\n", target_addr, src_addr); return ret_val; } // Erase the head (to handle growing bank 0) ret_val = nrf_dfu_flash_erase((uint32_t*) src_addr, split_size / CODE_PAGE_SIZE, NULL); if (ret_val != NRF_SUCCESS) { NRF_LOG_INFO("App update: Failure erasing page at addr: 0x%08x\r\n", src_addr); return ret_val; } s_dfu_settings.write_offset += cur_len; (void)nrf_dfu_settings_write(NULL); target_addr += cur_len; src_addr += cur_len; length_left -= cur_len; } while(length_left > 0); // Check the crc of the copied data. Enable if so. crc = crc32_compute((uint8_t*)MAIN_APPLICATION_START_ADDR, image_size, NULL); if (crc == s_dfu_settings.bank_1.image_crc) { NRF_LOG_INFO("Setting app as valid\r\n"); s_dfu_settings.bank_0.bank_code = NRF_DFU_BANK_VALID_APP; s_dfu_settings.bank_0.image_crc = crc; s_dfu_settings.bank_0.image_size = image_size; } else { NRF_LOG_INFO("CRC computation failed for copied app: src crc: 0x%08x, res crc: 0x08x\r\n", s_dfu_settings.bank_1.image_crc, crc); } nrf_dfu_invalidate_bank(&s_dfu_settings.bank_1); (void)nrf_dfu_settings_write(NULL); return ret_val; }
/* * Convert RFC Message-ID/References to FIDO ^AMSGID/^AREPLY */ char *s_msgid_rfc_to_fido(int *origid_flag, char *message_id, int part, int split, char *area) /* origid_flag - Flag for ^AORIGID */ /* message_id - Original RFC-ID */ /* part - part number */ /* split - != 0 # of parts */ /* area - FTN AREA */ { char *id, *host, *p; char *savep; Node node, *n; int hexflag, i; char hexid[16]; unsigned long crc32; TmpS *tmps; /****** Extract id and host from <id@host> *****/ savep = strsave(message_id); /* * Format of message_id is "<*****@*****.**> ..." * We want the the last one in the chain, which is the message id * of the article replied to. */ id = strrchr(savep, '<'); if(!id) { xfree(savep); return NULL; } id++; host = strchr(id, '@'); if(!host) { xfree(savep); return NULL; } *host++ = 0; p = strchr(host, '>'); if(!p) { xfree(savep); return NULL; } *p = 0; /* * Don't convert <funpack....@...> and <NOMSGID-...@...> IDs * generated by FIDOGATE */ if(!strncmp(id, "funpack", 7) || !strncmp(id, "NOMSGID_", 8)) { xfree(savep); return NULL; } /***** Check for old style FTN Message-IDs <abcd1234%[email protected]> *****/ if(!split) { /* * First check ID. A FIDO Message-ID is a hex number with an * optional %Domain string. The hex number must not start with * `0' and it's length must be no more then 8 digits. */ node.domain[0] = 0; p = id; hexflag = isxdigit(*p) && *p!='0'; for(p++, i=0; i<7 && *p && *p!='%'; i++, p++) if(!isxdigit(*p)) hexflag = FALSE; if(hexflag && *p=='%') /* Domain part follows */ { *p++ = 0; BUF_COPY(node.domain, p); } else if(*p) hexflag = FALSE; if(hexflag) { /* Pad with leading 0's */ str_copy(hexid, sizeof(hexid), "00000000"); str_copy(hexid+8-strlen(id), sizeof(hexid)-8+strlen(id), id); /* host must be an FTN address */ if( (n = inet_to_ftn(host)) ) { /* YEP! This is an old-style FTN Message-ID!!! */ node.zone = n->zone; node.net = n->net; node.node = n->node; node.point = n->point; tmps = tmps_printf("%s %s", znfp1(&node), hexid); xfree(savep); if(origid_flag) *origid_flag = FALSE; return tmps->s; } } } /**if(!split)**/ /***** Check for new-style <MSGID_mimeanything@domain> *****/ if(!strncmp(id, "MSGID_", 6)) { p = id + strlen("MSGID_"); tmps = tmps_alloc(strlen(id)+1); mime_dequote(tmps->s, tmps->len, p, MIME_QP|MIME_US); xfree(savep); if(origid_flag) *origid_flag = FALSE; return tmps->s; } /***** Generate ^AMSGID according to msgid.doc specs *****/ /* * New-style FIDO-Gatebau '94 ^AMSGID */ xfree(savep); savep = strsave(message_id); id = strrchr(savep, '<'); if(!id) id = savep; p = strchr(id, '>'); if(!p) p = id; else p++; *p = 0; crc32_init(); crc32_compute((unsigned char *)id, strlen(id)); if(area) crc32_compute((unsigned char *)area, strlen(area)); crc32 = crc32_value(); if(split) crc32 += part - 1; tmps = tmps_alloc(strlen(id)+1+/**Extra**/20); msgid_fts9_quote(tmps->s, id, tmps->len); str_printf(tmps->s + strlen(tmps->s), tmps->len - strlen(tmps->s), " %08lx", crc32); xfree(savep); if(origid_flag) *origid_flag = TRUE; return tmps->s; }