Rizin
unix-like reverse engineering framework and cli tools
crc32.c File Reference
#include "zutil.h"
#include "crc32.h"

Go to the source code of this file.

Macros

#define N   5
 
#define W   4
 
#define POLY   0xedb88320 /* p(x) reflected, with x^32 implied */
 

Functions

z_crc_t multmodp OF ((z_crc_t a, z_crc_t b))
 
z_crc_t x2nmodp OF ((z_off64_t n, unsigned k))
 
z_crc_t multmodp (z_crc_t a, z_crc_t b)
 
z_crc_t x2nmodp (z_off64_t n, unsigned k)
 
const z_crc_t FAR *ZEXPORT get_crc_table ()
 
unsigned long ZEXPORT crc32_z (unsigned long crc, const unsigned char FAR *buf, z_size_t len)
 
unsigned long ZEXPORT crc32 (unsigned long crc, const unsigned char FAR *buf, uInt len)
 
uLong ZEXPORT crc32_combine64 (uLong crc1, uLong crc2, z_off64_t len2)
 
uLong ZEXPORT crc32_combine (uLong crc1, uLong crc2, z_off_t len2)
 
uLong ZEXPORT crc32_combine_gen64 (z_off64_t len2)
 
uLong ZEXPORT crc32_combine_gen (z_off_t len2)
 
uLong crc32_combine_op (uLong crc1, uLong crc2, uLong op)
 

Macro Definition Documentation

◆ N

#define N   5

Definition at line 57 of file crc32.c.

◆ POLY

#define POLY   0xedb88320 /* p(x) reflected, with x^32 implied */

Definition at line 141 of file crc32.c.

◆ W

#define W   4

Definition at line 85 of file crc32.c.

Function Documentation

◆ crc32()

unsigned long ZEXPORT crc32 ( unsigned long  crc,
const unsigned char FAR buf,
uInt  len 
)

Definition at line 1063 of file crc32.c.

1067 {
1068  return crc32_z(crc, buf, len);
1069 }
size_t len
Definition: 6502dis.c:15
voidpf void * buf
Definition: ioapi.h:138
unsigned long ZEXPORT crc32_z(unsigned long crc, const unsigned char FAR *buf, z_size_t len)
Definition: crc32.c:739

References crc32_z(), and len.

Referenced by _zip_string_crc32(), crc_read(), deflate(), deflateResetKeep(), gunpipe(), gzinit(), gzscan(), gztack(), inflate(), log_append(), LZ4F_makeBlock(), oabd_sys_write(), out(), read_buf(), search_collisions(), test_file(), unzReadCurrentFile(), update_keys(), zip_source_crc_create(), zipCloseFileInZipRaw(), zipCloseFileInZipRaw64(), and zipWriteInFileInZip().

◆ crc32_combine()

uLong ZEXPORT crc32_combine ( uLong  crc1,
uLong  crc2,
z_off_t  len2 
)

Definition at line 1084 of file crc32.c.

1088 {
1089  return crc32_combine64(crc1, crc2, len2);
1090 }
uLong ZEXPORT crc32_combine64(uLong crc1, uLong crc2, z_off64_t len2)
Definition: crc32.c:1072

References crc32_combine64().

Referenced by gzcopy(), and gzip_normalize().

◆ crc32_combine64()

uLong ZEXPORT crc32_combine64 ( uLong  crc1,
uLong  crc2,
z_off64_t  len2 
)

Definition at line 1072 of file crc32.c.

1076 {
1077 #ifdef DYNAMIC_CRC_TABLE
1078  once(&made, make_crc_table);
1079 #endif /* DYNAMIC_CRC_TABLE */
1080  return multmodp(x2nmodp(len2, 3), crc1) ^ crc2;
1081 }
static uv_once_t once
Definition: threadpool.c:32
z_crc_t x2nmodp(z_off64_t n, unsigned k)
Definition: crc32.c:566
z_crc_t multmodp(z_crc_t a, z_crc_t b)
Definition: crc32.c:542

References multmodp(), once, and x2nmodp().

Referenced by crc32_combine().

◆ crc32_combine_gen()

uLong ZEXPORT crc32_combine_gen ( z_off_t  len2)

Definition at line 1103 of file crc32.c.

1105 {
1106  return crc32_combine_gen64(len2);
1107 }
uLong ZEXPORT crc32_combine_gen64(z_off64_t len2)
Definition: crc32.c:1093

References crc32_combine_gen64().

◆ crc32_combine_gen64()

