MCPcopy Create free account
hub / github.com/DescentDevelopers/Descent3 / cf_CalculateFileCRC

Function cf_CalculateFileCRC

cfile/cfile.cpp:900–945  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

898#define CRC_BUFFER_SIZE 5000
899
900uint32_t cf_CalculateFileCRC(CFILE *infile) {
901 int i, j;
902 uint8_t crcbuf[CRC_BUFFER_SIZE];
903 static bool Cfile_crc_calculated = false;
904 static uint32_t CRCTable[256];
905 uint32_t crc;
906 uint32_t temp1;
907 uint32_t temp2;
908 uint32_t readlen;
909
910 // Only make the lookup table once
911 if (!Cfile_crc_calculated) {
912 Cfile_crc_calculated = true;
913
914 for (i = 0; i <= 255; i++) {
915 crc = i;
916 for (j = 8; j > 0; j--) {
917 if (crc & 1)
918 crc = (crc >> 1) ^ CRC32_POLYNOMIAL;
919 else
920 crc >>= 1;
921 }
922 CRCTable[i] = crc;
923 }
924 }
925
926 crc = 0xffffffffl;
927 while (!cfeof(infile)) {
928 if ((infile->size - infile->position) < CRC_BUFFER_SIZE)
929 readlen = infile->size - infile->position;
930 else
931 readlen = CRC_BUFFER_SIZE;
932 if (!cf_ReadBytes(crcbuf, readlen, infile)) {
933 // Doh, error time!
934 Int3();
935 return 0xFFFFFFFF;
936 }
937 for (uint32_t a = 0; a < readlen; a++) {
938 temp1 = (crc >> 8) & 0x00FFFFFFL;
939 temp2 = CRCTable[((int)crc ^ crcbuf[a]) & 0xff];
940 crc = temp1 ^ temp2;
941 }
942 }
943
944 return crc ^ 0xffffffffl;
945}
946
947uint32_t cf_GetfileCRC(const std::filesystem::path &src) {
948 CFILE *infile = cfopen(src, "rb");

Callers 2

cf_GetfileCRCFunction · 0.85
TESTFunction · 0.85

Calls 2

cfeofFunction · 0.85
cf_ReadBytesFunction · 0.85

Tested by 1

TESTFunction · 0.68