File: crc.c

package info (click to toggle)
tsdecrypt 10.0-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,288 kB
  • sloc: ansic: 14,377; makefile: 245; sh: 166
file content (64 lines) | stat: -rw-r--r-- 1,451 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
 * CRC functions for mpeg PSI tables
 * Copyright (C) 2010-2011 Unix Solutions Ltd.
 *
 * Released under MIT license.
 * See LICENSE-MIT.txt for license terms.
 */
#include <netdb.h>

#include "tsfuncs.h"

#define CRC32_POLY 0x04C11DB7L

static int crc_table_initialized = 0;
static uint32_t crc32_table[256];

void ts_crc32_init(void) {
	int i, j;
	uint32_t crc;
	if (crc_table_initialized)
		return;
	crc_table_initialized = 1;
	for (i=0; i<256; i++) {
		crc = i << 24;
		for (j=0; j<8; j++) {
			if (crc & 0x80000000L)
				crc = (crc << 1) ^ CRC32_POLY;
			else
				crc = (crc << 1);
		}
		crc32_table[i] = crc;
	}
	crc_table_initialized = 1;
}

uint32_t ts_crc32(uint8_t *data, int data_size) {
	int i, j;
	uint32_t crc = 0xffffffff;
	if (!crc_table_initialized) {
		ts_crc32_init();
	}
	for (j=0; j<data_size; j++) {
		i = ((crc >> 24) ^ *data++) & 0xff;
		crc = (crc << 8) ^ crc32_table[i];
	}
	return crc;
}

u_int32_t ts_crc32_section(struct ts_section_header *section_header) {
	return ts_crc32(section_header->section_data, section_header->section_data_len);
}

int ts_crc32_section_check(struct ts_section_header *section_header, char *table) {
	uint32_t check_crc = ts_crc32(section_header->section_data, section_header->section_data_len);

	if (check_crc != 0) {
		ts_LOGf("!!! Wrong %s table CRC! It should be 0 but it is 0x%08x (CRC in data is 0x%08x)\n",
			table,
			check_crc,
			section_header->CRC);
		return 0;
	}
	return 1;
}