-
Notifications
You must be signed in to change notification settings - Fork 20
/
blocks.c
255 lines (189 loc) · 6.95 KB
/
blocks.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*\
* Yes, This Is Another Barcode Reader
* Copyright (C) 2013 Quentin SANTOS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
\*/
#include "blocks.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "data.h"
#include "modules.h"
#include "rs.h"
// please, refer to DOCUMENTATION for technical details
static void get_block(scanner_t* scanner);
static void put_block(scanner_t* scanner);
static void get_block(scanner_t* scanner)
{
// get block information
struct TwoBlockRuns runs = block_sizes[4 * scanner->v + scanner->c];
// current block
size_t cur = scanner->block_cur;
// find the data size of the current block
size_t codewords_per_block = cur < runs.first.n_blocks ? runs.first.data_codewords_per_block : runs.second.data_codewords_per_block;
scanner->block_dataw = codewords_per_block;
// rewind to start of symbol
scanner->i = scanner->s - 1;
scanner->j = scanner->s - 1;
// NOTE: in a symbol, all the blocks have the same number of error
// correction codewords but the first series of blocks can
// have one data codeword less
byte nblocks = runs.first.n_blocks + runs.second.n_blocks;
// BEGIN read data
// the next section handles the interleaving of data codewords
// handling the minimal number of codewords
// n is either codewords_per_block-1 (first blocks) or codewords_per_block-2 (last ones)
for (size_t i = 0; i < runs.first.data_codewords_per_block; i++)
{
skip_bits(scanner, cur * 8);
scanner->block_data[i] = get_codeword(scanner);
skip_bits(scanner, (nblocks-cur-1) * 8);
}
// interleaving specific to the second series of blocks
if (cur < runs.first.n_blocks) // first kind of block
{
skip_bits(scanner, runs.second.n_blocks * 8);
}
else // second kind
{
skip_bits(scanner, (cur - runs.first.n_blocks) * 8);
scanner->block_data[codewords_per_block - 1] = get_codeword(scanner);
skip_bits(scanner, (nblocks-cur - 1) * 8);
}
// END read data
// the module pointer is now at the end of all the data and at
// the beginning of the interleaved error correction codewords
// BEGIN read correction
// this section handles the interleaving of error correction codewords
// same for both types of blocks
size_t n_errwords = runs.first.total_codewords_per_block - runs.first.data_codewords_per_block;
for (size_t i = 0; i < n_errwords; i++)
{
skip_bits(scanner, cur * 8);
scanner->block_data[codewords_per_block+i] = get_codeword(scanner);
skip_bits(scanner, (nblocks-cur-1) * 8);
}
// END read correction
// apply Reed-Solomon error correction
if (rs_decode(codewords_per_block+n_errwords, scanner->block_data, n_errwords) != 0)
{
fprintf(stderr, "Could not correct errors\n");
exit(1);
}
scanner->block_cur = cur+1;
scanner->block_curbyte = 0;
scanner->block_curbit = 0;
}
unsigned int get_bits(scanner_t* scanner, size_t n)
{
if (!scanner->block_cur)
get_block(scanner);
// this bit-by-bit buffer reading is an abomination
unsigned int res = 0;
while (n--)
{
if (scanner->block_curbyte >= scanner->block_dataw)
get_block(scanner);
size_t B = scanner->block_curbyte;
size_t b = scanner->block_curbit;
res *= 2;
res += (scanner->block_data[B] >> (7 - b)) & 1;
scanner->block_curbit++;
if (scanner->block_curbit >= 8)
{
scanner->block_curbyte++;
scanner->block_curbit = 0;
}
}
return res;
}
static void put_block(scanner_t* scanner)
{
// get block information
struct TwoBlockRuns runs = block_sizes[4 * scanner->v + scanner->c];
// current block
size_t cur = scanner->block_cur;
// find the data size of the current block
size_t codewords_per_block = scanner->block_dataw;
// rewind to start of symbol
scanner->i = scanner->s-1;
scanner->j = scanner->s-1;
// apply Reed-Solomon error correction
// same for both types of blocks
size_t n_errwords = runs.first.total_codewords_per_block - runs.first.data_codewords_per_block;
rs_encode(codewords_per_block, scanner->block_data, n_errwords);
// NOTE: in a symbol, all the blocks have the same number of error
// correction codewords but the first series of blocks can
// have one data codeword less
byte nblocks = runs.first.n_blocks + runs.second.n_blocks;
// BEGIN write data
// the next section handles the interleaving of data codewords
// handling the minimal number of codewords
// n is either codewords_per_block-1 (first blocks) or codewords_per_block-2 (last ones)
for (size_t i = 0; i < runs.first.data_codewords_per_block; i++)
{
skip_bits(scanner, cur * 8);
put_codeword(scanner, scanner->block_data[i]);
skip_bits(scanner, (nblocks-cur-1) * 8);
}
// interleaving specific to the second series of blocks
if (runs.first.data_codewords_per_block == codewords_per_block) // first kind of block
{
skip_bits(scanner, runs.second.n_blocks * 8);
}
else // second kind
{
skip_bits(scanner, (cur - runs.first.n_blocks) * 8);
put_codeword(scanner, scanner->block_data[codewords_per_block - 1]);
skip_bits(scanner, (nblocks - cur - 1) * 8);
}
// END write data
// the module pointer is now at the end of all the data and at
// the beginning of the interleaved error correction codewords
// BEGIN write correction
// this section handles the interleaving of error correction codewords
for (size_t i = 0; i < n_errwords; i++)
{
skip_bits(scanner, cur * 8);
put_codeword(scanner, scanner->block_data[codewords_per_block + i]);
skip_bits(scanner, (nblocks - cur - 1) * 8);
}
// END write correction
}
void put_bits(scanner_t* scanner, size_t n, const char* stream)
{
struct TwoBlockRuns runs = block_sizes[4 * scanner->v + scanner->c];
size_t cur_block = 0;
size_t codewords_per_block = runs.first.data_codewords_per_block;
size_t n_blocks = runs.first.n_blocks + runs.second.n_blocks;
while (cur_block < n_blocks)
{
scanner->block_cur = cur_block;
scanner->block_dataw = codewords_per_block;
size_t data_to_copy_in_block = n < codewords_per_block ? n : codewords_per_block;
if (data_to_copy_in_block > 0)
{
memcpy(scanner->block_data, stream, data_to_copy_in_block);
n -= data_to_copy_in_block;
}
// padding
for (size_t i = data_to_copy_in_block; i < codewords_per_block; i++)
scanner->block_data[i] = (i - n) % 2 ? 0x11 : 0xec;
put_block(scanner);
stream += codewords_per_block;
cur_block++;
codewords_per_block = cur_block < runs.first.n_blocks ? runs.first.data_codewords_per_block : runs.second.data_codewords_per_block;
}
}