summaryrefslogtreecommitdiffstats
path: root/src/util/support/utf8_conv.c
blob: b8bf98969fb61cfd9228f6c941654825c82d9871 (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
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* util/support/utf8_conv.c */
/*
 * Copyright 2008 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 *
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 */
/*
 * Copyright 1998-2008 The OpenLDAP Foundation.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* Copyright (C) 1999, 2000 Novell, Inc. All Rights Reserved.
 *
 * THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND
 * TREATIES. USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT
 * TO VERSION 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS
 * AVAILABLE AT HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE"
 * IN THE TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION
 * OF THIS WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP
 * PUBLIC LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM NOVELL, COULD SUBJECT
 * THE PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
 */

/* This work is part of OpenLDAP Software <http://www.openldap.org/>. */

/*
 * UTF-8 Conversion Routines
 *
 * These routines convert between Wide Character and UTF-8,
 * or between MultiByte and UTF-8 encodings.
 *
 * Both single character and string versions of the functions are provided.
 * All functions return -1 if the character or string cannot be converted.
 */

#include <assert.h>
#include "k5-platform.h"
#include "k5-utf8.h"
#include "supp-int.h"

static unsigned char mask[] = { 0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };

static ssize_t
k5_utf8s_to_ucs2s(krb5_ucs2 *ucs2str,
                  const char *utf8str,
                  size_t count,
                  int little_endian)
{
    size_t ucs2len = 0;
    size_t utflen, i;
    krb5_ucs2 ch;

    /* If input ptr is NULL or empty... */
    if (utf8str == NULL || *utf8str == '\0') {
        if (ucs2str != NULL)
            *ucs2str = 0;

        return 0;
    }

    /* Examine next UTF-8 character.  */
    while (*utf8str && ucs2len < count) {
        /* Get UTF-8 sequence length from 1st byte */
        utflen = KRB5_UTF8_CHARLEN2(utf8str, utflen);

        if (utflen == 0 || utflen > KRB5_MAX_UTF8_LEN)
            return -1;

        /* First byte minus length tag */
        ch = (krb5_ucs2)(utf8str[0] & mask[utflen]);

        for (i = 1; i < utflen; i++) {
            /* Subsequent bytes must start with 10 */
            if ((utf8str[i] & 0xc0) != 0x80)
                return -1;

            ch <<= 6;                   /* 6 bits of data in each subsequent byte */
            ch |= (krb5_ucs2)(utf8str[i] & 0x3f);
        }

        if (ucs2str != NULL) {
#ifdef K5_BE
#ifndef SWAP16
#define SWAP16(X)       ((((X) << 8) | ((X) >> 8)) & 0xFFFF)
#endif
            if (little_endian)
                ucs2str[ucs2len] = SWAP16(ch);
            else
#endif
                ucs2str[ucs2len] = ch;
        }

        utf8str += utflen;      /* Move to next UTF-8 character */
        ucs2len++;              /* Count number of wide chars stored/required */
    }

    if (ucs2str != NULL && ucs2len < count) {
        /* Add null terminator if there's room in the buffer. */
        ucs2str[ucs2len] = 0;
    }

    return ucs2len;
}

int
krb5int_utf8s_to_ucs2s(const char *utf8s,
                       krb5_ucs2 **ucs2s,
                       size_t *ucs2chars)
{
    ssize_t len;
    size_t chars;

    chars = krb5int_utf8_chars(utf8s);
    *ucs2s = (krb5_ucs2 *)malloc((chars + 1) * sizeof(krb5_ucs2));
    if (*ucs2s == NULL) {
        return ENOMEM;
    }

    len = k5_utf8s_to_ucs2s(*ucs2s, utf8s, chars + 1, 0);
    if (len < 0) {
        free(*ucs2s);
        *ucs2s = NULL;
        return EINVAL;
    }

    if (ucs2chars != NULL) {
        *ucs2chars = chars;
    }

    return 0;
}

int
krb5int_utf8cs_to_ucs2s(const char *utf8s,
                        size_t utf8slen,
                        krb5_ucs2 **ucs2s,
                        size_t *ucs2chars)
{
    ssize_t len;
    size_t chars;

    chars = krb5int_utf8c_chars(utf8s, utf8slen);
    *ucs2s = (krb5_ucs2 *)malloc((chars + 1) * sizeof(krb5_ucs2));
    if (*ucs2s == NULL) {
        return ENOMEM;
    }

    len = k5_utf8s_to_ucs2s(*ucs2s, utf8s, chars, 0);
    if (len < 0) {
        free(*ucs2s);
        *ucs2s = NULL;
        return EINVAL;
    }
    (*ucs2s)[chars] = 0;

    if (ucs2chars != NULL) {
        *ucs2chars = chars;
    }

    return 0;
}

int
krb5int_utf8s_to_ucs2les(const char *utf8s,
                         unsigned char **ucs2les,
                         size_t *ucs2leslen)
{
    ssize_t len;
    size_t chars;

    chars = krb5int_utf8_chars(utf8s);

    *ucs2les = (unsigned char *)malloc((chars + 1) * sizeof(krb5_ucs2));
    if (*ucs2les == NULL) {
        return ENOMEM;
    }

    len = k5_utf8s_to_ucs2s((krb5_ucs2 *)*ucs2les, utf8s, chars + 1, 1);
    if (len < 0) {
        free(*ucs2les);
        *ucs2les = NULL;
        return EINVAL;
    }

    if (ucs2leslen != NULL) {
        *ucs2leslen = chars * sizeof(krb5_ucs2);
    }

    return 0;
}

int
krb5int_utf8cs_to_ucs2les(const char *utf8s,
                          size_t utf8slen,
                          unsigned char **ucs2les,
                          size_t *ucs2leslen)
{
    ssize_t len;
    size_t chars;
    krb5_ucs2 *ucs2s;

    *ucs2les = NULL;

    chars = krb5int_utf8c_chars(utf8s, utf8slen);
    ucs2s = malloc((chars + 1) * sizeof(krb5_ucs2));
    if (ucs2s == NULL)
        return ENOMEM;

    len = k5_utf8s_to_ucs2s(ucs2s, utf8s, chars, 1);
    if (len < 0) {
        free(ucs2s);
        return EINVAL;
    }
    ucs2s[chars] = 0;

    *ucs2les = (unsigned char *)ucs2s;
    if (ucs2leslen != NULL) {
        *ucs2leslen = chars * sizeof(krb5_ucs2);
    }

    return 0;
}

/*-----------------------------------------------------------------------------
  Convert a wide char string to a UTF-8 string.
  No more than 'count' bytes will be written to the output buffer.
  Return the # of bytes written to the output buffer, excl null terminator.

  ucs2len is -1 if the UCS-2 string is NUL terminated, otherwise it is the
  length of the UCS-2 string in characters
*/
static ssize_t
k5_ucs2s_to_utf8s(char *utf8str, const krb5_ucs2 *ucs2str,
                  size_t count, ssize_t ucs2len, int little_endian)
{
    int len = 0;
    int n;
    char *p = utf8str;
    krb5_ucs2 empty = 0, ch;

    if (ucs2str == NULL)        /* Treat input ptr NULL as an empty string */
        ucs2str = &empty;

    if (utf8str == NULL)        /* Just compute size of output, excl null */
    {
        while (ucs2len == -1 ? *ucs2str : --ucs2len >= 0) {
            /* Get UTF-8 size of next wide char */
            ch = *ucs2str++;
#ifdef K5_BE
            if (little_endian)
                ch = SWAP16(ch);
#endif

            n = krb5int_ucs2_to_utf8(ch, NULL);
            if (n < 1 || n > INT_MAX - len)
                return -1;
            len += n;
        }

        return len;
    }

    /* Do the actual conversion. */

    n = 1;                                      /* In case of empty ucs2str */
    while (ucs2len == -1 ? *ucs2str != 0 : --ucs2len >= 0) {
        ch = *ucs2str++;
#ifdef K5_BE
        if (little_endian)
            ch = SWAP16(ch);
#endif

        n = krb5int_ucs2_to_utf8(ch, p);

        if (n < 1)
            break;

        p += n;
        count -= n;                     /* Space left in output buffer */
    }

    /* If not enough room for last character, pad remainder with null
       so that return value = original count, indicating buffer full. */
    if (n == 0) {
        while (count--)
            *p++ = 0;
    }
    /* Add a null terminator if there's room. */
    else if (count)
        *p = 0;

    if (n == -1)                        /* Conversion encountered invalid wide char. */
        return -1;

    /* Return the number of bytes written to output buffer, excl null. */
    return (p - utf8str);
}

int
krb5int_ucs2s_to_utf8s(const krb5_ucs2 *ucs2s,
                       char **utf8s,
                       size_t *utf8slen)
{
    ssize_t len;

    len = k5_ucs2s_to_utf8s(NULL, ucs2s, 0, -1, 0);
    if (len < 0) {
        return EINVAL;
    }

    *utf8s = (char *)malloc((size_t)len + 1);
    if (*utf8s == NULL) {
        return ENOMEM;
    }

    len = k5_ucs2s_to_utf8s(*utf8s, ucs2s, (size_t)len + 1, -1, 0);
    if (len < 0) {
        free(*utf8s);
        *utf8s = NULL;
        return EINVAL;
    }

    if (utf8slen != NULL) {
        *utf8slen = len;
    }

    return 0;
}

int
krb5int_ucs2les_to_utf8s(const unsigned char *ucs2les,
                         char **utf8s,
                         size_t *utf8slen)
{
    ssize_t len;

    len = k5_ucs2s_to_utf8s(NULL, (krb5_ucs2 *)ucs2les, 0, -1, 1);
    if (len < 0)
        return EINVAL;

    *utf8s = (char *)malloc((size_t)len + 1);
    if (*utf8s == NULL) {
        return ENOMEM;
    }

    len = k5_ucs2s_to_utf8s(*utf8s, (krb5_ucs2 *)ucs2les, (size_t)len + 1, -1, 1);
    if (len < 0) {
        free(*utf8s);
        *utf8s = NULL;
        return EINVAL;
    }

    if (utf8slen != NULL) {
        *utf8slen = len;
    }

    return 0;
}

int
krb5int_ucs2cs_to_utf8s(const krb5_ucs2 *ucs2s,
                        size_t ucs2slen,
                        char **utf8s,
                        size_t *utf8slen)
{
    ssize_t len;

    if (ucs2slen > SSIZE_MAX)
        return ERANGE;

    len = k5_ucs2s_to_utf8s(NULL, (krb5_ucs2 *)ucs2s, 0,
                            (ssize_t)ucs2slen, 0);
    if (len < 0)
        return EINVAL;

    *utf8s = (char *)malloc((size_t)len + 1);
    if (*utf8s == NULL) {
        return ENOMEM;
    }

    len = k5_ucs2s_to_utf8s(*utf8s, (krb5_ucs2 *)ucs2s, (size_t)len,
                            (ssize_t)ucs2slen, 0);
    if (len < 0) {
        free(*utf8s);
        *utf8s = NULL;
        return EINVAL;
    }
    (*utf8s)[len] = '\0';

    if (utf8slen != NULL) {
        *utf8slen = len;
    }

    return 0;
}

int
krb5int_ucs2lecs_to_utf8s(const unsigned char *ucs2les,
                          size_t ucs2leslen,
                          char **utf8s,
                          size_t *utf8slen)
{
    ssize_t len;

    if (ucs2leslen > SSIZE_MAX)
        return ERANGE;

    len = k5_ucs2s_to_utf8s(NULL, (krb5_ucs2 *)ucs2les, 0,
                            (ssize_t)ucs2leslen, 1);
    if (len < 0)
        return EINVAL;

    *utf8s = (char *)malloc((size_t)len + 1);
    if (*utf8s == NULL) {
        return ENOMEM;
    }

    len = k5_ucs2s_to_utf8s(*utf8s, (krb5_ucs2 *)ucs2les, (size_t)len,
                            (ssize_t)ucs2leslen, 1);
    if (len < 0) {
        free(*utf8s);
        *utf8s = NULL;
        return EINVAL;
    }
    (*utf8s)[len] = '\0';

    if (utf8slen != NULL) {
        *utf8slen = len;
    }

    return 0;
}