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
|
/*
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: dk3se.ctr
*/
/*
Copyright (C) 2013-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 dk3se.c The dk3se module.
*/
#line 97 "dk3se.ctr"
#include "dk3all.h"
#include "dk3se.h"
#line 106 "dk3se.ctr"
/** Find ANSI character for 32-bit character.
@param dp Pointer to destination variable.
@param c32 32-bit character.
@return 1 on success (character found), 0 on error (not available).
*/
static
int
dk3se_find_ansi_char(char *dp, dk3_c32_t c32)
{
int back = 0;
unsigned u;
unsigned char uc;
char c = '\0';
#line 123 "dk3se.ctr"
if((dk3_c32_t)0x00010000UL > c32) {
u = (unsigned)c32;
if(((dk3_c32_t)0x00000019UL < c32) && ((dk3_c32_t)0x00000080UL > c32)) {
uc = (unsigned char)u; c = (char)uc;
back = 1;
} else {
if(((dk3_c32_t)0x0000009FUL < c32) && ((dk3_c32_t)0x00000100UL > c32)) {
uc = (unsigned char)u; c = (char)uc;
back = 1;
} else {
switch(u) {
case 0x20AC: { c = (char)0x80; back = 1; } break;
case 0x0081: { c = (char)0x81; back = 1; } break;
case 0x201A: { c = (char)0x82; back = 1; } break;
case 0x0192: { c = (char)0x83; back = 1; } break;
case 0x201E: { c = (char)0x84; back = 1; } break;
case 0x2026: { c = (char)0x85; back = 1; } break;
case 0x2020: { c = (char)0x86; back = 1; } break;
case 0x2021: { c = (char)0x87; back = 1; } break;
case 0x02C6: { c = (char)0x88; back = 1; } break;
case 0x2030: { c = (char)0x89; back = 1; } break;
case 0x0160: { c = (char)0x8A; back = 1; } break;
case 0x2039: { c = (char)0x8B; back = 1; } break;
case 0x0152: { c = (char)0x8C; back = 1; } break;
case 0x008D: { c = (char)0x8D; back = 1; } break;
case 0x017D: { c = (char)0x8E; back = 1; } break;
case 0x008F: { c = (char)0x8F; back = 1; } break;
case 0x0090: { c = (char)0x90; back = 1; } break;
case 0x2018: { c = (char)0x91; back = 1; } break;
case 0x2019: { c = (char)0x92; back = 1; } break;
case 0x201C: { c = (char)0x93; back = 1; } break;
case 0x201D: { c = (char)0x94; back = 1; } break;
case 0x2022: { c = (char)0x95; back = 1; } break;
case 0x2013: { c = (char)0x96; back = 1; } break;
case 0x2014: { c = (char)0x97; back = 1; } break;
case 0x02DC: { c = (char)0x98; back = 1; } break;
case 0x2122: { c = (char)0x99; back = 1; } break;
case 0x0161: { c = (char)0x9A; back = 1; } break;
case 0x203A: { c = (char)0x9B; back = 1; } break;
case 0x0153: { c = (char)0x9C; back = 1; } break;
case 0x009D: { c = (char)0x9D; back = 1; } break;
case 0x017E: { c = (char)0x9E; back = 1; } break;
case 0x0178: { c = (char)0x9F; back = 1; } break;
}
}
}
}
if(back) {
#line 171 "dk3se.ctr"
*dp = c;
}
#line 173 "dk3se.ctr"
return back;
}
/** Check whether a character must be hex-encoded for output
as URL.
@param c Character to check.
@return 1 for percent-hex encoding, 0 for direct output.
*/
static
int
dk3se_must_encode_for_url(char c)
{
int back = 1;
#line 189 "dk3se.ctr"
if(('a' <= c) && ('z' >= c)) {
back = 0;
} else {
if(('A' <= c) && ('Z' >= c)) {
back = 0;
} else {
if(('0' <= c) && ('9' >= c)) {
back = 0;
} else {
switch(c) {
case '-': case '_': case '.': case '~': {
back = 0;
} break;
}
}
}
}
#line 206 "dk3se.ctr"
return back;
}
/** Write one 32-bit character to 8-bit character file or check
whether writing would be possible.
@param c32 Character to write or test.
@param fipo File to write to (NULL for check only).
@param se Special encoding conditions.
@param ev Pointer to variable to store encoding violations.
@param we Pointer to write error variable.
@param app Application structure for diagnostics, may be NULL.
*/
static
int
dk3se_fputc_checked(
dk3_c32_t c32,
FILE *fipo,
int se,
int *ev,
int *we,
dk3_app_t *app
)
{
char buf[16];
int back = 0;
unsigned u;
unsigned char uc = '\0';
char c = '\0';
#line 237 "dk3se.ctr"
if(se & DK3SE_ANSI) {
#line 238 "dk3se.ctr"
back = dk3se_find_ansi_char(&c, c32);
if(!(back)) {
if(ev) { *ev = ((*ev) | DK3SE_ANSI); }
if(app) {
}
}
} else {
#line 245 "dk3se.ctr"
if((dk3_c32_t)0x00000100UL > c32) {
u = (unsigned)c32; uc = (unsigned char)u; c = (char)uc;
back = 1;
} else {
if(ev) { *ev = ((*ev) | DK3SE_URL); }
if(app) {
}
}
}
if(back) {
#line 255 "dk3se.ctr"
if(fipo) {
if(se & DK3SE_URL) {
#line 257 "dk3se.ctr"
if(dk3se_must_encode_for_url(c)) {
buf[0] = '%';
#line 259 "dk3se.ctr"
sprintf(&(buf[1]), "%02lx", (unsigned long)c32);
if(EOF == fputs(buf, fipo)) {
back = 0;
if(we) { *we = 1;}
}
} else {
#line 265 "dk3se.ctr"
if(EOF == fputc(c, fipo)) {
back = 0;
if(we) { *we = 1; }
}
}
} else {
#line 271 "dk3se.ctr"
if(EOF == fputc(c, fipo)) {
back = 0;
if(we) { *we = 1; }
}
}
}
}
#line 278 "dk3se.ctr"
return back;
}
/** Show results for testing or writing a string.
@param str String to write or test.
@param ev Pointer to result variable: Encoding violations.
@param we Pointer to result variable: Write error.
@param myev Found encoding violations.
@param mywe Found write errors.
@param myde Found decoding errors.
@param app Application structure for diagnostics, may be NULL.
*/
static
void
dk3se_show_results(
dkChar const *str,
int *ev,
int *we,
int myev,
int mywe,
int myde,
dk3_app_t *app
)
{
if(ev) { *ev = myev; }
if(we) { *we = mywe; }
if(app) {
if(myde) {
dk3app_log_i3(app, DK3_LL_ERROR, 392, 367, str);
}
if(myev & DK3SE_ANSI) {
dk3app_log_i3(app, DK3_LL_ERROR, 393, 367, str);
}
if(myev & DK3SE_URL) {
dk3app_log_i3(app, DK3_LL_ERROR, 394, 367, str);
}
if(mywe) {
dk3app_log_i3(app, DK3_LL_ERROR, 343, 344, str);
}
}
}
/** Write or test one string.
@param str String to write or test.
@param enc String encoding.
@param fipo Output file.
@param se Required encodings.
@param ev Pointer to result variable: Encoding violations.
@param we Pointer to result variable: Write error.
@param app Application structure for diagnostics, may be NULL.
@return 1 on success, 0 on errors.
*/
static
int
dk3se_fct
(
dkChar const *str,
int enc,
FILE *fipo,
int se,
int *ev,
int *we,
dk3_app_t *app
)
{
#if DK3_CHAR_SIZE > 1
#if DK3_CHAR_SIZE > 2
dkChar const *ptr;
int back = 1;
int myev = 0;
int mywe = 0;
#line 354 "dk3se.ctr"
ptr = str;
while(*ptr) {
if(!dk3se_fputc_checked(*ptr, fipo, se, &myev, &mywe, app)) { back = 0; }
ptr++;
}
if(!(back)) {
dk3se_show_results(str, ev, we, myev, mywe, 0, app);
}
#line 362 "dk3se.ctr"
return back;
#else
dk3_c16_t const *sp;
dk3_c32_t c32;
size_t sl;
size_t used;
int back = 1;
int myev = 0;
int mywe = 0;
int myde = 0;
#line 373 "dk3se.ctr"
sp = (dk3_c16_t const *)str;
sl = dk3str_len(str);
while(sl > 0) {
used = 0;
if(dk3enc_utf162uc(&c32, sp, sl, &used)) {
if(!dk3se_fputc_checked(c32, fipo, se, &myev, &mywe, app)) { back = 0; }
if(used > 0) {
if(sl >= used) {
sl = sl - used;
sp = &(sp[used]);
} else {
sl = 0;
back = 0;
myde = 1;
}
} else {
sl = 0;
back = 0;
myde = 1;
}
} else {
sl = 0;
back = 0;
myde = 1;
}
}
if(!(back)) {
dk3se_show_results(str, ev, we, myev, mywe, myde, app);
}
#line 402 "dk3se.ctr"
return back;
#endif
#else
dk3_c32_t c32;
unsigned char const *sp;
size_t sl;
size_t used;
int back = 1;
int myev = 0;
int mywe = 0;
int myde = 0;
char c;
#line 415 "dk3se.ctr"
sp = (unsigned char const *)str;
sl = dk3str_c8_len((char const *)sp);
if(DK3_ENCODING_UTF8 == enc) {
while(sl > 0) {
used = 0;
if(dk3enc_utf82uc(&c32, sp, sl, &used)) {
if(!dk3se_fputc_checked(c32, fipo, se, &myev, &mywe, app)) { back = 0; }
if(used > 0) {
if(sl >= used) {
sl = sl - used;
sp = &(sp[used]);
} else {
sl = 0;
back = 0;
myde = 1;
}
} else {
sl = 0;
back = 0;
myde = 1;
}
} else {
sl = 0;
back = 0;
myde = 1;
}
}
if(!(back)) {
dk3se_show_results(str, ev, we, myev, mywe, myde, app);
}
} else {
while(*sp) {
c = *(sp++);
c32 = (dk3_c32_t)c;
c32 &= 0x000000FFUL;
if(!dk3se_fputc_checked(c32, fipo, se, &myev, &mywe, app)) { back = 0; }
}
if(!(back)) {
dk3se_show_results(str, ev, we, myev, mywe, 0, app);
}
}
#line 456 "dk3se.ctr"
return back;
#endif
}
int
dk3se_ansi_fputc(dk3_c32_t c32, FILE *fipo)
{
return(dk3se_fputc_checked(c32, fipo, DK3SE_ANSI, NULL, NULL, NULL));
}
int
dk3se_url_fputc(dk3_c32_t c32, FILE *fipo)
{
return(dk3se_fputc_checked(c32, fipo, DK3SE_URL, NULL, NULL, NULL));
}
int
dk3se_ansi_url_fputc(dk3_c32_t c32, FILE *fipo)
{
return(dk3se_fputc_checked(c32,fipo,(DK3SE_ANSI | DK3SE_URL),NULL,NULL,NULL));
}
int
dk3se_ansi_fputs(dkChar const *str, int enc, FILE *fipo, dk3_app_t *app)
{
return(dk3se_fct(str, enc, fipo, DK3SE_ANSI, NULL, NULL, app));
}
int
dk3se_url_fputs(dkChar const *str, int enc, FILE *fipo, dk3_app_t *app)
{
return(dk3se_fct(str, enc, fipo, DK3SE_URL, NULL, NULL, app));
}
int
dk3se_ansi_url_fputs(dkChar const *str, int enc, FILE *fipo, dk3_app_t *app)
{
return(dk3se_fct(str, enc, fipo, (DK3SE_ANSI | DK3SE_URL), NULL, NULL, app));
}
int
dk3se_check(dkChar const *str, int enc, int se, int *ev, int *we,dk3_app_t *app)
{
return(dk3se_fct(str, enc, NULL, se, ev, we, app));
}
|