int main(int argc, char* argv[]) { printf("[i] Calc CRC.\n"); if(argc < 2){ // не задано имя файла прошивки printf("[!] Set file name!\n"); help(); return -1; } // имя файла задаётся первым параметром char* filename = argv[1]; printf("[i] file: %s\n", filename); FILE* fs=0; fs = fopen(filename, "rb"); if(!fs){ printf("[!] Error: cant open file: %s!\n", filename); return -2; } // // узнаем размер файла // fseek(fs, 0, SEEK_END); // позицию в конец фала unsigned fileSize = ftell(fs); // получаем позицию printf("[i] file size: %d\n", fileSize); // // считываем файл в буфер // uchar* buf = new uchar [fileSize]; if(!buf){ printf("[!] Error: cant allocate memory!\n"); fclose(fs); return -4; } // устанавливаемся на начало fseek(fs, 0, SEEK_SET); fread(buf, fileSize, 1, fs); fclose(fs); // // CRC // uint32_t crc; char file_crc[16]; // all file crc = CRC32_INIT_VALUE; crc = getCrc32((uint8_t *) (buf), (uint32_t) (fileSize), crc); sprintf(file_crc, "%08lX", (long unsigned int) crc); printf("[i] CRC file\t\t: 0x%s\n", file_crc); return 0; }
bool MISC::callProtocol(std::string input_str, std::string &result, const bool async_method, const unsigned int unique_id) { // Protocol std::string command; std::string data; const std::string::size_type found = input_str.find(":"); if (found==std::string::npos) // Check Invalid Format { command = input_str; } else { command = input_str.substr(0,found); data = input_str.substr(found+1); } if (command == "TIME") { getDateTime(data, result); } else if (command == "BEGUID") { getBEGUID(data, result); } else if (command == "CRC32") { getCrc32(data, result); } else if (command == "MD4") { getMD4(data, result); } else if (command == "MD5") { getMD5(data, result); } else if (command == "RANDOM_UNIQUE_STRING") { getRandomString(data, result); } else if (command == "TEST") { result = data; } else { result = "[0,\"Error Invalid Format\"]"; extension_ptr->logger->warn("extDB2: Misc Invalid Command: {0}", command); } return true; }
bool ElfRelocator::relocate(u64& memoryAddress) { int oldCrc = getCrc32(outputData.data(),outputData.size()); outputData.clear(); dataChanged = false; bool error = false; u64 start = memoryAddress; for (ElfRelocatorFile& file: files) { if (relocateFile(file,memoryAddress) == false) error = true; } int newCrc = getCrc32(outputData.data(),outputData.size()); if (oldCrc != newCrc) dataChanged = true; memoryAddress -= start; return !error; }
bool PsiSection::verify() const { if (Section::verify() && getSectionSyntaxIndicator()) /* crc32 exists */ return getCrc32() == calculateCrc(data, getSectionLength() - 1); return false; }
/*************************************************************************** * Function Name: parse_post_data * Description : This function parses HTTP POST data which is the contents of * a new image to write to flash memory. * Returns : UPLOAD_OK or UPLOAD_xxx error ***************************************************************************/ static char parse_post_data( int s, unsigned char *post_data_start, int post_data_length, unsigned char **image_start_ptr, int *image_len_ptr, int *image_format_ptr ) { char ret = UPLOAD_OK; char *p = post_data_start; int boundary_size = 0; /* Convert the start boundary field into a string. It will be compared * against the end boundary field below. */ while( *p != '\r' && *p != '\n' && ((int) p - (int) post_data_start) < post_data_length ) { p++; } if( *p == '\r' || *p == '\n' ) { *p++ = '\0'; boundary_size = strlen(post_data_start); } else { console_log("web error: HTTP POST start bound field not found."); ret = UPLOAD_FATAL; } /* Verify that a filename has been entered. */ if( ret == UPLOAD_OK ) { char *fname = NULL; while( memcmp( p, "\r\n\r\n", strlen("\r\n\r\n") ) ) { if( *p == 'f' && !memcmp( p, "filename=", strlen("filename=" ) ) ) { p += strlen("filename="); fname = p + 1; if( p[0] == '"' && p[1] != '"' ) { p++; while( *p != '"' && *p != '\r' && *p != '\n' ) p++; *p = '\0'; } else { console_log("web error: HTTP POST filename not specified."); ret = UPLOAD_FAIL_NO_FILENAME; } break; } p++; } if( fname == NULL ) { console_log("web error: HTTP POST filename field not found."); ret = UPLOAD_FATAL; } } /* Find the start of the image which starts after two consecutive * carriage return, linefeed pairs. */ if( ret == UPLOAD_OK ) { while( memcmp( p, "\r\n\r\n", strlen("\r\n\r\n") ) ) p++; p += strlen("\r\n\r\n"); if( p[0] != '\r' || p[1] != '\n' || memcmp(p + 2, post_data_start, boundary_size ) ) { *image_start_ptr = p; } else { console_log("web error: HTTP POST no image data."); ret = UPLOAD_FAIL_ILLEGAL_IMAGE; } } /* Find the end of the image which contains the same boundary field as * at the start of the buffer. */ if( ret == UPLOAD_OK ) { p = post_data_start + post_data_length - 1; while( *p == '\r' || *p == '\n' || *p == '-' ) p--; p[1] = '\0'; p -= boundary_size + 1; if( !memcmp( p + strlen("\r\n"), post_data_start, boundary_size ) ) *image_len_ptr = (int) p - (int) *image_start_ptr; else { console_log("web error: HTTP POST end bound field not found."); ret = UPLOAD_FATAL; } } /* Verify that the image is (or should be) a Broadcom flash format file or * a flash image format. */ if( ret == UPLOAD_OK ) { /* Align the image on a 16 byte boundary */ if( ((unsigned long) *image_start_ptr & 0x0f) != 0 ) { unsigned char *dest = (unsigned char *) ((unsigned long) *image_start_ptr & ~0x0f); unsigned char *src = *image_start_ptr; memmove( dest, src, *image_len_ptr ); *image_start_ptr = dest; } /* Check if the first part of the image is the Broadcom defined TAG * record. */ if( verifyTag( (FILE_TAG *) *image_start_ptr, 0 ) == -1 ) { /* It is not a Broadcom flash format file. Now check if it is a * flash image format file. A flash image format file must have a * CRC at the end of the image. */ unsigned char *image_ptr = *image_start_ptr; unsigned long image_len = (unsigned long) *image_len_ptr - TOKEN_LEN; unsigned long crc = CRC32_INIT_VALUE; crc = getCrc32(image_ptr, image_len, crc); if (memcmp(&crc, image_ptr + image_len, CRC_LEN) == 0) { console_log("web info: Upload %lu bytes, flash image format.", *image_len_ptr); *image_format_ptr = FLASH_IMAGE_FORMAT; } else { console_log("web info: Upload %lu bytes, invalid image format.", *image_len_ptr); ret = UPLOAD_FAIL_ILLEGAL_IMAGE; } } else { console_log("web info: Upload %lu bytes, Broadcom image format.", *image_len_ptr); *image_format_ptr = BROADCOM_IMAGE_FORMAT; } } return( ret ); } /* parse_post_data */