summaryrefslogtreecommitdiff
path: root/libxslt/xsltlocale.c
blob: 0b76ec09c8ea65fa4a3803b4435b240abc636647 (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
/*
 * xsltlocale.c: locale handling
 *
 * Reference:
 * RFC 3066: Tags for the Identification of Languages
 * http://www.ietf.org/rfc/rfc3066.txt
 * ISO 639-1, ISO 3166-1
 *
 * Author: Nick Wellnhofer
 */

#define IN_LIBXSLT
#include "libxslt.h"

#include <string.h>
#include <libxml/xmlmemory.h>

#include "xsltlocale.h"
#include "xsltutils.h"

#if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 2
#define newlocale __newlocale
#define freelocale __freelocale
#define strxfrm_l __strxfrm_l
#define LC_COLLATE_MASK (1 << LC_COLLATE)
#endif

#define ISALPHA(c) ((c & 0xc0) == 0x40 && (unsigned)((c & 0x1f) - 1) < 26)
#define TOUPPER(c) (c & ~0x20)
#define TOLOWER(c) (c | 0x20)

/**
 * xsltNewLocale:
 * @languageTag: RFC 3066 language tag
 *
 * Creates a new locale of an opaque system dependent type based on the
 * language tag. Three-letter language codes (ISO 639-2 Alpha-3) are not
 * supported.
 *
 * Returns the locale or NULL on error or if no matching locale was found
 */
xsltLocale
xsltNewLocale(const xmlChar *languageTag) {
#ifdef XSLT_LOCALE_XLOCALE
    xsltLocale locale;
    char localeName[23]; /* 8*lang + "-" + 8*region + ".utf8\0" */
    const xmlChar *p = languageTag;
    const char *region = NULL;
    char *q = localeName;
    int c, i, llen;
    
    /* Convert something like "pt-br" to "pt_BR.utf8" */
    
    if (languageTag == NULL)
    	return(NULL);
    
    for (i=0; i<8 && ISALPHA(*p); ++i)
	*q++ = TOLOWER(*p++);
    
    if (i == 0)
    	return(NULL);
    
    llen = i;
    *q++ = '_';
    
    if (*p) {
    	if (*p++ != '-')
    	    return(NULL);
	
	for (i=0; i<8 && ISALPHA(*p); ++i)
	    *q++ = TOUPPER(*p++);
    
    	if (i == 0 || *p)
    	    return(NULL);
    	
        memcpy(q, ".utf8", 6);
        locale = newlocale(LC_COLLATE_MASK, localeName, NULL);
        if (locale != NULL)
            return(locale);
        
        q = localeName + llen + 1;
    }
    
    /* Try to find most common country for language */
    
    if (llen != 2)
        return(NULL);

    c = localeName[1];
    
    /* This is based on the locales from glibc 2.3.3 */
    
    switch (localeName[0]) {
        case 'a':
            if (c == 'a' || c == 'm') region = "ET";
            else if (c == 'f') region = "ZA";
            else if (c == 'n') region = "ES";
            else if (c == 'r') region = "AE";
            else if (c == 'z') region = "AZ";
            break;
        case 'b':
            if (c == 'e') region = "BY";
            else if (c == 'g') region = "BG";
            else if (c == 'n') region = "BD";
            else if (c == 'r') region = "FR";
            else if (c == 's') region = "BA";
            break;
        case 'c':
            if (c == 'a') region = "ES";
            else if (c == 's') region = "CZ";
            else if (c == 'y') region = "GB";
            break;
        case 'd':
            if (c == 'a') region = "DK";
            else if (c == 'e') region = "DE";
            break;
        case 'e':
            if (c == 'l') region = "GR";
            else if (c == 'n') region = "US";
            else if (c == 's' || c == 'u') region = "ES";
            else if (c == 't') region = "EE";
            break;
        case 'f':
            if (c == 'a') region = "IR";
            else if (c == 'i') region = "FI";
            else if (c == 'o') region = "FO";
            else if (c == 'r') region = "FR";
            break;
        case 'g':
            if (c == 'a') region = "IE";
            else if (c == 'l') region = "ES";
            else if (c == 'v') region = "GB";
            break;
        case 'h':
            if (c == 'e') region = "IL";
            else if (c == 'i') region = "IN";
            else if (c == 'r') region = "HT";
            else if (c == 'u') region = "HU";
            break;
        case 'i':
            if (c == 'd') region = "ID";
            else if (c == 's') region = "IS";
            else if (c == 't') region = "IT";
            else if (c == 'w') region = "IL";
            break;
        case 'j':
            if (c == 'a') region = "JP";
            break;
        case 'k':
            if (c == 'l') region = "GL";
            else if (c == 'o') region = "KR";
            else if (c == 'w') region = "GB";
            break;
        case 'l':
            if (c == 't') region = "LT";
            else if (c == 'v') region = "LV";
            break;
        case 'm':
            if (c == 'k') region = "MK";
            else if (c == 'l' || c == 'r') region = "IN";
            else if (c == 'n') region = "MN";
            else if (c == 's') region = "MY";
            else if (c == 't') region = "MT";
            break;
        case 'n':
            if (c == 'b' || c == 'n' || c == 'o') region = "NO";
            else if (c == 'e') region = "NP";
            else if (c == 'l') region = "NL";
            break;
        case 'o':
            if (c == 'm') region = "ET";
            break;
        case 'p':
            if (c == 'a') region = "IN";
            else if (c == 'l') region = "PL";
            else if (c == 't') region = "PT";
            break;
        case 'r':
            if (c == 'o') region = "RO";
            else if (c == 'u') region = "RU";
            break;
        case 's':
            switch (c) {
                case 'e': region = "NO"; break;
                case 'h': region = "YU"; break;
                case 'k': region = "SK"; break;
                case 'l': region = "SI"; break;
                case 'o': region = "ET"; break;
                case 'q': region = "AL"; break;
                case 't': region = "ZA"; break;
                case 'v': region = "SE"; break;
            }
            break;
        case 't':
            if (c == 'a' || c == 'e') region = "IN";
            else if (c == 'h') region = "TH";
            else if (c == 'i') region = "ER";
            else if (c == 'r') region = "TR";
            else if (c == 't') region = "RU";
            break;
        case 'u':
            if (c == 'k') region = "UA";
            else if (c == 'r') region = "PK";
            break;
        case 'v':
            if (c == 'i') region = "VN";
            break;
        case 'w':
            if (c == 'a') region = "BE";
            break;
        case 'x':
            if (c == 'h') region = "ZA";
            break;
        case 'z':
            if (c == 'h') region = "CN";
            else if (c == 'u') region = "ZA";
            break;
    }
    
    if (region == NULL)
        return(NULL);
    
    *q++ = region[0];
    *q++ = region[1];
    memcpy(q, ".utf8", 6);
    locale = newlocale(LC_COLLATE_MASK, localeName, NULL);
    
    return(locale);
#endif

#ifdef XSLT_LOCALE_MSVCRT
    const char *localeName = NULL;
    int c;
    
    /* We only look at the language and ignore the region. I think Windows
       doesn't care about the region for LC_COLLATE anyway. */
    
    if (languageTag == NULL ||
        !languageTag[0] ||
        !languageTag[1] ||
        languageTag[2] && languageTag[2] != '-')
    	return(NULL);
    
    c = TOLOWER(languageTag[1]);
    
    switch (TOLOWER(languageTag[0])) {
        case 'c':
            if (c == 's') localeName = "csy"; /* Czech */
            break;
        case 'd':
            if (c == 'a') localeName = "dan"; /* Danish */
            else if (c == 'e') localeName = "deu"; /* German */
            break;
        case 'e':
            if (c == 'l') localeName = "ell"; /* Greek */
            else if (c == 'n') localeName = "english";
            else if (c == 's') localeName = "esp"; /* Spanish */
            break;
        case 'f':
            if (c == 'i') localeName = "fin"; /* Finnish */
            else if (c == 'r') localeName = "fra"; /* French */
            break;
        case 'h':
            if (c == 'u') localeName = "hun"; /* Hungarian */
            break;
        case 'i':
            if (c == 's') localeName = "isl"; /* Icelandic */
            else if (c == 't') localeName = "ita"; /* Italian */
            break;
        case 'j':
            if (c == 'a') localeName = "jpn"; /* Japanese */
            break;
        case 'k':
            if (c == 'o') localeName = "kor"; /* Korean */
            break;
        case 'n':
            if (c == 'l') localeName = "nld"; /* Dutch */
            else if (c == 'o') localeName = "norwegian";
            break;
        case 'p':
            if (c == 'l') localeName = "plk"; /* Polish */
            else if (c == 't') localeName = "ptg"; /* Portuguese */
            break;
        case 'r':
            if (c == 'u') localeName = "rus"; /* Russian */
            break;
        case 's':
            if (c == 'k') localeName = "sky"; /* Slovak */
            else if (c == 'v') localeName = "sve"; /* Swedish */
            break;
        case 't':
            if (c == 'r') localeName = "trk"; /* Turkish */
            break;
        case 'z':
            if (c == 'h') localeName = "chinese";
            break;
    }
    
    if (localeName == NULL)
        return(NULL);

    return(_create_locale(LC_COLLATE, localeName));
#endif

#ifdef XSLT_LOCALE_NONE
    return(NULL);
#endif
}

/**
 * xsltFreeLocale:
 *
 * Frees a locale created with xsltNewLocale
 */
void
xsltFreeLocale(xsltLocale locale) {
#ifdef XSLT_LOCALE_XLOCALE
    freelocale(locale);
#endif

#ifdef XSLT_LOCALE_MSVCRT
    _free_locale(locale);
#endif
}

/**
 * xsltStrxfrm:
 * @locale: locale created with xsltNewLocale
 * @string: UTF-8 string to transform
 *
 * Transforms a string according to locale. The transformed string must then be
 * compared with xsltLocaleStrcmp and freed with xmlFree.
 *
 * Returns the transformed string or NULL on error
 */
xsltLocaleChar *
xsltStrxfrm(xsltLocale locale, const xmlChar *string)
{
#ifdef XSLT_LOCALE_NONE
    return(NULL);
#else
    size_t xstrlen, r;
    xsltLocaleChar *xstr;
    
#ifdef XSLT_LOCALE_XLOCALE
    xstrlen = strxfrm_l(NULL, (const char *)string, 0, locale) + 1;
    xstr = (xsltLocaleChar *) xmlMalloc(xstrlen);
    if (xstr == NULL) {
	xsltTransformError(NULL, NULL, NULL,
	    "xsltStrxfrm : out of memory error\n");
	return(NULL);
    }

    r = strxfrm_l((char *)xstr, (const char *)string, xstrlen, locale);
#endif

#ifdef XSLT_LOCALE_MSVCRT
    wchar_t *wcs;
    wchar_t dummy;
    int wcslen;
    int i, j;
    
    /* convert UTF8 to Windows wide chars (UTF16) */
    
    wcslen = xmlUTF8Strlen(string);
    if (wcslen < 0) {
	xsltTransformError(NULL, NULL, NULL,
	    "xsltStrxfrm : invalid UTF-8 string\n");
        return(NULL);
    }
    wcs = (wchar_t *) xmlMalloc(sizeof(wchar_t) * (wcslen + 1));
    if (wcs == NULL) {
	xsltTransformError(NULL, NULL, NULL,
	    "xsltStrxfrm : out of memory error\n");
	return(NULL);
    }

    for (i=0, j=0; i<wcslen; ++i) {
        int len = 4; /* not really, but string is already checked */
        int c = xmlGetUTF8Char(string, &len);
#if 0        
        if (c < 0) {
	    xsltTransformError(NULL, NULL, NULL,
	        "xsltStrxfrm : invalid UTF-8 string\n");
            xmlFree(wcs);
            return(NULL);
        }
#endif

        if (c == (wchar_t)c) {
            wcs[j] = (wchar_t)c;
            ++j;
        }
        
        string += len;
    }
    
    wcs[j] = 0;
    
    /* _wcsxfrm_l needs a dummy strDest because it always writes at least one
       terminating zero wchar */
    xstrlen = _wcsxfrm_l(&dummy, wcs, 0, locale);
    if (xstrlen == INT_MAX) {
	xsltTransformError(NULL, NULL, NULL, "xsltStrxfrm : strxfrm failed\n");
        xmlFree(wcs);
        return(NULL);
    }
    ++xstrlen;
    xstr = (wchar_t *) xmlMalloc(sizeof(wchar_t) * xstrlen);
    if (xstr == NULL) {
	xsltTransformError(NULL, NULL, NULL,
	    "xsltStrxfrm : out of memory error\n");
        xmlFree(wcs);
	return(NULL);
    }

    r = _wcsxfrm_l(xstr, wcs, xstrlen, locale);
    
    xmlFree(wcs);
#endif /* XSLT_LOCALE_MSVCRT */
    
    if (r >= xstrlen) {
	xsltTransformError(NULL, NULL, NULL, "xsltStrxfrm : strxfrm failed\n");
        xmlFree(xstr);
        return(NULL);
    }

    return(xstr);
#endif /* XSLT_LOCALE_NONE */
}

/**
 * xsltLocaleStrcmp:
 * @str1: a string transformed with xsltStrxfrm
 * @str2: a string transformed with xsltStrxfrm
 *
 * Compares two strings transformed with xsltStrxfrm
 *
 * Returns a value < 0 if str1 sorts before str2,
 *         a value > 0 if str1 sorts after str2,
 *         0 if str1 and str2 are equal wrt sorting
 */
int
xsltLocaleStrcmp(const xsltLocaleChar *str1, const xsltLocaleChar *str2) {
#ifdef XSLT_LOCALE_MSVCRT
    if (str1 == str2) return(0);
    if (str1 == NULL) return(-1);
    if (str2 == NULL) return(1);
    return(wcscmp(str1, str2));
#else
    return(xmlStrcmp(str1, str2));
#endif
}