summaryrefslogtreecommitdiff
path: root/support/dktools/dk3enc.c
blob: 384db91bd55993caf805b82610ae1516c32a4a04 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
/*
	WARNING: This file was generated by dkct.
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dk3enc.ctr
*/

/*
Copyright (C) 2011-2017, Dirk Krause

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above opyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used
  to endorse or promote products derived from this software without specific
  prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**	@file dk3enc.c The dk3enc module.
*/


#line 312 "dk3enc.ctr"

#include "dk3all.h"





#line 317 "dk3enc.ctr"



/**	Binary to text encoding names.
	Order here must match order of DK3_DATA_ENCODING_xxx in dk3const.h.
*/
static dkChar const * const dk3enc_data_encoding_names[] = {
/*  0 */ dkT("HEX"),
/*  1 */ dkT("ASCII-85"),
/*  2 */ dkT("R-ASCII-85"),
/*  3 */ dkT("H"),
/*  4 */ dkT("A"),
/*  5 */ dkT("R"),
/*  6 */ dkT("A85"),
/*  7 */ dkT("RA85"),
NULL
};



/**	Encoding names for file contents encodings.
*/
static dkChar const * const dk3enc_text_encoding_names[] = {
/*  0 */ dkT("PLAIN"),
/*  1 */ dkT("UTF-8"),
/*  2 */ dkT("UTF-16"),
/*  3 */ dkT("UTF-16.MSB"),
/*  4 */ dkT("UTF-16.LSB"),
/*  5 */ dkT("UC32"),
/*  6 */ dkT("UC32.MSB"),
/*  7 */ dkT("UC32.LSB"),
/*  8 */ dkT("ASCII"),
/*  9 */ dkT("ISO-LATIN-1"),
/* 10 */ dkT("UTF8"),
/* 11 */ dkT("UTF16"),
/* 12 */ dkT("UTF-16-LE"),
/* 13 */ dkT("UTF-16LE"),
/* 14 */ dkT("UTF16LE"),
/* 15 */ dkT("UTF-16-LSB"),
/* 16 */ dkT("UTF-16LSB"),
/* 17 */ dkT("UTF16LSB"),
/* 18 */ dkT("UTF-16-BE"),
/* 19 */ dkT("UTF-16BE"),
/* 20 */ dkT("UTF16BE"),
/* 21 */ dkT("UTF-16-MSB"),
/* 22 */ dkT("UTF-16MSB"),
/* 23 */ dkT("UTF16MSB"),
/* 24 */ dkT("C32"),
/* 25 */ dkT("C32-LE"),
/* 26 */ dkT("C32LE"),
/* 27 */ dkT("C32-LSB"),
/* 28 */ dkT("C32LSB"),
/* 29 */ dkT("C32-BE"),
/* 30 */ dkT("C32BE"),
/* 31 */ dkT("C32-MSB"),
/* 32 */ dkT("C32MSB"),
NULL
};



/**	Encoding names for UTF-8 encoding.
*/
static char const * const	dk3enc_utf8_names[] = {
/* 0 */
"utf-8",

/* 1 */
"utf8",

NULL


#line 385 "dk3enc.ctr"
};


/**	Digits used to show hexadecimal characters.
*/
static char const dk3enc_hex_digits[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '\0'
};


/**	Bit mask to obtain last byte of a double word.
*/
static unsigned long const dk3enc_last_byte = 0x000000FFUL;



/**	Factors for ASCII85 and reverse ASCII85 encoding.
*/
static unsigned long const dk3enc_f2[] = {
  1UL,
  85UL,
  (85UL * 85UL),
  (85UL * 85UL * 85UL),
  (85UL * 85UL * 85UL * 85UL)
};



/**	Swap byte order for unsigned long.
	@param	ul	Original value.
	@return	Value in swapped byte order.
*/
static
unsigned long
dk3enc_swap_ul(unsigned long ul)
{
  unsigned long back = 0UL;
  back =  ((ul >> 24) & 0x000000FFUL);
  back |= ((ul >>  8) & 0x0000FF00UL);
  back |= ((ul <<  8) & 0x00FF0000UL);
  back |= ((ul << 24) & 0xFF000000UL);
  return back;
}



/**	Swap byte order for unsigned short.
	@param	us	Original value.
	@return	Value in swapped byte order.
*/
static
unsigned short
dk3enc_swap_us(unsigned short us)
{
  unsigned short back = 0U;
  back =  ((us >> 8) & 0x00FFU);
  back |= ((us << 8) & 0xFF00U);
  return back;
}


unsigned long
dk3enc_ntohl(unsigned long x)
{
  unsigned long back;
#if DK3_WORDS_BIGENDIAN
  back = x;
#else
  back = dk3enc_swap_ul(x);
#endif
  return back;
}


unsigned long
dk3enc_htonl(unsigned long x)
{
  unsigned long back;
#if DK3_WORDS_BIGENDIAN
  back = x;
#else
  back = dk3enc_swap_ul(x);
#endif
  return back;
}



unsigned short
dk3enc_ntohs(unsigned short x)
{
  unsigned short back;
#if DK3_WORDS_BIGENDIAN
  back = x;
#else
  back = dk3enc_swap_us(x);
#endif
  return back;
}



unsigned short
dk3enc_htons(unsigned short x)
{
  unsigned short back;
#if DK3_WORDS_BIGENDIAN
  back = x;
#else
  back = dk3enc_swap_us(x);
#endif
  return back;
}



