742{
743
744 if (buf ==
Z_NULL)
return 0;
745
746#ifdef DYNAMIC_CRC_TABLE
747 once(&made, make_crc_table);
748#endif
749
750
751 crc ^= 0xffffffff;
752
753#ifdef W
754
755
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
763 while (len && ((z_size_t)buf & (
W - 1)) != 0) {
764 len--;
765 crc = (crc >> 8) ^
crc_table[(crc ^ *buf++) & 0xff];
766 }
767
768
769 blks = len / (
N *
W);
771 words = (z_word_t const *)buf;
772
773
774
775
776
777 endian = 1;
778 if (*(unsigned char *)&endian) {
779
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
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
824
825
826 while (--blks) {
827
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
845
846
847
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
886
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
905 }
906 else {
907
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
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
946
947
948 while (--blks) {
949
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
967
968
969
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
1008
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
1027 crc = byte_swap(comb);
1028 }
1029
1030
1031
1032
1033 buf = (unsigned char const *)words;
1034 }
1035
1036#endif
1037
1038
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
1056 return crc ^ 0xffffffff;
1057}
const z_crc_t FAR crc_table[]