-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
as_barcode.pkb
6067 lines (6062 loc) · 222 KB
/
as_barcode.pkb
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
create or replace package body as_barcode
is
/**********************************************
**
** Author: Anton Scheffer
**
** Changelog:
** Date: 2016-12-14
** beta release
** Date: 2017-02-11
** version 0.20
** Date: 2017-08-15
** fixed bug in used png format
** Date: 2022-11-04
** added svg output
** Date: 2022-12-14
** started with datamatrix
** Date: 2023-01-30
** UPC-A
** Date: 2023-02-08
** bmp-format
** Date: 2023-03-17
** gif-format
** Date: 2023-04-12
** allow larger datauri_barcode_svg
** Date: 2023-09-23
** added "transparant" to p_parm options
** fixed bug for multi-byte characters in QR-code
** Date: 2024-06-20
** added transparant to gif format
** fixed colors for bmp format
** fixed bmp and gif format for code39, code128 and itf
** added logo to QR in svg format
** added jpeg format
** Date: 2024-09-08
** added some postal 4-state barcodes
** * Royal Mail 4-State Customer Code
** * Australia Post 4-State Customer Barcode
** * Dutch PostNL KIX
** Date: 2024-10-21
** compile on database 12.2 and older
******************************************************************************
******************************************************************************
Copyright (C) 2016-2024 by Anton Scheffer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
******************************************************************************
******************************************** */
type tp_bits is table of pls_integer index by pls_integer;
type tp_matrix is table of tp_bits index by pls_integer;
type tp_bar_row is table of number;
type tp_mapping is table of tp_bar_row index by pls_integer;
--
procedure converttoblob( dest_blob in out nocopy blob
, src_clob in clob
)
is
l_dest integer := 1;
l_src integer := 1;
l_ctx integer := dbms_lob.default_lang_ctx;
l_warn integer;
begin
dbms_lob.converttoblob( dest_blob
, src_clob
, dbms_lob.lobmaxsize
, l_dest
, l_src
, dbms_lob.default_csid
, l_ctx
, l_warn
);
end;
--
function base64_encode( p_src blob )
return clob
is
l_rv clob;
l_chunk_size pls_integer := 21000;
begin
if p_src is null
then
return null;
end if;
for i in 0 .. trunc( ( dbms_lob.getlength( p_src ) - 1 ) / l_chunk_size )
loop
l_rv := l_rv || translate( utl_raw.cast_to_varchar2(
utl_encode.base64_encode(
dbms_lob.substr( p_src
, l_chunk_size
, 1 + i * l_chunk_size
) ) )
, 'a' || chr(10) || chr(13), 'a'
);
end loop;
return l_rv;
end;
--
function xjv
( p_json varchar2 character set any_cs
, p_path varchar2
, p_unescape varchar2 := 'Y'
)
return varchar2 character set p_json%charset
is
c_double_quote constant varchar2(1) character set p_json%charset := '"';
c_single_quote constant varchar2(1) character set p_json%charset := '''';
c_back_slash constant varchar2(1) character set p_json%charset := '\';
c_space constant varchar2(1) character set p_json%charset := ' ';
c_colon constant varchar2(1) character set p_json%charset := ':';
c_comma constant varchar2(1) character set p_json%charset := ',';
c_end_brace constant varchar2(1) character set p_json%charset := '}';
c_start_brace constant varchar2(1) character set p_json%charset := '{';
c_end_bracket constant varchar2(1) character set p_json%charset := ']';
c_start_bracket constant varchar2(1) character set p_json%charset := '[';
c_ht constant varchar2(1) character set p_json%charset := chr(9);
c_lf constant varchar2(1) character set p_json%charset := chr(10);
c_cr constant varchar2(1) character set p_json%charset := chr(13);
c_ws constant varchar2(4) character set p_json%charset := c_space || c_ht || c_cr || c_lf;
--
g_idx number;
g_end number;
--
l_nchar boolean := isnchar( c_space );
l_pos number;
l_ind number;
l_start number;
l_rv_end number;
l_rv_start number;
l_path varchar2(32767);
l_name varchar2(32767);
l_tmp_name varchar2(32767);
l_rv varchar2(32767) character set p_json%charset;
l_chr varchar2(10) character set p_json%charset;
--
procedure skip_whitespace
is
begin
while substr( p_json, g_idx, 1 ) in ( c_space, c_lf, c_cr, c_ht )
loop
g_idx:= g_idx+ 1;
end loop;
if g_idx > g_end
then
raise_application_error( -20001, 'Unexpected end of JSON' );
end if;
end;
--
procedure skip_value;
procedure skip_array;
--
procedure skip_object
is
begin
if substr( p_json, g_idx, 1 ) = c_start_brace
then
g_idx := g_idx + 1;
loop
skip_whitespace;
exit when substr( p_json, g_idx, 1 ) = c_end_brace; -- empty object or object with "trailing comma"
skip_value; -- skip name
skip_whitespace;
if substr( p_json, g_idx, 1 ) != c_colon
then
raise_application_error( -20002, 'No valid JSON, expected a colon at position ' || g_idx );
end if;
g_idx := g_idx + 1; -- skip colon
skip_value; -- skip value
skip_whitespace;
case substr( p_json, g_idx, 1 )
when c_comma then g_idx := g_idx + 1;
when c_end_brace then exit;
else raise_application_error( -20003, 'No valid JSON, expected a comma or end brace at position ' || g_idx );
end case;
end loop;
g_idx := g_idx + 1;
end if;
end;
--
procedure skip_array
is
begin
if substr( p_json, g_idx, 1 ) = c_start_bracket
then
g_idx := g_idx + 1;
loop
skip_whitespace;
exit when substr( p_json, g_idx, 1 ) = c_end_bracket; -- empty array or array with "trailing comma"
skip_value;
skip_whitespace;
case substr( p_json, g_idx, 1 )
when c_comma then g_idx := g_idx + 1;
when c_end_bracket then exit;
else raise_application_error( -20004, 'No valid JSON, expected a comma or end bracket at position ' || g_idx );
end case;
end loop;
g_idx := g_idx + 1;
end if;
end;
--
procedure skip_value
is
begin
skip_whitespace;
case substr( p_json, g_idx, 1 )
when c_double_quote
then
loop
g_idx := instr( p_json, c_double_quote, g_idx + 1 );
exit when substr( p_json, g_idx - 1, 1 ) != c_back_slash
or g_idx = 0
or ( substr( p_json, g_idx - 2, 2 ) = c_back_slash || c_back_slash
and substr( p_json, g_idx - 3, 1 ) != c_back_slash
); -- doesn't handle cases of values ending with multiple (escaped) \
end loop;
if g_idx = 0
then
raise_application_error( -20005, 'No valid JSON, no end string found' );
end if;
g_idx := g_idx + 1;
when c_single_quote
then
g_idx := instr( p_json, c_single_quote, g_idx ) + 1;
if g_idx = 1
then
raise_application_error( -20006, 'No valid JSON, no end string found' );
end if;
when c_start_brace
then
skip_object;
when c_start_bracket
then
skip_array;
else -- should be a JSON-number, TRUE, FALSE or NULL, but we don't check for it
g_idx := least( coalesce( nullif( instr( p_json, c_space, g_idx ), 0 ), g_end + 1 )
, coalesce( nullif( instr( p_json, c_comma, g_idx ), 0 ), g_end + 1 )
, coalesce( nullif( instr( p_json, c_end_brace, g_idx ), 0 ), g_end + 1 )
, coalesce( nullif( instr( p_json, c_end_bracket, g_idx ), 0 ), g_end + 1)
, coalesce( nullif( instr( p_json, c_colon, g_idx ), 0 ), g_end + 1 )
);
if g_idx = g_end + 1
then
raise_application_error( -20007, 'No valid JSON, no end string found' );
end if;
end case;
end;
begin
if p_json is null
then
return null;
end if;
l_path := ltrim( p_path, c_ws );
if l_path is null
then
return null;
end if;
g_idx := 1;
g_end := length( p_json );
for i in 1 .. 20 -- max 20 levels deep in p_path
loop
l_path := ltrim( l_path, c_ws );
l_pos := least( nvl( nullif( instr( l_path, '.' ), 0 ), 32768 )
, nvl( nullif( instr( l_path, c_start_bracket ), 0 ), 32768 )
, nvl( nullif( instr( l_path, c_end_bracket ), 0 ), 32768 )
);
if l_pos = 32768
then
l_name := l_path;
l_path := null;
elsif substr( l_path, l_pos, 1 ) = '.'
then
l_name := substr( l_path, 1, l_pos - 1 );
l_path := substr( l_path, l_pos + 1 );
elsif substr( l_path, l_pos, 1 ) = c_start_bracket and l_pos > 1
then
l_name := substr( l_path, 1, l_pos - 1 );
l_path := substr( l_path, l_pos );
elsif substr( l_path, l_pos, 1 ) = c_start_bracket and l_pos = 1
then
l_pos := instr( l_path, c_end_bracket );
if l_pos = 0
then
raise_application_error( -20008, 'No valid path, end bracket expected' );
end if;
l_name := substr( l_path, 1, l_pos );
if substr( l_path, l_pos + 1, 1 ) = '.'
then
l_path := substr( l_path, l_pos + 2 );
else
l_path := substr( l_path, l_pos + 1 );
end if;
end if;
l_name := rtrim( l_name, c_ws );
--
skip_whitespace;
if substr( p_json, g_idx, 1 ) = c_start_brace and substr( l_name, 1, 1 ) != c_start_bracket
then -- search for a name inside JSON object
-- json unescape name?
loop
g_idx := g_idx + 1; -- skip start brace or comma
skip_whitespace;
if substr( p_json, g_idx, 1 ) = c_end_brace
then
return null;
end if;
l_start := g_idx;
skip_value; -- skip a name
l_tmp_name := substr( p_json, l_start, g_idx - l_start ); -- look back to get the name skipped
-- json unescape name?
skip_whitespace;
if substr( p_json, g_idx, 1 ) != c_colon
then
raise_application_error( -20002, 'No valid JSON, expected a colon at position ' || g_idx );
end if;
g_idx := g_idx + 1; -- skip colon
skip_whitespace;
l_rv_start := g_idx;
skip_value;
if l_tmp_name in ( c_double_quote || l_name || c_double_quote
, c_single_quote || l_name || c_single_quote
, l_name
)
then
l_rv_end := g_idx;
exit;
else
skip_whitespace;
if substr( p_json, g_idx, 1 ) = c_comma
then
null; -- OK, keep on searching for name
else
return null; -- searched name not found
end if;
end if;
end loop;
elsif substr( p_json, g_idx, 1 ) = c_start_bracket and substr( l_name, 1, 1 ) = c_start_bracket
then
begin
l_ind := to_number( rtrim( ltrim( l_name, c_start_bracket ), c_end_bracket ) );
exception
when value_error
then
raise_application_error( -20009, 'No valid path, array index number expected' );
end;
for i in 0 .. l_ind loop
g_idx := g_idx + 1; -- skip start bracket or comma
skip_whitespace;
if substr( p_json, g_idx, 1 ) = c_end_bracket
then
return null;
end if;
l_rv_start := g_idx;
skip_value;
if i = l_ind
then
l_rv_end := g_idx;
exit;
else
skip_whitespace;
if substr( p_json, g_idx, 1 ) = c_comma
then
null; -- OK
else
return null;
end if;
end if;
end loop;
else
return null;
end if;
exit when l_path is null;
g_idx := l_rv_start;
g_end := l_rv_end - 1;
end loop;
if ( ( substr( p_json, l_rv_start, 1 ) = c_double_quote
and substr( p_json, l_rv_end - 1, 1 ) = c_double_quote
)
or ( substr( p_json, l_rv_start, 1 ) = c_single_quote
and substr( p_json, l_rv_end - 1, 1 ) = c_single_quote
)
)
then
l_rv_start := l_rv_start + 1;
l_rv_end := l_rv_end - 1;
end if;
l_pos := instr( p_json, c_back_slash, l_rv_start );
if l_pos = 0 or l_pos >= l_rv_end or nvl( substr( upper( p_unescape ), 1, 1 ), 'Y' ) = 'N'
then -- no JSON unescaping needed
return substr( p_json, l_rv_start, l_rv_end - l_rv_start );
end if;
l_start := l_rv_start;
loop
l_chr := substr( p_json, l_pos + 1, 1 );
if l_chr in ( '"', '\', '/' )
then
l_rv := l_rv || ( substr( p_json, l_start, l_pos - l_start ) || l_chr );
elsif l_chr in ( 'b', 'f', 'n', 'r', 't' )
then
l_chr := translate( l_chr
, 'btnfr'
, chr(8) || chr(9) || chr(10) || chr(12) || chr(13)
);
l_rv := l_rv || ( substr( p_json, l_start, l_pos - l_start ) || l_chr );
elsif l_chr = 'u'
then -- unicode character
if l_nchar
then
l_chr := utl_i18n.raw_to_nchar( hextoraw( substr( p_json, l_pos + 2, 4 ) ), 'AL16UTF16' );
else
l_chr := utl_i18n.raw_to_char( hextoraw( substr( p_json, l_pos + 2, 4 ) ), 'AL16UTF16' );
end if;
l_rv := l_rv || ( substr( p_json, l_start, l_pos - l_start ) || l_chr );
l_pos := l_pos + 4;
else
raise_application_error( -20011, 'No valid JSON, unexpected back slash at position ' || l_pos );
end if;
l_start := l_pos + 2;
l_pos := instr( p_json, c_back_slash, l_start );
if l_pos = 0 or l_pos >= l_rv_end
then
l_rv := l_rv || substr( p_json, l_start, l_rv_end - l_start );
exit;
end if;
end loop;
return l_rv;
end;
--
function jv( p_json varchar2 character set any_cs
, p_path varchar2
, p_unescape varchar2 := 'Y'
)
return varchar2 character set p_json%charset
is
begin
return xjv( p_json, p_path, p_unescape );
exception when others then
return null;
end;
--
function check_int( p_parm varchar2, p_name varchar2, p_max pls_integer := null, p_min pls_integer := null )
return pls_integer
is
l_int pls_integer;
begin
l_int := trunc( to_number( xjv( p_parm, p_name ) ) );
if l_int not between nvl( p_min, 0 ) and nvl( p_max, l_int )
then
l_int := null;
end if;
return l_int;
exception when others then return null;
end;
function check_pos( p_parm varchar2, p_name varchar2 )
return boolean
is
begin
return coalesce( lower( jv( p_parm, p_name ) ) in ( 'true', 'y', 'yes' ), false );
end;
--
function check_neg( p_parm varchar2, p_name varchar2 )
return boolean
is
begin
return coalesce( lower( jv( p_parm, p_name ) ) in ( 'false', 'n', 'no' ), false );
end;
--
function generate_png( p_dat blob, p_width pls_integer, p_height pls_integer, p_parm varchar2 )
return raw
is
t_ihdr raw(25);
t_plte raw(32);
t_trns raw(32);
t_idat raw(32767);
--
function crc32( p_src raw )
return raw
is
begin
return utl_raw.reverse( utl_raw.substr( utl_raw.substr( utl_compress.lz_compress( p_src ), -8 ), 1, 4 ) );
end;
--
function method0_compress( p_val blob )
return raw
is
t_tmp raw(32767);
function adler32( p_val blob )
return varchar2
is
s1 pls_integer := 1;
s2 pls_integer := 0;
t_val varchar2(32766);
t_pos number := 1;
t_len number := dbms_lob.getlength( p_val );
begin
loop
exit when t_pos > t_len;
t_val := rawtohex( dbms_lob.substr( p_val, 16383, t_pos ) );
for i in 1 .. length( t_val ) / 2
loop
begin
s1 := s1 + to_number( substr( t_val, i * 2 - 1, 2 ), 'XX' );
exception
when others then
s1 := mod( s1, 65521 ) + to_number( substr( t_val, i * 2 - 1, 2 ), 'XX' );
end;
begin
s2 := s2 + s1;
exception
when others then
s2 := mod( s2, 65521 ) + s1;
end;
end loop;
t_pos := t_pos + 16383;
end loop;
s1 := mod( s1, 65521 );
s2 := mod( s2, 65521 );
return to_char( s2, 'fm0XXX' ) || to_char( s1, 'fm0XXX' );
end;
begin
t_tmp := utl_compress.lz_compress( p_val );
return utl_raw.concat( '789C'
, utl_raw.substr( t_tmp, 11, utl_raw.length( t_tmp ) - 18 )
, adler32( p_val )
);
end;
--
function parse_color( p_parm varchar2, p_which varchar2 )
return raw
is
l_tmp varchar2(100);
begin
l_tmp := coalesce( xjv( p_parm, p_which ) -- hex rgb #00FF00
, xjv( p_parm, p_which || '_color' )
);
l_tmp := lower( ltrim( l_tmp, '#') );
l_tmp := replace( l_tmp, 'black' , '000000' );
l_tmp := replace( l_tmp, 'red' , 'ff0000' );
l_tmp := replace( l_tmp, 'green' , '00ff00' );
l_tmp := replace( l_tmp, 'blue' , '0000ff' );
l_tmp := replace( l_tmp, 'aqua' , '00ffff' );
l_tmp := replace( l_tmp, 'cyan' , '00ffff' );
l_tmp := replace( l_tmp, 'fuchsia', 'ff00ff' );
l_tmp := replace( l_tmp, 'magenta', 'ff00ff' );
l_tmp := replace( l_tmp, 'yellow' , 'ffff00' );
l_tmp := replace( l_tmp, 'white' , 'ffffff' );
return to_char( to_number( l_tmp, 'XXXXXX' ), 'fm0XXXXX' );
exception when others then return null;
end;
--
begin
t_ihdr := utl_raw.concat( '49484452' -- IHDR
, to_char( p_width, 'fm0XXXXXXX' )
, to_char( p_height, 'fm0XXXXXXX' )
, '0803000000' -- Bit depth 8
-- Colour type 3 palet
-- Compression method 0
-- Filter method 0
-- Interlace method 0
);
t_plte := utl_raw.concat( '504C5445' -- PLTE
, nvl( parse_color( p_parm, 'dark' ) , '000000' )
, nvl( parse_color( p_parm, 'light' ), 'FFFFFF' )
);
if check_pos( p_parm, 'transparant' )
then -- make background transparent
t_trns := utl_raw.concat( '74524E53' -- tRNS
, 'FF' -- index value 0 is not transparent
, '00' -- index value 1 is fully transparent
);
t_trns := utl_raw.concat( '00000002' -- length tRNS
, t_trns
, crc32( t_trns )
);
end if;
t_idat := utl_raw.concat( '49444154' -- IDAT
, method0_compress( p_dat )
);
return utl_raw.concat( '89504E470D0A1A0A' -- signature
, '0000000D' -- length IHDR
, t_ihdr
, crc32( t_ihdr )
, '00000006' -- length PLTE
, t_plte
, crc32( t_plte )
, t_trns
, to_char( utl_raw.length( t_idat ) - 4, 'fm0XXXXXXX' )
, t_idat
, crc32( t_idat )
, '0000000049454E44AE426082' -- IEND
);
end generate_png;
--
function bitxor( x number, y number )
return number
is
begin
return x + y - 2 * bitand( x, y );
end;
--
procedure append_bits( p_bits in out tp_bits, p_val number, p_cnt number )
is
begin
for j in reverse 0 .. p_cnt - 1
loop
p_bits( p_bits.count ) := sign( bitand( p_val, power( 2, j ) ) );
end loop;
end;
--
function bitstoword( p_bits tp_bits, p_sz pls_integer )
return tp_bits
is
l_val pls_integer;
l_rv tp_bits;
l_first pls_integer := p_bits.first;
begin
for i in l_first .. p_bits.count / p_sz - l_first - 1
loop
l_val := 0;
for j in 0 .. p_sz - 1
loop
l_val := l_val * 2 + p_bits( l_first + ( i - l_first ) * p_sz + j );
end loop;
l_rv( i - l_first ) := l_val;
end loop;
return l_rv;
end;
--
function reed_solomon
( p_data tp_bits
, p_primitive pls_integer := 285
, p_size pls_integer := 256
, p_degree pls_integer := 16
, p_b pls_integer := 1
)
return tp_bits
is
type tp_ecc is table of pls_integer index by pls_integer;
t_exp tp_ecc;
t_log tp_ecc;
t_g tp_ecc;
t_ecc tp_ecc;
t_x pls_integer;
t_rv tp_bits;
--
begin
t_x := 1;
for i in 0 .. p_size - 1
loop
t_exp( i ) := t_x;
t_x := t_x * 2;
if t_x >= p_size
then
t_x := bitand( p_size - 1, bitxor( p_primitive, t_x ) );
end if;
end loop;
for i in 0 .. p_size - 2
loop
t_log( t_exp( i ) ) := i;
end loop;
--
t_g(0) := 1;
for i in 1 .. p_degree
loop
t_x := i - 1 + p_b;
t_g(i) := t_exp( mod( t_log( t_g( i-1 ) ) + t_x, p_size - 1 ) );
for j in reverse 1 .. i - 1
loop
t_g(j) := bitxor( t_exp( mod( t_log(t_g(j-1)) + t_x, p_size - 1 ) )
, t_g(j)
);
end loop;
-- t_log( 1 ) is altijd 0
-- t_g(0) := bitxor( 0, t_exp( t_log( t_g( 0 ) ) + t_log( 1 ) ) );
end loop;
--
t_x := p_data.first;
for i in t_x .. p_data.last
loop
t_ecc( i - t_x ) := p_data( i );
end loop;
for i in t_ecc.count .. t_ecc.count + p_degree - 1
loop
t_ecc( i ) := 0;
end loop;
--
while t_ecc.count >= t_g.count
loop
t_x := t_ecc( t_ecc.first );
if t_x > 0 then
for i in 0 .. t_g.count - 1
loop
t_ecc( t_ecc.first + i ) := bitxor( t_ecc( t_ecc.first + i )
, t_exp( mod( t_log( t_g( i ) ) + t_log( t_x ), p_size - 1 ) )
);
end loop;
end if;
t_ecc.delete( t_ecc.first );
end loop;
--
t_x := t_ecc.first;
for i in t_ecc.first .. t_ecc.last
loop
t_rv( i - t_x ) := t_ecc( i );
end loop;
return t_rv;
end;
--
function upc_checksum( p_val in varchar2 )
return varchar2
is
l_tmp pls_integer := 0;
begin
for i in 1 .. length( p_val )
loop
l_tmp := l_tmp + to_number( substr( p_val, - i, 1 ) )
* ( 1 + 2 * mod( i, 2 ) );
end loop;
return to_char( ceil( l_tmp / 10 ) * 10 - l_tmp, 'fm0' );
end;
--
function check_ean_val_add_checknr( p_val varchar2, p_len pls_integer )
return varchar2
is
l_val varchar2(100);
begin
if ltrim( p_val ) is null
or ltrim( p_val, ' 0123456789' ) is not null
or length( trim( p_val ) ) > p_len
or trim( p_val ) <> replace( p_val, ' ' )
then
return null;
end if;
l_val := trim( p_val );
if length( l_val ) = p_len
then
return l_val;
end if;
l_val := lpad( l_val, p_len - 1, '0' );
return l_val || upc_checksum( l_val );
end;
--
procedure add_quiet( p_matrix in out nocopy tp_matrix, p_parm varchar2, p_quiet pls_integer )
is
l_height pls_integer := p_matrix.count;
l_width pls_integer := p_matrix( p_matrix.first ).count;
l_quiet pls_integer;
begin
l_quiet := coalesce( check_int( p_parm, 'quiet' ), p_quiet, 0 );
if l_quiet = 0
then
return;
end if;
for i in reverse 0 .. l_height - 1 loop
for j in reverse 0 .. l_width - 1 loop
p_matrix( i + l_quiet )( j + l_quiet ) := p_matrix(i)(j);
end loop;
for j in 0 .. l_quiet - 1 loop
p_matrix( i + l_quiet )(j) := 0;
p_matrix( i + l_quiet )( j + l_width + l_quiet ) := 0;
end loop;
end loop;
for j in 0 .. l_width + 2 * l_quiet - 1 loop
p_matrix(0)(j) := 0;
end loop;
for i in 0 .. l_quiet - 1 loop
p_matrix(i) := p_matrix(0);
p_matrix( i + l_quiet + l_height ) := p_matrix(0);
end loop;
end add_quiet;
--
procedure gen_qrcode_matrix( p_val varchar2 character set any_cs
, p_parm varchar2
, p_matrix out tp_matrix
)
is
l_version pls_integer;
l_eclevel pls_integer;
l_stream tp_bits;
l_tmp raw(32767);
l_sz pls_integer;
l_len pls_integer;
type tp_config is table of pls_integer;
type tp_ecc_config is table of tp_config;
type tp_qr_config is table of tp_ecc_config;
l_qr_config tp_qr_config;
--
function get_formatinfo( p_eclevel in pls_integer, p_mask pls_integer )
return pls_integer
is
type tp_format is table of tp_config;
l_format tp_format;
begin
l_format := tp_format( tp_config( 30660, 29427, 32170, 30877, 26159, 25368, 27713, 26998 )
, tp_config( 21522, 20773, 24188, 23371, 17913, 16590, 20375, 19104 )
, tp_config( 13663, 12392, 16177, 14854, 9396, 8579, 11994, 11245 )
, tp_config( 5769, 5054, 7399, 6608, 1890, 597, 3340, 2107 )
);
return l_format( p_eclevel )( p_mask + 1 );
end;
--
procedure add_patterns( p_version pls_integer
, p_matrix in out nocopy tp_matrix
)
is
l_width pls_integer := 4 * p_version + 17;
type tp_inf is table of pls_integer;
type tp_pos is table of pls_integer;
type tp_align is table of tp_pos;
l_align tp_align;
l_info tp_inf;
l_version_info pls_integer;
l_cnt pls_integer;
l_bit pls_integer;
--
procedure add_finder( p_x pls_integer, p_y pls_integer, p_w pls_integer )
is
l_sx pls_integer := case p_w when 2 then l_width - 8 else 7 end;
l_sy pls_integer := case p_w when 3 then l_width - 8 else 7 end;
l_dx pls_integer := case p_w when 2 then 1 else -1 end;
l_dy pls_integer := case p_w when 3 then 1 else -1 end;
begin
for i in -3 .. 3 loop
for j in -3 .. 3 loop
p_matrix( p_x + i )( p_y + j ) := 1;
end loop;
end loop;
for i in -2 .. 2 loop
p_matrix( p_x + i )( p_y - 2 ) := 0;
p_matrix( p_x + i )( p_y + 2 ) := 0;
p_matrix( p_x - 2 )( p_y + i ) := 0;
p_matrix( p_x + 2 )( p_y - i ) := 0;
end loop;
for i in 0 .. 7 loop
p_matrix( p_x + ( i - 4 ) * l_dx )( p_y - 4 * l_dy ) := 0;
if p_w != 3
then -- reserved for format information
p_matrix( p_x + ( i - 4 ) * l_dx )( p_y + 5 ) := 1;
end if;
p_matrix( p_x - 4 * l_dx )( p_y + ( i - 4 ) * l_dy ) := 0;
if p_w != 2
then -- reserved for format information
p_matrix( p_x + 5 )( p_y + ( i - 4 ) * l_dy ) := 1;
end if;
end loop;
end;
--
procedure add_aligment( p_x pls_integer, p_y pls_integer )
is
begin
for i in -2 .. 2 loop
for j in -2 .. 2 loop
p_matrix( p_x + i )( p_y + j ) := 1;
end loop;
end loop;
for i in -1 .. 1 loop
p_matrix( p_x + i )( p_y - 1 ) := 0;
p_matrix( p_x + i )( p_y + 1 ) := 0;
end loop;
p_matrix( p_x + 1 )( p_y ) := 0;
p_matrix( p_x - 1 )( p_y ) := 0;
end;
begin
for r in 0 .. l_width - 1
loop
for c in 0 .. l_width - 1
loop
p_matrix( r )( c ) := 3; -- init everything to dark
end loop;
end loop;
--
add_finder( 3, 3, 1 );
add_finder( l_width - 4, 3, 2 );
add_finder( 3, l_width - 4, 3 );
p_matrix( 8 )( 8 ) := 1; -- reserved for format information
--
for i in 8 .. l_width - 9 loop
p_matrix( i )( 6 ) := 1 - mod( i, 2 ); -- timing
p_matrix( 6 )( i ) := 1 - mod( i, 2 ); -- timing
end loop;
--
if p_version > 1
then
add_aligment( l_width - 7, l_width - 7 );
if p_version > 6
then
l_align := tp_align( tp_pos( 6, 22, 38 ) -- 7
, tp_pos( 6, 24, 42 ) -- 8
, tp_pos( 6, 26, 46 ) -- 9
, tp_pos( 6, 28, 50 ) -- 10
, tp_pos( 6, 30, 54 ) -- 11
, tp_pos( 6, 32, 58 ) -- 12
, tp_pos( 6, 34, 62 ) -- 13
, tp_pos( 6, 26, 46, 66 ) -- 14
, tp_pos( 6, 26, 48, 70 ) -- 15
, tp_pos( 6, 26, 50, 74 ) -- 16
, tp_pos( 6, 30, 54, 78 ) -- 17
, tp_pos( 6, 30, 56, 82 ) -- 18
, tp_pos( 6, 30, 58, 86 ) -- 19
, tp_pos( 6, 34, 62, 90 ) -- 20
, tp_pos( 6, 28, 50, 72, 94 ) -- 21
, tp_pos( 6, 26, 50, 74, 98 ) -- 22
, tp_pos( 6, 30, 54, 78, 102 ) -- 23
, tp_pos( 6, 28, 54, 80, 106 ) -- 24
, tp_pos( 6, 32, 58, 84, 110 ) -- 25
, tp_pos( 6, 30, 58, 86, 114 ) -- 26
, tp_pos( 6, 34, 62, 90, 118 ) -- 27
, tp_pos( 6, 26, 50, 74, 98, 122 ) -- 28
, tp_pos( 6, 30, 54, 78, 102, 126 ) -- 29
, tp_pos( 6, 26, 52, 78, 104, 130 ) -- 30
, tp_pos( 6, 30, 56, 82, 108, 134 ) -- 31
, tp_pos( 6, 34, 60, 86, 112, 138 ) -- 32
, tp_pos( 6, 30, 58, 86, 114, 142 ) -- 33
, tp_pos( 6, 34, 62, 90, 118, 146 ) -- 34
, tp_pos( 6, 30, 54, 78, 102, 126, 150 ) -- 35
, tp_pos( 6, 24, 50, 76, 102, 128, 154 ) -- 36
, tp_pos( 6, 28, 54, 80, 106, 132, 158 ) -- 37
, tp_pos( 6, 32, 58, 84, 110, 136, 162 ) -- 38
, tp_pos( 6, 26, 54, 82, 110, 138, 166 ) -- 39
, tp_pos( 6, 30, 58, 86, 114, 142, 170 ) -- 40
);
l_cnt := l_align( l_version - 6 ).count;
for i in 1 .. l_cnt loop
for j in 1 .. l_cnt loop
if i between 2 and l_cnt - 1 or j between 2 and l_cnt - 1
then
add_aligment( l_align( l_version - 6 )( i )
, l_align( l_version - 6 )( j )
);
end if;
end loop;
end loop;
--
l_info := tp_inf
( 31892, 34236, 39577, 42195, 48118, 51042, 55367, 58893, 63784
, 68472, 70749, 76311, 79154, 84390, 87683, 92361, 96236, 102084
, 102881, 110507, 110734, 117786, 119615, 126325, 127568, 133589
, 136944, 141498, 145311, 150283, 152622, 158308, 161089, 167017
);
l_version_info := l_info( l_version - 6 );
for i in 0 .. 5
loop
for j in 0 .. 2
loop
l_bit := sign( bitand( l_version_info, power( 2, i * 3 + j ) ) );
p_matrix( l_width - 11 + j )( i ) := l_bit; -- lower left
p_matrix( i )( l_width - 11 + j ) := l_bit; -- upper right
end loop;
end loop;
end if;
end if;
end add_patterns;
--
procedure add_stream( p_width pls_integer
, p_stream tp_bits
, p_matrix in out nocopy tp_matrix
)
is
l_x pls_integer;
l_y pls_integer;
l_direction pls_integer := -1;
procedure next_pos
is
begin
if l_x is null
then
l_x := p_width - 1;
l_y := p_width - 1;
else
if ( l_x > 5 and mod( l_x, 2 ) = 0
or l_x < 6 and mod( l_x, 2 ) = 1
)
then
l_x := l_x - 1;
else
l_x := l_x + 1;
l_y := l_y + l_direction;
end if;
if l_y < 0
then
l_x := l_x - case when l_x = 8 then 3 else 2 end; -- skip vertical timing column
l_y := 0;
l_direction := 1;
elsif l_y >= p_width