/**	Convert 32-bit character to UTF-8 following RFC2279.
	@param	c	32-bit character to convert.
	@param	u8p	Destination buffer.
	@param	u8l	Length of @a u8p (number of bytes).
	@return	Number of bytes written to @a u8p.
*/
static
size_t
dk3enc_uc2utf8_rfc2279(dk3_c32_t c, unsigned char *u8p, size_t u8l)
{
  size_t back = 0;
  dk3_c32_t x = 0UL;	/* Variable for temporary results. */
  

#line 514 "dk3enc.ctr"
  if(u8p) {
  if(u8l > 0) {
    if(c > (dk3_c32_t)0x7FUL) {
      if(c > (dk3_c32_t)0x000007FFUL) {
	if(c > (dk3_c32_t)0x0000FFFFUL) {
	  if(c > (dk3_c32_t)0x001FFFFFUL) {
	    if(c > (dk3_c32_t)0x03FFFFFFUL) {
	      if(c < (dk3_c32_t)0x80000000UL) {		/* 6 Byte */
		if(u8l >= 6) {
		  back = 6;
		  x = c >> 30;
		  x &= 0x00000001UL;
		  x |= 0x000000FCUL;
		  u8p[0] = (unsigned char)x;
		  x = c >> 24; x &= 0x0000003FUL; x |= 0x00000080UL;
		  u8p[1] = (unsigned char)x;
		  x = c >> 18; x &= 0x0000003FUL; x |= 0x00000080UL;
		  u8p[2] = (unsigned char)x;
		  x = c >> 12; x &= 0x0000003FUL; x |= 0x00000080UL;
		  u8p[3] = (unsigned char)x;
		  x = c >>  6; x &= 0x0000003FUL; x |= 0x00000080UL;
		  u8p[4] = (unsigned char)x;
		  x = c & 0x0000003FUL; x |= 0x00000080UL;
		  u8p[5] = (unsigned char)x;
		}
	      }
	    } else {				/* 5 Byte */
	      if(u8l >= 5) {
		back = 5;
		x = c >> 24;
		x &= 0x00000003UL;
		x |= 0x000000F8UL;
		u8p[0] = (unsigned char)x;
		x = c >> 18; x &= 0x0000003FUL; x |= 0x00000080UL;
		u8p[1] = (unsigned char)x;
		x = c >> 12; x &= 0x0000003FUL; x |= 0x00000080UL;
		u8p[2] = (unsigned char)x;
		x = c >>  6; x &= 0x0000003FUL; x |= 0x00000080UL;
		u8p[3] = (unsigned char)x;
		x = c & 0x0000003FUL; x |= 0x00000080UL;
		u8p[4] = (unsigned char)x;
	      }
	    }
	  } else {				/* 4 Byte */
	    if(u8l >= 4) {
	      back = 4;
	      x = c >> 18;
	      x &= 0x00000007UL;
	      x |= 0x000000F0UL;
	      u8p[0] = (unsigned char)x;
	      x = c >> 12;
	      x &= 0x0000003FUL;
	      x |= 0x00000080UL;
	      u8p[1] = (unsigned char)x;
	      x = c >>  6;
	      x &= 0x0000003FUL;
	      x |= 0x00000080UL;
	      u8p[2] = (unsigned char)x;
	      x = c & 0x0000003FUL;
	      x |= 0x00000080UL;
	      u8p[3] = (unsigned char)x;
	    }
	  }
	} else {				/* 3 Byte */
	  if(u8l >= 3) {
	    back = 3;
	    x = c >> 12;
	    x &= 0x0000000FUL;
	    x |= 0x000000E0UL;
	    u8p[0] = (unsigned char)x;
	    x = c >>  6;
	    x &= 0x0000003FUL;
	    x |= 0x00000080UL;
	    u8p[1] = (unsigned char)x;
	    x = c & 0x0000003FUL;
	    x |= 0x00000080UL;
	    u8p[2] = (unsigned char)x;
	  }
	}
      } else {					/* 2 Byte */
	if(u8l >= 2) {
	  back = 2;
	  x = c >> 6;
	  x &= 0x0000001FUL;
	  x |= 0x000000C0UL;
	  u8p[0] = (unsigned char)x;
	  x = c & 0x0000003FUL;
	  x |= 0x00000080UL;
	  u8p[1] = (unsigned char)x;
	}
      }
    } else {					/* 1 Byte */
      if(u8l >= 1) {
	back = 1;
	u8p[0] = (unsigned char)(c & 0x0000007FUL);
      }
    }
  }
  } 

#line 613 "dk3enc.ctr"
  return back;
}



/**	Get one 32-bit character from UTF-8 encoded string or buffer.
	@param	ucp	Pointer to destination variable.
	@param	u8p	Buffer containing UTF-8 encoded data.
	@param	u8l	Length of @a u8p (number of bytes).
	@param	u8u	Pointer to variable for used bytes from @a u8p.
	@return	1 on success, 0 on error.
*/
static
int
dk3enc_utf82uc_rfc2279(
  dk3_c32_t *ucp, unsigned char const *u8p,size_t u8l,size_t *u8u
)
{
  int		back = 0;
  unsigned char	u1 = 0x00;		/* Character to process. */
  dk3_c32_t	res = 0UL;		/* Result. */
  dk3_c32_t	x1 = 0UL;		/* Result component from 1st byte. */
  dk3_c32_t	x2 = 0UL;		/* Result component from 2nd byte. */
  dk3_c32_t	x3 = 0UL;		/* Result component from 3rd byte. */
  dk3_c32_t	x4 = 0UL;		/* Result component from 4th byte. */
  dk3_c32_t	x5 = 0UL;		/* Result component from 5th byte. */
  dk3_c32_t	x6 = 0UL;		/* Result component from 6th byte. */
  size_t	needed_length = 0;	/* Number of btes needed. */
  

#line 642 "dk3enc.ctr"
  needed_length = 0;
  if(ucp) {
  if(u8p) {
  if(u8l) {
  if(u8u) {
  if((u8l) > 0) {
    u1 = *u8p; needed_length = 1;
    if(u1 > 0x7F) {					/* > 1 Byte */
      if((u1 & 0xE0) == 0xC0) {				/* 2 Byte */
	needed_length = 2;
	if(u8l >= needed_length) {
	if((u8p[1] & 0xC0) == 0x80) {
	  x1 = u1;
	  x1 = x1 << 6;
	  x1 = x1 & 0x000007C0UL;
	  x2 = u8p[1];
	  x2 = x2 & 0x0000003FUL;
	  res = (x1 | x2); back = 1;
	}
	}
      } else {
	if((u1 & 0xF0) == 0xE0) {			/* 3 Byte */
	  needed_length = 3;
	  if(u8l >= needed_length) {
	    if((u8p[1] & 0xC0) == 0x80) {
	    if((u8p[2] & 0xC0) == 0x80) {
	      x1 = u1;
	      x1 = x1 << 12;
	      x1 = x1 & 0x0000F000UL;
	      x2 = u8p[1];
	      x2 = x2 << 6;
	      x2 = x2 & 0x00000FC0UL;
	      x3 = u8p[2];
	      x3 = x3 & 0x0000003FUL;
	      res = (x1 | x2 | x3); back = 1;
	    }
	    }
	  }
	} else {
	  if((u1 & 0xF8) == 0xF0) {			/* 4 Byte */
	    needed_length = 4;
	    if(u8l >= needed_length) {
	      if((u8p[1] & 0xC0) == 0x80) {
	      if((u8p[2] & 0xC0) == 0x80) {
	      if((u8p[3] & 0xC0) == 0x80) {
		x1 = u1;
		x2 = u8p[1];
		x3 = u8p[2];
		x4 = u8p[3];
		x1 = x1 << 18;
		x1 = x1 & 0x001C0000UL;
		x2 = x2 << 12;
		x2 = x2 & 0x0003F000UL;
		x3 = x3 << 6;
		x3 = x3 & 0x00000FC0UL;
		x4 = x4 & 0x0000003FUL;
		res = (x1 | x2 | x3 | x4); back = 1;
	      }
	      }
	      }
	    }
	  } else {
	    if((u1 & 0xFC) == 0xF8) {			/* 5 Byte */
	      needed_length = 5;
	      if(u8l >= needed_length) {
		if((u8p[1] & 0xC0) == 0x80) {
		if((u8p[2] & 0xC0) == 0x80) {
		if((u8p[3] & 0xC0) == 0x80) {
		if((u8p[4] & 0xC0) == 0x80) {
		  x1 = u1;
		  x2 = u8p[1];
		  x3 = u8p[2];
		  x4 = u8p[3];
		  x5 = u8p[4];
		  x1 = x1 << 24;
		  x1 = x1 & 0x03000000UL;
		  x2 = x2 << 18;
		  x2 = x2 & 0x00FC0000UL;
		  x3 = x3 << 12;
		  x3 = x3 & 0x0003F000UL;
		  x4 = x4 <<  6;
		  x4 = x4 & 0x00000FC0UL;
		  x5 = x5 & 0x0000003FUL;
		  res = (x1 | x2 | x3 | x4 | x5); back = 1;
		}
		}
		}
		}
	      }
	    } else {
	      if((u1 & 0xFE) == 0xFC) {			/* 6 Byte */
		needed_length = 6;
		if(u8l >= needed_length) {
		  if((u8p[1] & 0xC0) == 0x80) {
		  if((u8p[2] & 0xC0) == 0x80) {
		  if((u8p[3] & 0xC0) == 0x80) {
		  if((u8p[4] & 0xC0) == 0x80) {
		  if((u8p[5] & 0xC0) == 0x80) {
		    x1 = 0x40000000UL;
		    x2 = u8p[1]; x3 = u8p[2]; x4 = u8p[3];
		    x5 = u8p[4]; x6 = u8p[5];
		    x2 = x2 << 24;
		    x3 = x3 << 18;
		    x4 = x4 << 12;
		    x5 = x5 <<  6;
		    x2 = x2 & 0x3F000000UL;
		    x3 = x3 & 0x00FC0000UL;
		    x4 = x4 & 0x0003F000UL;
		    x5 = x5 & 0x00000FC0UL;
		    x6 = x6 & 0x0000003FUL;
		    res = (x1 | x2 | x3 | x4 | x5 | x6);
		    back = 1;
		  }
		  }
		  }
		  }
		  }
		}
	      }
	    }
	  }
	}
      }
    } else {						/* 1 Byte */
      res = u1;
      back = 1;
    }
  }
  }
  }
  }
  }
  if(back) {
    *u8u = needed_length;
    *ucp = res;
  } 

#line 778 "dk3enc.ctr"
  return back;
}



