0

My current issue is that I am computing a CRC32 hash in software and then checking it in the firmware, however when I compute the hash in firmware its double what it is supposed to be.

software(written in C#):

        public string SCRC(string input)
        {
            //Calculate CRC-32
            Crc32 crc32 = new Crc32();
            string hash = "";

            byte[] convert = Encoding.ASCII.GetBytes(input);

            MemoryStream ms = new MemoryStream(System.Text.Encoding.Default.GetBytes(input));

            foreach (byte b in crc32.ComputeHash(ms))
                hash += b.ToString("x2").ToLower();

            return hash;


        }

firmware functions used(written in C):

unsigned long chksum_crc32 (unsigned char *block, unsigned int length)
{
   register unsigned long crc;
   unsigned long i;

   crc = 0xFFFFFFFF;
   for (i = 0; i < length; i++)
   {
      crc = ((crc >> 8) & 0x00FFFFFF) ^ crc_tab[(crc ^ *block++) & 0xFF];
   }
   return (crc ^ 0xFFFFFFFF);
}

/* chksum_crc32gentab() --      to a global crc_tab[256], this one will
 *              calculate the crcTable for crc32-checksums.
 *              it is generated to the polynom [..]
 */

void chksum_crc32gentab ()
{
   unsigned long crc, poly;
   int i, j;

   poly = 0xEDB88320L;
   for (i = 0; i < 256; i++)
   {
      crc = i;
      for (j = 8; j > 0; j--)
      {
         if (crc & 1)
         {
            crc = (crc >> 1) ^ poly;
         }
         else
         {
            crc >>= 1;
         }
      }
      crc_tab[i] = crc;
   }
}

Firmware Code where the functions above are called(Written in C):

//CommandPtr should now be pointing to the rest of the command      
        chksum_crc32gentab();
        HardCRC = chksum_crc32( (unsigned)CommandPtr, strlen(CommandPtr));
        printf("Hardware CRC val is %lu\n", HardCRC);

Note, the CommandPTR is a refrence to the same data named, "string input" in the software method.

Does anyone have any idea why I could be getting approximately double the value I am using in the software?? Aka HardCRC is double what its supposed to be, I am guessing it has something to do with my unsigned char cast.

Recurrsion
  • 231
  • 3
  • 13
  • We can't help without seeing the implementation of the `Crc32` class from your C# code, or knowing where you got it from. – vcsjones Jul 30 '12 at 15:10
  • Which C# function would you need? – Recurrsion Jul 30 '12 at 15:46
  • Double? Approximately double? What are you actually getting, and what are you actually expecting? For what input? – Mark Adler Jul 30 '12 at 19:18
  • Why are you referring to this as a "hash"? What is the application? – Mark Adler Jul 30 '12 at 19:18
  • I figured the Issue out, its a type conversion error. The string i am using in the firmware level is not getting converted to an unsigned char array that matches the string to byte array conversion in the software level. I need to figure out how to overcome this limitation iguess. – Recurrsion Jul 30 '12 at 19:28
  • please post your findings as an answer to your question. This way, others might find it useful, if they stumble upon similar error. – maths-help-seeker Sep 13 '12 at 15:07

0 Answers0