static void process_file(const char *filename) { int i; FILE *fp; if (!(fp = fopen(filename, "rb"))) { fprintf(stderr, "! %s : %s\n", filename, strerror(errno)); return; } fseek(fp, 0, SEEK_END); long size = ftell(fp); rewind(fp); printf("file name : %s, file size : %ld, CRC32: ", filename, size); unsigned char *buf = (unsigned char *) malloc(size); if (buf == NULL) { fprintf(stderr, "%s:%d: malloc failed\n", __FUNCTION__, __LINE__); exit(EXIT_FAILURE); } long count = fread(buf, 1, size, fp); assert(count == size); CRC32_t crc; CRC32_Init(&crc); CRC32_Update(&crc, buf, count); unsigned char crc_out[4]; CRC32_Final(crc_out, crc); for (i = 0; i < 4; i++) { printf("%02x ", crc_out[i]); } printf("\n"); fclose(fp); }
BOOL WINAPI SFileVerifyFile(HANDLE hMpq, const char * szFileName, DWORD dwFlags) { crc32_context crc32_ctx; md5_context md5_ctx; TMPQFile * hf; TMPQCRC32 Crc32; TMPQMD5 Md5; BYTE Buffer[0x1000]; HANDLE hFile = NULL; DWORD dwBytesRead; BOOL bResult = TRUE; // Attempt to open the file if(SFileOpenFileEx(hMpq, szFileName, 0, &hFile)) { // Initialize the CRC32 and MD5 counters CRC32_Init(&crc32_ctx); MD5_Init(&md5_ctx); hf = (TMPQFile *)hFile; // Go through entire file and update both CRC32 and MD5 for(;;) { // Read data from file SFileReadFile(hFile, Buffer, sizeof(Buffer), &dwBytesRead, NULL); if(dwBytesRead == 0) break; // Update CRC32 value if(dwFlags & MPQ_ATTRIBUTE_CRC32) CRC32_Update(&crc32_ctx, Buffer, (int)dwBytesRead); // Update MD5 value if(dwFlags & MPQ_ATTRIBUTE_MD5) MD5_Update(&md5_ctx, Buffer, (int)dwBytesRead); } // Check if the CRC32 matches if((dwFlags & MPQ_ATTRIBUTE_CRC32) && hf->pCrc32 != NULL) { CRC32_Finish(&crc32_ctx, (unsigned long *)&Crc32.dwValue); if(Crc32.dwValue != hf->pCrc32->dwValue) bResult = FALSE; } // Check if MD5 matches if((dwFlags & MPQ_ATTRIBUTE_MD5) && hf->pMd5 != NULL) { MD5_Finish(&md5_ctx, Md5.Value); if(memcmp(Md5.Value, hf->pMd5->Value, sizeof(TMPQMD5))) bResult = FALSE; } SFileCloseFile(hFile); } return bResult; }
static int cfputc(int c, FILE *stream) { unsigned char ch; ch = c; CRC32_Update(&checksum, &ch, 1); return fputc(c, stream); }
static int sevenzip_decrypt(unsigned char *derived_key, unsigned char *data) { unsigned char out[cur_salt->length]; AES_KEY akey; unsigned char iv[16]; union { unsigned char crcc[4]; unsigned int crci; } _crc_out; unsigned char *crc_out = _crc_out.crcc; unsigned int ccrc; CRC32_t crc; int i; int nbytes, margin; memcpy(iv, cur_salt->iv, 16); if(AES_set_decrypt_key(derived_key, 256, &akey) < 0) { fprintf(stderr, "AES_set_decrypt_key failed in crypt!\n"); } AES_cbc_encrypt(cur_salt->data, out, cur_salt->length, &akey, iv, AES_DECRYPT); /* various verifications tests */ // test 0, padding check, bad hack :-( margin = nbytes = cur_salt->length - cur_salt->unpacksize; i = cur_salt->length - 1; while (nbytes > 0) { if (out[i] != 0) return -1; nbytes--; i--; } if (margin > 7) { // printf("valid padding test ;-)\n"); // print_hex(out, cur_salt->length); return 0; } // test 1, CRC test CRC32_Init(&crc); CRC32_Update(&crc, out, cur_salt->unpacksize); CRC32_Final(crc_out, crc); ccrc = _crc_out.crci; // computed CRC if (ccrc == cur_salt->crc) return 0; // XXX don't be too eager! // XXX test 2, "well-formed folder" test if (validFolder(out)) { printf("validFolder check ;-)\n"); return 0; } return -1; }
static void charset_checksum_header(struct charset_header *header) { CRC32_Update(&checksum, header->version, sizeof(header->version)); CRC32_Update(&checksum, &header->min, 1); CRC32_Update(&checksum, &header->max, 1); CRC32_Update(&checksum, &header->length, 1); CRC32_Update(&checksum, &header->count, 1); CRC32_Update(&checksum, header->offsets, sizeof(header->offsets)); CRC32_Update(&checksum, header->order, sizeof(header->order)); CRC32_Final(header->check, checksum); }