| 1 | /************************************************************************** |
|---|
| 2 | * Copyright (C) 2004-2007 by Michael Medin <michael@medin.name> * |
|---|
| 3 | * * |
|---|
| 4 | * This code is part of NSClient++ - http://trac.nakednuns.org/nscp * |
|---|
| 5 | * * |
|---|
| 6 | * This program is free software; you can redistribute it and/or modify * |
|---|
| 7 | * it under the terms of the GNU General Public License as published by * |
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
|---|
| 9 | * (at your option) any later version. * |
|---|
| 10 | * * |
|---|
| 11 | * This program is distributed in the hope that it will be useful, * |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
|---|
| 14 | * GNU General Public License for more details. * |
|---|
| 15 | * * |
|---|
| 16 | * You should have received a copy of the GNU General Public License * |
|---|
| 17 | * along with this program; if not, write to the * |
|---|
| 18 | * Free Software Foundation, Inc., * |
|---|
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
|---|
| 20 | ***************************************************************************/ |
|---|
| 21 | |
|---|
| 22 | #include <utils.h> |
|---|
| 23 | |
|---|
| 24 | static unsigned long crc32_table[256]; |
|---|
| 25 | static bool hascrc32 = false; |
|---|
| 26 | void generate_crc32_table(void){ |
|---|
| 27 | unsigned long crc, poly; |
|---|
| 28 | int i, j; |
|---|
| 29 | poly=0xEDB88320L; |
|---|
| 30 | for(i=0;i<256;i++){ |
|---|
| 31 | crc=i; |
|---|
| 32 | for(j=8;j>0;j--){ |
|---|
| 33 | if(crc & 1) |
|---|
| 34 | crc=(crc>>1)^poly; |
|---|
| 35 | else |
|---|
| 36 | crc>>=1; |
|---|
| 37 | } |
|---|
| 38 | crc32_table[i]=crc; |
|---|
| 39 | } |
|---|
| 40 | hascrc32 = true; |
|---|
| 41 | } |
|---|
| 42 | unsigned long calculate_crc32(const char *buffer, int buffer_size){ |
|---|
| 43 | if (!hascrc32) |
|---|
| 44 | generate_crc32_table(); |
|---|
| 45 | register unsigned long crc; |
|---|
| 46 | int this_char; |
|---|
| 47 | int current_index; |
|---|
| 48 | |
|---|
| 49 | crc=0xFFFFFFFF; |
|---|
| 50 | |
|---|
| 51 | for(current_index=0;current_index<buffer_size;current_index++){ |
|---|
| 52 | this_char=(int)buffer[current_index]; |
|---|
| 53 | crc=((crc>>8) & 0x00FFFFFF) ^ crc32_table[(crc ^ this_char) & 0xFF]; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | return (crc ^ 0xFFFFFFFF); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | unsigned long calculate_crc32(const unsigned char *buffer, int buffer_size){ |
|---|
| 60 | if (!hascrc32) |
|---|
| 61 | generate_crc32_table(); |
|---|
| 62 | register unsigned long crc; |
|---|
| 63 | int this_char; |
|---|
| 64 | int current_index; |
|---|
| 65 | |
|---|
| 66 | crc=0xFFFFFFFF; |
|---|
| 67 | |
|---|
| 68 | for(current_index=0;current_index<buffer_size;current_index++){ |
|---|
| 69 | this_char=(int)buffer[current_index]; |
|---|
| 70 | crc=((crc>>8) & 0x00FFFFFF) ^ crc32_table[(crc ^ this_char) & 0xFF]; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | return (crc ^ 0xFFFFFFFF); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | |
|---|