Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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 56 of file crc32.c.

◆ POLY

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

Definition at line 140 of file crc32.c.

◆ W

#define W   4

Definition at line 84 of file crc32.c.

Function Documentation

◆ crc32()

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

Definition at line 1062 of file crc32.c.

1066{
1067 return crc32_z(crc, buf, len);
1068}
unsigned long ZEXPORT crc32_z(unsigned long crc, const unsigned char FAR *buf, z_size_t len)
Definition: crc32.c:738

Referenced by deflate(), deflateResetKeep(), inflate(), and read_buf().

◆ crc32_combine()

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

Definition at line 1083 of file crc32.c.

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

◆ crc32_combine64()

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

Definition at line 1071 of file crc32.c.

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

Referenced by crc32_combine().

◆ crc32_combine_gen()

uLong ZEXPORT crc32_combine_gen ( z_off_t  len2)

Definition at line 1102 of file crc32.c.

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

◆ crc32_combine_gen64()

uLong ZEXPORT crc32_combine_gen64 ( z_off64_t  len2)

Definition at line 1092 of file crc32.c.

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

Referenced by crc32_combine_gen().

◆ crc32_combine_op()

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

Definition at line 1109 of file crc32.c.

1113{
1114 return multmodp(op, crc1) ^ crc2;
1115}

◆ crc32_z()

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

Definition at line 738 of file crc32.c.

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

Referenced by crc32().

◆ get_crc_table()

const z_crc_t FAR *ZEXPORT get_crc_table ( )

Definition at line 585 of file crc32.c.

586{
587#ifdef DYNAMIC_CRC_TABLE
588 once(&made, make_crc_table);
589#endif /* DYNAMIC_CRC_TABLE */
590 return (const z_crc_t FAR *)crc_table;
591}

◆ multmodp()

z_crc_t multmodp ( z_crc_t  a,
z_crc_t  b 
)

Definition at line 541 of file crc32.c.

544{
545 z_crc_t m, p;
546
547 m = (z_crc_t)1 << 31;
548 p = 0;
549 for (;;) {
550 if (a & m) {
551 p ^= b;
552 if ((a & (m - 1)) == 0)
553 break;
554 }
555 m >>= 1;
556 b = b & 1 ? (b >> 1) ^ POLY : b >> 1;
557 }
558 return p;
559}
#define POLY
Definition: crc32.c:140

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

◆ OF() [1/2]

z_crc_t multmodp OF ( (z_crc_t a, z_crc_t b)  )

◆ OF() [2/2]

z_crc_t x2nmodp OF ( (z_off64_t n, unsigned k)  )

◆ x2nmodp()

z_crc_t x2nmodp ( z_off64_t  n,
unsigned  k 
)

Definition at line 565 of file crc32.c.

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

Referenced by crc32_combine64(), and crc32_combine_gen64().