forked from dmtx/libdmtx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmtxencodestream.c
212 lines (186 loc) · 4.21 KB
/
dmtxencodestream.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
/**
* 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 dmtxencodestream.c
* \brief DmtxEncodeStream implementation
*/
/**
*
*
*/
static DmtxEncodeStream
StreamInit(DmtxByteList *input, DmtxByteList *output)
{
DmtxEncodeStream stream;
stream.input = input;
stream.output = output;
stream.currentScheme = DmtxSchemeAscii;
stream.inputNext = 0;
stream.outputChainValueCount = 0;
stream.outputChainWordCount = 0;
stream.reason = NULL;
stream.sizeIdx = DmtxUndefined;
stream.status = DmtxStatusEncoding;
return stream;
}
/**
*
*
*/
static void
StreamCopy(DmtxEncodeStream *dst, DmtxEncodeStream *src)
{
DmtxPassFail passFail;
dst->currentScheme = src->currentScheme;
dst->inputNext = src->inputNext;
dst->outputChainValueCount = src->outputChainValueCount;
dst->outputChainWordCount = src->outputChainWordCount;
dst->reason = src->reason;
dst->sizeIdx = src->sizeIdx;
dst->status = src->status;
dst->input = src->input;
dst->fnc1 = src->fnc1;
dmtxByteListCopy(dst->output, src->output, &passFail);
}
/**
*
*
*/
static void
StreamMarkComplete(DmtxEncodeStream *stream, int sizeIdx)
{
if(stream->status == DmtxStatusEncoding)
{
stream->sizeIdx = sizeIdx;
stream->status = DmtxStatusComplete;
assert(stream->reason == NULL);
}
}
/**
*
*
*/
static void
StreamMarkInvalid(DmtxEncodeStream *stream, int reasonIdx)
{
stream->status = DmtxStatusInvalid;
stream->reason = dmtxErrorMessage[reasonIdx];
}
/**
*
*
*/
static void
StreamMarkFatal(DmtxEncodeStream *stream, int reasonIdx)
{
stream->status = DmtxStatusFatal;
stream->reason = dmtxErrorMessage[reasonIdx];
}
/**
* push on newest/last append
* used for encoding each output cw
*/
static void
StreamOutputChainAppend(DmtxEncodeStream *stream, DmtxByte value)
{
DmtxPassFail passFail;
dmtxByteListPush(stream->output, value, &passFail);
if(passFail == DmtxPass)
stream->outputChainWordCount++;
else
StreamMarkFatal(stream, DmtxErrorOutOfBounds);
}
/**
* pop off newest/last
* used for edifact
*/
static DmtxByte
StreamOutputChainRemoveLast(DmtxEncodeStream *stream)
{
DmtxByte value;
DmtxPassFail passFail;
if(stream->outputChainWordCount > 0)
{
value = dmtxByteListPop(stream->output, &passFail);
stream->outputChainWordCount--;
}
else
{
value = 0;
StreamMarkFatal(stream, DmtxErrorEmptyList);
}
return value;
}
/**
* overwrite arbitrary element
* used for binary length changes
*/
static void
StreamOutputSet(DmtxEncodeStream *stream, int index, DmtxByte value)
{
if(index < 0 || index >= stream->output->length)
StreamMarkFatal(stream, DmtxErrorOutOfBounds);
else
stream->output->b[index] = value;
}
/**
*
*
*/
static DmtxBoolean
StreamInputHasNext(DmtxEncodeStream *stream)
{
return (stream->inputNext < stream->input->length) ? DmtxTrue : DmtxFalse;
}
/**
* peek at first/oldest
* used for ascii double digit
*/
static DmtxByte
StreamInputPeekNext(DmtxEncodeStream *stream)
{
DmtxByte value = 0;
if(StreamInputHasNext(stream))
value = stream->input->b[stream->inputNext];
else
StreamMarkFatal(stream, DmtxErrorOutOfBounds);
return value;
}
/**
* used as each input cw is processed
*
* \param value Value to populate, can be null (for blind dequeues)
* \param stream
*/
static DmtxByte
StreamInputAdvanceNext(DmtxEncodeStream *stream)
{
DmtxByte value;
value = StreamInputPeekNext(stream);
if(stream->status == DmtxStatusEncoding)
stream->inputNext++; /* XXX is this what we really mean here? */
return value;
}
/**
* used as each input cw is processed
*
* \param value Value to populate, can be null (for blind dequeues)
* \param stream
*/
static void
StreamInputAdvancePrev(DmtxEncodeStream *stream)
{
if(stream->inputNext > 0)
stream->inputNext--;
else
StreamMarkFatal(stream, DmtxErrorOutOfBounds);
}