<commit_before>/* crc.c  01/04/2015  D.J.Whale
 *
 * Implements ITU-V.41 FCS algorithm.
 */

#include <stdio.h>

typedef unsigned char BYTE;
typedef unsigned short WORD;

WORD UpdateCrc(BYTE ch, WORD *pwCrc)
{
  ch = (ch^(BYTE)((*pwCrc) & 0x00FF));
  ch = (ch^(ch<<4));
  *pwCrc = (*pwCrc >> 8) 
         ^ ((WORD)ch << 8) 
         ^ ((WORD)ch<<3) 
         ^ ((WORD)ch>>4);
  return *pwCrc;
}

WORD ComputeCrc(BYTE *data, unsigned int len, BYTE *crc0, BYTE *crc1)
{
  BYTE bBlock;
  WORD wCrc;

  wCrc = 0x6363;

  do
  {
    bBlock = *data++;
    UpdateCrc(bBlock, &wCrc);
  }
  while (--len);

  *crc0 = (BYTE) (wCrc & 0xFF);
  *crc1 = (BYTE) ((wCrc >> 8) & 0xFF);
  return wCrc;
}


void test(BYTE* buf, unsigned int len)
{
  int i;
  BYTE crc0, crc1;

  printf("buf:");

  for(i=0; i<len; i++)
  {
    printf("%02X ",buf[i]);
  }

  ComputeCrc(buf, len, &crc0, &crc1);

  printf("\nCRC:%02X %02X\n", (unsigned int)crc0, (unsigned int)crc1);
}


/* Test vectors from: st.com AN4433 */

/* Select NFC T4 application */
BYTE buf1[] = {0x02,0x00,0xA4,0x04,0x00,0x07,0xD2,0x76,0x00,0x00,0x85,0x01,0x01,0x00};

/* Select CC file */
BYTE buf2[] = {0x03,0x00,0xA4,0x00,0x0C,0x02,0xE1,0x03};

/* Read CC file length */
BYTE buf3[] = {0x02,0x00,0xB0,0x00,0x00,0x02};

/* Read CC file */
BYTE buf4[] = {0x03,0x00,0xB0,0x00,0x00,0x0F};

/* Select NDEF file */
BYTE buf5[] = {0x02,0x00,0xA4,0x00,0x0C,0x02,0x00,0x01};

/* Read NDEF message length */
BYTE buf6[] = {0x03,0x00,0xB0,0x00,0x00,0x02};
//NOTE: appnote says 0x6B 0x7D, but returns 0x40, 0x79

/* Read NDEF message */
BYTE buf7[] = {0x02,0x00,0xB0,0x00,0x02,0x14};
//NOTE: appnote does not give expected CRC due to printing error

/* Deselect */
BYTE buf8[] = {0xC2};


int main(void)
{
  test(buf1, sizeof(buf1));
  test(buf2, sizeof(buf2));
  test(buf3, sizeof(buf3));
  test(buf4, sizeof(buf4));
  test(buf5, sizeof(buf5));
  test(buf6, sizeof(buf6));
  test(buf7, sizeof(buf7));
  test(buf8, sizeof(buf8));

  return 0;
}<commit_msg>Added some more test vectors, and test numbers.<commit_after>/* crc.c  01/04/2015  D.J.Whale
 *
 * Implements ITU-V.41 FCS algorithm.
 */

#include <stdio.h>

typedef unsigned char BYTE;
typedef unsigned short WORD;

WORD UpdateCrc(BYTE ch, WORD *pwCrc)
{
  ch = (ch^(BYTE)((*pwCrc) & 0x00FF));
  ch = (ch^(ch<<4));
  *pwCrc = (*pwCrc >> 8) 
         ^ ((WORD)ch << 8) 
         ^ ((WORD)ch<<3) 
         ^ ((WORD)ch>>4);
  return *pwCrc;
}

WORD ComputeCrc(BYTE *data, unsigned int len, BYTE *crc0, BYTE *crc1)
{
  BYTE bBlock;
  WORD wCrc;

  wCrc = 0x6363;

  do
  {
    bBlock = *data++;
    UpdateCrc(bBlock, &wCrc);
  }
  while (--len);

  *crc0 = (BYTE) (wCrc & 0xFF);
  *crc1 = (BYTE) ((wCrc >> 8) & 0xFF);
  return wCrc;
}


void test(char *id, BYTE* buf, unsigned int len)
{
  int i;
  BYTE crc0, crc1;

  printf("buf %s:", id);

  for(i=0; i<len; i++)
  {
    printf("%02X ",buf[i]);
  }

  ComputeCrc(buf, len, &crc0, &crc1);

  printf("\nCRC:%02X %02X\n", (unsigned int)crc0, (unsigned int)crc1);
}


/* Test vectors from: st.com AN4433 */

/* Select NFC T4 application */
BYTE buf1_tx[] = {0x02,0x00,0xA4,0x04,0x00,0x07,0xD2,0x76,0x00,0x00,0x85,0x01,0x01,0x00};
BYTE buf1_rx[] = {0x02,0x90,0x00};

/* Select CC file */
BYTE buf2_tx[] = {0x03,0x00,0xA4,0x00,0x0C,0x02,0xE1,0x03};

/* Read CC file length */
BYTE buf3_tx[] = {0x02,0x00,0xB0,0x00,0x00,0x02};

/* Read CC file */
BYTE buf4_tx[] = {0x03,0x00,0xB0,0x00,0x00,0x0F};

/* Select NDEF file */
BYTE buf5_tx[] = {0x02,0x00,0xA4,0x00,0x0C,0x02,0x00,0x01};

/* Read NDEF message length */
BYTE buf6_tx[] = {0x03,0x00,0xB0,0x00,0x00,0x02};
//NOTE: appnote says 0x6B 0x7D, but returns 0x40, 0x79

/* Read NDEF message */
BYTE buf7_tx[] = {0x02,0x00,0xB0,0x00,0x02,0x14};
//NOTE: appnote does not give expected CRC due to printing error

/* Deselect */
BYTE buf8_tx[] = {0xC2};


int main(void)
{
  test("1tx", buf1_tx, sizeof(buf1_tx));
  test("1rx", buf1_rx, sizeof(buf1_rx));

  test("2tx", buf2_tx, sizeof(buf2_tx));
  test("3tx", buf3_tx, sizeof(buf3_tx));
  test("4tx", buf4_tx, sizeof(buf4_tx));
  test("5tx", buf5_tx, sizeof(buf5_tx));
  test("6tx", buf6_tx, sizeof(buf6_tx));
  test("7tx", buf7_tx, sizeof(buf7_tx));
  test("8tx", buf8_tx, sizeof(buf8_tx));

  return 0;
}
{'fraction_non_alphanumeric': 0.19343410486537554, 'fraction_numerical': 0.11596598960793576, 'mean_word_length': 3.8510882016036656, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3250763', 'n_tokens_mistral': 2217, 'n_tokens_neox': 2015, 'n_words': 409}