]> git.gir.st - ttxd.git/blob - src/vtx2ascii-src/vtxtools.c
update to dvb-t2 (new hardware, software)
[ttxd.git] / src / vtx2ascii-src / vtxtools.c
1 /*
2 * vtxtools.c: Misceallaneous routines for VideoteXt (parity checking, handling of hexadecimal
3 * page-numbers)
4 *
5 * $Id: vtxtools.c,v 1.1 1996/10/14 21:36:23 mb Exp mb $
6 *
7 * Copyright (c) 1994-96 Martin Buck <martin-2.buck@student.uni-ulm.de>
8 * Read COPYING for more information
9 *
10 */
11
12 #include <sys/vtx.h>
13 #include "misc.h"
14 #include "vtxtools.h"
15
16
17 static byte_t parity_table[256];
18 static int init_done;
19
20
21
22 static void
23 tools_init(void) {
24 int pos, val, bit;
25
26 for (pos = 0; pos <= 255; pos++) { /* Set up parity_table: If (parity_table[foo] & 0x80), */
27 bit = 0; /* foo has odd number of bits set */
28 val = pos;
29 while (val) { /* Count number of set bits in val; see K&R, Exercise 2-9 */
30 bit ^= 0x80;
31 val &= val - 1;
32 }
33 parity_table[pos] = bit | 0x7f;
34 }
35 /* parity_table is also used for hamming decoding: If (parity_table[foo] & 0x40), foo has
36 * more than one bit-error and can't be corrected; otherwise the correct(ed) value is
37 * parity_table[foo] & 0xf
38 */
39 for (pos = 0; pos <= 15; pos++) {
40 val = ( !(pos & 1) ^ !!(pos & 4) ^ !!(pos & 8)) << 0 | !!(pos & 1) << 1 |
41 ( !(pos & 1) ^ !!(pos & 2) ^ !!(pos & 8)) << 2 | !!(pos & 2) << 3 |
42 ( !(pos & 1) ^ !!(pos & 2) ^ !!(pos & 4)) << 4 | !!(pos & 4) << 5 |
43 (!!(pos & 2) ^ !!(pos & 4) ^ !!(pos & 8)) << 6 | !!(pos & 8) << 7;
44 for (bit = 0; bit <= 8; bit++) {
45 parity_table[val ^ ((1 << bit) & 0xff)] &= (0x80 | pos);
46 }
47 }
48 init_done = TRUE;
49 }
50
51
52 /* Check parity of *val (parity bit is bit 7, odd parity)
53 * Clear bit 7 of *val if parity OK & return TRUE, FALSE otherwise
54 */
55 int
56 vtx_chkparity(byte_t *val) {
57 if (!init_done)
58 tools_init();
59 if (parity_table[*val] & 0x80) {
60 *val &= 0x7f;
61 return TRUE;
62 } else return FALSE;
63 }
64
65
66 /* Add odd parity bit to val
67 */
68 byte_t
69 vtx_mkparity(byte_t val) {
70 if (!init_done)
71 tools_init();
72 val &= 0x7f;
73 return val | ((parity_table[val] & 0x80) ? 0 : 128);
74 }
75
76
77 /* Check hamming-encoding of val, return decoded value if OK, -1 otherwise
78 */
79 int
80 vtx_chkhamming(byte_t val) {
81 if (!init_done)
82 tools_init();
83 if (parity_table[val] & 0x40) {
84 return -1;
85 } else {
86 return parity_table[val] & 0xf;
87 }
88 }
89
90
91 /* Increase page-number. Skip over hexadecimal pages
92 */
93 int
94 inc_vtxpage(int page) {
95 page++;
96 if ((page & 0xf) >= 0xa)
97 page = (page & ~0xf) + 0x10;
98 if ((page & 0xf0) >= 0xa0)
99 page = (page & ~0xff) + 0x100;
100 if (page >= 0x899)
101 page = 0x100;
102 return page;
103 }
104
105
106 /* Decrease page-number. Skip over hexadecimal pages
107 */
108 int
109 dec_vtxpage(int page) {
110 page--;
111 if ((page & 0xf) >= 0xa)
112 page = (page & ~0xf) + 9;
113 if ((page & 0xf0) >= 0xa0)
114 page = (page & ~0xff) + 0x99;
115 if (page < 0x100)
116 page = 0x899;
117 return page;
118 }
119
120
121 int
122 vtx_hex2dec(int pgnum) {
123 return (pgnum / 0x100) * 100 + ((pgnum / 0x10) % 0x10) * 10 + (pgnum % 0x10);
124 }
125
126
127 int
128 vtx_dec2hex(int pgnum) {
129 return (pgnum / 100) * 0x100 + ((pgnum / 10) % 10) * 0x10 + (pgnum % 10);
130 }
131
132
133 int
134 vtx_chkpgnum(int pgnum, int hex_ok) {
135 if (hex_ok) {
136 return (pgnum >= 0x100 && pgnum <= 0x8ff);
137 } else {
138 return (pgnum >= 0x100 && pgnum <= 0x899 && (pgnum & 0xff) <= 0x99 && (pgnum & 0xf) <= 9);
139 }
140 }
Imprint / Impressum