forked from dmtx/libdmtx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmtxencodeedifact.c
190 lines (163 loc) · 5.64 KB
/
dmtxencodeedifact.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
/**
* 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 dmtxencodeedifact.c
* \brief Edifact encoding rules
*/
/**
*
*
*/
static void
EncodeNextChunkEdifact(DmtxEncodeStream *stream)
{
DmtxByte value;
if(StreamInputHasNext(stream))
{
/* Check for FNC1 character, which needs to be sent in ASCII */
value = StreamInputPeekNext(stream); CHKERR;
if((value < 32 || value > 94)) {
StreamMarkInvalid(stream, DmtxChannelUnsupportedChar);
return;
}
if (stream->fnc1 != DmtxUndefined && (int)value == stream->fnc1) {
EncodeChangeScheme(stream, DmtxSchemeAscii, DmtxUnlatchExplicit); CHKERR;
StreamInputAdvanceNext(stream); CHKERR;
AppendValueAscii(stream, DmtxValueFNC1); CHKERR;
return;
}
value = StreamInputAdvanceNext(stream); CHKERR;
AppendValueEdifact(stream, value); CHKERR;
}
}
/**
*
*
*/
static void
AppendValueEdifact(DmtxEncodeStream *stream, DmtxByte value)
{
DmtxByte edifactValue, previousOutput;
CHKSCHEME(DmtxSchemeEdifact);
/*
* TODO: KECA -> korean, circles
* TODO: UNOX -> ISO-2022-JP
* TODO: and so on
*/
if(value < 31 || value > 94)
{
StreamMarkInvalid(stream, DmtxChannelUnsupportedChar);
return;
}
edifactValue = (value & 0x3f) << 2;
switch(stream->outputChainValueCount % 4)
{
case 0:
StreamOutputChainAppend(stream, edifactValue); CHKERR;
break;
case 1:
previousOutput = StreamOutputChainRemoveLast(stream); CHKERR;
StreamOutputChainAppend(stream, previousOutput | (edifactValue >> 6)); CHKERR;
StreamOutputChainAppend(stream, edifactValue << 2); CHKERR;
break;
case 2:
previousOutput = StreamOutputChainRemoveLast(stream); CHKERR;
StreamOutputChainAppend(stream, previousOutput | (edifactValue >> 4)); CHKERR;
StreamOutputChainAppend(stream, edifactValue << 4); CHKERR;
break;
case 3:
previousOutput = StreamOutputChainRemoveLast(stream); CHKERR;
StreamOutputChainAppend(stream, previousOutput | (edifactValue >> 2)); CHKERR;
break;
}
stream->outputChainValueCount++;
}
/**
* Complete EDIFACT encoding if it matches a known end-of-symbol condition.
*
* Term Clean Symbol ASCII Codeword
* Cond Bound Remain Remain Sequence
* ---- ----- ------ ------ -----------
* (a) Y 0 0 [none]
* (b) Y 1 0 PAD
* (c) Y 1 1 ASCII
* (d) Y 2 0 PAD PAD
* (e) Y 2 1 ASCII PAD
* (f) Y 2 2 ASCII ASCII
* - - 0 UNLATCH
*
* If not matching any of the above, continue without doing anything.
*/
static void
CompleteIfDoneEdifact(DmtxEncodeStream *stream, int sizeIdxRequest)
{
int i;
int sizeIdx;
int symbolRemaining;
DmtxBoolean cleanBoundary;
DmtxPassFail passFail;
DmtxByte outputTmpStorage[3];
DmtxByteList outputTmp;
if(stream->status == DmtxStatusComplete)
return;
/*
* If we just completed a triplet (cleanBoundary), 1 or 2 symbol codewords
* remain, and our remaining inputs (if any) represented in ASCII would fit
* in the remaining space, encode them in ASCII with an implicit unlatch.
*/
cleanBoundary = (stream->outputChainValueCount % 4 == 0) ? DmtxTrue : DmtxFalse;
if(cleanBoundary == DmtxTrue)
{
/* Encode up to 3 codewords to a temporary stream */
outputTmp = EncodeTmpRemainingInAscii(stream, outputTmpStorage,
sizeof(outputTmpStorage), &passFail);
if(passFail == DmtxFail)
{
StreamMarkFatal(stream, DmtxErrorUnknown);
return;
}
if(outputTmp.length < 3)
{
/* Find minimum symbol size for projected length */
sizeIdx = FindSymbolSize(stream->output->length + outputTmp.length, sizeIdxRequest); CHKSIZE;
/* Find remaining capacity over current length */
symbolRemaining = GetRemainingSymbolCapacity(stream->output->length, sizeIdx); CHKERR;
if(symbolRemaining < 3 && outputTmp.length <= symbolRemaining)
{
EncodeChangeScheme(stream, DmtxSchemeAscii, DmtxUnlatchImplicit); CHKERR;
for(i = 0; i < outputTmp.length; i++)
{
AppendValueAscii(stream, outputTmp.b[i]); CHKERR;
}
/* Register progress since encoding happened outside normal path */
stream->inputNext = stream->input->length;
/* Pad remaining if necessary */
PadRemainingInAscii(stream, sizeIdx); CHKERR;
StreamMarkComplete(stream, sizeIdx);
return;
}
}
}
if(!StreamInputHasNext(stream))
{
sizeIdx = FindSymbolSize(stream->output->length, sizeIdxRequest); CHKSIZE;
symbolRemaining = GetRemainingSymbolCapacity(stream->output->length, sizeIdx); CHKERR;
/* Explicit unlatch required unless on clean boundary and full symbol */
if(cleanBoundary == DmtxFalse || symbolRemaining > 0)
{
EncodeChangeScheme(stream, DmtxSchemeAscii, DmtxUnlatchExplicit); CHKERR;
sizeIdx = FindSymbolSize(stream->output->length, sizeIdxRequest); CHKSIZE;
PadRemainingInAscii(stream, sizeIdx); CHKERR;
}
StreamMarkComplete(stream, sizeIdx);
}
}