resetting manifest requested domain to floor
[platform/upstream/gptfdisk.git] / crc32.cc
1 /*\r
2  * efone - Distributed internet phone system.\r
3  *\r
4  * (c) 1999,2000 Krzysztof Dabrowski\r
5  * (c) 1999,2000 ElysiuM deeZine\r
6  *\r
7  * This program is free software; you can redistribute it and/or\r
8  * modify it under the terms of the GNU General Public License\r
9  * as published by the Free Software Foundation; either version\r
10  * 2 of the License, or (at your option) any later version.\r
11  *\r
12  */\r
13 \r
14 /* based on implementation by Finn Yannick Jacobs */\r
15 \r
16 #include <stdio.h>\r
17 #include <stdlib.h>\r
18 #include <sys/types.h>\r
19 #include "crc32.h"\r
20 \r
21 /* crc_tab[] -- this crcTable is being build by chksum_crc32GenTab().\r
22  *              so make sure, you call it before using the other\r
23  *              functions!\r
24  */\r
25 uint32_t crc_tab[256];\r
26 \r
27 /* chksum_crc() -- to a given block, this one calculates the\r
28  *                              crc32-checksum until the length is\r
29  *                              reached. the crc32-checksum will be\r
30  *                              the result.\r
31  */\r
32 uint32_t chksum_crc32 (unsigned char *block, unsigned int length)\r
33 {\r
34    register unsigned long crc;\r
35    unsigned long i;\r
36 \r
37    crc = 0xFFFFFFFF;\r
38    for (i = 0; i < length; i++)\r
39    {\r
40       crc = ((crc >> 8) & 0x00FFFFFF) ^ crc_tab[(crc ^ *block++) & 0xFF];\r
41    }\r
42    return (crc ^ 0xFFFFFFFF);\r
43 }\r
44 \r
45 /* chksum_crc32gentab() --      to a global crc_tab[256], this one will\r
46  *                              calculate the crcTable for crc32-checksums.\r
47  *                              it is generated to the polynom [..]\r
48  */\r
49 \r
50 void chksum_crc32gentab ()\r
51 {\r
52    unsigned long crc, poly;\r
53    int i, j;\r
54 \r
55    poly = 0xEDB88320L;\r
56    for (i = 0; i < 256; i++)\r
57    {\r
58       crc = i;\r
59       for (j = 8; j > 0; j--)\r
60       {\r
61          if (crc & 1)\r
62          {\r
63             crc = (crc >> 1) ^ poly;\r
64          }\r
65          else\r
66          {\r
67             crc >>= 1;\r
68          }\r
69       }\r
70       crc_tab[i] = crc;\r
71    }\r
72 }\r