forked from dmtx/libdmtx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmtxencodec40textx12.c
593 lines (523 loc) · 17.8 KB
/
dmtxencodec40textx12.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/**
* libdmtx - Data Matrix Encoding/Decoding Library
* Copyright 2011 Mike Laughton. All rights reserved.
* Copyright 2012-2016 Vadim A. Misbakh-Soloviov. All rights reserved.
*
* See LICENSE file in the main project directory for full
* terms of use and distribution.
*
* Contact:
* Vadim A. Misbakh-Soloviov <[email protected]>
* Mike Laughton <[email protected]>
*
* \file dmtxencodec40textx12.c
* \brief C40/Text/X12 encoding rules
*/
#undef CHKERR
#define CHKERR { if(stream->status != DmtxStatusEncoding) { return; } }
#undef CHKSIZE
#define CHKSIZE { if(sizeIdx == DmtxUndefined) { StreamMarkInvalid(stream, DmtxErrorUnknown); return; } }
#undef CHKPASS
#define CHKPASS { if(passFail == DmtxFail) { StreamMarkFatal(stream, DmtxErrorUnknown); return; } }
#undef RETURN_IF_FAIL
#define RETURN_IF_FAIL { if(*passFail == DmtxFail) return; }
/**
*
*
*/
static void
EncodeNextChunkCTX(DmtxEncodeStream *stream, int sizeIdxRequest)
{
int i;
DmtxPassFail passFail;
DmtxByte inputValue;
DmtxByte valueListStorage[6];
DmtxByteList valueList = dmtxByteListBuild(valueListStorage, sizeof(valueListStorage));
while(StreamInputHasNext(stream))
{
if(stream->currentScheme == DmtxSchemeX12)
{
/* Check for FNC1 character */
inputValue = StreamInputPeekNext(stream); CHKERR;
if(stream->fnc1 != DmtxUndefined && (int)inputValue == stream->fnc1) {
/* X12 does not allow partial blocks, resend last 1 or 2 as ASCII */
EncodeChangeScheme(stream, DmtxSchemeAscii, DmtxUnlatchExplicit); CHKERR;
for(i = 0; i < valueList.length % 3; i++)
StreamInputAdvancePrev(stream); CHKERR;
while(i) {
inputValue = StreamInputAdvanceNext(stream); CHKERR;
AppendValueAscii(stream, inputValue + 1); CHKERR;
i--;
}
StreamInputAdvanceNext(stream); CHKERR;
AppendValueAscii(stream, DmtxValueFNC1); CHKERR;
return;
}
}
inputValue = StreamInputAdvanceNext(stream); CHKERR;
/* Expand next input value into up to 4 CTX values and add to valueList */
PushCTXValues(&valueList, inputValue, stream->currentScheme, &passFail, stream->fnc1);
if(passFail == DmtxFail)
{
/* XXX Perhaps PushCTXValues should return this error code */
StreamMarkInvalid(stream, DmtxErrorUnsupportedCharacter);
return;
}
/* If there at least 3 CTX values available encode them to output */
while(valueList.length >= 3)
{
AppendValuesCTX(stream, &valueList); CHKERR;
ShiftValueListBy3(&valueList, &passFail); CHKPASS;
}
/* Finished on byte boundary -- done with current chunk */
if(valueList.length == 0)
break;
}
/*
* Special case: If all input values have been consumed and 1 or 2 unwritten
* C40/Text/X12 values remain, finish encoding the symbol according to the
* established end-of-symbol conditions.
*/
if(!StreamInputHasNext(stream) && valueList.length > 0)
{
if(stream->currentScheme == DmtxSchemeX12)
{
CompletePartialX12(stream, &valueList, sizeIdxRequest); CHKERR;
}
else
{
CompletePartialC40Text(stream, &valueList, sizeIdxRequest); CHKERR;
}
}
}
/**
*
*
*/
static void
AppendValuesCTX(DmtxEncodeStream *stream, DmtxByteList *valueList)
{
int pairValue;
DmtxByte cw0, cw1;
if(!IsCTX(stream->currentScheme))
{
StreamMarkFatal(stream, DmtxErrorUnexpectedScheme);
return;
}
if(valueList->length < 3)
{
StreamMarkFatal(stream, DmtxErrorIncompleteValueList);
return;
}
/* Build codewords from computed value */
pairValue = (1600 * valueList->b[0]) + (40 * valueList->b[1]) + valueList->b[2] + 1;
cw0 = pairValue / 256;
cw1 = pairValue % 256;
/* Append 2 codewords */
StreamOutputChainAppend(stream, cw0); CHKERR;
StreamOutputChainAppend(stream, cw1); CHKERR;
/* Update count for 3 encoded values */
stream->outputChainValueCount += 3;
}
/**
*
*
*/
static void
AppendUnlatchCTX(DmtxEncodeStream *stream)
{
if(!IsCTX(stream->currentScheme))
{
StreamMarkFatal(stream, DmtxErrorUnexpectedScheme);
return;
}
/* Verify we are on byte boundary */
if(stream->outputChainValueCount % 3 != 0)
{
StreamMarkInvalid(stream, DmtxErrorNotOnByteBoundary);
return;
}
StreamOutputChainAppend(stream, DmtxValueCTXUnlatch); CHKERR;
stream->outputChainValueCount++;
}
/**
* Complete C40/Text/X12 encoding if it matches a known end-of-symbol condition.
*
* Term Trip Symbol Codeword
* Cond Size Remain Sequence
* ---- ---- ------ -----------------------
* (a) 3 2 Special case
* - - UNLATCH [PAD]
*/
static void
CompleteIfDoneCTX(DmtxEncodeStream *stream, int sizeIdxRequest)
{
int sizeIdx;
int symbolRemaining;
if(stream->status == DmtxStatusComplete)
return;
if(!StreamInputHasNext(stream))
{
sizeIdx = FindSymbolSize(stream->output->length, sizeIdxRequest); CHKSIZE;
symbolRemaining = GetRemainingSymbolCapacity(stream->output->length, sizeIdx);
if(symbolRemaining > 0)
{
EncodeChangeScheme(stream, DmtxSchemeAscii, DmtxUnlatchExplicit); CHKERR;
PadRemainingInAscii(stream, sizeIdx);
}
StreamMarkComplete(stream, sizeIdx);
}
}
/**
* The remaining values can exist in 3 possible cases:
*
* a) 1 C40/Text/X12 remaining == 1 data
* b) 2 C40/Text/X12 remaining == 1 shift + 1 data
* c) 2 C40/Text/X12 remaining == 1 data + 1 data
*
* To distinguish between cases (b) and (c), encode the final input value to
* C40/Text/X12 in a temporary location and check the resulting length. If
* it expands to multiple values it represents (b); otherwise it is (c). This
* accounts for both shift and upper shift conditions.
*
* Note that in cases (a) and (c) the final C40/Text/X12 value encoded in the
* previous chunk may have been a shift value, but this will be ignored by
* the decoder due to the implicit shift to ASCII. <-- what if symbol is much
* larger though?
*
* Term Value Symbol Codeword
* Cond Count Remain Sequence
* ---- ------- ------ ------------------------
* (b) C40 2 2 C40+C40+0
* (d) ASCII 1 1 ASCII (implicit unlatch)
* (c) ASCII 1 2 UNLATCH ASCII
* - - UNLATCH (finish ASCII)
*/
static void
CompletePartialC40Text(DmtxEncodeStream *stream, DmtxByteList *valueList, int sizeIdxRequest)
{
int i;
int sizeIdx1, sizeIdx2;
int symbolRemaining1, symbolRemaining2;
DmtxPassFail passFail;
DmtxByte inputValue;
DmtxByte outputTmpStorage[4];
DmtxByteList outputTmp = dmtxByteListBuild(outputTmpStorage, sizeof(outputTmpStorage));
if(stream->currentScheme != DmtxSchemeC40 && stream->currentScheme != DmtxSchemeText)
{
StreamMarkFatal(stream, DmtxErrorUnexpectedScheme);
return;
}
/* Should have exactly one or two input values left */
assert(valueList->length == 1 || valueList->length == 2);
sizeIdx1 = FindSymbolSize(stream->output->length + 1, sizeIdxRequest);
sizeIdx2 = FindSymbolSize(stream->output->length + 2, sizeIdxRequest);
symbolRemaining1 = GetRemainingSymbolCapacity(stream->output->length, sizeIdx1);
symbolRemaining2 = GetRemainingSymbolCapacity(stream->output->length, sizeIdx2);
if(valueList->length == 2 && symbolRemaining2 == 2)
{
/* End of symbol condition (b) -- Use Shift1 to pad final list value */
dmtxByteListPush(valueList, DmtxValueCTXShift1, &passFail); CHKPASS;
AppendValuesCTX(stream, valueList); CHKERR;
StreamMarkComplete(stream, sizeIdx2);
}
else
{
/*
* Rollback progress of previously consumed input value(s) since ASCII
* encoder will be used to finish the symbol. 2 rollbacks are needed if
* valueList holds 2 data words (i.e., not shifts or upper shifts).
*/
StreamInputAdvancePrev(stream); CHKERR;
inputValue = StreamInputPeekNext(stream); CHKERR;
/* Test-encode most recently consumed input value to C40/Text/X12 */
PushCTXValues(&outputTmp, inputValue, stream->currentScheme, &passFail, stream->fnc1);
if(valueList->length == 2 && outputTmp.length == 1)
StreamInputAdvancePrev(stream); CHKERR;
/* Re-use outputTmp to hold ASCII representation of 1-2 input values */
/* XXX Refactor how the DmtxByteList is passed back here */
outputTmp = EncodeTmpRemainingInAscii(stream, outputTmpStorage,
sizeof(outputTmpStorage), &passFail);
if(passFail == DmtxFail)
{
StreamMarkFatal(stream, DmtxErrorUnknown);
return;
}
if(outputTmp.length == 1 && symbolRemaining1 == 1)
{
/* End of symbol condition (d) */
EncodeChangeScheme(stream, DmtxSchemeAscii, DmtxUnlatchImplicit); CHKERR;
AppendValueAscii(stream, outputTmp.b[0]); CHKERR;
/* Register progress since encoding happened outside normal path */
stream->inputNext = stream->input->length;
StreamMarkComplete(stream, sizeIdx1);
}
else
{
/* Finish in ASCII (c) */
EncodeChangeScheme(stream, DmtxSchemeAscii, DmtxUnlatchExplicit); CHKERR;
for(i = 0; i < outputTmp.length; i++)
AppendValueAscii(stream, outputTmp.b[i]); CHKERR;
sizeIdx1 = FindSymbolSize(stream->output->length, sizeIdxRequest);
PadRemainingInAscii(stream, sizeIdx1);
/* Register progress since encoding happened outside normal path */
stream->inputNext = stream->input->length;
StreamMarkComplete(stream, sizeIdx1);
}
}
}
/**
* Partial chunks are not valid in X12. Encode using ASCII instead, using
* an implied unlatch if there is exactly one ascii codeword and one symbol
* codeword remaining. Otherwise use explicit unlatch.
*/
static void
CompletePartialX12(DmtxEncodeStream *stream, DmtxByteList *valueList, int sizeIdxRequest)
{
int i;
int sizeIdx;
int symbolRemaining;
DmtxPassFail passFail;
DmtxByte outputTmpStorage[2];
DmtxByteList outputTmp;
if(stream->currentScheme != DmtxSchemeX12)
{
StreamMarkFatal(stream, DmtxErrorUnexpectedScheme);
return;
}
/* Should have exactly one or two input values left */
assert(valueList->length == 1 || valueList->length == 2);
/* Roll back input progress */
for(i = 0; i < valueList->length; i++)
{
StreamInputAdvancePrev(stream); CHKERR;
}
/* Encode up to 2 codewords to a temporary stream */
outputTmp = EncodeTmpRemainingInAscii(stream, outputTmpStorage,
sizeof(outputTmpStorage), &passFail);
sizeIdx = FindSymbolSize(stream->output->length + 1, sizeIdxRequest);
symbolRemaining = GetRemainingSymbolCapacity(stream->output->length, sizeIdx);
if(outputTmp.length == 1 && symbolRemaining == 1)
{
/* End of symbol condition (XXX) */
EncodeChangeScheme(stream, DmtxSchemeAscii, DmtxUnlatchImplicit); CHKERR;
AppendValueAscii(stream, outputTmp.b[0]); CHKERR;
/* Register progress since encoding happened outside normal path */
stream->inputNext = stream->input->length;
StreamMarkComplete(stream, sizeIdx);
}
else
{
/* Finish in ASCII (XXX) */
EncodeChangeScheme(stream, DmtxSchemeAscii, DmtxUnlatchExplicit); CHKERR;
for(i = 0; i < outputTmp.length; i++)
AppendValueAscii(stream, outputTmp.b[i]); CHKERR;
sizeIdx = FindSymbolSize(stream->output->length, sizeIdxRequest);
PadRemainingInAscii(stream, sizeIdx);
/* Register progress since encoding happened outside normal path */
stream->inputNext = stream->input->length;
StreamMarkComplete(stream, sizeIdx);
}
}
/**
* Return DmtxTrue 1 or 2 X12 values remain, otherwise DmtxFalse
*/
static DmtxBoolean
PartialX12ChunkRemains(DmtxEncodeStream *stream)
{
DmtxEncodeStream streamTmp;
DmtxByte inputValue;
DmtxByte valueListStorage[6];
DmtxByteList valueList = dmtxByteListBuild(valueListStorage, sizeof(valueListStorage));
DmtxPassFail passFail;
/* Create temporary copy of stream to track test input progress */
streamTmp = *stream;
streamTmp.currentScheme = DmtxSchemeX12;
streamTmp.outputChainValueCount = 0;
streamTmp.outputChainWordCount = 0;
streamTmp.reason = NULL;
streamTmp.sizeIdx = DmtxUndefined;
streamTmp.status = DmtxStatusEncoding;
streamTmp.output = NULL;
while(StreamInputHasNext(&streamTmp))
{
inputValue = StreamInputAdvanceNext(&streamTmp);
if(stream->status != DmtxStatusEncoding)
{
StreamMarkInvalid(stream, DmtxErrorUnknown);
return DmtxFalse;
}
/* Expand next input value into up to 4 CTX values and add to valueList */
PushCTXValues(&valueList, inputValue, streamTmp.currentScheme, &passFail, stream->fnc1);
if(passFail == DmtxFail)
{
StreamMarkInvalid(stream, DmtxErrorUnknown);
return DmtxFalse;
}
/* Not a final partial chunk */
if(valueList.length >= 3)
return DmtxFalse;
}
return (valueList.length == 0) ? DmtxFalse : DmtxTrue;
}
/**
*
*
*/
static void
PushCTXValues(DmtxByteList *valueList, DmtxByte inputValue, int targetScheme,
DmtxPassFail *passFail, int fnc1)
{
assert(valueList->length <= 2);
/* Handle extended ASCII with Upper Shift character */
if(inputValue > 127 && (fnc1 == DmtxUndefined || (int)inputValue != fnc1))
{
if(targetScheme == DmtxSchemeX12)
{
*passFail = DmtxFail;
return;
}
else
{
dmtxByteListPush(valueList, DmtxValueCTXShift2, passFail); RETURN_IF_FAIL;
dmtxByteListPush(valueList, 30, passFail); RETURN_IF_FAIL;
inputValue -= 128;
}
}
/* Handle all other characters according to encodation scheme */
if(targetScheme == DmtxSchemeX12)
{
if(inputValue == 13)
{
dmtxByteListPush(valueList, 0, passFail); RETURN_IF_FAIL;
}
else if(inputValue == 42)
{
dmtxByteListPush(valueList, 1, passFail); RETURN_IF_FAIL;
}
else if(inputValue == 62)
{
dmtxByteListPush(valueList, 2, passFail); RETURN_IF_FAIL;
}
else if(inputValue == 32)
{
dmtxByteListPush(valueList, 3, passFail); RETURN_IF_FAIL;
}
else if(inputValue >= 48 && inputValue <= 57)
{
dmtxByteListPush(valueList, inputValue - 44, passFail); RETURN_IF_FAIL;
}
else if(inputValue >= 65 && inputValue <= 90)
{
dmtxByteListPush(valueList, inputValue - 51, passFail); RETURN_IF_FAIL;
}
else
{
*passFail = DmtxFail;
return;
}
}
else
{
/* targetScheme is C40 or Text */
/* Check for FNC1 character */
if(fnc1 != DmtxUndefined && (int)inputValue == fnc1)
{
dmtxByteListPush(valueList, DmtxValueCTXShift2, passFail); RETURN_IF_FAIL;
dmtxByteListPush(valueList, 27, passFail); RETURN_IF_FAIL; /* C40 version of FNC1 */
}
else if(inputValue <= 31)
{
dmtxByteListPush(valueList, DmtxValueCTXShift1, passFail); RETURN_IF_FAIL;
dmtxByteListPush(valueList, inputValue, passFail); RETURN_IF_FAIL;
}
else if(inputValue == 32)
{
dmtxByteListPush(valueList, 3, passFail); RETURN_IF_FAIL;
}
else if(inputValue <= 47)
{
dmtxByteListPush(valueList, DmtxValueCTXShift2, passFail); RETURN_IF_FAIL;
dmtxByteListPush(valueList, inputValue - 33, passFail); RETURN_IF_FAIL;
}
else if(inputValue <= 57)
{
dmtxByteListPush(valueList, inputValue - 44, passFail); RETURN_IF_FAIL;
}
else if(inputValue <= 64)
{
dmtxByteListPush(valueList, DmtxValueCTXShift2, passFail); RETURN_IF_FAIL;
dmtxByteListPush(valueList, inputValue - 43, passFail); RETURN_IF_FAIL;
}
else if(inputValue <= 90 && targetScheme == DmtxSchemeC40)
{
dmtxByteListPush(valueList, inputValue - 51, passFail); RETURN_IF_FAIL;
}
else if(inputValue <= 90 && targetScheme == DmtxSchemeText)
{
dmtxByteListPush(valueList, DmtxValueCTXShift3, passFail); RETURN_IF_FAIL;
dmtxByteListPush(valueList, inputValue - 64, passFail); RETURN_IF_FAIL;
}
else if(inputValue <= 95)
{
dmtxByteListPush(valueList, DmtxValueCTXShift2, passFail); RETURN_IF_FAIL;
dmtxByteListPush(valueList, inputValue - 69, passFail); RETURN_IF_FAIL;
}
else if(inputValue == 96 && targetScheme == DmtxSchemeText)
{
dmtxByteListPush(valueList, DmtxValueCTXShift3, passFail); RETURN_IF_FAIL;
dmtxByteListPush(valueList, 0, passFail); RETURN_IF_FAIL;
}
else if(inputValue <= 122 && targetScheme == DmtxSchemeText)
{
dmtxByteListPush(valueList, inputValue - 83, passFail); RETURN_IF_FAIL;
}
else if(inputValue <= 127)
{
dmtxByteListPush(valueList, DmtxValueCTXShift3, passFail); RETURN_IF_FAIL;
dmtxByteListPush(valueList, inputValue - 96, passFail); RETURN_IF_FAIL;
}
else
{
*passFail = DmtxFail;
return;
}
}
*passFail = DmtxPass;
}
/**
*
*
*/
static DmtxBoolean
IsCTX(int scheme)
{
DmtxBoolean isCTX;
if(scheme == DmtxSchemeC40 || scheme == DmtxSchemeText || scheme == DmtxSchemeX12)
isCTX = DmtxTrue;
else
isCTX = DmtxFalse;
return isCTX;
}
/**
*
*
*/
static void
ShiftValueListBy3(DmtxByteList *list, DmtxPassFail *passFail)
{
int i;
/* Shift values */
for(i = 0; i < list->length - 3; i++)
list->b[i] = list->b[i+3];
/* Shorten list by 3 (or less) */
for(i = 0; i < 3; i++)
{
dmtxByteListPop(list, passFail);
if(*passFail == DmtxFail)
return;
if(list->length == 0)
break;
}
*passFail = DmtxPass;
}