-
Notifications
You must be signed in to change notification settings - Fork 4
/
uhelper.pas
485 lines (447 loc) · 12.2 KB
/
uhelper.pas
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
unit uhelper;
{$mode objfpc}{$H+}
interface
uses sysutils,zint;
const RHODIUM = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:';
const NEON = '0123456789';
function IsTrue(aBoolean: Boolean): Boolean;
function IsTrue(aInteger: Integer): Boolean;
function iif(const aBoolean: integer; const aCh1,aCh2: pchar): pchar;
function iif(const aBoolean: integer; const aCh1,aCh2: char): char;
function iif(const aBoolean: Boolean; const aV1,aV2: Word): word;
procedure concat(const aText: pchar; const aChar: char);
procedure concat(const aText: pchar; const aText2: pchar);
procedure concat(var aText: array of char; const aText2: pchar);
procedure strcpy(const aText: pchar; const aText2: pchar);
procedure strcpy(var aText: array of char; const aText2: pchar); overload;
function posn(const aText: pchar; const aChar: Char): integer;
function strlen(const aText: array of char): integer;
function is_sane(test_string: PChar; source: PBYTE; length: Integer): Integer;
function utf8toutf16(symbol: PointerTo_zint_symbol; source: PBYTE; vals: PInteger; length: PInteger): Integer;
procedure set_module(symbol: PointerTo_zint_symbol; y_coord: Integer; x_coord: Integer);
procedure unset_module(symbol: PointerTo_zint_symbol; y_coord: Integer; x_coord: Integer);
function module_is_set(symbol: PointerTo_zint_symbol; y_coord: Integer; x_coord: Integer): Boolean;
function PostInc(var v: integer): integer;
function PostDec(var v: integer): integer;
function is_extendable(symbology: Integer): Boolean;
function is_stackable(symbology: Integer): Boolean;
function NotBoolean(const aValue: integer): Boolean;
function NotBoolean(const aValue: Boolean): Boolean;
function latin1_process(symbol: PointerTo_zint_symbol; source: PBYTE; preprocessed: PBYTE; length: PInteger): Integer;
function ctoi(c: char): integer;
function ctoi(c: BYTE): integer;
function BooleanNot(const aValue: integer): Boolean;
procedure memset(const p: Pointer; const aValue: BYTE; const aSize: integer);
function istwodigits(source: PBYTE; position: Integer): Boolean;
implementation
function istwodigits(source: PBYTE; position: Integer): Boolean;
begin
if (Char(source[position]) in ['0'..'9']) and (Char(source[position+1]) in ['0'..'9']) then begin
Result:=true;
end else begin
Result:=false;
end;
end;
function PostInc(var v: integer): integer;
begin
Result:=v;
inc(v);
end;
function PostDec(var v: integer): integer;
begin
Result:=v;
Dec(v);
end;
function IsTrue(aBoolean: Boolean): Boolean;
begin
Result:=aBoolean;
end;
function IsTrue(aInteger: Integer): Boolean;
begin
if aInteger=0 then Result:=false else Result:=true;
end;
function iif(const aBoolean: integer; const aCh1, aCh2: pchar): pchar;
begin
if aBoolean=0 then begin
Result:=aCh2;
end else begin
Result:=aCh1;
end;
end;
function iif(const aBoolean: integer; const aCh1,aCh2: char): char;
begin
if aBoolean=0 then begin
Result:=aCh2;
end else begin
Result:=aCh1;
end;
end;
function iif(const aBoolean: Boolean; const aV1,aV2: Word): word;
begin
if aBoolean then Result:=aV1 else Result:=aV2;
end;
procedure concat(const aText: pchar; const aChar: char);
var
i: integer;
begin
i:=sysutils.strlen(aText);
aText[i]:=aChar;
inc(i);
aText[i]:=#0;
end;
procedure concat(const aText: pchar; const aText2: pchar);
var
i: integer;
begin
i:=sysutils.strlen(aText);
move(aText2^,aText[i],sysutils.strlen(aText2)+1);
end;
procedure concat(var aText: array of char; const aText2: pchar);
begin
concat(pchar(@aText[0]),aText2);
end;
procedure strcpy(const aText: pchar; const aText2: pchar);
begin
move(aText2^,aText^,sysutils.strlen(aText2)+1);
end;
procedure strcpy(var aText: array of char; const aText2: pchar);
begin
move(aText2^,aText[0],sysutils.strlen(aText2)+1);
end;
function posn(const aText: pchar; const aChar: Char): integer;
var
p: Pchar;
c: integer;
begin
p:=aText;
c:=0;
while p^<>#0 do begin
if p^<>aChar then begin
inc(c);
inc(p);
end else begin
Exit(c);
end;
end;
Result:=0;
end;
function strlen(const aText: array of char): integer;
begin
Result:=sysutils.strlen(pchar(@aText[0]));
end;
function is_sane(test_string: PChar; source: PBYTE; length: Integer): Integer;
var
latch: Boolean;
j: Cardinal;
i: Integer;
lt: Cardinal;
begin
{INITCODE} lt := sysutils.strlen (test_string);
i := 0;
while i < length do
begin
latch := FALSE;
j := 0;
while j < lt do
begin
if Boolean(source[i] = BYTE(test_string[j])) then
begin
latch := TRUE;
break;
end;
Inc (j);
end;
if Boolean(NotBoolean (latch)) then
begin
exit (ERROR_INVALID_DATA);
end;
Inc (i);
end;
exit (0);
end;
function utf8toutf16(symbol: PointerTo_zint_symbol; source: PBYTE; vals: PInteger; length: PInteger): Integer;
var
error_number: Integer;
jpos: Integer;
bpos: Integer;
next: Integer;
begin
bpos := 0;
jpos := 0;
error_number := 0;
next := 0;
repeat
if source[bpos] <= $7F then
begin
vals[jpos] := source[bpos];
next := bpos + 1;
Inc (jpos);
end else begin
if (source[bpos] >= $80) and (source[bpos] <= $BF) then
begin
strcpy (symbol^.errtxt, 'Corrupt Unicode data');
exit (ERROR_INVALID_DATA);
end;
if (source[bpos] >= $C0) and (source[bpos] <= $C1) then
begin
strcpy (symbol^.errtxt, 'Overlong encoding not supported');
exit (ERROR_INVALID_DATA);
end;
if (source[bpos] >= $C2) and (source[bpos] <= $DF) then
begin
vals[jpos] := ((source[bpos] and $1F) shl 6) + (source[bpos + 1] and $3F);
next := bpos + 2;
Inc (jpos);
end else begin
if (source[bpos] >= $E0) and (source[bpos] <= $EF) then
begin
vals[jpos] := ((source[bpos] and $0F) shl 12) + ((source[bpos + 1] and $3F) shl 6) + (source[bpos + 2] and $3F);
next := bpos + 3;
Inc (jpos);
end else begin
if source[bpos] >= $F0 then
begin
strcpy (symbol^.errtxt, 'Unicode sequences of more than 3 bytes not supported');
exit (ERROR_INVALID_DATA);
end;
end;
end;
end;
bpos := next;
until not (bpos < length^);
length^ := jpos;
exit (error_number);
end;
function module_is_set(symbol: PointerTo_zint_symbol; y_coord: Integer; x_coord: Integer): Boolean;
var
x_sub: Integer;
x_char: Integer;
begin
x_char := x_coord div 7;
x_sub := x_coord mod 7;
{$PUSH}{$R-}
Result:=((symbol^.encoded_data[y_coord][x_coord div 7] shr (x_coord mod 7)) and 1)<>0;
{$POP}
exit;
Result:=false;
case x_sub of
0: if Boolean((symbol^.encoded_data[y_coord][x_char] and $01) <> 0) then
begin
result := true;
end;
1: if Boolean((symbol^.encoded_data[y_coord][x_char] and $02) <> 0) then
begin
result := true;
end;
2: if Boolean((symbol^.encoded_data[y_coord][x_char] and $04) <> 0) then
begin
result := true;
end;
3: if Boolean((symbol^.encoded_data[y_coord][x_char] and $08) <> 0) then
begin
result := true;
end;
4: if Boolean((symbol^.encoded_data[y_coord][x_char] and $10) <> 0) then
begin
result := true;
end;
5: if Boolean((symbol^.encoded_data[y_coord][x_char] and $20) <> 0) then
begin
result := true;
end;
6: if Boolean((symbol^.encoded_data[y_coord][x_char] and $40) <> 0) then
begin
result := true;
end;
else
result:=false;
end;
end;
procedure set_module(symbol: PointerTo_zint_symbol; y_coord: Integer; x_coord: Integer);
var
x_sub: Integer;
x_char: Integer;
begin
x_char := x_coord div 7;
x_sub := x_coord mod 7;
case x_sub of
0: symbol^.encoded_data[y_coord][x_char] := symbol^.encoded_data[y_coord][x_char] + $01;
1: symbol^.encoded_data[y_coord][x_char] := symbol^.encoded_data[y_coord][x_char] + $02;
2: symbol^.encoded_data[y_coord][x_char] := symbol^.encoded_data[y_coord][x_char] + $04;
3: symbol^.encoded_data[y_coord][x_char] := symbol^.encoded_data[y_coord][x_char] + $08;
4: symbol^.encoded_data[y_coord][x_char] := symbol^.encoded_data[y_coord][x_char] + $10;
5: symbol^.encoded_data[y_coord][x_char] := symbol^.encoded_data[y_coord][x_char] + $20;
6: symbol^.encoded_data[y_coord][x_char] := symbol^.encoded_data[y_coord][x_char] + $40;
end;
end;
procedure unset_module(symbol: PointerTo_zint_symbol; y_coord: Integer; x_coord: Integer);
var
x_sub: Integer;
x_char: Integer;
begin
x_char := x_coord div 7;
x_sub := x_coord mod 7;
case x_sub of
0: symbol^.encoded_data[y_coord][x_char] := symbol^.encoded_data[y_coord][x_char] - $01;
1: symbol^.encoded_data[y_coord][x_char] := symbol^.encoded_data[y_coord][x_char] - $02;
2: symbol^.encoded_data[y_coord][x_char] := symbol^.encoded_data[y_coord][x_char] - $04;
3: symbol^.encoded_data[y_coord][x_char] := symbol^.encoded_data[y_coord][x_char] - $08;
4: symbol^.encoded_data[y_coord][x_char] := symbol^.encoded_data[y_coord][x_char] - $10;
5: symbol^.encoded_data[y_coord][x_char] := symbol^.encoded_data[y_coord][x_char] - $20;
6: symbol^.encoded_data[y_coord][x_char] := symbol^.encoded_data[y_coord][x_char] - $40;
end;
end;
procedure to_upper(a: PBYTE);
begin
while a^<>0 do begin
if char(a^) in ['a'..'z'] then begin
a^:=BYTE(upCase(char(a^)));
end;
inc(a);
end;
end;
function ctoi(c: char): integer;
begin
if c in ['0'..'9'] then begin
Result:=BYTE(c)-48;
end else begin
Result:=-1;
end;
end;
function ctoi(c: BYTE): integer;
begin
Result:=ctoi(char(c));
end;
function BooleanNot(const aValue: integer): Boolean;
begin
Result:=NotBoolean(aValue);
end;
procedure memset(const p: Pointer; const aValue: BYTE; const aSize: integer);
begin
FillByte(PBYTE(p)^,aSize,aValue);
end;
function is_extendable(symbology: Integer): Boolean;
begin
if symbology = BARCODE_EANX then
begin
exit (true);
end;
if symbology = BARCODE_UPCA then
begin
exit (true);
end;
if symbology = BARCODE_UPCE then
begin
exit (true);
end;
if symbology = BARCODE_ISBNX then
begin
exit (true);
end;
if symbology = BARCODE_UPCA_CC then
begin
exit (true);
end;
if symbology = BARCODE_UPCE_CC then
begin
exit (true);
end;
if symbology = BARCODE_EANX_CC then
begin
exit (true);
end;
exit (false);
end;
function is_stackable(symbology: Integer): Boolean;
begin
if symbology < BARCODE_PDF417 then
begin
exit (true);
end;
if symbology = BARCODE_CODE128B then
begin
exit (true);
end;
if symbology = BARCODE_ISBNX then
begin
exit (true);
end;
if symbology = BARCODE_EAN14 then
begin
exit (true);
end;
if symbology = BARCODE_NVE18 then
begin
exit (true);
end;
if symbology = BARCODE_KOREAPOST then
begin
exit (true);
end;
if symbology = BARCODE_PLESSEY then
begin
exit (true);
end;
if symbology = BARCODE_TELEPEN_NUM then
begin
exit (true);
end;
if symbology = BARCODE_ITF14 then
begin
exit (true);
end;
if symbology = BARCODE_CODE32 then
begin
exit (true);
end;
exit (false);
end;
function NotBoolean(const aValue: integer): Boolean;
begin
if aValue=0 then Result:=true else Result:=false;
end;
function NotBoolean(const aValue: Boolean): Boolean;
begin
if aValue=false then Result:=true else Result:=false;
end;
function latin1_process(symbol: PointerTo_zint_symbol; source: PBYTE; preprocessed: PBYTE; length: PInteger): Integer;
var
next: Integer;
i: Integer;
j: Integer;
begin
j := 0;
i := 0;
repeat
next := -1;
if Boolean(source[i] < 128) then
begin
preprocessed[j] := source[i];
Inc (j);
next := i + 1;
end else begin
if Boolean(source[i] = $C2) then
begin
preprocessed[j] := source[i + 1];
Inc (j);
next := i + 2;
end;
if Boolean(source[i] = $C3) then
begin
preprocessed[j] := source[i + 1] + 64;
Inc (j);
next := i + 2;
end;
end;
if Boolean(next = -1) then
begin
strcpy (symbol^.errtxt, 'error: Invalid character in input string (only Latin-1 characters supported)');
exit (ERROR_INVALID_DATA);
end;
i := next;
until not (i < length^);
preprocessed[j] := 0;
length^ := j;
exit (0);
end;
end.