summaryrefslogtreecommitdiff
path: root/support/dktools/dk3se.ctr
blob: 115f02d8817407b3c878b8b0063dff1912579642 (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
%%	options

copyright owner	=	Dirk Krause
copyright year	=	2013-xxxx
license		=	bsd


%%	header

#include <dk3conf.h>
#include <dk3types.h>

#ifdef __cplusplus
extern "C" {
#endif

/**	Write ANSI character or check ANSI compatibility.
	@param	c32	Character to write or check.
	@param	fipo	File to write to (NULL for check).
	@return	1 on success, 0 on error (No representation for this
	character in ANSI encoding).
*/
int
dk3se_ansi_fputc(dk3_c32_t c32, FILE *fipo);

/**	Write URL character or check URL compatibility.
	@param	c32	Character to write or check.
	@param	fipo	File to write to (NULL for check).
	@return	1 on success, 0 on error.
*/
int
dk3se_url_fputc(dk3_c32_t c32, FILE *fipo);

/**	Convert to ANSI and write URL character or check URL compatibility.
	@param	c32	Character to write or check.
	@param	fipo	File to write to (NULL for check).
	@return	1 on success, 0 on error.
*/
int
dk3se_ansi_url_fputc(dk3_c32_t c32, FILE *fipo);

/**	Write ANSI string or check ANSI compatibility.
	@param	str	String to write.
	@param	enc	String encoding.
	@param	fipo	File to write to (NULL for compatibility check).
	@param	app	Application structure for diagnostics, may be NULL.
	@return	1 on success, 0 on error.
*/
int
dk3se_ansi_fputs(dkChar const *str, int enc, FILE *fipo, dk3_app_t *app);

/**	Write URL string or check URL compatibility.
	@param	str	String to write.
	@param	enc	String encoding.
	@param	fipo	File to write to (NULL for compatibility check).
	@param	app	Application structure for diagnostics, may be NULL.
	@return	1 on success, 0 on error.
*/
int
dk3se_url_fputs(dkChar const *str, int enc, FILE *fipo, dk3_app_t *app);

/**	Convert to ANSI and write URL string or check URL compatibility.
	@param	str	String to write.
	@param	enc	String encoding.
	@param	fipo	File to write to (NULL for compatibility check).
	@param	app	Application structure for diagnostics, may be NULL.
	@return	1 on success, 0 on error.
*/
int
dk3se_ansi_url_fputs(dkChar const *str, int enc, FILE *fipo, dk3_app_t *app);

/**	Check whether a string can be converted to ANSI or URL.
	@param	str	String to test.
	@param	enc	String encoding.
	@param	se	Output encoding (DK3SE_ANSI and/or DK3SE_URL).
	@param	ev	Pointer to encoding violation variable.
	@param	we	Pointer to write error variable.
	@param	app	Application structure for diagnostics, may be NULL.
	@return	1 on success, 0 on error.
*/
int
dk3se_check(dkChar const *str,int enc,int se,int *ev,int *we,dk3_app_t *app);

#ifdef __cplusplus
}
#endif

/**	ANSI encoding.
*/
#define	DK3SE_ANSI	1

/**	URL encoding.
*/
#define	DK3SE_URL	2

%%	module



#include "dk3all.h"
#include "dk3se.h"



$!trace-include



/**	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';
  $? "+ dk3se_find_ansi_char 0x%lx %lu", (unsigned long)c32, (unsigned long)c32
  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) {	$? ". success \"%!8c\"", c
    *dp = c;
  }	$? "- dk3se_find_ansi_char %d", back
  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;
  $? "+ dk3se_must_encode_for_url %x \"%!8c\"", (int)c, c
  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;
	}
      }
    }
  } $? "- dk3se_must_encode_for_url %d", back
  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';
  $? "+ dk3se_fputc_checked 0x%lx %lu", (unsigned long)c32, (unsigned long)c32
  if(se & DK3SE_ANSI) {	$? ". ANSI required"
    back = dk3se_find_ansi_char(&c, c32);
    if(!(back)) {
      if(ev) { *ev = ((*ev) | DK3SE_ANSI); }
      if(app) {
      }
    }
  } else {		$? ". ANSI not required"
    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) {		$? ". ok so far"
    if(fipo) {
      if(se & DK3SE_URL) {	$? ". output as URL"
        if(dk3se_must_encode_for_url(c)) {
	  buf[0] = '%';		$? ". percent/hex encoding"
	  sprintf(&(buf[1]), "%02lx", (unsigned long)c32);
	  if(EOF == fputs(buf, fipo)) {
	    back = 0;
	    if(we) { *we = 1;}
	  }
	} else {		$? ". no percent/hex encoding necessary"
	  if(EOF == fputc(c, fipo)) {
	    back = 0;
	    if(we) { *we = 1; }
	  }
	}
      } else {			$? ". output as is"
        if(EOF == fputc(c, fipo)) {
	  back = 0;
	  if(we) { *we = 1; }
	}
      }
    }
  } $? "- dk3se_fputc_checked %d", back
  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;
  $? "+ dk3se_fct (32 bit)"
  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);
  } $? "- dk3se_fct %d", back
  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;
  $? "+ dk3se_fct (16 bit)"
  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);
  } $? "- dk3se_fct %d", back
  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;
  $? "+ dk3se_fct (8 bit)"
  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);
    }
  } $? "- dk3se_fct %d", back
  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));
}