#if DK3_HAVE_RFC_2279

/*
	RFC 2279 allowed UTF-8 encoding for 32-bit characters in the
	range 0 ... 0x7FFFFFFF.
*/

size_t
dk3enc_uc2utf8(dk3_c32_t c, unsigned char *u8p, size_t u8l)
{
  size_t back;
  

#line 795 "dk3enc.ctr"
  back = dk3enc_uc2utf8_rfc2279(c, u8p, u8l);
  

#line 797 "dk3enc.ctr"
  return back;
}

int
dk3enc_utf82uc(dk3_c32_t *ucp, unsigned char const *u8p,size_t u8l,size_t *u8u)
{
  int back;
  back = dk3enc_utf82uc_rfc2279(ucp, u8p, u8l, u8u);
  return back;
}


#else

/*
	RFC 3629 replaces (obsoletes) RFC 2279.
	32-bit characters are now allowed in the range
	0 ... 0x0010FFFF only.
*/

size_t
dk3enc_uc2utf8(dk3_c32_t c, unsigned char *u8p, size_t u8l)
{
  size_t	back = 0;
  

#line 822 "dk3enc.ctr"
  if(c <= (dk3_c32_t)0x0010FFFFUL) {
    back = dk3enc_uc2utf8_rfc2279(c, u8p, u8l);
  } 

#line 825 "dk3enc.ctr"
  return back;
}

int
dk3enc_utf82uc(dk3_c32_t *ucp, unsigned char const *u8p,size_t u8l,size_t *u8u)
{
  int		back	= 0;
  back = dk3enc_utf82uc_rfc2279(ucp, u8p, u8l, u8u);
  if(back) {
    if((*ucp) > (dk3_c32_t)0x0010FFFFUL) {
      back = 0;
    }
  }
  return back;
}

#endif



size_t
dk3enc_uc2utf16(dk3_c32_t c, dk3_c16_t *u16p, size_t u16l)
{
  size_t	back = 0;
  dk3_c32_t	ul = 0UL;	/* Low surrogate. */
  dk3_c32_t	um = 0UL;	/* Hight surrogate. */
  if((u16p) && (u16l)) {
    if(c >= (dk3_c32_t)0x00010000UL) {
      if(c <= (dk3_c32_t)0x0010FFFFUL) {
        if(u16l >= 2) {
	  ul = c - 0x00010000UL;
	  um = ((ul >> 10) & 0x000003FFUL);
	  um |= 0x0000D800UL;
	  u16p[0] = (dk3_c16_t)um;
	  um = (ul & 0x000003FFUL);
	  um |= 0x0000DC00UL;
	  u16p[1] = (dk3_c16_t)um;
	  back = 2;
	}
      }
    } else {
#if VERSION_BEFORE_20141125
      if((c & 0x0000DC00UL) != 0x0000DC00UL) {
        if((c & 0x0000DC00UL) != 0x0000D800UL) {
	  *u16p = (dk3_c16_t)c; back = 1;
	}
      }
#else
      if ((0x0000D800UL > c) || (0x0000DFFF < c)) {
        *u16p = (dk3_c16_t)c; back = 1;
      }
#endif
    }
  }
  return back;
}



int
dk3enc_utf162uc(dk3_c32_t *ucp, dk3_c16_t const *u16p, size_t u16l, size_t *u16u)
{
  int back = 0;
  dk3_c32_t		result = 0UL;	/* Result 32-bit character. */
  dk3_c32_t		ul1 = 0UL;	/* First UC16 used. */
  dk3_c32_t		ul2 = 0UL;	/* Second UC16 used (if any). */

  if((ucp) && (u16p) && (u16l) && (u16u)) {
    *u16u = 0;
    ul1 = (((dk3_c32_t)(u16p[0])) & 0x0000FFFFUL);
    ul2 = 0UL;
    if(u16l > 1) {
      ul2 = (((dk3_c32_t)(u16p[1])) & 0x0000FFFFUL);
    }
    if((ul1 & 0x0000DC00UL) == 0x0000DC00UL) {
      if(u16l > 1) {
        if((ul2 & 0x0000DC00UL) == 0x0000D800UL) {
	  ul2 = ((ul2 << 10) & 0x000FFC00UL);
	  ul1 = (ul1 & 0x000003FFUL);
	  result = ul1 | ul2;
	  result = result + 0x00010000UL;
	  *ucp = result; *u16u = 2; back = 1;
	}
      }
    } else {
      if((ul1 & 0x0000DC00UL) == 0x0000D800UL) {
        if(u16l > 1) {
	  if((ul2 & 0x0000DC00UL) == 0x0000DC00UL) {
	    ul1 = ((ul1 << 10) & 0x000FFC00UL);
	    ul2 = (ul2 & 0x000003FFUL);
	    result = ul1 | ul2;
	    result = result + 0x00010000UL;
	    *ucp = result; *u16u = 2; back = 1;
	  }
	}
      } else {
        ul1 &= 0x0000FFFFUL;
	*ucp = ul1; *u16u = 1; back = 1;
      }
    }
  }
  return back;
}



