libtsfuncs is a library for mpeg PSI parsing and generation. https://georgi.unixsol.org/programs/libtsfuncs/

crc.c 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <netdb.h>
  2. #include "tsfuncs.h"
  3. #define CRC32_POLY 0x04C11DB7L
  4. static int crc_table_initialized = 0;
  5. static uint32_t crc32_table[256];
  6. void ts_crc32_init() {
  7. int i, j;
  8. uint32_t crc;
  9. if (crc_table_initialized)
  10. return;
  11. crc_table_initialized = 1;
  12. for (i=0; i<256; i++) {
  13. crc = i << 24;
  14. for (j=0; j<8; j++) {
  15. if (crc & 0x80000000L)
  16. crc = (crc << 1) ^ CRC32_POLY;
  17. else
  18. crc = (crc << 1);
  19. }
  20. crc32_table[i] = crc;
  21. }
  22. crc_table_initialized = 1;
  23. }
  24. uint32_t ts_crc32(uint8_t *data, int data_size) {
  25. int i, j;
  26. uint32_t crc = 0xffffffff;
  27. if (!crc_table_initialized) {
  28. ts_crc32_init();
  29. }
  30. for (j=0; j<data_size; j++) {
  31. i = ((crc >> 24) ^ *data++) & 0xff;
  32. crc = (crc << 8) ^ crc32_table[i];
  33. }
  34. return crc;
  35. }
  36. u_int32_t ts_crc32_section(struct ts_section_header *section_header) {
  37. return ts_crc32(section_header->section_data, section_header->section_data_len);
  38. }
  39. int ts_crc32_section_check(struct ts_section_header *section_header, char *table) {
  40. uint32_t check_crc = ts_crc32(section_header->section_data, section_header->section_data_len);
  41. if (check_crc != 0) {
  42. ts_LOGf("!!! Wrong %s table CRC! It should be 0 but it is 0x%08x (CRC in data is 0x%08x)\n",
  43. table,
  44. check_crc,
  45. section_header->CRC);
  46. return 0;
  47. }
  48. return 1;
  49. }