summaryrefslogtreecommitdiff
path: root/support/dktools/dk4enc.c
blob: 0659794b1cdafffbb31d961b3d9a1a2913b70f58 (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
/*
	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: dk4enc.ctr
*/

/*
Copyright (C) 2015-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 dk4enc.c The dk4enc module.
*/


#line 138 "dk4enc.ctr"

#include "dk4enc.h"
#include "dk4mem.h"
#include "dk4strd.h"



/**	Encoding names in variations.
*/
static const dkChar * const dk4enc_encoding_names[] = {
/* 0 */
dkT("plain"),

/* 1 */
dkT("ascii"),

/* 2 */
dkT("ansi"),

/* 3 */
dkT("utf-8"),

/* 4 */
dkT("utf8"),

/* 5 */
dkT("utf-16"),

/* 6 */
dkT("utf16"),

/* 7 */
dkT("utf-16-le"),

/* 8 */
dkT("utf-16le"),

/* 9 */
dkT("utf16le"),

/* 10 */
dkT("utf-16-lsb"),

/* 11 */
dkT("utf-16lsb"),

/* 12 */
dkT("utf16lsb"),

/* 13 */
dkT("utf-16-be"),

/* 14 */
dkT("utf-16be"),

/* 15 */
dkT("utf16be"),

/* 16 */
dkT("utf-16-msb"),

/* 17 */
dkT("utf-16msb"),

/* 18 */
dkT("utf16msb"),

/* 19 */
dkT("c32"),

/* 20 */
dkT("c32-le"),

/* 21 */
dkT("c32le"),

/* 22 */
dkT("c32-lsb"),

/* 23 */
dkT("c32lsb"),

/* 24 */
dkT("c32-be"),

/* 25 */
dkT("c32be"),

/* 26 */
dkT("c32-msb"),

/* 27 */
dkT("c32msb"),

/* 28 */
dkT("utf-16.msb"),

/* 29 */
dkT("utf-16.lsb"),

/* 30 */
dkT("uc32"),

/* 31 */
dkT("uc32.msb"),

/* 32 */
dkT("uc32.lsb"),

/* 33 */
dkT("iso-latin-1"),

/* 34 */
dkT("iso-8859-1"),

NULL


#line 215 "dk4enc.ctr"
};



/**	Keywords for further options.
*/
static const dkChar * const dk4enc_option_keywords[] = {
/* 0 */
dkT("le"),

/* 1 */
dkT("lsb"),

/* 2 */
dkT("be"),

/* 3 */
dkT("msb"),

/* 4 */
dkT("bom"),

/* 5 */
dkT("nobom"),

NULL


#line 230 "dk4enc.ctr"
};



int
dk4enc_find(int *encptr, int *bomptr, const dkChar *src, dk4_er_t *erp)
{
  dkChar	buf[64];		/* Private copy for modification */
  dkChar	*p1;			/* Start of text */
  dkChar	*p2;			/* Start of options */
  int		res	=	0;	/* Array index */
  int		back	=	0;	/* Function result */
  int		ae	=	0;	/* Flag: Allow ending specification */
  int		enc	=	0;	/* Encoding found */
  int		bom	=	0;	/* Flag: BOM keyword found */
  int		bom_f	=	0;	/* Flag: BOM information found */

  if ((NULL != encptr) && (NULL != src)) {
    if (0 != dk4str_cpy_s(buf, DK4_SIZEOF(buf,dkChar), src, erp)) {
      p1 = dk4str_start(buf, NULL);
      if (NULL != p1) {
        p2 = dk4str_chr(buf, dkT(','));
	if (NULL == p2) {
	  p2 = dk4str_chr(buf, dkT('.'));
	}
	if (NULL != p2) { *(p2++) = dkT('\0'); p2 = dk4str_start(p2, NULL); }
	dk4str_normalize(p1, NULL);
	switch (dk4str_array_index(dk4enc_encoding_names, p1, 0)) {
	  case 0: case 1: case 33: case 34: {
	    enc = DK4_FILE_ENCODING_ASCII;
	    back = 1;
	  } break;
	  case 2: {
	    enc = DK4_FILE_ENCODING_ANSI;
	    back = 1;
	  } break;
	  case 3: case 4: {
	    enc = DK4_FILE_ENCODING_UTF8;
	    back = 1;
	  } break;
	  case 5: case 6: {
	    enc = DK4_ENCODING_UTF16;
	    ae  = 1;
	    back = 1;
	    bom = 1;
	  } break;
	  case 7: case 8: case 9: case 10: case 11: case 12: case 29: {
	    enc = DK4_FILE_ENCODING_UTF16_LE;
	    back = 1;
	    bom = 1;
	  } break;
	  case 13: case 14: case 15: case 16: case 17: case 18: case 28: {
	    enc = DK4_FILE_ENCODING_UTF16_BE;
	    back = 1;
	    bom = 1;
	  } break;
	  case 19: case 30: {
	    enc = DK4_ENCODING_32;
	    ae  = 1;
	    back = 1;
	    bom = 1;
	  } break;
	  case 20: case 21: case 22: case 23: case 32: {
	    enc = DK4_FILE_ENCODING_32_LE;
	    back = 1;
	    bom = 1;
	  } break;
	  case 24: case 25: case 26: case 27: case 31: {
	    enc = DK4_FILE_ENCODING_32_BE;
	    back = 1;
	    bom = 1;
	  } break;
	  default: {
	    dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
	  } break;
	}
	if (1 == back) {
	  while (NULL != p2) {
	    p1 = dk4str_chr(p2, dkT(','));
	    if (NULL != p1) { *(p1++) = dkT('\0'); p1 = dk4str_start(p1,NULL); }
	    dk4str_normalize(p2, NULL);
	    switch (res = dk4str_array_index(dk4enc_option_keywords, p2, 0)) {
	      case 0: case 1: case 2: case 3: {
	        if (0 != ae) {
		  switch (enc) {
		    case DK4_ENCODING_UTF16: {
		      enc = (
		        ((2 == res) || (3 == res))
			? DK4_FILE_ENCODING_UTF16_BE
			: DK4_FILE_ENCODING_UTF16_LE
		      );
		    } break;
		    case DK4_ENCODING_32: {
		      enc = (
		        ((2 == res) || (3 == res))
			? DK4_FILE_ENCODING_32_BE
			: DK4_FILE_ENCODING_32_LE
		      );
		    } break;
		  }
		  ae = 0;
		} else {
		  back = 0;
		  dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
		}
	      } break;
	      case 4: {
	        switch (enc) {
		  case DK4_FILE_ENCODING_UTF8:
		  case DK4_FILE_ENCODING_UTF16_LE:
		  case DK4_FILE_ENCODING_UTF16_BE:
		  case DK4_FILE_ENCODING_32_LE:
		  case DK4_FILE_ENCODING_32_BE: {
		    bom = 1;
		    bom_f = 1;
		  } break;
		  default: {
		    back = 0;
		    dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
		  } break;
		}
	      } break;
	      case 5: {
	        bom = 0;
		bom_f = 1;
	      } break;
	      default: {
	        back = 0;
		dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
	      } break;
	    }
	    p2 = p1;
	  }
	}
      } else {
        /* ERROR: Empty string */
	dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
      }
    } else {
      dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
    }
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  }
  if (NULL != encptr) { *encptr = enc; }
  if (0 != bom_f) { if (NULL != bomptr) { *bomptr = bom; } }
  return back;
}