forked from peterkvt80/vbit2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hamm.c
167 lines (145 loc) · 3.82 KB
/
hamm.c
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* libzvbi -- Error correction functions
*
* Copyright (C) 2001, 2002, 2003, 2004, 2007 Michael H. Schimek
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*/
/* $Id: hamm.c,v 1.11 2013/07/10 11:37:08 mschimek Exp $ */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <limits.h> /* CHAR_BIT */
#include "hamm.h"
#include "hamm-tables.h"
/**
* @ingroup Error
*
* @param p Array of unsigned bytes.
* @param n Size of array.
*
* Of each byte of the array, changes the most significant
* bit to make the number of set bits odd.
*
* @since 0.2.12
*/
void
vbi_par (uint8_t * p,
unsigned int n)
{
while (n-- > 0) {
uint8_t c = *p;
/* if 0 == (inv_par[] & 32) change msb of *p. */
*p++ = c ^ (128 & ~(_vbi_hamm24_inv_par[0][c] << 2));
}
}
/**
* @ingroup Error
* @param p Array of unsigned bytes.
* @param n Size of array.
*
* Tests the parity and clears the most significant bit of
* each byte of the array.
*
* @return
* A negative value if any byte of the array had even
* parity (sum of bits modulo 2 is 0).
*
* @since 0.2.12
*/
int
vbi_unpar (uint8_t * p,
unsigned int n)
{
int r = 0;
while (n-- > 0) {
uint8_t c = *p;
/* if 0 == (inv_par[] & 32) set msb of r. */
r |= ~ _vbi_hamm24_inv_par[0][c]
<< (sizeof (int) * CHAR_BIT - 1 - 5);
*p++ = c & 127;
}
return r;
}
/**
* @ingroup Error
* @param p A Hamming 24/18 protected 24 bit word will be stored here,
* last significant byte first, lsb first transmitted.
* @param c Integer between 0 ... 1 << 18 - 1.
*
* Encodes an 18 bit word with Hamming 24/18 protection
* as specified in ETS 300 706, Section 8.3.
*
* @since 0.2.27
*/
void
vbi_ham24p (uint8_t * p,
unsigned int c)
{
unsigned int D5_D11;
unsigned int D12_D18;
unsigned int P5, P6;
unsigned int Byte_0;
Byte_0 = (_vbi_hamm24_fwd_0 [(c >> 0) & 0xFF]
^ _vbi_hamm24_fwd_1 [(c >> 8) & 0xFF]
^ _vbi_hamm24_fwd_2 [(c >> 16) & 0x03]);
p[0] = Byte_0;
D5_D11 = (c >> 4) & 0x7F;
D12_D18 = (c >> 11) & 0x7F;
P5 = 0x80 & ~(_vbi_hamm24_inv_par[0][D12_D18] << 2);
p[1] = D5_D11 | P5;
P6 = 0x80 & ((_vbi_hamm24_inv_par[0][Byte_0]
^ _vbi_hamm24_inv_par[0][D5_D11]) << 2);
p[2] = D12_D18 | P6;
}
/**
* @ingroup Error
* @param p Pointer to a Hamming 24/18 protected 24 bit word,
* last significant byte first, lsb first transmitted.
*
* Decodes a Hamming 24/18 protected byte triplet
* as specified in ETS 300 706, Section 8.3.
*
* @return
* Triplet data bits D18 [msb] ... D1 [lsb] or a negative value
* if the triplet contained uncorrectable errors.
*
* @since 0.2.12
*/
int
vbi_unham24p (const uint8_t * p)
{
unsigned int D1_D4;
unsigned int D5_D11;
unsigned int D12_D18;
unsigned int ABCDEF;
int32_t d;
D1_D4 = _vbi_hamm24_inv_d1_d4[p[0] >> 2];
D5_D11 = p[1] & 0x7F;
D12_D18 = p[2] & 0x7F;
d = D1_D4 | (D5_D11 << 4) | (D12_D18 << 11);
ABCDEF = (_vbi_hamm24_inv_par[0][p[0]]
^ _vbi_hamm24_inv_par[1][p[1]]
^ _vbi_hamm24_inv_par[2][p[2]]);
/* Correct single bit error, set MSB on double bit error. */
return d ^ (int) _vbi_hamm24_inv_err[ABCDEF];
}
/*
Local variables:
c-set-style: K&R
c-basic-offset: 8
End:
*/