/**	Check whether a character is a digit.
	@param	c	Character to check.
	@return	1 on success, 0 on error.
*/
static int
dk3enc_c8_is_digit(char c)
{
  int back = 0;
  switch(c) {
    case '0': case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9':
    {
      back = 1;
    }
    break;
  }
  return back;
}



/**	Convert digit to corresponding unsigned long.
	@param	c	Digit.
	@return	Unsigned long for the digit.
*/
static
unsigned long
dk3enc_c8_to_ul(char c)
{
  unsigned long back = 0UL;
  if(c == '1') back = 1UL;
  if(c == '2') back = 2UL;
  if(c == '3') back = 3UL;
  if(c == '4') back = 4UL;
  if(c == '5') back = 5UL;
  if(c == '6') back = 6UL;
  if(c == '7') back = 7UL;
  if(c == '8') back = 8UL;
  if(c == '9') back = 9UL;
  return back;
}



int
dk3enc_ipaddr_to_ul_app(dkChar const *str, unsigned long *ul, dk3_app_t *app)
{
  char		bu[512];
  int		back = 0;
  

#line 981 "dk3enc.ctr"
  if((str) && (ul)) {
    if(dk3str_string_to_c8_simple_app(bu, sizeof(bu), str, app)) { 

#line 983 "dk3enc.ctr"
      back = dk3enc_c8_ipaddr_to_ul_app(bu, ul, app);
    } else {
      /* ERROR: Conversion failed! */
    }
  } 

#line 988 "dk3enc.ctr"
  return back;
}



int
dk3enc_c8_ipaddr_to_ul_app(char const *str, unsigned long *ul, dk3_app_t *app)
{
  int		back = 0;
  int		state = 0;	/* Current state. */
  char const	*ptr = NULL;	/* Used to traverse string. */
  unsigned long	ul1 = 0UL;	/* First part of IP address. */
  unsigned long	ul2 = 0UL;	/* Second part of IP address. */
  unsigned long	ul3 = 0UL;	/* Third part of IP address. */
  unsigned long	ulval = 0UL;	/* Resulting IP address. */
  

#line 1004 "dk3enc.ctr"
  if(str && ul) {
    state = 0; ptr = str; back = 1;
    ul1 = ul2 = ul3 = ulval = 0UL;
    while(back && (*ptr)) {
      if(dk3enc_c8_is_digit(*ptr)) {
	switch(state) {
	  case 0:
	  case 1:
	  case 2:
	  case 4:
	  case 5:
	  case 6:
	  case 8:
	  case 9:
	  case 10:
	  case 12:
	  case 13:
	  case 14: {
	    ulval = 10UL * ulval + dk3enc_c8_to_ul(*ptr); state++;
	  } break;
	  default: {
	    back = 0;
	  } break;
	}
      } else {
        if(*ptr == '.') {
	  switch(state) {
	    case 0:
	    case 1:
	    case 2:
	    case 3: {
	      ul1 = ulval; ulval = 0UL; state = 4;
	    } break;
	    case 4:
	    case 5:
	    case 6:
	    case 7: {
	      ul2 = ulval; ulval = 0UL; state = 8;
	    } break;
	    case 8:
	    case 9:
	    case 10:
	    case 11: {
	      ul3 = ulval; ulval = 0UL; state = 12;
	    } break;
	  }
	} else {
	  back = 0;
	}
      }
      ptr++;
    }
    if((state < 12) || (state > 15)) {
      back = 0;
    }
    if(back) {
      if(ul1 > 255UL) back = 1;
      if(ul2 > 255UL) back = 1;
      if(ul3 > 255UL) back = 1;
      if(ulval > 255UL) back = 1;
    }
    if(back) {
      ul1 = ul1 << 24;
      ul1 &= 0xFF000000UL;
      ul2 = ul2 << 16;
      ul2 &= 0x00FF0000UL;
      ul3 = ul3 <<  8;
      ul3 &= 0x0000FF00UL;
      ulval &= 0x000000FFUL;
      ulval = ulval | ul1 | ul2 | ul3;
      *ul = ulval;
    } else {
      if(app) {
        /* Not an IP address! */
      }
    }
  }
  

#line 1082 "dk3enc.ctr"
  return back;
}



size_t
dk3enc_size_bin_to_a85(size_t s)
{
#if VERSION_BEFORE_20140809
  size_t	back = 0;
  int		ec = 0;		/* Error code. */
  unsigned long ul1 = 0UL;	/* Original size converted to unsigned long. */
  unsigned long	ul2 = 0UL;	/* Remainder of division by 4. */
  unsigned long	ul3 = 0UL;	/* Result division by 4. */
  

#line 1097 "dk3enc.ctr"
  ul1 = (unsigned long)s;
  ul2 = ul1 % 4UL;
  ul3 = ul1 / 4UL;
  if(ul2) ul2++;			/* for last incomplete block */
  ul1 = dk3ma_ul_add_ok(
    dk3ma_ul_mul_ok(ul3, 5UL, &ec),
    ul2,
    &ec
  );					/* add 25 percent */
  ul1++;				/* final 0x00 byte for string */
  back = (size_t)ul1;
  if(ec) back = 0;			/* error checking */
  if((unsigned long)back != ul1) back = 0;
  

#line 1111 "dk3enc.ctr"
  return back;
#else
  size_t	back	= 0;
  size_t	rem	= 0;	/* Division remainder */
  size_t	res	= 0;	/* Division result */
  int		ec	= 0;	/* Error code */
  

#line 1118 "dk3enc.ctr"
  rem = s % 4;
  res = s / 4;
  if (rem) { rem++; }
  /* back = 5 * res + rem + 1 */
  rem++;
  back = dk3mem_add_size_t(rem,   dk3mem_mul_size_t(5, res, &ec),   &ec);
  if (ec) { back = 0; }
  

#line 1126 "dk3enc.ctr"
  return back;
#endif
}



