import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / drivers / net / wan / 8253x / crc32.c
1 /* -*- linux-c -*- */
2 /******************************************************************************
3  *  FILE:      crc32.c
4  *
5  *  Copyright: Telford Tools, Inc.
6  *             1996
7  *
8  * Copyright (C) 2001 By Joachim Martillo, Telford Tools, Inc.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version
13  * 2 of the License, or (at your option) any later version.
14  *
15  ******************************************************************************
16  */
17 /******************************************************************************
18  *  REVISION HISTORY: (Most Recent First)
19  *  -------------------------------------
20  *  2-Apr-91    ALEX    Introduced "fn_calc_novram_crc32()".
21  ******************************************************************************
22  */
23
24 /****************************************************/
25 /*                  header files                    */
26 /****************************************************/
27
28 #include "crc32dcl.h"
29 #include "endian.h"
30
31 /****************************************************/
32 /*                  constants                       */
33 /****************************************************/
34
35 #define k_poly              ((unsigned int)(0xedb88320))
36 #define k_crc_table_size    (256)
37
38 #if defined (AMD29K)
39 pragma Code ("rkernel");
40 pragma Off(cross_jump);
41 #endif
42
43 /****************************************************/
44 /*                      static data                 */
45 /****************************************************/
46
47 #if defined(AMD29K)
48 pragma Data (Export,"fastbss");
49 #endif // defined(AMD29K)
50
51 unsigned int gg_a_crc_table[k_crc_table_size];
52
53 #if defined(AMD29K)
54 pragma Data;
55 #endif // defined(AMD29K)
56
57
58 /****************************************************/
59 /*                  global procedures               */
60 /****************************************************/
61
62 void
63 fn_init_crc_table()
64 {
65         short   i_table;
66         
67         for (i_table = 0; i_table < k_crc_table_size; i_table++)
68         {
69                 unsigned int    result = 0;
70                 short   i_bit;
71                 
72                 for (i_bit = 0; i_bit < 8; i_bit++)
73                 {
74                         unsigned int    bit = ((i_table  & (1 << i_bit)) != 0);
75                         
76                         if ((bit ^ (result & 1)) != 0)
77                                 result = (result >> 1) ^ k_poly;
78                         else
79                                 result >>= 1;
80                 }
81                 
82                 gg_a_crc_table[i_table] = result;
83         }
84         
85 } /* end of fn_init_crc_table */
86
87 /****************************************************/
88
89 static unsigned int
90 fn_calc_memory_chunk_crc32(void *p, unsigned int n_bytes, unsigned int crc)
91 {
92         unsigned char    *p_uc   = (unsigned char*)p;
93         unsigned int   result  = ~crc;
94         
95         while (n_bytes-- > 0)
96         {
97                 result = (result >> 8) ^ gg_a_crc_table[(result ^ *p_uc++) & 0xff];
98         }
99         
100         return(~result);
101         
102 } /* end of fn_calc_memory_chunk_crc32 */
103
104 /****************************************************/
105
106 unsigned int
107 fn_calc_memory_crc32(void *p, unsigned int n_bytes)
108 {
109         fnm_assert_stmt(n_bytes > 4);
110         
111         return(fn_calc_memory_chunk_crc32(p, n_bytes, k_initial_crc_value));
112         
113 } /* end of fn_calc_memory_crc32 */
114
115 /****************************************************/
116
117 unsigned int
118 fn_check_memory_crc32(void *p, unsigned int n_bytes, unsigned int crc)
119 {
120         return(fn_calc_memory_crc32(p, n_bytes) == crc);
121         
122 } /* end of fn_check_memory_crc32 */
123
124
125 /****************************************************/
126 /* Adds current longword to the crc value and       */
127 /* returns that value.                              */
128 unsigned int
129 fn_update_crc(char *val, unsigned int crcval)
130 {
131         long i;
132         
133         /* ----< break long into bytes >---- */
134         /* ----< put bytes into crc >---- */
135         for (i = 0; i < 4; i++)
136         {
137                 crcval = gg_a_crc_table[(crcval ^ val[i]) & 0xff] ^
138                         ((crcval >> 8) & 0x00ffffff);
139         }
140         return(crcval);
141 }                                       /* endfunc--fn_update_crc */
142
143
144 /****************************************************/
145 /****************************************************/
146 /*          End source file "crc32.c"               */
147 /****************************************************/
148 /****************************************************/