uLong ZEXPORT crc32_combine_gen64 ( z_off64_t  len2)

Definition at line 1093 of file crc32.c.

1095 {
1096 #ifdef DYNAMIC_CRC_TABLE
1097  once(&made, make_crc_table);
1098 #endif /* DYNAMIC_CRC_TABLE */
1099  return x2nmodp(len2, 3);
1100 }

References once, and x2nmodp().

Referenced by crc32_combine_gen().

◆ crc32_combine_op()

uLong crc32_combine_op ( uLong  crc1,
uLong  crc2,
uLong  op 
)

Definition at line 1110 of file crc32.c.

1114 {
1115  return multmodp(op, crc1) ^ crc2;
1116 }
Definition: dis.c:32

References multmodp().

◆ crc32_z()

unsigned long ZEXPORT crc32_z ( unsigned long  crc,
const unsigned char FAR buf,
z_size_t  len 
)

Definition at line 739 of file crc32.c.

743 {
744  /* Return initial CRC, if requested. */
745  if (buf == Z_NULL) return 0;
746 
747 #ifdef DYNAMIC_CRC_TABLE
748  once(&made, make_crc_table);
749 #endif /* DYNAMIC_CRC_TABLE */
750 
751  /* Pre-condition the CRC */
752  crc ^= 0xffffffff;
753 
754 #ifdef W
755 
756  /* If provided enough bytes, do a braided CRC calculation. */
757  if (len >= N * W + W - 1) {
758  z_size_t blks;
759  z_word_t const *words;
760  unsigned endian;
761  int k;
762 
763  /* Compute the CRC up to a z_word_t boundary. */
764  while (len && ((z_size_t)buf & (W - 1)) != 0) {
765  len--;
766  crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
767  }
768 
769  /* Compute the CRC on as many N z_word_t blocks as are available. */
770  blks = len / (N * W);
771  len -= blks * N * W;
772  words = (z_word_t const *)buf;
773 
774  /* Do endian check at execution time instead of compile time, since ARM
775  processors can change the endianess at execution time. If the
776  compiler knows what the endianess will be, it can optimize out the
777  check and the unused branch. */
778  endian = 1;
779  if (*(unsigned char *)&endian) {
780  /* Little endian. */
781 
782  z_crc_t crc0;
783  z_word_t word0;
784 #if N > 1
785  z_crc_t crc1;
786  z_word_t word1;
787 #if N > 2
788  z_crc_t crc2;
789  z_word_t word2;
790 #if N > 3
791  z_crc_t crc3;
792  z_word_t word3;
793 #if N > 4
794  z_crc_t crc4;
795  z_word_t word4;
796 #if N > 5
797  z_crc_t crc5;
798  z_word_t word5;
799 #endif
800 #endif
801 #endif
802 #endif
803 #endif
804 
805  /* Initialize the CRC for each braid. */
806  crc0 = crc;
807 #if N > 1
808  crc1 = 0;
809 #if N > 2
810  crc2 = 0;
811 #if N > 3
812  crc3 = 0;
813 #if N > 4
814  crc4 = 0;
815 #if N > 5
816  crc5 = 0;
817 #endif
818 #endif
819 #endif
820 #endif
821 #endif
822 
823  /*
824  Process the first blks-1 blocks, computing the CRCs on each braid
825  independently.
826  */
827  while (--blks) {
828  /* Load the word for each braid into registers. */
829  word0 = crc0 ^ words[0];
830 #if N > 1
831  word1 = crc1 ^ words[1];
832 #if N > 2
833  word2 = crc2 ^ words[2];
834 #if N > 3
835  word3 = crc3 ^ words[3];
836 #if N > 4
837  word4 = crc4 ^ words[4];
838 #if N > 5
839  word5 = crc5 ^ words[5];
840 #endif
841 #endif
842 #endif
843 #endif
844 #endif
845  words += N;
846 
847  /* Compute and update the CRC for each word. The loop should
848  get unrolled. */
849  crc0 = crc_braid_table[0][word0 & 0xff];
850 #if N > 1
851  crc1 = crc_braid_table[0][word1 & 0xff];
852 #if N > 2
853  crc2 = crc_braid_table[0][word2 & 0xff];
854 #if N > 3
855  crc3 = crc_braid_table[0][word3 & 0xff];
856 #if N > 4
857  crc4 = crc_braid_table[0][word4 & 0xff];
858 #if N > 5
859  crc5 = crc_braid_table[0][word5 & 0xff];
860 #endif
861 #endif
862 #endif
863 #endif
864 #endif
865  for (k = 1; k < W; k++) {
866  crc0 ^= crc_braid_table[k][(word0 >> (k << 3)) & 0xff];
867 #if N > 1
868  crc1 ^= crc_braid_table[k][(word1 >> (k << 3)) & 0xff];
869 #if N > 2
870  crc2 ^= crc_braid_table[k][(word2 >> (k << 3)) & 0xff];
871 #if N > 3
872  crc3 ^= crc_braid_table[k][(word3 >> (k << 3)) & 0xff];
873 #if N > 4
874  crc4 ^= crc_braid_table[k][(word4 >> (k << 3)) & 0xff];
875 #if N > 5
876  crc5 ^= crc_braid_table[k][(word5 >> (k << 3)) & 0xff];
877 #endif
878 #endif
879 #endif
880 #endif
881 #endif
882  }
883  }
884 
885  /*
886  Process the last block, combining the CRCs of the N braids at the
887  same time.
888  */
889  crc = crc_word(crc0 ^ words[0]);
890 #if N > 1
891  crc = crc_word(crc1 ^ words[1] ^ crc);
892 #if N > 2
893  crc = crc_word(crc2 ^ words[2] ^ crc);
894 #if N > 3
895  crc = crc_word(crc3 ^ words[3] ^ crc);
896 #if N > 4
897  crc = crc_word(crc4 ^ words[4] ^ crc);
898 #if N > 5
899  crc = crc_word(crc5 ^ words[5] ^ crc);
900 #endif
901 #endif
902 #endif
903 #endif
904 #endif
905  words += N;
906  }
907  else {
908  /* Big endian. */
909 
910  z_word_t crc0, word0, comb;
911 #if N > 1
912  z_word_t crc1, word1;
913 #if N > 2
914  z_word_t crc2, word2;
915 #if N > 3
916  z_word_t crc3, word3;
917 #if N > 4
918  z_word_t crc4, word4;
919 #if N > 5
920  z_word_t crc5, word5;
921 #endif
922 #endif
923 #endif
924 #endif
925 #endif
926 
927  /* Initialize the CRC for each braid. */
928  crc0 = byte_swap(crc);
929 #if N > 1
930  crc1 = 0;
931 #if N > 2
932  crc2 = 0;
933 #if N > 3
934  crc3 = 0;
935 #if N > 4
936  crc4 = 0;
937 #if N > 5
938  crc5 = 0;
939 #endif
940 #endif
941 #endif
942 #endif
943 #endif
944 
945  /*
946  Process the first blks-1 blocks, computing the CRCs on each braid
947  independently.
948  */
949  while (--blks) {
950  /* Load the word for each braid into registers. */
951  word0 = crc0 ^ words[0];
952 #if N > 1
953  word1 = crc1 ^ words[1];
954 #if N > 2
955  word2 = crc2 ^ words[2];
956 #if N > 3
957  word3 = crc3 ^ words[3];
958 #if N > 4
959  word4 = crc4 ^ words[4];
960 #if N > 5
961  word5 = crc5 ^ words[5];
962 #endif
963 #endif
964 #endif
965 #endif
966 #endif
967  words += N;
968 
969  /* Compute and update the CRC for each word. The loop should
970  get unrolled. */
971  crc0 = crc_braid_big_table[0][word0 & 0xff];
972 #if N > 1
973  crc1 = crc_braid_big_table[0][word1 & 0xff];
974 #if N > 2
975  crc2 = crc_braid_big_table[0][word2 & 0xff];
976 #if N > 3
977  crc3 = crc_braid_big_table[0][word3 & 0xff];
978 #if N > 4
979  crc4 = crc_braid_big_table[0][word4 & 0xff];
980 #if N > 5
981  crc5 = crc_braid_big_table[0][word5 & 0xff];
982 #endif
983 #endif
984 #endif
985 #endif
986 #endif
987  for (k = 1; k < W; k++) {
988  crc0 ^= crc_braid_big_table[k][(word0 >> (k << 3)) & 0xff];
989 #if N > 1
990  crc1 ^= crc_braid_big_table[k][(word1 >> (k << 3)) & 0xff];
991 #if N > 2
992  crc2 ^= crc_braid_big_table[k][(word2 >> (k << 3)) & 0xff];
993 #if N > 3
994  crc3 ^= crc_braid_big_table[k][(word3 >> (k << 3)) & 0xff];
995 #if N > 4
996  crc4 ^= crc_braid_big_table[k][(word4 >> (k << 3)) & 0xff];
997 #if N > 5
998  crc5 ^= crc_braid_big_table[k][(word5 >> (k << 3)) & 0xff];
999 #endif
1000 #endif
1001 #endif
1002 #endif
1003 #endif
1004  }
1005  }
1006 
1007  /*
1008  Process the last block, combining the CRCs of the N braids at the
1009  same time.
1010  */
1011  comb = crc_word_big(crc0 ^ words[0]);
1012 #if N > 1
1013  comb = crc_word_big(crc1 ^ words[1] ^ comb);
1014 #if N > 2
1015  comb = crc_word_big(crc2 ^ words[2] ^ comb);
1016 #if N > 3
1017  comb = crc_word_big(crc3 ^ words[3] ^ comb);
1018 #if N > 4
1019  comb = crc_word_big(crc4 ^ words[4] ^ comb);
1020 #if N > 5
1021  comb = crc_word_big(crc5 ^ words[5] ^ comb);
1022 #endif
1023 #endif
1024 #endif
1025 #endif
1026 #endif
1027  words += N;
1028  crc = byte_swap(comb);
1029  }
1030 
1031  /*
1032  Update the pointer to the remaining bytes to process.
1033  */
1034  buf = (unsigned char const *)words;
1035  }
1036 
1037 #endif /* W */
1038 
1039  /* Complete the computation of the CRC on any remaining bytes. */
1040  while (len >= 8) {
1041  len -= 8;
1042  crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
1043  crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
1044  crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
1045  crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
1046  crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
1047  crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
1048  crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
1049  crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
1050  }
1051  while (len) {
1052  len--;
1053  crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
1054  }
1055 
1056  /* Return the CRC, post-conditioned. */
1057  return crc ^ 0xffffffff;
1058 }
const char * k
Definition: dsignal.c:11
unsigned long z_crc_t
Definition: zconf.h:431
unsigned long z_size_t
Definition: zconf.h:250
#define N
Definition: crc32.c:57
#define W
Definition: crc32.c:85
const z_crc_t FAR crc_table[]
Definition: crc32.h:5
#define Z_NULL
Definition: zlib.h:212