/**	Convert binary data to ASCII-85 encoded string.
	@param	dp	Destination pointer.
	@param	ds	Destination size.
	@param	sp	Source pointer.
	@param	ss	Source size.
*/
static
void
dk3enc_do_bin_to_ra85(char *dp, size_t ds, char const *sp, size_t ss)
{
  register char const		*mysp = NULL;	/* Source pointer. */
  register unsigned char	*mydp = NULL;	/* Destination pointer. */
  register unsigned long	v = 0UL;	/* Output value. */
  register size_t		i = 0;		/* Current byte index. */
  register short		vused = 0;	/* Flag: v is used. */
  

#line 1148 "dk3enc.ctr"
  mydp = (unsigned char *)dp; mysp = sp; v = 0UL; vused = 0;
  for(i = 0; i < ss; i++) {	
    switch(vused++) {
      case 3: {
        v |=
	((((unsigned long)((unsigned char)(*(mysp++)))) << 24) & 0xFF000000UL);
      } break;
      case 2: {
        v |=
	((((unsigned long)((unsigned char)(*(mysp++)))) << 16) & 0x00FF0000UL);
      } break;
      case 1: {
        v |=
	((((unsigned long)((unsigned char)(*(mysp++)))) <<  8) & 0x0000FF00UL);
      } break;
      default: {
        v |=
	((((unsigned long)((unsigned char)(*(mysp++))))      ) & dk3enc_last_byte);
      } break;
    }
    if(vused >= 4) {
      *(mydp++) = (unsigned char)((v % 85UL) + 33UL);
      v = v / 85UL;
      *(mydp++) = (unsigned char)((v % 85UL) + 33UL);
      v = v / 85UL;
      *(mydp++) = (unsigned char)((v % 85UL) + 33UL);
      v = v / 85UL;
      *(mydp++) = (unsigned char)((v % 85UL) + 33UL);
      v = v / 85UL;
      *(mydp++) = (unsigned char)((v % 85UL) + 33UL);
      vused = 0; v = 0UL;
    }
  }
  if(vused) {
    vused++;
    while(vused--) {
      *(mydp++) = (unsigned char)((v % 85UL) + 33UL);
      v = v / 85UL;
    }
  }
  *mydp = '\0';	

#line 1189 "dk3enc.ctr"
}



int
dk3enc_bin_to_ra85_app(
  char *dp, size_t ds, char const *sp, size_t ss, dk3_app_t *app
)
{
  int		back = 0;
  size_t	needed_size = 0;	/* Destination buffer minimum size. */
  

#line 1201 "dk3enc.ctr"
  if((dp) && (sp) && (ds) && (ss)) {
    needed_size = dk3enc_size_bin_to_a85(ss);
    if(needed_size) {
      if(ds >= needed_size) {
        dk3enc_do_bin_to_ra85(dp, ds, sp, ss);
	back = 1;
      } else {
        if(app) {
	  /* Destination buffer too small! */
	  dk3app_log_i1(app, DK3_LL_ERROR, 38);
	}
      }
    }
  } 

#line 1215 "dk3enc.ctr"
  return back;
}



size_t
dk3enc_size_a85_to_bin(size_t s)
{
  size_t	back = 0;
  unsigned long	ul1 = 0UL;	/* Original size converted to unsigned long. */
  unsigned long	ul2 = 0UL;	/* Remainder of division by 5. */
  unsigned long	ul3 = 0UL;	/* Result of division by 5. */
  

#line 1228 "dk3enc.ctr"
  ul1 = (unsigned long)s;
  ul2 = ul1 % 5UL;
  ul3 = ul1 / 5UL;
  ul1 = 4UL * ul3 + ul2;
  back = (size_t)ul1;
  

#line 1234 "dk3enc.ctr"
  return back;
}



/**	Check whether a character is from the ASCII-85 charset.
	@param	c	Character to check.
	@return	1 on success, 0 on error.
*/
static
int
dk3enc_c8_is_a85(char c)
{
  int back = 1;
  if((int)c < 33) {
    back = 0;
  } else {
    if((int)c > 117) {
      back = 0;
    }
  }
  return back;
}



/**	Convert reverse ASCII-85 encoded data to binary data.
	@param	dp	Destination pointer.
	@param	ds	Destination size.
	@param	sp	Source pointer.
	@param	ss	Source size.
	@param	app	Application structure for diagnostics, may be NULL.
	@return	Number of binary bytes produced.
*/
static
size_t
dk3enc_do_ra85_to_bin(char *dp, size_t ds, char const *sp, size_t ss, dk3_app_t *app)
{
  size_t	back = 0;
  unsigned char	*mydp = NULL;	/* Destination pointer. */
  char const	*mysp = NULL;	/* Source pointer. */
  unsigned long	v = 0UL;	/* Double-word. */
  short		vused = 0;	/* Flag: v is used. */
  size_t	i = 0;		/* Index of current source byte. */
  int reported_illegal_char = 0;	/* Flag: Error already reported. */
  

#line 1280 "dk3enc.ctr"
  mydp = (unsigned char *)dp; mysp = sp; v = 0UL; vused = 0;
  for(i = 0; i < ss; i++) {
    if(dk3enc_c8_is_a85(*mysp)) {
      v += dk3enc_f2[vused++] * ((((unsigned long)((unsigned char)(*mysp))) & dk3enc_last_byte) - 33UL);
      if(vused >= 5) {
        *(mydp++) = (unsigned char)(v         & dk3enc_last_byte);
	*(mydp++) = (unsigned char)((v >>  8) & dk3enc_last_byte);
	*(mydp++) = (unsigned char)((v >> 16) & dk3enc_last_byte);
	*(mydp++) = (unsigned char)((v >> 24) & dk3enc_last_byte);
	back += 4;
        v = 0UL; vused = 0;
      }
    } else {
      if(app) {
        if(!reported_illegal_char) {
	  reported_illegal_char = 1;
	  /* Illegal character(s) in source string! */
	  dk3app_log_i1(app, DK3_LL_ERROR, 67);
	}
      }
    }
    mysp++;
  }
  if(vused) {
    vused--;
    while(vused--) {
      *(mydp++) = (unsigned char)(v         & dk3enc_last_byte);
      back++;
      v = v >> 8;
    }
  } 

#line 1311 "dk3enc.ctr"
  return back;
}



size_t
dk3enc_ra85_to_bin_app(
  char *dp, size_t ds, char const *sp, size_t ss, dk3_app_t *app
)
{
  size_t back = 0;
  size_t needed_size = 0;	/* Minimum output buffer size. */
  

#line 1324 "dk3enc.ctr"
  if((dp) && (sp) && (ds) && (ss)) {
    needed_size = dk3enc_size_a85_to_bin(ss);
    if(needed_size) {
      if(ds >= needed_size) {
        back = dk3enc_do_ra85_to_bin(dp, ds, sp, ss, app);
      } else {
        if(app) {
	  /* Destination buffer too small! */
	  dk3app_log_i1(app, DK3_LL_ERROR, 38);
	}
      }
    }
  } 

#line 1337 "dk3enc.ctr"
  return back;
}



size_t
dk3enc_ra85string_to_bin_app(
  char *dp, size_t ds, char const *sp, dk3_app_t *app
)
{
  size_t back = 0;
  

#line 1349 "dk3enc.ctr"
  if(sp) {
    back = dk3enc_ra85_to_bin_app(dp, ds, sp, dk3str_c8_len(sp), app);
  }
  return back;
}



