recoding .c function into xojo msp430_crc.c

Below I’ve pasted a function in .c for calculating the CRC (check sum) for a string. I want to duplicate this functionality in Xojo, however my .c skills are a quite dusty. Below the .c snippet I’ve pasted some Xojo code that was suggested by a great source in our forum. However, it’s not working like the c. version does. Any help with this conversion would be greatly appreciated! :slight_smile:

[code]#include “msp430_crc.h”

/* msp430_crc ***********************************************************************//

  • @brief Calculates a 16-bit cyclic error code for a string.

  • @param[in] ptr: the string needing a crc16 value.

  • @param[in] len: the length of the string in characters.

  • @return None
    /
    uint16_t msp430_crc(const void
    ptr, unsigned len)
    {
    uint16_t crc = ~0x0;
    const uint8_t* str = ptr;

    while(len–)
    {
    unsigned i;
    crc ^= *str++;

     /* Works from LSb to MSb so the polynomial needs to be bit reversed. */
     for(i = 0; i < 8; i++) {
     	crc = (crc >> 1) ^ (-(int)(crc & 1) & BIT_REVERSE_SHORT(POLYNOMIAL16));
     }
    

    }

    return crc;
    }

/************************************** END OF FILE **************************************/[/code]

[code]// Thanks to Christian Schmitz for posting this earlier.

dim m as memoryblock = “3E6400104395FFF938DAB6C641B9BC243FD7B51D000D” // crc: 6D7E

dim crc as integer = &hFFFF
dim u as integer = m.size-1
for i as integer = 0 to u
dim ch as integer = m.UInt8Value(i)

dim cl as integer = 0
for j as integer = 0 to 7
  cl = BitwiseXor( crc, ch)
  
  crc = Bitwise.ShiftRight(crc, 1)
  
  if BitwiseAnd(cl, 1) <> 0 then
    crc = BitwiseXor(crc, &h1021)
  end if
  
  ch = Bitwise.ShiftRight(ch, 1)
next

next

crc = BitwiseAnd(crc, &hFFFF)[/code]

whats BIT_REVERSE_SHORT(POLYNOMIAL16) ?
there’s probably a definition in one of the .h files

Good point Norman, here’s the full contents of the header:

[code]/************************************** START OF FILE **************************************/
#ifndef MSP430_CRC_H
#define MSP430_CRC_H

#include <stdint.h>

#ifdef __cplusplus
extern “C” {
#endif

#define POLYNOMIAL16 (0x1021)

#define BIT_REVERSE_SHORT(x) (_BRS(x))
#define _BRS(x) (((__BRS(x) >> 8) & 0x00FF) | ((__BRS(x) & 0x00FF) << 8))
#define __BRS(x) (((___BRS(x) >> 4) & 0x0F0F) | ((___BRS(x) & 0x0F0F) << 4))
#define ___BRS(x) (((____BRS(x) >> 2) & 0x3333) | ((____BRS(x) & 0x3333) << 2))
#define ____BRS(x) (((x >> 1) & 0x5555) | ((x & 0x5555) << 1))

/* Public Function Prototypes ---------------------------------------------------------- /
uint16_t msp430_crc(const void
, unsigned);

#ifdef __cplusplus
}
#endif

#endif
/************************************** END OF FILE **************************************/[/code]

I think the only thing missing from christians is the macro conversion to some source code to do what the macro does (in a hugely unsafe way :slight_smile:

This “seems” about right (but its “forum code” meaning its completely untested as its all written inline here so it may not even compile)
Should be close enough to get you started

Protected Function BIT_REVERSE_SHORT(x as Uint16) As Uint16
  return x_BRS(x)
End Function
Protected Function x_BRS(x as uint16) As uint16
  return BiteWise.BitOr( Bitwise.ShiftRight(( x__BRS(x), 8) and &h00FF) ,  BitWise.ShiftLeft( (x__BRS(x) and &h00FF) , 8 ))  
End Function
Protected Function x__BRS(x as uint16) As uint16
  return 
  Bitwise.BitOr( BitWise.ShiftRight(x___BRS(x) , 4)  and &h0F0F ,  BitWise.ShiftLeft(x___BRS(x) and &h0F0F , 4) )
End Function
Protected Function x___BRS(x as uint16) As uint16
  return BitWise.BitOr( BitWise.ShiftRight( x____BRS(x), 2) and &h3333 , BitWise.ShiftLeft( (x____BRS(x) and &h3333) , 2) )
End Function
Protected Function x____BRS(x as uint16) As uint16
  return BitWise.BitOr( (bitwise.ShiftRight(x,1) and &h5555), BitWise.ShiftLeft((x and &h5555), 1) )
End Function