473,799 Members | 3,093 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Whats wrong with this crc32 code?

I need to write a function crc(msg, len) that gets a char array of
length len and then calculates the crc32 for the code. I don't
understand what's going wrong in the code I have. It goes bit-by-bit,
XORing the most significant bit of the FCS with the msb of the
character.

All comments are welcome. Yes, this is for a school assignment. If I
wanted to just grab some googled code I would; I want to actually
understand what I am doing wrong.

Thanks in advance.

Here's what I have so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXFILESIZE 512
#define CRCDIV 0x04C11DB7

/* prototypes. */
unsigned int crc(unsigned char msg[], int len);
/* this function creates a binary representation of any integer. */
void d2b(char buff[], int i);

int main(int argc, char *argv[])
{
unsigned char msg[MAXFILESIZE];
FILE *infile;
unsigned int r;
int len = 0;
unsigned int c;
char buff[33];

/* make sure we get a file. */
if (argc != 2) {
printf("USAGE: %s file-to-encode.txt\n", argv[0]);
exit(1);
}

/* now try to open the file. */
if ((infile = fopen(argv[1], "r")) == NULL) {
perror("fopen failed");
exit(1);
}

/* load infile into msg. */
while ( (c = fgetc(infile)) != EOF) {
msg[len++] = c;
}

/* print CRCDIV just to see what it looks like. */
d2b(buff, CRCDIV);

#ifdef DEBUG
printf("CRCDIV: %s\n\n", buff);
#endif

/* call the crc and print the results. */
r = crc(msg, len);
printf("crc32 for msg: %08x.\n", r);

return 0;
}

unsigned int crc(unsigned char msg[], int len)
{
unsigned char c;
unsigned int FCS;
unsigned int y;
int i, j;
char buff[33];

FCS = 0;

/* outer loop through each unsigned char in msg. */
for (i=0; i<len; i++) {
c = msg[i];

#ifdef DEBUG
printf("c is %c:%d.\n", c, c);
#endif

/* now loop bit-by-bit. */
for (j=0; j<8; j++) {
d2b(buff, FCS);

#ifdef DEBUG
printf("before calculating y:\n");
printf("FCS: %x\n", FCS);
printf("%s\n", buff);
#endif

/* Calculate y. */
y = (FCS >> 31) ^ (c >> 7);
FCS <<= 1;
if (y)
FCS = FCS ^ CRCDIV;
c <<= 1;
d2b(buff, FCS);
#ifdef DEBUG
printf("FCS >> 31: %d; c >> 7: %d; y: %d.\n\n",
(FCS >> 31), (c >> 7), y);

printf("after calculating y:\n");
printf("FCS: %x\n", FCS);
printf("%s\n", buff);
#endif

}

#ifdef DEBUG
printf("\n");
#endif

}
return FCS;
}

void d2b(char buff[], int i)
{
int q, r, index;

for (index=0; index<32; index++)
buff[index] = '0';
buff[32] = '\0';

index = 31;
while (i > 0) {
r = i % 2;
q = i / 2;
if (r)
buff[index] = '1';
index--;
i = q;
}
}
--
My public key:
gpg --recv-keys --keyserver www.mandrakesecure.net 0x8D10BFD5
Nov 14 '05 #1
1 2751
On Mon, 16 Feb 2004 17:04:10 GMT, Matthew Wilson
<ma**@overlook. homelinux.net> wrote in comp.lang.c:
I need to write a function crc(msg, len) that gets a char array of
length len and then calculates the crc32 for the code. I don't
understand what's going wrong in the code I have. It goes bit-by-bit,
XORing the most significant bit of the FCS with the msb of the
character.

All comments are welcome. Yes, this is for a school assignment. If I
wanted to just grab some googled code I would; I want to actually
understand what I am doing wrong.

Thanks in advance.

Here's what I have so far:


[snip code]

No, sorry, I'm just not going to wade through all that code without
any idea of what you think is "going wrong". And I've written a few
CRC implementations in my time, even one published in a book on C
programming a few years back.

At the very least you need to supply more information, specifically
what you mean by "going wrong".

If you get compiler errors (or linker errors), copy the text and paste
it into the body of your message along with the source code. If the
program builds but crashes when run, describe the symptoms of the
crash. If it build and runs but outputs incorrect results, you need
to explain what results you expected and what you got instead.

If you provide that much additional information along with the source
code, you will most likely get some useful advice.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
4356
by: Ricky Romaya | last post by:
Hi, I'm working on a file upload script. I need to calculate the CRC32 of the file(s) which are successfully uploaded. How can I do this? PHP only have CRC32 function for strings. However, the uploaded file(s) are mostly binaries, and assumed have large size (5-12 MB per file). Are there any ways other than CRC32 (which supported by default PHP installation) to generate a unique hash of an arbitrary files? (the users of my script are...
2
3422
by: nobody | last post by:
1) Does anyone know if the CRC32 algorithm in binascii has a name? There seem to be a TON of different CRC32 methods; different polynomials, different byte orders, different seeds, some flip the bits and some XOR the result with something else. I've been looking around and a lot of documents (RFCs, etc) refer to ISO3309 and/or ITU-T V.42 (neither of which appear to be available without paid access), yet are often incompatible with each...
6
5837
by: Weiguang Shi | last post by:
Hi there, I'm thinking of using binascii.crc32 as a hash-function when I read in the reference http://www.python.org/doc/current/lib/module-binascii.html: crc32( data) Compute CRC-32, the 32-bit checksum of data, starting with an initial crc. This is consistent with the ZIP file checksum.
3
2397
by: Chris Geerdink | last post by:
combo with PHP. what is wrong with the Javascript? else { include("mysql.php"); $query1 = mysql_query("INSERT INTO gbook (naam, email, text) VALUES ('".$_POST."', '".$_POST."', '".$_POST."')"); ?> <script language="JavaScript"> <!--
14
8793
by: Don | last post by:
Hi NG. Does anyone know of a place where I could download/get a C implementation of a CRC32 check. I would like a simple function that, for example, had a pointer to where the data to be CRC32 calculated reside, an indication of the length of the data and perhaps the polynomium as input arguments and then would return the calculated crc32 value like e.g. an unsigned long. Don
9
10858
by: UnixUser | last post by:
I am looking for some source code to run on Linux that will enable me to calculate and return a CRC32 value from a string of text. I have found one from snippets.org, but I cannot get it to compile. Please help me find something that is simple to install, includes all header and language files and that will compile.
6
4395
by: Paul M. | last post by:
Hello, does anyone have either a User Function Library (or the source for one) to create a CRC32 checksum for a given string? I want to use the function in a crystal formula thus: formula = CRC32("123456789") This would then display on the report the CRC32 checksum for "123456789".
12
9883
by: Larry Bates | last post by:
I'm trying to get the results of binascii.crc32 to match the results of another utility that produces 32 bit unsigned CRCs. binascii.crc32 returns results in the range of -2**31-1 and 2**21-1. Has anyone ever worked out any "bit twiddling" code to get a proper unsigned 32 bit result from binascii.crc32? Output snip from test on three files: binascii.crc32=-1412119273, oldcrc32= 2221277246
5
2323
by: islayer | last post by:
can someone tell me what is wrong with the bold code? i am just learning perl. the program should create a perl file with a random name (5 letters, followed by a number), but the name is always just the number. whats wrong with my code? #!/usr/bin/perl use Fcntl; @array = (a..z); srand; foreach (1..5) { $name = int(rand scalar(@array));
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10249
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10219
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10025
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7563
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5461
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5584
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.