/**	Convert binary data to ASCII-85 string.
	@param	dp	Destination pointer.
	@param	ds	Destination size.
	@param	sp	Source pointer.
	@param	ss	Source size.
*/
static
void
dk3enc_do_bin_to_a85(char *dp, size_t ds, char const *sp, size_t ss)
{
  register unsigned char *mydp = NULL;	/* Destination pointer. */
  register unsigned char *mysp = NULL;	/* Source pointer. */
  register unsigned long v = 0UL;	/* Double-word. */
  register short vused = 0;		/* Flag: v used. */
  register short addval = 0;		/* Value to add. */
  register size_t i = 0;		/* Index of current source byte. */
  

#line 1374 "dk3enc.ctr"
  mydp = (unsigned char *)dp;
  mysp = (unsigned char *)sp;
  v = 0UL;
  vused = 0;
  for(i = 0; i < ss; i++) {
    switch(vused) {
      case 3: {
        v |= ( ((unsigned long)(*(mysp++)))        & 0x000000FFUL);
      } break;
      case 2: {
        v |= ((((unsigned long)(*(mysp++))) <<  8) & 0x0000FF00UL);
      } break;
      case 1: {
        v |= ((((unsigned long)(*(mysp++))) << 16) & 0x00FF0000UL);
      } break;
      default: {
        v |= ((((unsigned long)(*(mysp++))) << 24) & 0xFF000000UL);
      } break;
    }
    if(++vused >= 4) {
      vused = 5;
      while(vused--) {
        *(mydp++) = (unsigned char)(33UL + v / dk3enc_f2[vused]);
	v = v % dk3enc_f2[vused];
      }
      v = 0UL; vused = 0;
    }
  }
  if(vused) {
    vused++; addval = 5 - vused;
    while(vused--) {
      *(mydp++) = (unsigned char)(33UL + v / dk3enc_f2[vused + addval]);
      v = v % dk3enc_f2[vused + addval];
    }
  }
  *mydp = '\0'; 

#line 1410 "dk3enc.ctr"
}



int
dk3enc_bin_to_a85_app(
  char *dp, size_t ds, char const *sp, size_t ss, dk3_app_t *app
)
{
  int back = 0;
  size_t needed_size = 0;	/* Minimum output buffer size. */
  

#line 1422 "dk3enc.ctr"
  if((dp) && (sp) && (ds) && (ss)) {
    needed_size = dk3enc_size_bin_to_a85(ss);
    if(needed_size) {
      if(ds >= needed_size) {
        dk3enc_do_bin_to_a85(dp, ds, sp, ss);
	back = 1;
      } else {
        if(app) {
	  /* Destination buffer too small! */
	  dk3app_log_i1(app, DK3_LL_ERROR, 38);
	}
      }
    }
  } 

#line 1436 "dk3enc.ctr"
  return back;
}



/**	Convert ASCII-85 encoded data to binary data.
	@param	dp	Destination pointer.
	@param	ds	Destination size.
	@param	sp	Source pointer.
	@param	ss	Source size.
	@param	app	Application structure for diagnostics, may be NULL.
	@return	Number of binary bytes produced.
*/
static
size_t
dk3enc_do_a85_to_bin(char *dp, size_t ds, char const *sp, size_t ss, dk3_app_t *app)
{
  register size_t back = 0;
  register unsigned char *mydp = NULL;		/* Destination pointer. */
  register unsigned char const *mysp = NULL;	/* Source pointer. */
  register unsigned long v = 0UL;		/* Double-word. */
  register short vused = 0;			/* Flag: v used. */
  register size_t i = 0;			/* Current source byte index. */
  unsigned long u1 = 0UL;			/* Temporary value. */
  unsigned long u2 = 0UL;			/* Temporary value. */
  unsigned long u3 = 0UL;			/* Temporary value. */
  int reported_illegal_character = 0;
  

#line 1464 "dk3enc.ctr"
  mydp = (unsigned char *)dp; mysp = (unsigned char *)sp;
  v = 0UL; vused = 0;
  for(i = 0; i < ss; i++) {
    if(*mysp) {
      if(dk3enc_c8_is_a85(*mysp)) {
        v += dk3enc_f2[4 - vused] *
	((((unsigned long)(*mysp)) & 0x000000FFUL) - 33UL);
      } else {
        if(!reported_illegal_character) {
	  reported_illegal_character = 1;
	  if(app) {
	    /* Illegal character in source data! */
	    dk3app_log_i1(app, DK3_LL_ERROR, 67);
	  }
	}
      }
      vused++;
      if(vused >= 5) {
        *(mydp++) = (unsigned char)((v >> 24) & 0x000000FFUL); back++;
	*(mydp++) = (unsigned char)((v >> 16) & 0x000000FFUL); back++;
	*(mydp++) = (unsigned char)((v >>  8) & 0x000000FFUL); back++;
	*(mydp++) = (unsigned char)( v        & 0x000000FFUL); back++;
        v = 0UL; vused = 0;
      }
    }
    mysp++;
  }
  if(vused) {
    u1 = (v >> 24) & 0x000000FFUL;
    u2 = (v >> 16) & 0x000000FFUL;
    u3 = (v >>  8) & 0x000000FFUL;
    switch(vused) {
      case 2: {
        if(v & 0x00FFFFFFUL) {
	  u1++; if(u1 >= 256UL) u1 = 0UL;
	}
	*(mydp++) = (unsigned char)(u1 & 0x000000FFUL); back++;
      } break;
      case 3: {
        if(v & 0x0000FFFFUL) {
	  u2++;
	  if(u2 >= 256UL) {
	    u2 = 0UL;
	    u1++; if(u1 >= 256UL) u1 = 0UL;
	  }
	}
	*(mydp++) = (unsigned char)(u1 & 0x000000FFUL); back++;
	*(mydp++) = (unsigned char)(u2 & 0x000000FFUL); back++;
      } break;
      case 4: {
        if(v & 0x000000FFUL) {
	  u3++;
	  if(u3 >= 256UL) {
	    u3 = 0UL;
	    u2++;
	    if(u2 >= 256UL) {
	      u2 = 0UL;
	      u1++; if(u1 >= 256UL) u1 = 0UL;
	    }
	  }
	}
	*(mydp++) = (unsigned char)(u1 & 0x000000FFUL); back++;
	*(mydp++) = (unsigned char)(u2 & 0x000000FFUL); back++;
	*(mydp++) = (unsigned char)(u3 & 0x000000FFUL); back++;
      } break;
    }
  } 

#line 1531 "dk3enc.ctr"
  return back;
}



size_t
dk3enc_a85_to_bin_app(
  char *dp, size_t ds, char const *sp, size_t ss, dk3_app_t *app
)
{
  size_t back = 0;
  size_t needed_size = 0;	/* Minimum output buffer size. */
  

#line 1544 "dk3enc.ctr"
  if((dp) && (sp) && (ds) && (ss)) {
    needed_size = dk3enc_size_a85_to_bin(ss);
    if(needed_size) {
      if(ds >= needed_size) {
        back = dk3enc_do_a85_to_bin(dp, ds, sp, ss, app);
      } else {
        if(app) {
	  /* Destination buffer too small! */
	  dk3app_log_i1(app, DK3_LL_ERROR, 38);
	}
      }
    }
  } 

#line 1557 "dk3enc.ctr"
  return back;
}