References crc_table, k, len, N, once, W, and Z_NULL.

Referenced by crc32(), and getFileCrc().

◆ get_crc_table()

const z_crc_t FAR* ZEXPORT get_crc_table ( )

Definition at line 586 of file crc32.c.

587 {
588 #ifdef DYNAMIC_CRC_TABLE
589  once(&made, make_crc_table);
590 #endif /* DYNAMIC_CRC_TABLE */
591  return (const z_crc_t FAR *)crc_table;
592 }
#define FAR
Definition: zconf.h:387

References crc_table, FAR, and once.

Referenced by unzOpenCurrentFile3(), and zipOpenNewFileInZip4_64().

◆ multmodp()

z_crc_t multmodp ( z_crc_t  a,
z_crc_t  b 
)

Definition at line 542 of file crc32.c.

545 {
546  z_crc_t m, p;
547 
548  m = (z_crc_t)1 << 31;
549  p = 0;
550  for (;;) {
551  if (a & m) {
552  p ^= b;
553  if ((a & (m - 1)) == 0)
554  break;
555  }
556  m >>= 1;
557  b = b & 1 ? (b >> 1) ^ POLY : b >> 1;
558  }
559  return p;
560 }
void * p
Definition: libc.cpp:67
#define b(i)
Definition: sha256.c:42
#define a(i)
Definition: sha256.c:41
#define POLY
Definition: crc32.c:141

References a, b, regress::m, p, and POLY.

Referenced by crc32_combine64(), crc32_combine_op(), and x2nmodp().

◆ OF() [1/2]

◆ OF() [2/2]

◆ x2nmodp()

z_crc_t x2nmodp ( z_off64_t  n,
unsigned  k 
)

Definition at line 566 of file crc32.c.

569 {
570  z_crc_t p;
571 
572  p = (z_crc_t)1 << 31; /* x^0 == 1 */
573  while (n) {
574  if (n & 1)
575  p = multmodp(x2n_table[k & 31], p);
576  n >>= 1;
577  k++;
578  }
579  return p;
580 }
int n
Definition: mipsasm.c:19
const z_crc_t FAR x2n_table[]
Definition: crc32.h:9439

References k, multmodp(), n, p, and x2n_table.

Referenced by crc32_combine64(), and crc32_combine_gen64().