]>
git.gir.st - ttxd.git/blob - src/vtx2ascii-src/vtxtools.c
2 * vtxtools.c: Misceallaneous routines for VideoteXt (parity checking, handling of hexadecimal
5 * $Id: vtxtools.c,v 1.1 1996/10/14 21:36:23 mb Exp mb $
7 * Copyright (c) 1994-96 Martin Buck <martin-2.buck@student.uni-ulm.de>
8 * Read COPYING for more information
17 static byte_t parity_table
[256];
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 */
29 while (val
) { /* Count number of set bits in val; see K&R, Exercise 2-9 */
33 parity_table
[pos
] = bit
| 0x7f;
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
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
);
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
56 vtx_chkparity(byte_t
*val
) {
59 if (parity_table
[*val
] & 0x80) {
66 /* Add odd parity bit to val
69 vtx_mkparity(byte_t val
) {
73 return val
| ((parity_table
[val
] & 0x80) ? 0 : 128);
77 /* Check hamming-encoding of val, return decoded value if OK, -1 otherwise
80 vtx_chkhamming(byte_t val
) {
83 if (parity_table
[val
] & 0x40) {
86 return parity_table
[val
] & 0xf;
91 /* Increase page-number. Skip over hexadecimal pages
94 inc_vtxpage(int page
) {
96 if ((page
& 0xf) >= 0xa)
97 page
= (page
& ~0xf) + 0x10;
98 if ((page
& 0xf0) >= 0xa0)
99 page
= (page
& ~0xff) + 0x100;
106 /* Decrease page-number. Skip over hexadecimal pages
109 dec_vtxpage(int page
) {
111 if ((page
& 0xf) >= 0xa)
112 page
= (page
& ~0xf) + 9;
113 if ((page
& 0xf0) >= 0xa0)
114 page
= (page
& ~0xff) + 0x99;
122 vtx_hex2dec(int pgnum
) {
123 return (pgnum
/ 0x100) * 100 + ((pgnum
/ 0x10) % 0x10) * 10 + (pgnum
% 0x10);
128 vtx_dec2hex(int pgnum
) {
129 return (pgnum
/ 100) * 0x100 + ((pgnum
/ 10) % 10) * 0x10 + (pgnum
% 10);
134 vtx_chkpgnum(int pgnum
, int hex_ok
) {
136 return (pgnum
>= 0x100 && pgnum
<= 0x8ff);
138 return (pgnum
>= 0x100 && pgnum
<= 0x899 && (pgnum
& 0xff) <= 0x99 && (pgnum
& 0xf) <= 9);