~ubuntu-branches/debian/jessie/tsdecrypt/jessie

« back to all changes in this revision

Viewing changes to libtsfuncs/crc.c

  • Committer: Package Import Robot
  • Author(s): Alessio Treglia
  • Date: 2012-04-04 09:42:43 UTC
  • Revision ID: package-import@ubuntu.com-20120404094243-qsc40h18oolnxw5r
Tags: upstream-7.0
ImportĀ upstreamĀ versionĀ 7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * CRC functions for mpeg PSI tables
 
3
 * Copyright (C) 2010-2011 Unix Solutions Ltd.
 
4
 *
 
5
 * Released under MIT license.
 
6
 * See LICENSE-MIT.txt for license terms.
 
7
 */
 
8
#include <netdb.h>
 
9
 
 
10
#include "tsfuncs.h"
 
11
 
 
12
#define CRC32_POLY 0x04C11DB7L
 
13
 
 
14
static int crc_table_initialized = 0;
 
15
static uint32_t crc32_table[256];
 
16
 
 
17
void ts_crc32_init(void) {
 
18
        int i, j;
 
19
        uint32_t crc;
 
20
        if (crc_table_initialized)
 
21
                return;
 
22
        crc_table_initialized = 1;
 
23
        for (i=0; i<256; i++) {
 
24
                crc = i << 24;
 
25
                for (j=0; j<8; j++) {
 
26
                        if (crc & 0x80000000L)
 
27
                                crc = (crc << 1) ^ CRC32_POLY;
 
28
                        else
 
29
                                crc = (crc << 1);
 
30
                }
 
31
                crc32_table[i] = crc;
 
32
        }
 
33
        crc_table_initialized = 1;
 
34
}
 
35
 
 
36
uint32_t ts_crc32(uint8_t *data, int data_size) {
 
37
        int i, j;
 
38
        uint32_t crc = 0xffffffff;
 
39
        if (!crc_table_initialized) {
 
40
                ts_crc32_init();
 
41
        }
 
42
        for (j=0; j<data_size; j++) {
 
43
                i = ((crc >> 24) ^ *data++) & 0xff;
 
44
                crc = (crc << 8) ^ crc32_table[i];
 
45
        }
 
46
        return crc;
 
47
}
 
48
 
 
49
u_int32_t ts_crc32_section(struct ts_section_header *section_header) {
 
50
        return ts_crc32(section_header->section_data, section_header->section_data_len);
 
51
}
 
52
 
 
53
int ts_crc32_section_check(struct ts_section_header *section_header, char *table) {
 
54
        uint32_t check_crc = ts_crc32(section_header->section_data, section_header->section_data_len);
 
55
 
 
56
        if (check_crc != 0) {
 
57
                ts_LOGf("!!! Wrong %s table CRC! It should be 0 but it is 0x%08x (CRC in data is 0x%08x)\n",
 
58
                        table,
 
59
                        check_crc,
 
60
                        section_header->CRC);
 
61
                return 0;
 
62
        }
 
63
        return 1;
 
64
}