// Compute the checksum of the chunk pointer and its header. static INLINE u16 computeChecksum(const void *Ptr, UnpackedHeader *Header) { UnpackedHeader ZeroChecksumHeader = *Header; ZeroChecksumHeader.Checksum = 0; uptr HeaderHolder[sizeof(UnpackedHeader) / sizeof(uptr)]; memcpy(&HeaderHolder, &ZeroChecksumHeader, sizeof(HeaderHolder)); const u32 Crc = computeCRC32(Cookie, reinterpret_cast<uptr>(Ptr), HeaderHolder, ARRAY_SIZE(HeaderHolder)); return static_cast<u16>(Crc); }
int command_check(const char *filename) { int ci, ti; unsigned long compcrc; unsigned long matchcrc; char *string; char results[9]; char *regex_errbuf = NULL; size_t regex_errbuf_size; regmatch_t rmatch; regex_t regex; if(access(filename, F_OK | R_OK) != 0) LERROR(ExitArgumentError, errno, "%s", filename); if((string = basename((char*)filename)) == NULL) LERROR(ExitArgumentError, 0, "could not extract specified path's basename."); /* compile regex */ if((ci = regcomp(®ex, crcregex, REG_ICASE)) != 0) { regex_errbuf_size = regerror(ci, ®ex, NULL, 0); regex_errbuf = malloc(regex_errbuf_size); regerror(ci, ®ex, regex_errbuf, regex_errbuf_size); if(regex_errbuf == NULL) LERROR(ExitRegexError, 0, "regex error: failed to allocate memory for the regex error message."); LERROR(ExitRegexError, 0, "%s", regex_errbuf); free(regex_errbuf); // not reached } /* match CRC32 hexstrings */ switch(regexec((const regex_t*) ®ex, string, 1, &rmatch, 0)) { case 0: for(ci = rmatch.rm_so, ti = 0; ci < rmatch.rm_eo; ++ci) results[ti++] = string[ci]; results[ti] = '\0'; break; case REG_NOMATCH: LERROR(ExitNoMatch, 0, "the filename does not contain a CRC32 hexstring."); return ExitNoMatch; // Not reached } regfree(®ex); /* eval */ compcrc = computeCRC32(filename); matchcrc = (unsigned long) strtol(results, NULL, 16); if(compcrc != matchcrc) { printf("mismatch: filename(%08lX) != computed(%08lX)\n", matchcrc, compcrc); return ExitNoMatch; } else { printf("match: filename(%08lX) == computed(%08lX)\n", matchcrc, compcrc); return ExitMatch; } }
int main(int argc, char **argv) { uint32_t crcNEW = 0; unsigned long crcOLD = 0L; crcNEW = compute_crc32(argv[argc-1]); crcOLD = computeCRC32(argv[argc-1]); printf("crcNEW = %08X\ncrcOLD = %08lX\n", crcNEW, crcOLD); printf("sizeof(uint32_t)=%" PRIuPTR, sizeof(uint32_t)); puts(""); printf("sizeof(unsigned long)=%" PRIuPTR, sizeof(unsigned long)); puts(""); return 0; }
int main(int argc, char **argv) { /* parse arguments */ if(argc == 1) error(1, 0, "Too few arguments. Specify the -h option for usage information."); QCoreApplication qcapp(argc, argv); QStringList args = qcapp.arguments(); bool options_ended = false; QString farg; args.removeFirst(); // executable name Q_FOREACH(const QString &arg, args) { if(arg == QString("--")) { if(options_ended) { farg = arg; break; } else { options_ended = true; continue; } } if(arg == QString("-h")) { if(options_ended) { farg = arg; break; } else { usage(); return EXIT_SUCCESS; } } farg = arg; break; } if(farg.isEmpty()) ERROR(EXIT_ARGUMENT_ERROR, "Too few arguments: missing file argument."); /* test for file access, find CRC32 sequence in filename */ QFileInfo fi(farg); QString basename; QStringList crcmatches; QRegExp regex("([a-fA-F0-9]{8})"); if(!fi.exists()) ERROR(EXIT_FILE_ACCESS_ERROR, "file <%s> does not exist.", qPrintable(farg)); if(!fi.isReadable()) ERROR(EXIT_FILE_ACCESS_ERROR, "file <%s> is not readable.", qPrintable(farg)); basename = fi.fileName(); int idx = 0; while((idx = basename.indexOf(regex, idx)) != -1) { crcmatches.append(regex.cap(1)); idx += regex.matchedLength(); } if(crcmatches.length() == 0) ERROR(EXIT_REGEX_NOMATCH, "The filename <%s> does not contain any CRC32 checksums.", qPrintable(farg)); /* compute CRC32 and compare to the matches in the filename */ bool dirty = false; bool havematch = false; unsigned long crcsum = computeCRC32(qPrintable(fi.absoluteFilePath())); Q_FOREACH(const QString &match, crcmatches) { unsigned long matchsum = match.toULong(0, 16); if(crcsum != matchsum) { dirty = true; printf("CRC32 mismatch: computed checksum [%08lX] != matched checksum [%08lX] (dirty: %d)\n", crcsum, matchsum, dirty); } else { printf("CRC32 match: computed checksum [%08lX] == matched checksum [%08lx] (dirty: %d)\n", matchsum, crcsum, dirty); havematch = true; } }