size_t
dk3enc_a85string_to_bin_app(char *dp, size_t ds, char const *sp, dk3_app_t *app)
{
  size_t back = 0;
  

#line 1567 "dk3enc.ctr"
  if(sp) {
    back = dk3enc_a85_to_bin_app(dp, ds, sp, dk3str_c8_len(sp), app);
  } 

#line 1570 "dk3enc.ctr"
  return back;
}



size_t
dk3enc_size_bin_to_hex(size_t s)
{
#if VERSION_BEFORE_20140809
  size_t back = 0;
  int ec = 0;			/* Error code. */
  unsigned long ul1 = 0UL;	/* Temporary value for calculation. */
  

#line 1583 "dk3enc.ctr"
  ul1 = (unsigned long)s;
  ul1 = dk3ma_ul_mul_ok(ul1, 2UL, &ec);
  ul1 = dk3ma_ul_add_ok(ul1, 1UL, &ec);
  back = (size_t)ul1;
  if(ec) back = 0;
  if((unsigned long)back != ul1) back = 0;
  

#line 1590 "dk3enc.ctr"
  return back;
#else
  size_t	back	= 0;
  int		ec	= 0;	/* Error code */
  

#line 1595 "dk3enc.ctr"
  back = dk3mem_add_size_t(1,   dk3mem_mul_size_t(2, s, &ec),   &ec);
  if (ec) { back = 0; }
  

#line 1598 "dk3enc.ctr"
  return back;
#endif
}



size_t
dk3enc_size_hex_to_bin(size_t s)
{
  size_t back;
  

#line 1609 "dk3enc.ctr"
  back = s / 2;
  back++; 

#line 1611 "dk3enc.ctr"
  return back;
}



/**	Get higher half-byte from character.
	@param	c	Source character.
	@return	Higher half-byte of \a c.
*/
static
char
dk3enc_c8_high_nibble_hex(char c)
{
  char back;
  back = dk3enc_hex_digits[ (((unsigned short)c) >> 4) & 0x000FU ];
  return back;
}



/**	Get lower half-byte from character.
	@param	c	Source character.
	@return	Lower half-byte of \a c.
*/
static
char
dk3enc_c8_low_nibble_hex(char c)
{
  char back;
  back = dk3enc_hex_digits[  ((unsigned short)c)       & 0x000FU ];
  return back;
}



/**	Convert binary data to hexadecimal string.
	@param	dp	Destination pointer.
	@param	ds	Destination size.
	@param	sp	Source pointer.
	@param	ss	Source size.
	@param	app	Application structure for diagnostics, may be NULL.
*/
static
void
dk3enc_do_bin_to_hex(char *dp, size_t ds, char const *sp, size_t ss, dk3_app_t *app)
{
  register char *mydp = NULL;		/* Destination pointer. */
  register char const *mysp = NULL;	/* Source pointer. */
  register size_t i = 0;		/* Current source byte index. */
  

#line 1661 "dk3enc.ctr"
  mydp = dp; mysp = sp;
  for(i = 0; i < ss; i++) {
    *(mydp++) = dk3enc_c8_high_nibble_hex(*mysp);
    *(mydp++) = dk3enc_c8_low_nibble_hex(*(mysp++));
  }
  *mydp = '\0'; 

#line 1667 "dk3enc.ctr"
}



int
dk3enc_bin_to_hex_app(
  char *dp, size_t ds, char const *sp, size_t ss, dk3_app_t *app
)
{
  int back = 0;
  size_t needed_bytes = 0;	/* Minimum output buffer size. */
  

#line 1679 "dk3enc.ctr"
  if((dp) && (ds) && (sp) && (ss)) {
    needed_bytes = dk3enc_size_bin_to_hex(ss);
    if(needed_bytes) {
      if(ds >= needed_bytes) {
        dk3enc_do_bin_to_hex(dp, ds, sp, ss, app); back = 1;
      } else {
        if(app) {
	  /* Destination buffer too small1 */
	  dk3app_log_i1(app, DK3_LL_ERROR, 38);
	}
      }
    }
  } 

#line 1692 "dk3enc.ctr"
  return back;
}



/**	Convert hexadecimal data to binary data.
	@param	dp	Destination pointer.
	@param	ds	Destination size.
	@param	sp	Source pointer.
	@param	ss	Source size.
	@param	app	Application structure for diagnostics, may be NULL.
	@return	Number of bytes created in destination.
*/
static
size_t
dk3enc_do_hex_to_bin(char *dp, size_t ds, char const *sp, size_t ss, dk3_app_t *app)
{
  register size_t back = 0;
  register char const *mysp = NULL;	/* Source pointer. */
  register unsigned char *mydp = NULL;	/* Destination pointer. */
  register unsigned char v = 0x00;	/* Current byte to process. */
  register short int vused = 0;		/* Flag: v used. */
  register size_t i = 0;		/* Current source byte index. */
  int reported_illegal_character = 0;	/* Flag: Error already reported. */
  

#line 1717 "dk3enc.ctr"
  mydp = (unsigned char *)dp;
  mysp = sp; v = 0x00; vused = 0U;
  for(i = 0; i < ss; i++) {
    switch(*mysp) {
      case '0':
      case '1':
      case '2':
      case '3':
      case '4':
      case '5':
      case '6':
      case '7':
      case '8':
      case '9':
      case 'a':
      case 'A':
      case 'b':
      case 'B':
      case 'c':
      case 'C':
      case 'd':
      case 'D':
      case 'e':
      case 'E':
      case 'f':
      case 'F':
      {
        if(vused) {
	  switch(*mysp) {
	    case '0': { v |= 0x00; } break;
	    case '1': { v |= 0x01; } break;
	    case '2': { v |= 0x02; } break;
	    case '3': { v |= 0x03; } break;
	    case '4': { v |= 0x04; } break;
	    case '5': { v |= 0x05; } break;
	    case '6': { v |= 0x06; } break;
	    case '7': { v |= 0x07; } break;
	    case '8': { v |= 0x08; } break;
	    case '9': { v |= 0x09; } break;
	    case 'a': { v |= 0x0A; } break;
	    case 'A': { v |= 0x0A; } break;
	    case 'b': { v |= 0x0B; } break;
	    case 'B': { v |= 0x0B; } break;
	    case 'c': { v |= 0x0C; } break;
	    case 'C': { v |= 0x0C; } break;
	    case 'd': { v |= 0x0D; } break;
	    case 'D': { v |= 0x0D; } break;
	    case 'e': { v |= 0x0E; } break;
	    case 'E': { v |= 0x0E; } break;
	    case 'f': { v |= 0x0F; } break;
	    case 'F': { v |= 0x0F; } break;
	  }
	  *(mydp++) = v; back++; v = 0; vused = 0U;
	} else {
	  switch(*mysp) {
	    case '0': { v = 0x00; } break;
	    case '1': { v = 0x10; } break;
	    case '2': { v = 0x20; } break;
	    case '3': { v = 0x30; } break;
	    case '4': { v = 0x40; } break;
	    case '5': { v = 0x50; } break;
	    case '6': { v = 0x60; } break;
	    case '7': { v = 0x70; } break;
	    case '8': { v = 0x80; } break;
	    case '9': { v = 0x90; } break;
	    case 'a': { v = 0xA0; } break;
	    case 'A': { v = 0xA0; } break;
	    case 'b': { v = 0xB0; } break;
	    case 'B': { v = 0xB0; } break;
	    case 'c': { v = 0xC0; } break;
	    case 'C': { v = 0xC0; } break;
	    case 'd': { v = 0xD0; } break;
	    case 'D': { v = 0xD0; } break;
	    case 'e': { v = 0xE0; } break;
	    case 'E': { v = 0xE0; } break;
	    case 'f': { v = 0xF0; } break;
	    case 'F': { v = 0xF0; } break;
	  }
	  vused = 1U;
	}
      } break;
      default: {
        if(!reported_illegal_character) {
	  reported_illegal_character = 1;
	  if(app) {
	    /* Illegal character(s) in source! */
	    dk3app_log_i1(app, DK3_LL_ERROR, 67);
	  }
	}
      } break;
    }
    mysp++;
  }
  if(vused) {
    *mydp = v; back++;
  } 

#line 1813 "dk3enc.ctr"
  return back;
}



size_t
dk3enc_hex_to_bin_app(
  char *dp, size_t ds, char const *sp, size_t ss, dk3_app_t *app
)
{
  size_t back = 0;
  size_t needed_bytes = 0;	/* Minimum output buffer size. */
  

#line 1826 "dk3enc.ctr"
  if((dp) && (ds) && (sp) && (ss)) {
    needed_bytes = dk3enc_size_hex_to_bin(ss);
    if(needed_bytes) {
      if(ds >= needed_bytes) {
        back = dk3enc_do_hex_to_bin(dp, ds, sp, ss, app);
      } else {
        if(app) {
	  /* Destination buffer too small! */
	  dk3app_log_i1(app, DK3_LL_ERROR, 38);
	}
      }
    }
  } 

#line 1839 "dk3enc.ctr"
  return back;
}



size_t
dk3enc_hexstring_to_bin_app(
  char *dp, size_t ds, char const *sp, dk3_app_t *app
)
{
  size_t back = 0;
  

#line 1851 "dk3enc.ctr"
  if(sp) {
    back = dk3enc_hex_to_bin_app(dp, ds, sp, dk3str_c8_len(sp), app);
  } 

#line 1854 "dk3enc.ctr"
  return back;
}



size_t
dk3enc_size_hexstring_to_bin(char const *s)
{
  size_t back = 0;
  

#line 1864 "dk3enc.ctr"
  if(s) {
    back = dk3str_c8_len(s);
    back = dk3enc_size_hex_to_bin(back);
  } 

#line 1868 "dk3enc.ctr"
  return back;
}



size_t
dk3enc_size_a85string_to_bin(char const *s)
{
  size_t back = 0;
  

#line 1878 "dk3enc.ctr"
  if(s) {
    back = dk3str_c8_len(s);
    back = dk3enc_size_a85_to_bin(back);
  } 

#line 1882 "dk3enc.ctr"
  return back;
}



int
dk3enc_get_type_app(dkChar const *n, dk3_app_t *app)
{
  int back = -1;
  if(n) {
    back = dk3str_array_index(dk3enc_data_encoding_names, n, 0);
    if(back >= 0) {
      switch(back) {
        case 0: case 3: {
	  back = DK3_DATA_ENCODING_HEX;
	} break;
	case 1: case 4: case 6: {
	  back = DK3_DATA_ENCODING_ASCII85;
	} break;
	case 2: case 5: case 7: {
	  back = DK3_DATA_ENCODING_REVERSE_ASCII85;
	} break;
      }
    } else {
      if(app) {
        dk3app_log_i3(app, DK3_LL_ERROR, 129, 130, n);
      }
    }
  }
  return back;
}


dkChar const *
dk3enc_get_data_encoding_name(int t)
{
  dkChar const *back = NULL;
  if((t >= 0) && (t <= 2)) {
    back = dk3enc_data_encoding_names[t];
  }
  return back;
}



int
dk3enc_get_text_encoding_app(dkChar const *en, dk3_app_t *app)
{
  int back = -1;
  int i;
  

#line 1933 "dk3enc.ctr"
  if(app) { back = dk3app_get_output_encoding(app); }
  if(en) {
    i = dk3str_array_index(dk3enc_text_encoding_names, en, 0); 

#line 1936 "dk3enc.ctr"
    switch(i) {
      case 0: case 8: case 9: {
        back = DK3_FILE_ENCODING_ASCII;
      } break;
      case 1: case 10: {
        back = DK3_FILE_ENCODING_UTF8;
      } break;
      case 2: case 11: {
#if DK3_HAVE_BIGENDIAN
	back = DK3_FILE_ENCODING_UTF16_MSB_FIRST;
#else
	back = DK3_FILE_ENCODING_UTF16_LSB_FIRST;
#endif
      } break;
      case 3: case 18: case 19: case 20: case 21: case 22: case 23: {
        back = DK3_FILE_ENCODING_UTF16_MSB_FIRST;
      } break;
      case 4: case 12: case 13: case 14: case 15: case 16: case 17: {
        back = DK3_FILE_ENCODING_UTF16_LSB_FIRST;
      } break;
      case 5: case 24: {
#if DK3_HAVE_BIGENDIAN
	back = DK3_FILE_ENCODING_UNICODE_MSB_FIRST;
#else
	back = DK3_FILE_ENCODING_UNICODE_LSB_FIRST;
#endif
      } break;
      case 6: case 29: case 30: case 31: case 32: {
        back = DK3_FILE_ENCODING_UNICODE_MSB_FIRST;
      } break;
      case 7: case 25: case 26: case 27: case 28: {
        back = DK3_FILE_ENCODING_UNICODE_LSB_FIRST;
      } break;
      default: {
        if(app) {
	  /* ERROR: Unknown text encoding name! */
	  dk3app_log_i3(app, DK3_LL_ERROR, 129, 130, en);
	}
      } break;
    }
  } 

#line 1977 "dk3enc.ctr"
  return back;
}



int
dk3enc_get_encoding(dk3_app_t *app)
{
  int		 back = 0;
#if DK3_CHAR_SIZE == 1
  char		*ptr;
#endif
  if(app) {
    back = dk3app_get_encoding(app);
  } else {
#if DK3_CHAR_SIZE > 1
#if DK3_CHAR_SIZE > 2
    back = DK3_ENCODING_UNICODE;
#else
    back = DK3_ENCODING_UTF16;
#endif
#else
    back = DK3_ENCODING_PLAIN;
    ptr = getenv("LANG");
    if(ptr) {
      ptr = dk3str_c8_chr(ptr, '.');
      if(ptr) {
        ptr++;
	switch(dk3str_c8_array_index(dk3enc_utf8_names, ptr, 0)) {
	  case 0: case 1: {
	    back = DK3_ENCODING_UTF8;
	  } break;
	}
      }
    }
#endif
  }
  return back;
}



/* vim: set ai sw=2 : */