Coverage Report

Created: 2025-07-18 06:26

/src/glib/glib/gutf8.c
Line
Count
Source (jump to first uncovered line)
1
/* gutf8.c - Operations on UTF-8 strings.
2
 *
3
 * Copyright (C) 1999 Tom Tromey
4
 * Copyright (C) 2000 Red Hat, Inc.
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
#include "config.h"
21
22
#include <stdlib.h>
23
#ifdef HAVE_CODESET
24
#include <langinfo.h>
25
#endif
26
#include <string.h>
27
28
#ifdef G_PLATFORM_WIN32
29
#include <stdio.h>
30
#define STRICT
31
#include <windows.h>
32
#undef STRICT
33
#endif
34
35
#include "gconvert.h"
36
#include "ghash.h"
37
#include "gstrfuncs.h"
38
#include "gtestutils.h"
39
#include "gtypes.h"
40
#include "gthread.h"
41
#include "glibintl.h"
42
43
#define UTF8_COMPUTE(Char, Mask, Len)               \
44
185k
  if (Char < 128)                   \
45
185k
    {                       \
46
18.7k
      Len = 1;                      \
47
18.7k
      Mask = 0x7f;                    \
48
18.7k
    }                        \
49
185k
  else if ((Char & 0xe0) == 0xc0)               \
50
167k
    {                       \
51
3.29k
      Len = 2;                      \
52
3.29k
      Mask = 0x1f;                    \
53
3.29k
    }                        \
54
167k
  else if ((Char & 0xf0) == 0xe0)               \
55
163k
    {                       \
56
163k
      Len = 3;                      \
57
163k
      Mask = 0x0f;                    \
58
163k
    }                        \
59
163k
  else if ((Char & 0xf8) == 0xf0)               \
60
656
    {                       \
61
656
      Len = 4;                      \
62
656
      Mask = 0x07;                    \
63
656
    }                        \
64
656
  else if ((Char & 0xfc) == 0xf8)               \
65
0
    {                       \
66
0
      Len = 5;                      \
67
0
      Mask = 0x03;                    \
68
0
    }                        \
69
0
  else if ((Char & 0xfe) == 0xfc)               \
70
0
    {                       \
71
0
      Len = 6;                      \
72
0
      Mask = 0x01;                    \
73
0
    }                        \
74
0
  else                        \
75
0
    Len = -1;
76
77
#define UTF8_LENGTH(Char)              \
78
51.1M
  ((Char) < 0x80 ? 1 :                 \
79
51.1M
   ((Char) < 0x800 ? 2 :               \
80
51.0M
    ((Char) < 0x10000 ? 3 :            \
81
50.9M
     ((Char) < 0x200000 ? 4 :          \
82
1.68k
      ((Char) < 0x4000000 ? 5 : 6)))))
83
   
84
85
#define UTF8_GET(Result, Chars, Count, Mask, Len)           \
86
185k
  (Result) = (Chars)[0] & (Mask);               \
87
517k
  for ((Count) = 1; (Count) < (Len); ++(Count))             \
88
331k
    {                       \
89
331k
      if (((Chars)[(Count)] & 0xc0) != 0x80)             \
90
331k
  {                     \
91
0
    (Result) = -1;                  \
92
0
    break;                    \
93
0
  }                      \
94
331k
      (Result) <<= 6;                   \
95
331k
      (Result) |= ((Chars)[(Count)] & 0x3f);              \
96
331k
    }
97
    
98
/*
99
 * Check whether a Unicode (5.2) char is in a valid range.
100
 *
101
 * The first check comes from the Unicode guarantee to never encode
102
 * a point above 0x0010ffff, since UTF-16 couldn't represent it.
103
 * 
104
 * The second check covers surrogate pairs (category Cs).
105
 *
106
 * @param Char the character
107
 */
108
#define UNICODE_VALID(Char)                   \
109
2.07M
    ((Char) < 0x110000 &&                     \
110
2.07M
     (((Char) & 0xFFFFF800) != 0xD800))
111
112
    
113
static const gchar utf8_skip_data[256] = {
114
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
115
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
116
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
117
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
118
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
119
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
120
  2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
121
  3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1
122
};
123
124
const gchar * const g_utf8_skip = utf8_skip_data;
125
126
/**
127
 * g_utf8_find_prev_char:
128
 * @str: pointer to the beginning of a UTF-8 encoded string
129
 * @p: pointer to some position within @str
130
 * 
131
 * Given a position @p with a UTF-8 encoded string @str, find the start
132
 * of the previous UTF-8 character starting before @p. Returns %NULL if no
133
 * UTF-8 characters are present in @str before @p.
134
 *
135
 * @p does not have to be at the beginning of a UTF-8 character. No check
136
 * is made to see if the character found is actually valid other than
137
 * it starts with an appropriate byte.
138
 *
139
 * Returns: (transfer none) (nullable): a pointer to the found character or %NULL.
140
 */
141
gchar *
142
g_utf8_find_prev_char (const gchar *str,
143
           const gchar *p)
144
0
{
145
0
  while (p > str)
146
0
    {
147
0
      --p;
148
0
      if ((*p & 0xc0) != 0x80)
149
0
  return (gchar *)p;
150
0
    }
151
0
  return NULL;
152
0
}
153
154
/**
155
 * g_utf8_find_next_char:
156
 * @p: a pointer to a position within a UTF-8 encoded string
157
 * @end: (nullable): a pointer to the byte following the end of the string,
158
 *     or %NULL to indicate that the string is nul-terminated
159
 *
160
 * Finds the start of the next UTF-8 character in the string after @p.
161
 *
162
 * @p does not have to be at the beginning of a UTF-8 character. No check
163
 * is made to see if the character found is actually valid other than
164
 * it starts with an appropriate byte.
165
 * 
166
 * If @end is %NULL, the return value will never be %NULL: if the end of the
167
 * string is reached, a pointer to the terminating nul byte is returned. If
168
 * @end is non-%NULL, the return value will be %NULL if the end of the string
169
 * is reached.
170
 *
171
 * Returns: (transfer none) (nullable): a pointer to the found character or %NULL if @end is
172
 *    set and is reached
173
 */
174
gchar *
175
g_utf8_find_next_char (const gchar *p,
176
           const gchar *end)
177
0
{
178
0
  if (end)
179
0
    {
180
0
      for (++p; p < end && (*p & 0xc0) == 0x80; ++p)
181
0
        ;
182
0
      return (p >= end) ? NULL : (gchar *)p;
183
0
    }
184
0
  else
185
0
    {
186
0
      for (++p; (*p & 0xc0) == 0x80; ++p)
187
0
        ;
188
0
      return (gchar *)p;
189
0
    }
190
0
}
191
192
/**
193
 * g_utf8_prev_char:
194
 * @p: a pointer to a position within a UTF-8 encoded string
195
 *
196
 * Finds the previous UTF-8 character in the string before @p.
197
 *
198
 * @p does not have to be at the beginning of a UTF-8 character. No check
199
 * is made to see if the character found is actually valid other than
200
 * it starts with an appropriate byte. If @p might be the first
201
 * character of the string, you must use g_utf8_find_prev_char() instead.
202
 * 
203
 * Returns: (transfer none) (not nullable): a pointer to the found character
204
 */
205
gchar *
206
g_utf8_prev_char (const gchar *p)
207
0
{
208
0
  while (TRUE)
209
0
    {
210
0
      p--;
211
0
      if ((*p & 0xc0) != 0x80)
212
0
  return (gchar *)p;
213
0
    }
214
0
}
215
 
216
/**
217
 * g_utf8_strlen:
218
 * @p: pointer to the start of a UTF-8 encoded string
219
 * @max: the maximum number of bytes to examine. If @max
220
 *       is less than 0, then the string is assumed to be
221
 *       nul-terminated. If @max is 0, @p will not be examined and
222
 *       may be %NULL. If @max is greater than 0, up to @max
223
 *       bytes are examined
224
 *
225
 * Computes the length of the string in characters, not including
226
 * the terminating nul character. If the @max'th byte falls in the
227
 * middle of a character, the last (partial) character is not counted.
228
 *
229
 * Returns: the length of the string in characters
230
 */
231
glong
232
g_utf8_strlen (const gchar *p,
233
               gssize       max)
234
0
{
235
0
  glong len = 0;
236
0
  const gchar *start = p;
237
0
  g_return_val_if_fail (p != NULL || max == 0, 0);
238
239
0
  if (max < 0)
240
0
    {
241
0
      while (*p)
242
0
        {
243
0
          p = g_utf8_next_char (p);
244
0
          ++len;
245
0
        }
246
0
    }
247
0
  else
248
0
    {
249
0
      if (max == 0 || !*p)
250
0
        return 0;
251
252
0
      p = g_utf8_next_char (p);
253
254
0
      while (p - start < max && *p)
255
0
        {
256
0
          ++len;
257
0
          p = g_utf8_next_char (p);
258
0
        }
259
260
      /* only do the last len increment if we got a complete
261
       * char (don't count partial chars)
262
       */
263
0
      if (p - start <= max)
264
0
        ++len;
265
0
    }
266
267
0
  return len;
268
0
}
269
270
/**
271
 * g_utf8_substring:
272
 * @str: a UTF-8 encoded string
273
 * @start_pos: a character offset within @str
274
 * @end_pos: another character offset within @str
275
 *
276
 * Copies a substring out of a UTF-8 encoded string.
277
 * The substring will contain @end_pos - @start_pos characters.
278
 *
279
 * Returns: (transfer full): a newly allocated copy of the requested
280
 *     substring. Free with g_free() when no longer needed.
281
 *
282
 * Since: 2.30
283
 */
284
gchar *
285
g_utf8_substring (const gchar *str,
286
                  glong        start_pos,
287
                  glong        end_pos)
288
0
{
289
0
  gchar *start, *end, *out;
290
291
0
  start = g_utf8_offset_to_pointer (str, start_pos);
292
0
  end = g_utf8_offset_to_pointer (start, end_pos - start_pos);
293
294
0
  out = g_malloc (end - start + 1);
295
0
  memcpy (out, start, end - start);
296
0
  out[end - start] = 0;
297
298
0
  return out;
299
0
}
300
301
/**
302
 * g_utf8_get_char:
303
 * @p: a pointer to Unicode character encoded as UTF-8
304
 * 
305
 * Converts a sequence of bytes encoded as UTF-8 to a Unicode character.
306
 *
307
 * If @p does not point to a valid UTF-8 encoded character, results
308
 * are undefined. If you are not sure that the bytes are complete
309
 * valid Unicode characters, you should use g_utf8_get_char_validated()
310
 * instead.
311
 * 
312
 * Returns: the resulting character
313
 */
314
gunichar
315
g_utf8_get_char (const gchar *p)
316
185k
{
317
185k
  int i, mask = 0, len;
318
185k
  gunichar result;
319
185k
  unsigned char c = (unsigned char) *p;
320
321
185k
  UTF8_COMPUTE (c, mask, len);
322
185k
  if (len == -1)
323
0
    return (gunichar)-1;
324
185k
  UTF8_GET (result, p, i, mask, len);
325
326
185k
  return result;
327
185k
}
328
329
/**
330
 * g_utf8_offset_to_pointer:
331
 * @str: a UTF-8 encoded string
332
 * @offset: a character offset within @str
333
 *
334
 * Converts from an integer character offset to a pointer to a position
335
 * within the string.
336
 *
337
 * Since 2.10, this function allows to pass a negative @offset to
338
 * step backwards. It is usually worth stepping backwards from the end
339
 * instead of forwards if @offset is in the last fourth of the string,
340
 * since moving forward is about 3 times faster than moving backward.
341
 *
342
 * Note that this function doesn't abort when reaching the end of @str.
343
 * Therefore you should be sure that @offset is within string boundaries
344
 * before calling that function. Call g_utf8_strlen() when unsure.
345
 * This limitation exists as this function is called frequently during
346
 * text rendering and therefore has to be as fast as possible.
347
 *
348
 * Returns: (transfer none): the resulting pointer
349
 */
350
gchar *
351
g_utf8_offset_to_pointer  (const gchar *str,
352
         glong        offset)
353
0
{
354
0
  const gchar *s = str;
355
356
0
  if (offset > 0) 
357
0
    while (offset--)
358
0
      s = g_utf8_next_char (s);
359
0
  else
360
0
    {
361
0
      const char *s1;
362
363
      /* This nice technique for fast backwards stepping 
364
       * through a UTF-8 string was dubbed "stutter stepping" 
365
       * by its inventor, Larry Ewing.
366
       */
367
0
      while (offset)
368
0
  {
369
0
    s1 = s;
370
0
    s += offset;
371
0
    while ((*s & 0xc0) == 0x80)
372
0
      s--;
373
374
0
    offset += g_utf8_pointer_to_offset (s, s1);
375
0
  }
376
0
    }
377
378
0
  return (gchar *)s;
379
0
}
380
381
/**
382
 * g_utf8_pointer_to_offset:
383
 * @str: a UTF-8 encoded string
384
 * @pos: a pointer to a position within @str
385
 * 
386
 * Converts from a pointer to position within a string to an integer
387
 * character offset.
388
 *
389
 * Since 2.10, this function allows @pos to be before @str, and returns
390
 * a negative offset in this case.
391
 * 
392
 * Returns: the resulting character offset
393
 */
394
glong    
395
g_utf8_pointer_to_offset (const gchar *str,
396
        const gchar *pos)
397
0
{
398
0
  const gchar *s = str;
399
0
  glong offset = 0;    
400
401
0
  if (pos < str) 
402
0
    offset = - g_utf8_pointer_to_offset (pos, str);
403
0
  else
404
0
    while (s < pos)
405
0
      {
406
0
  s = g_utf8_next_char (s);
407
0
  offset++;
408
0
      }
409
  
410
0
  return offset;
411
0
}
412
413
414
/**
415
 * g_utf8_strncpy:
416
 * @dest: (transfer none): buffer to fill with characters from @src
417
 * @src: UTF-8 encoded string
418
 * @n: character count
419
 * 
420
 * Like the standard C strncpy() function, but copies a given number
421
 * of characters instead of a given number of bytes. The @src string
422
 * must be valid UTF-8 encoded text. (Use g_utf8_validate() on all
423
 * text before trying to use UTF-8 utility functions with it.)
424
 * 
425
 * Note you must ensure @dest is at least 4 * @n to fit the
426
 * largest possible UTF-8 characters
427
 *
428
 * Returns: (transfer none): @dest
429
 */
430
gchar *
431
g_utf8_strncpy (gchar       *dest,
432
    const gchar *src,
433
    gsize        n)
434
0
{
435
0
  const gchar *s = src;
436
0
  while (n && *s)
437
0
    {
438
0
      s = g_utf8_next_char(s);
439
0
      n--;
440
0
    }
441
0
  strncpy(dest, src, s - src);
442
0
  dest[s - src] = 0;
443
0
  return dest;
444
0
}
445
446
/* unicode_strchr */
447
448
/**
449
 * g_unichar_to_utf8:
450
 * @c: a Unicode character code
451
 * @outbuf: (out caller-allocates) (optional): output buffer, must have at
452
 *       least 6 bytes of space. If %NULL, the length will be computed and
453
 *       returned and nothing will be written to @outbuf.
454
 * 
455
 * Converts a single character to UTF-8.
456
 * 
457
 * Returns: number of bytes written
458
 */
459
int
460
g_unichar_to_utf8 (gunichar c,
461
       gchar   *outbuf)
462
51.1M
{
463
  /* If this gets modified, also update the copy in g_string_insert_unichar() */
464
51.1M
  guint len = 0;    
465
51.1M
  int first;
466
51.1M
  int i;
467
468
51.1M
  if (c < 0x80)
469
27.6k
    {
470
27.6k
      first = 0;
471
27.6k
      len = 1;
472
27.6k
    }
473
51.0M
  else if (c < 0x800)
474
101k
    {
475
101k
      first = 0xc0;
476
101k
      len = 2;
477
101k
    }
478
50.9M
  else if (c < 0x10000)
479
50.9M
    {
480
50.9M
      first = 0xe0;
481
50.9M
      len = 3;
482
50.9M
    }
483
1.32k
   else if (c < 0x200000)
484
1.32k
    {
485
1.32k
      first = 0xf0;
486
1.32k
      len = 4;
487
1.32k
    }
488
0
  else if (c < 0x4000000)
489
0
    {
490
0
      first = 0xf8;
491
0
      len = 5;
492
0
    }
493
0
  else
494
0
    {
495
0
      first = 0xfc;
496
0
      len = 6;
497
0
    }
498
499
51.1M
  if (outbuf)
500
51.1M
    {
501
153M
      for (i = len - 1; i > 0; --i)
502
102M
  {
503
102M
    outbuf[i] = (c & 0x3f) | 0x80;
504
102M
    c >>= 6;
505
102M
  }
506
51.1M
      outbuf[0] = c | first;
507
51.1M
    }
508
509
51.1M
  return len;
510
51.1M
}
511
512
/**
513
 * g_utf8_strchr:
514
 * @p: a nul-terminated UTF-8 encoded string
515
 * @len: the maximum length of @p
516
 * @c: a Unicode character
517
 * 
518
 * Finds the leftmost occurrence of the given Unicode character
519
 * in a UTF-8 encoded string, while limiting the search to @len bytes.
520
 * If @len is -1, allow unbounded search.
521
 * 
522
 * Returns: (transfer none) (nullable): %NULL if the string does not contain the character,
523
 *     otherwise, a pointer to the start of the leftmost occurrence
524
 *     of the character in the string.
525
 */
526
gchar *
527
g_utf8_strchr (const char *p,
528
         gssize      len,
529
         gunichar    c)
530
0
{
531
0
  gchar ch[10];
532
533
0
  gint charlen = g_unichar_to_utf8 (c, ch);
534
0
  ch[charlen] = '\0';
535
  
536
0
  return g_strstr_len (p, len, ch);
537
0
}
538
539
540
/**
541
 * g_utf8_strrchr:
542
 * @p: a nul-terminated UTF-8 encoded string
543
 * @len: the maximum length of @p
544
 * @c: a Unicode character
545
 * 
546
 * Find the rightmost occurrence of the given Unicode character
547
 * in a UTF-8 encoded string, while limiting the search to @len bytes.
548
 * If @len is -1, allow unbounded search.
549
 * 
550
 * Returns: (transfer none) (nullable): %NULL if the string does not contain the character,
551
 *     otherwise, a pointer to the start of the rightmost occurrence
552
 *     of the character in the string.
553
 */
554
gchar *
555
g_utf8_strrchr (const char *p,
556
    gssize      len,
557
    gunichar    c)
558
0
{
559
0
  gchar ch[10];
560
561
0
  gint charlen = g_unichar_to_utf8 (c, ch);
562
0
  ch[charlen] = '\0';
563
  
564
0
  return g_strrstr_len (p, len, ch);
565
0
}
566
567
568
/* Like g_utf8_get_char, but take a maximum length
569
 * and return (gunichar)-2 on incomplete trailing character;
570
 * also check for malformed or overlong sequences
571
 * and return (gunichar)-1 in this case.
572
 */
573
static inline gunichar
574
g_utf8_get_char_extended (const  gchar *p,
575
        gssize max_len)
576
2.25M
{
577
2.25M
  guint i, len;
578
2.25M
  gunichar min_code;
579
2.25M
  gunichar wc = (guchar) *p;
580
2.25M
  const gunichar partial_sequence = (gunichar) -2;
581
2.25M
  const gunichar malformed_sequence = (gunichar) -1;
582
583
2.25M
  if (wc < 0x80)
584
2.08M
    {
585
2.08M
      return wc;
586
2.08M
    }
587
167k
  else if (G_UNLIKELY (wc < 0xc0))
588
0
    {
589
0
      return malformed_sequence;
590
0
    }
591
167k
  else if (wc < 0xe0)
592
3.29k
    {
593
3.29k
      len = 2;
594
3.29k
      wc &= 0x1f;
595
3.29k
      min_code = 1 << 7;
596
3.29k
    }
597
163k
  else if (wc < 0xf0)
598
163k
    {
599
163k
      len = 3;
600
163k
      wc &= 0x0f;
601
163k
      min_code = 1 << 11;
602
163k
    }
603
656
  else if (wc < 0xf8)
604
656
    {
605
656
      len = 4;
606
656
      wc &= 0x07;
607
656
      min_code = 1 << 16;
608
656
    }
609
0
  else if (wc < 0xfc)
610
0
    {
611
0
      len = 5;
612
0
      wc &= 0x03;
613
0
      min_code = 1 << 21;
614
0
    }
615
0
  else if (wc < 0xfe)
616
0
    {
617
0
      len = 6;
618
0
      wc &= 0x01;
619
0
      min_code = 1 << 26;
620
0
    }
621
0
  else
622
0
    {
623
0
      return malformed_sequence;
624
0
    }
625
626
167k
  if (G_UNLIKELY (max_len >= 0 && len > max_len))
627
0
    {
628
0
      for (i = 1; i < max_len; i++)
629
0
  {
630
0
    if ((((guchar *)p)[i] & 0xc0) != 0x80)
631
0
      return malformed_sequence;
632
0
  }
633
0
      return partial_sequence;
634
0
    }
635
636
498k
  for (i = 1; i < len; ++i)
637
331k
    {
638
331k
      gunichar ch = ((guchar *)p)[i];
639
640
331k
      if (G_UNLIKELY ((ch & 0xc0) != 0x80))
641
0
  {
642
0
    if (ch)
643
0
      return malformed_sequence;
644
0
    else
645
0
      return partial_sequence;
646
0
  }
647
648
331k
      wc <<= 6;
649
331k
      wc |= (ch & 0x3f);
650
331k
    }
651
652
167k
  if (G_UNLIKELY (wc < min_code))
653
0
    return malformed_sequence;
654
655
167k
  return wc;
656
167k
}
657
658
/**
659
 * g_utf8_get_char_validated:
660
 * @p: a pointer to Unicode character encoded as UTF-8
661
 * @max_len: the maximum number of bytes to read, or -1 if @p is nul-terminated
662
 *
663
 * Convert a sequence of bytes encoded as UTF-8 to a Unicode character.
664
 * This function checks for incomplete characters, for invalid characters
665
 * such as characters that are out of the range of Unicode, and for
666
 * overlong encodings of valid characters.
667
 *
668
 * Note that g_utf8_get_char_validated() returns (gunichar)-2 if
669
 * @max_len is positive and any of the bytes in the first UTF-8 character
670
 * sequence are nul.
671
 * 
672
 * Returns: the resulting character. If @p points to a partial
673
 *     sequence at the end of a string that could begin a valid 
674
 *     character (or if @max_len is zero), returns (gunichar)-2; 
675
 *     otherwise, if @p does not point to a valid UTF-8 encoded 
676
 *     Unicode character, returns (gunichar)-1.
677
 */
678
gunichar
679
g_utf8_get_char_validated (const gchar *p,
680
         gssize       max_len)
681
2.07M
{
682
2.07M
  gunichar result;
683
684
2.07M
  if (max_len == 0)
685
0
    return (gunichar)-2;
686
687
2.07M
  result = g_utf8_get_char_extended (p, max_len);
688
689
  /* Disallow codepoint U+0000 as it’s a nul byte,
690
   * and all string handling in GLib is nul-terminated */
691
2.07M
  if (result == 0 && max_len > 0)
692
0
    return (gunichar) -2;
693
694
2.07M
  if (result & 0x80000000)
695
0
    return result;
696
2.07M
  else if (!UNICODE_VALID (result))
697
0
    return (gunichar)-1;
698
2.07M
  else
699
2.07M
    return result;
700
2.07M
}
701
702
0
#define CONT_BYTE_FAST(p) ((guchar)*p++ & 0x3f)
703
704
/**
705
 * g_utf8_to_ucs4_fast:
706
 * @str: a UTF-8 encoded string
707
 * @len: the maximum length of @str to use, in bytes. If @len < 0,
708
 *     then the string is nul-terminated.
709
 * @items_written: (out caller-allocates) (optional): location to store the
710
 *     number of characters in the result, or %NULL.
711
 *
712
 * Convert a string from UTF-8 to a 32-bit fixed width
713
 * representation as UCS-4, assuming valid UTF-8 input.
714
 * This function is roughly twice as fast as g_utf8_to_ucs4()
715
 * but does no error checking on the input. A trailing 0 character
716
 * will be added to the string after the converted text.
717
 * 
718
 * Returns: (transfer full): a pointer to a newly allocated UCS-4 string.
719
 *     This value must be freed with g_free().
720
 */
721
gunichar *
722
g_utf8_to_ucs4_fast (const gchar *str,
723
         glong        len,              
724
         glong       *items_written)    
725
0
{
726
0
  gunichar *result;
727
0
  gint n_chars, i;
728
0
  const gchar *p;
729
730
0
  g_return_val_if_fail (str != NULL, NULL);
731
732
0
  p = str;
733
0
  n_chars = 0;
734
0
  if (len < 0)
735
0
    {
736
0
      while (*p)
737
0
  {
738
0
    p = g_utf8_next_char (p);
739
0
    ++n_chars;
740
0
  }
741
0
    }
742
0
  else
743
0
    {
744
0
      while (p < str + len && *p)
745
0
  {
746
0
    p = g_utf8_next_char (p);
747
0
    ++n_chars;
748
0
  }
749
0
    }
750
  
751
0
  result = g_new (gunichar, n_chars + 1);
752
  
753
0
  p = str;
754
0
  for (i=0; i < n_chars; i++)
755
0
    {
756
0
      guchar first = (guchar)*p++;
757
0
      gunichar wc;
758
759
0
      if (first < 0xc0)
760
0
  {
761
          /* We really hope first < 0x80, but we don't want to test an
762
           * extra branch for invalid input, which this function
763
           * does not care about. Handling unexpected continuation bytes
764
           * here will do the least damage. */
765
0
    wc = first;
766
0
  }
767
0
      else
768
0
  {
769
0
          gunichar c1 = CONT_BYTE_FAST(p);
770
0
          if (first < 0xe0)
771
0
            {
772
0
              wc = ((first & 0x1f) << 6) | c1;
773
0
            }
774
0
          else
775
0
            {
776
0
              gunichar c2 = CONT_BYTE_FAST(p);
777
0
              if (first < 0xf0)
778
0
                {
779
0
                  wc = ((first & 0x0f) << 12) | (c1 << 6) | c2;
780
0
                }
781
0
              else
782
0
                {
783
0
                  gunichar c3 = CONT_BYTE_FAST(p);
784
0
                  wc = ((first & 0x07) << 18) | (c1 << 12) | (c2 << 6) | c3;
785
0
                  if (G_UNLIKELY (first >= 0xf8))
786
0
                    {
787
                      /* This can't be valid UTF-8, but g_utf8_next_char()
788
                       * and company allow out-of-range sequences */
789
0
                      gunichar mask = 1 << 20;
790
0
                      while ((wc & mask) != 0)
791
0
                        {
792
0
                          wc <<= 6;
793
0
                          wc |= CONT_BYTE_FAST(p);
794
0
                          mask <<= 5;
795
0
                        }
796
0
                      wc &= mask - 1;
797
0
                    }
798
0
                }
799
0
            }
800
0
  }
801
0
      result[i] = wc;
802
0
    }
803
0
  result[i] = 0;
804
805
0
  if (items_written)
806
0
    *items_written = i;
807
808
0
  return result;
809
0
}
810
811
static gpointer
812
try_malloc_n (gsize n_blocks, gsize n_block_bytes, GError **error)
813
7.88k
{
814
7.88k
    gpointer ptr = g_try_malloc_n (n_blocks, n_block_bytes);
815
7.88k
    if (ptr == NULL)
816
0
      g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_MEMORY,
817
0
                           _("Failed to allocate memory"));
818
7.88k
    return ptr;
819
7.88k
}
820
821
/**
822
 * g_utf8_to_ucs4:
823
 * @str: a UTF-8 encoded string
824
 * @len: the maximum length of @str to use, in bytes. If @len < 0,
825
 *     then the string is nul-terminated.
826
 * @items_read: (out caller-allocates) (optional): location to store number of
827
  *    bytes read, or %NULL.
828
 *     If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will be
829
 *     returned in case @str contains a trailing partial
830
 *     character. If an error occurs then the index of the
831
 *     invalid input is stored here.
832
 * @items_written: (out caller-allocates) (optional): location to store number
833
 *     of characters written or %NULL. The value here stored does not include
834
 *     the trailing 0 character.
835
 * @error: location to store the error occurring, or %NULL to ignore
836
 *     errors. Any of the errors in #GConvertError other than
837
 *     %G_CONVERT_ERROR_NO_CONVERSION may occur.
838
 *
839
 * Convert a string from UTF-8 to a 32-bit fixed width
840
 * representation as UCS-4. A trailing 0 character will be added to the
841
 * string after the converted text.
842
 * 
843
 * Returns: (transfer full): a pointer to a newly allocated UCS-4 string.
844
 *     This value must be freed with g_free(). If an error occurs,
845
 *     %NULL will be returned and @error set.
846
 */
847
gunichar *
848
g_utf8_to_ucs4 (const gchar *str,
849
    glong        len,             
850
    glong       *items_read,      
851
    glong       *items_written,   
852
    GError     **error)
853
0
{
854
0
  gunichar *result = NULL;
855
0
  gint n_chars, i;
856
0
  const gchar *in;
857
  
858
0
  in = str;
859
0
  n_chars = 0;
860
0
  while ((len < 0 || str + len - in > 0) && *in)
861
0
    {
862
0
      gunichar wc = g_utf8_get_char_extended (in, len < 0 ? 6 : str + len - in);
863
0
      if (wc & 0x80000000)
864
0
  {
865
0
    if (wc == (gunichar)-2)
866
0
      {
867
0
        if (items_read)
868
0
    break;
869
0
        else
870
0
    g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
871
0
                                     _("Partial character sequence at end of input"));
872
0
      }
873
0
    else
874
0
      g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
875
0
                                 _("Invalid byte sequence in conversion input"));
876
877
0
    goto err_out;
878
0
  }
879
880
0
      n_chars++;
881
882
0
      in = g_utf8_next_char (in);
883
0
    }
884
885
0
  result = try_malloc_n (n_chars + 1, sizeof (gunichar), error);
886
0
  if (result == NULL)
887
0
      goto err_out;
888
889
0
  in = str;
890
0
  for (i=0; i < n_chars; i++)
891
0
    {
892
0
      result[i] = g_utf8_get_char (in);
893
0
      in = g_utf8_next_char (in);
894
0
    }
895
0
  result[i] = 0;
896
897
0
  if (items_written)
898
0
    *items_written = n_chars;
899
900
0
 err_out:
901
0
  if (items_read)
902
0
    *items_read = in - str;
903
904
0
  return result;
905
0
}
906
907
/**
908
 * g_ucs4_to_utf8:
909
 * @str: a UCS-4 encoded string
910
 * @len: the maximum length (number of characters) of @str to use. 
911
 *     If @len < 0, then the string is nul-terminated.
912
 * @items_read: (out caller-allocates) (optional): location to store number of
913
 *     characters read, or %NULL.
914
 * @items_written: (out caller-allocates) (optional): location to store number
915
 *     of bytes written or %NULL. The value here stored does not include the
916
 *     trailing 0 byte.
917
 * @error: location to store the error occurring, or %NULL to ignore
918
 *         errors. Any of the errors in #GConvertError other than
919
 *         %G_CONVERT_ERROR_NO_CONVERSION may occur.
920
 *
921
 * Convert a string from a 32-bit fixed width representation as UCS-4.
922
 * to UTF-8. The result will be terminated with a 0 byte.
923
 * 
924
 * Returns: (transfer full): a pointer to a newly allocated UTF-8 string.
925
 *     This value must be freed with g_free(). If an error occurs,
926
 *     %NULL will be returned and @error set. In that case, @items_read
927
 *     will be set to the position of the first invalid input character.
928
 */
929
gchar *
930
g_ucs4_to_utf8 (const gunichar *str,
931
    glong           len,              
932
    glong          *items_read,       
933
    glong          *items_written,    
934
    GError        **error)
935
0
{
936
0
  gint result_length;
937
0
  gchar *result = NULL;
938
0
  gchar *p;
939
0
  gint i;
940
941
0
  result_length = 0;
942
0
  for (i = 0; len < 0 || i < len ; i++)
943
0
    {
944
0
      if (!str[i])
945
0
  break;
946
947
0
      if (str[i] >= 0x80000000)
948
0
  {
949
0
    g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
950
0
                               _("Character out of range for UTF-8"));
951
0
    goto err_out;
952
0
  }
953
      
954
0
      result_length += UTF8_LENGTH (str[i]);
955
0
    }
956
957
0
  result = try_malloc_n (result_length + 1, 1, error);
958
0
  if (result == NULL)
959
0
      goto err_out;
960
961
0
  p = result;
962
963
0
  i = 0;
964
0
  while (p < result + result_length)
965
0
    p += g_unichar_to_utf8 (str[i++], p);
966
  
967
0
  *p = '\0';
968
969
0
  if (items_written)
970
0
    *items_written = p - result;
971
972
0
 err_out:
973
0
  if (items_read)
974
0
    *items_read = i;
975
976
0
  return result;
977
0
}
978
979
3.00k
#define SURROGATE_VALUE(h,l) (((h) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000)
980
981
/**
982
 * g_utf16_to_utf8:
983
 * @str: a UTF-16 encoded string
984
 * @len: the maximum length (number of #gunichar2) of @str to use. 
985
 *     If @len < 0, then the string is nul-terminated.
986
 * @items_read: (out caller-allocates) (optional): location to store number of
987
 *     words read, or %NULL. If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will
988
 *     be returned in case @str contains a trailing partial character. If
989
 *     an error occurs then the index of the invalid input is stored here.
990
 * @items_written: (out caller-allocates) (optional): location to store number
991
 *     of bytes written, or %NULL. The value stored here does not include the
992
 *     trailing 0 byte.
993
 * @error: location to store the error occurring, or %NULL to ignore
994
 *     errors. Any of the errors in #GConvertError other than
995
 *     %G_CONVERT_ERROR_NO_CONVERSION may occur.
996
 *
997
 * Convert a string from UTF-16 to UTF-8. The result will be
998
 * terminated with a 0 byte.
999
 *
1000
 * Note that the input is expected to be already in native endianness,
1001
 * an initial byte-order-mark character is not handled specially.
1002
 * g_convert() can be used to convert a byte buffer of UTF-16 data of
1003
 * ambiguous endianness.
1004
 *
1005
 * Further note that this function does not validate the result
1006
 * string; it may e.g. include embedded NUL characters. The only
1007
 * validation done by this function is to ensure that the input can
1008
 * be correctly interpreted as UTF-16, i.e. it doesn't contain
1009
 * unpaired surrogates or partial character sequences.
1010
 *
1011
 * Returns: (transfer full): a pointer to a newly allocated UTF-8 string.
1012
 *     This value must be freed with g_free(). If an error occurs,
1013
 *     %NULL will be returned and @error set.
1014
 **/
1015
gchar *
1016
g_utf16_to_utf8 (const gunichar2  *str,
1017
     glong             len,
1018
     glong            *items_read,
1019
     glong            *items_written,
1020
     GError          **error)
1021
6.25k
{
1022
  /* This function and g_utf16_to_ucs4 are almost exactly identical -
1023
   * The lines that differ are marked.
1024
   */
1025
6.25k
  const gunichar2 *in;
1026
6.25k
  gchar *out;
1027
6.25k
  gchar *result = NULL;
1028
6.25k
  gint n_bytes;
1029
6.25k
  gunichar high_surrogate;
1030
1031
6.25k
  g_return_val_if_fail (str != NULL, NULL);
1032
1033
6.25k
  n_bytes = 0;
1034
6.25k
  in = str;
1035
6.25k
  high_surrogate = 0;
1036
51.1M
  while ((len < 0 || in - str < len) && *in)
1037
51.1M
    {
1038
51.1M
      gunichar2 c = *in;
1039
51.1M
      gunichar wc;
1040
1041
51.1M
      if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1042
1.74k
  {
1043
1.74k
    if (high_surrogate)
1044
1.68k
      {
1045
1.68k
        wc = SURROGATE_VALUE (high_surrogate, c);
1046
1.68k
        high_surrogate = 0;
1047
1.68k
      }
1048
60
    else
1049
60
      {
1050
60
        g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1051
60
                                   _("Invalid sequence in conversion input"));
1052
60
        goto err_out;
1053
60
      }
1054
1.74k
  }
1055
51.1M
      else
1056
51.1M
  {
1057
51.1M
    if (high_surrogate)
1058
34
      {
1059
34
        g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1060
34
                                   _("Invalid sequence in conversion input"));
1061
34
        goto err_out;
1062
34
      }
1063
1064
51.1M
    if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1065
1.80k
      {
1066
1.80k
        high_surrogate = c;
1067
1.80k
        goto next1;
1068
1.80k
      }
1069
51.1M
    else
1070
51.1M
      wc = c;
1071
51.1M
  }
1072
1073
      /********** DIFFERENT for UTF8/UCS4 **********/
1074
51.1M
      n_bytes += UTF8_LENGTH (wc);
1075
1076
51.1M
    next1:
1077
51.1M
      in++;
1078
51.1M
    }
1079
1080
6.16k
  if (high_surrogate && !items_read)
1081
90
    {
1082
90
      g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1083
90
                           _("Partial character sequence at end of input"));
1084
90
      goto err_out;
1085
90
    }
1086
  
1087
  /* At this point, everything is valid, and we just need to convert
1088
   */
1089
  /********** DIFFERENT for UTF8/UCS4 **********/
1090
6.07k
  result = try_malloc_n (n_bytes + 1, 1, error);
1091
6.07k
  if (result == NULL)
1092
0
      goto err_out;
1093
1094
6.07k
  high_surrogate = 0;
1095
6.07k
  out = result;
1096
6.07k
  in = str;
1097
51.1M
  while (out < result + n_bytes)
1098
51.1M
    {
1099
51.1M
      gunichar2 c = *in;
1100
51.1M
      gunichar wc;
1101
1102
51.1M
      if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1103
1.32k
  {
1104
1.32k
    wc = SURROGATE_VALUE (high_surrogate, c);
1105
1.32k
    high_surrogate = 0;
1106
1.32k
  }
1107
51.1M
      else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1108
1.32k
  {
1109
1.32k
    high_surrogate = c;
1110
1.32k
    goto next2;
1111
1.32k
  }
1112
51.1M
      else
1113
51.1M
  wc = c;
1114
1115
      /********** DIFFERENT for UTF8/UCS4 **********/
1116
51.1M
      out += g_unichar_to_utf8 (wc, out);
1117
1118
51.1M
    next2:
1119
51.1M
      in++;
1120
51.1M
    }
1121
  
1122
  /********** DIFFERENT for UTF8/UCS4 **********/
1123
6.07k
  *out = '\0';
1124
1125
6.07k
  if (items_written)
1126
    /********** DIFFERENT for UTF8/UCS4 **********/
1127
0
    *items_written = out - result;
1128
1129
6.25k
 err_out:
1130
6.25k
  if (items_read)
1131
0
    *items_read = in - str;
1132
1133
6.25k
  return result;
1134
6.07k
}
1135
1136
/**
1137
 * g_utf16_to_ucs4:
1138
 * @str: a UTF-16 encoded string
1139
 * @len: the maximum length (number of #gunichar2) of @str to use. 
1140
 *     If @len < 0, then the string is nul-terminated.
1141
 * @items_read: (out caller-allocates) (optional): location to store number of
1142
 *     words read, or %NULL. If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will
1143
 *     be returned in case @str contains a trailing partial character. If
1144
 *     an error occurs then the index of the invalid input is stored here.
1145
 * @items_written: (out caller-allocates) (optional): location to store number
1146
 *     of characters written, or %NULL. The value stored here does not include
1147
 *     the trailing 0 character.
1148
 * @error: location to store the error occurring, or %NULL to ignore
1149
 *     errors. Any of the errors in #GConvertError other than
1150
 *     %G_CONVERT_ERROR_NO_CONVERSION may occur.
1151
 *
1152
 * Convert a string from UTF-16 to UCS-4. The result will be
1153
 * nul-terminated.
1154
 * 
1155
 * Returns: (transfer full): a pointer to a newly allocated UCS-4 string.
1156
 *     This value must be freed with g_free(). If an error occurs,
1157
 *     %NULL will be returned and @error set.
1158
 */
1159
gunichar *
1160
g_utf16_to_ucs4 (const gunichar2  *str,
1161
     glong             len,              
1162
     glong            *items_read,       
1163
     glong            *items_written,    
1164
     GError          **error)
1165
0
{
1166
0
  const gunichar2 *in;
1167
0
  gchar *out;
1168
0
  gchar *result = NULL;
1169
0
  gint n_bytes;
1170
0
  gunichar high_surrogate;
1171
1172
0
  g_return_val_if_fail (str != NULL, NULL);
1173
1174
0
  n_bytes = 0;
1175
0
  in = str;
1176
0
  high_surrogate = 0;
1177
0
  while ((len < 0 || in - str < len) && *in)
1178
0
    {
1179
0
      gunichar2 c = *in;
1180
1181
0
      if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1182
0
  {
1183
0
    if (high_surrogate)
1184
0
      {
1185
0
        high_surrogate = 0;
1186
0
      }
1187
0
    else
1188
0
      {
1189
0
        g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1190
0
                                   _("Invalid sequence in conversion input"));
1191
0
        goto err_out;
1192
0
      }
1193
0
  }
1194
0
      else
1195
0
  {
1196
0
    if (high_surrogate)
1197
0
      {
1198
0
        g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1199
0
                                   _("Invalid sequence in conversion input"));
1200
0
        goto err_out;
1201
0
      }
1202
1203
0
    if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1204
0
      {
1205
0
        high_surrogate = c;
1206
0
        goto next1;
1207
0
      }
1208
0
  }
1209
1210
      /********** DIFFERENT for UTF8/UCS4 **********/
1211
0
      n_bytes += sizeof (gunichar);
1212
1213
0
    next1:
1214
0
      in++;
1215
0
    }
1216
1217
0
  if (high_surrogate && !items_read)
1218
0
    {
1219
0
      g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1220
0
                           _("Partial character sequence at end of input"));
1221
0
      goto err_out;
1222
0
    }
1223
  
1224
  /* At this point, everything is valid, and we just need to convert
1225
   */
1226
  /********** DIFFERENT for UTF8/UCS4 **********/
1227
0
  result = try_malloc_n (n_bytes + 4, 1, error);
1228
0
  if (result == NULL)
1229
0
      goto err_out;
1230
1231
0
  high_surrogate = 0;
1232
0
  out = result;
1233
0
  in = str;
1234
0
  while (out < result + n_bytes)
1235
0
    {
1236
0
      gunichar2 c = *in;
1237
0
      gunichar wc;
1238
1239
0
      if (c >= 0xdc00 && c < 0xe000) /* low surrogate */
1240
0
  {
1241
0
    wc = SURROGATE_VALUE (high_surrogate, c);
1242
0
    high_surrogate = 0;
1243
0
  }
1244
0
      else if (c >= 0xd800 && c < 0xdc00) /* high surrogate */
1245
0
  {
1246
0
    high_surrogate = c;
1247
0
    goto next2;
1248
0
  }
1249
0
      else
1250
0
  wc = c;
1251
1252
      /********** DIFFERENT for UTF8/UCS4 **********/
1253
0
      *(gunichar *)out = wc;
1254
0
      out += sizeof (gunichar);
1255
1256
0
    next2:
1257
0
      in++;
1258
0
    }
1259
1260
  /********** DIFFERENT for UTF8/UCS4 **********/
1261
0
  *(gunichar *)out = 0;
1262
1263
0
  if (items_written)
1264
    /********** DIFFERENT for UTF8/UCS4 **********/
1265
0
    *items_written = (out - result) / sizeof (gunichar);
1266
1267
0
 err_out:
1268
0
  if (items_read)
1269
0
    *items_read = in - str;
1270
1271
0
  return (gunichar *)result;
1272
0
}
1273
1274
/**
1275
 * g_utf8_to_utf16:
1276
 * @str: a UTF-8 encoded string
1277
 * @len: the maximum length (number of bytes) of @str to use.
1278
 *     If @len < 0, then the string is nul-terminated.
1279
 * @items_read: (out caller-allocates) (optional): location to store number of
1280
 *     bytes read, or %NULL. If %NULL, then %G_CONVERT_ERROR_PARTIAL_INPUT will
1281
 *     be returned in case @str contains a trailing partial character. If
1282
 *     an error occurs then the index of the invalid input is stored here.
1283
 * @items_written: (out caller-allocates) (optional): location to store number
1284
 *     of #gunichar2 written, or %NULL. The value stored here does not include
1285
 *     the trailing 0.
1286
 * @error: location to store the error occurring, or %NULL to ignore
1287
 *     errors. Any of the errors in #GConvertError other than
1288
 *     %G_CONVERT_ERROR_NO_CONVERSION may occur.
1289
 *
1290
 * Convert a string from UTF-8 to UTF-16. A 0 character will be
1291
 * added to the result after the converted text.
1292
 *
1293
 * Returns: (transfer full): a pointer to a newly allocated UTF-16 string.
1294
 *     This value must be freed with g_free(). If an error occurs,
1295
 *     %NULL will be returned and @error set.
1296
 */
1297
gunichar2 *
1298
g_utf8_to_utf16 (const gchar *str,
1299
     glong        len,
1300
     glong       *items_read,
1301
     glong       *items_written,
1302
     GError     **error)
1303
1.81k
{
1304
1.81k
  gunichar2 *result = NULL;
1305
1.81k
  gint n16;
1306
1.81k
  const gchar *in;
1307
1.81k
  gint i;
1308
1309
1.81k
  g_return_val_if_fail (str != NULL, NULL);
1310
1311
1.81k
  in = str;
1312
1.81k
  n16 = 0;
1313
187k
  while ((len < 0 || str + len - in > 0) && *in)
1314
185k
    {
1315
185k
      gunichar wc = g_utf8_get_char_extended (in, len < 0 ? 6 : str + len - in);
1316
185k
      if (wc & 0x80000000)
1317
0
  {
1318
0
    if (wc == (gunichar)-2)
1319
0
      {
1320
0
        if (items_read)
1321
0
    break;
1322
0
        else
1323
0
    g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
1324
0
                                     _("Partial character sequence at end of input"));
1325
0
      }
1326
0
    else
1327
0
      g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1328
0
                                 _("Invalid byte sequence in conversion input"));
1329
1330
0
    goto err_out;
1331
0
  }
1332
1333
185k
      if (wc < 0xd800)
1334
140k
  n16 += 1;
1335
45.8k
      else if (wc < 0xe000)
1336
0
  {
1337
0
    g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1338
0
                               _("Invalid sequence in conversion input"));
1339
1340
0
    goto err_out;
1341
0
  }
1342
45.8k
      else if (wc < 0x10000)
1343
45.1k
  n16 += 1;
1344
656
      else if (wc < 0x110000)
1345
656
  n16 += 2;
1346
0
      else
1347
0
  {
1348
0
    g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1349
0
                               _("Character out of range for UTF-16"));
1350
1351
0
    goto err_out;
1352
0
  }
1353
      
1354
185k
      in = g_utf8_next_char (in);
1355
185k
    }
1356
1357
1.81k
  result = try_malloc_n (n16 + 1, sizeof (gunichar2), error);
1358
1.81k
  if (result == NULL)
1359
0
      goto err_out;
1360
1361
1.81k
  in = str;
1362
187k
  for (i = 0; i < n16;)
1363
185k
    {
1364
185k
      gunichar wc = g_utf8_get_char (in);
1365
1366
185k
      if (wc < 0x10000)
1367
185k
  {
1368
185k
    result[i++] = wc;
1369
185k
  }
1370
656
      else
1371
656
  {
1372
656
    result[i++] = (wc - 0x10000) / 0x400 + 0xd800;
1373
656
    result[i++] = (wc - 0x10000) % 0x400 + 0xdc00;
1374
656
  }
1375
      
1376
185k
      in = g_utf8_next_char (in);
1377
185k
    }
1378
1379
1.81k
  result[i] = 0;
1380
1381
1.81k
  if (items_written)
1382
1.81k
    *items_written = n16;
1383
1384
1.81k
 err_out:
1385
1.81k
  if (items_read)
1386
0
    *items_read = in - str;
1387
  
1388
1.81k
  return result;
1389
1.81k
}
1390
1391
/**
1392
 * g_ucs4_to_utf16:
1393
 * @str: a UCS-4 encoded string
1394
 * @len: the maximum length (number of characters) of @str to use. 
1395
 *     If @len < 0, then the string is nul-terminated.
1396
 * @items_read: (out caller-allocates) (optional): location to store number of
1397
 *     bytes read, or %NULL. If an error occurs then the index of the invalid
1398
 *     input is stored here.
1399
 * @items_written: (out caller-allocates) (optional): location to store number
1400
 *     of #gunichar2  written, or %NULL. The value stored here does not include
1401
 *     the trailing 0.
1402
 * @error: location to store the error occurring, or %NULL to ignore
1403
 *     errors. Any of the errors in #GConvertError other than
1404
 *     %G_CONVERT_ERROR_NO_CONVERSION may occur.
1405
 *
1406
 * Convert a string from UCS-4 to UTF-16. A 0 character will be
1407
 * added to the result after the converted text.
1408
 * 
1409
 * Returns: (transfer full): a pointer to a newly allocated UTF-16 string.
1410
 *     This value must be freed with g_free(). If an error occurs,
1411
 *     %NULL will be returned and @error set.
1412
 */
1413
gunichar2 *
1414
g_ucs4_to_utf16 (const gunichar  *str,
1415
     glong            len,              
1416
     glong           *items_read,       
1417
     glong           *items_written,    
1418
     GError         **error)
1419
0
{
1420
0
  gunichar2 *result = NULL;
1421
0
  gint n16;
1422
0
  gint i, j;
1423
1424
0
  n16 = 0;
1425
0
  i = 0;
1426
0
  while ((len < 0 || i < len) && str[i])
1427
0
    {
1428
0
      gunichar wc = str[i];
1429
1430
0
      if (wc < 0xd800)
1431
0
  n16 += 1;
1432
0
      else if (wc < 0xe000)
1433
0
  {
1434
0
    g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1435
0
                               _("Invalid sequence in conversion input"));
1436
1437
0
    goto err_out;
1438
0
  }
1439
0
      else if (wc < 0x10000)
1440
0
  n16 += 1;
1441
0
      else if (wc < 0x110000)
1442
0
  n16 += 2;
1443
0
      else
1444
0
  {
1445
0
    g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1446
0
                               _("Character out of range for UTF-16"));
1447
1448
0
    goto err_out;
1449
0
  }
1450
1451
0
      i++;
1452
0
    }
1453
1454
0
  result = try_malloc_n (n16 + 1, sizeof (gunichar2), error);
1455
0
  if (result == NULL)
1456
0
      goto err_out;
1457
1458
0
  for (i = 0, j = 0; j < n16; i++)
1459
0
    {
1460
0
      gunichar wc = str[i];
1461
1462
0
      if (wc < 0x10000)
1463
0
  {
1464
0
    result[j++] = wc;
1465
0
  }
1466
0
      else
1467
0
  {
1468
0
    result[j++] = (wc - 0x10000) / 0x400 + 0xd800;
1469
0
    result[j++] = (wc - 0x10000) % 0x400 + 0xdc00;
1470
0
  }
1471
0
    }
1472
0
  result[j] = 0;
1473
1474
0
  if (items_written)
1475
0
    *items_written = n16;
1476
  
1477
0
 err_out:
1478
0
  if (items_read)
1479
0
    *items_read = i;
1480
  
1481
0
  return result;
1482
0
}
1483
1484
#define VALIDATE_BYTE(mask, expect)                      \
1485
669k
  G_STMT_START {                                         \
1486
669k
    if (G_UNLIKELY((*(guchar *)p & (mask)) != (expect))) \
1487
669k
      goto error;                                        \
1488
669k
  } G_STMT_END
1489
1490
/* see IETF RFC 3629 Section 4 */
1491
1492
static const gchar *
1493
fast_validate (const char *str)
1494
1495
34.3k
{
1496
34.3k
  const gchar *p;
1497
1498
2.10M
  for (p = str; *p; p++)
1499
2.07M
    {
1500
2.07M
      if (*(guchar *)p < 128)
1501
2.07M
  /* done */;
1502
0
      else 
1503
0
  {
1504
0
    const gchar *last;
1505
1506
0
    last = p;
1507
0
    if (*(guchar *)p < 0xe0) /* 110xxxxx */
1508
0
      {
1509
0
        if (G_UNLIKELY (*(guchar *)p < 0xc2))
1510
0
    goto error;
1511
0
      }
1512
0
    else
1513
0
      {
1514
0
        if (*(guchar *)p < 0xf0) /* 1110xxxx */
1515
0
    {
1516
0
      switch (*(guchar *)p++ & 0x0f)
1517
0
        {
1518
0
        case 0:
1519
0
          VALIDATE_BYTE(0xe0, 0xa0); /* 0xa0 ... 0xbf */
1520
0
          break;
1521
0
        case 0x0d:
1522
0
          VALIDATE_BYTE(0xe0, 0x80); /* 0x80 ... 0x9f */
1523
0
          break;
1524
0
        default:
1525
0
          VALIDATE_BYTE(0xc0, 0x80); /* 10xxxxxx */
1526
0
        }
1527
0
    }
1528
0
        else if (*(guchar *)p < 0xf5) /* 11110xxx excluding out-of-range */
1529
0
    {
1530
0
      switch (*(guchar *)p++ & 0x07)
1531
0
        {
1532
0
        case 0:
1533
0
          VALIDATE_BYTE(0xc0, 0x80); /* 10xxxxxx */
1534
0
          if (G_UNLIKELY((*(guchar *)p & 0x30) == 0))
1535
0
      goto error;
1536
0
          break;
1537
0
        case 4:
1538
0
          VALIDATE_BYTE(0xf0, 0x80); /* 0x80 ... 0x8f */
1539
0
          break;
1540
0
        default:
1541
0
          VALIDATE_BYTE(0xc0, 0x80); /* 10xxxxxx */
1542
0
        }
1543
0
      p++;
1544
0
      VALIDATE_BYTE(0xc0, 0x80); /* 10xxxxxx */
1545
0
    }
1546
0
        else
1547
0
    goto error;
1548
0
      }
1549
1550
0
    p++;
1551
0
    VALIDATE_BYTE(0xc0, 0x80); /* 10xxxxxx */
1552
1553
0
    continue;
1554
1555
0
  error:
1556
0
    return last;
1557
0
  }
1558
2.07M
    }
1559
1560
34.3k
  return p;
1561
34.3k
}
1562
1563
static const gchar *
1564
fast_validate_len (const char *str,
1565
       gssize      max_len)
1566
1567
19.5M
{
1568
19.5M
  const gchar *p;
1569
1570
19.5M
  g_assert (max_len >= 0);
1571
1572
320M
  for (p = str; ((p - str) < max_len) && *p; p++)
1573
301M
    {
1574
301M
      if (*(guchar *)p < 128)
1575
300M
  /* done */;
1576
395k
      else 
1577
395k
  {
1578
395k
    const gchar *last;
1579
1580
395k
    last = p;
1581
395k
    if (*(guchar *)p < 0xe0) /* 110xxxxx */
1582
126k
      {
1583
126k
        if (G_UNLIKELY (max_len - (p - str) < 2))
1584
8.71k
    goto error;
1585
        
1586
117k
        if (G_UNLIKELY (*(guchar *)p < 0xc2))
1587
16.0k
    goto error;
1588
117k
      }
1589
269k
    else
1590
269k
      {
1591
269k
        if (*(guchar *)p < 0xf0) /* 1110xxxx */
1592
71.5k
    {
1593
71.5k
      if (G_UNLIKELY (max_len - (p - str) < 3))
1594
6.58k
        goto error;
1595
1596
64.9k
      switch (*(guchar *)p++ & 0x0f)
1597
64.9k
        {
1598
6.37k
        case 0:
1599
6.37k
          VALIDATE_BYTE(0xe0, 0xa0); /* 0xa0 ... 0xbf */
1600
3.20k
          break;
1601
5.02k
        case 0x0d:
1602
5.02k
          VALIDATE_BYTE(0xe0, 0x80); /* 0x80 ... 0x9f */
1603
3.37k
          break;
1604
53.5k
        default:
1605
53.5k
          VALIDATE_BYTE(0xc0, 0x80); /* 10xxxxxx */
1606
64.9k
        }
1607
64.9k
    }
1608
197k
        else if (*(guchar *)p < 0xf5) /* 11110xxx excluding out-of-range */
1609
183k
    {
1610
183k
      if (G_UNLIKELY (max_len - (p - str) < 4))
1611
15.1k
        goto error;
1612
1613
167k
      switch (*(guchar *)p++ & 0x07)
1614
167k
        {
1615
41.0k
        case 0:
1616
41.0k
          VALIDATE_BYTE(0xc0, 0x80); /* 10xxxxxx */
1617
36.1k
          if (G_UNLIKELY((*(guchar *)p & 0x30) == 0))
1618
2.41k
      goto error;
1619
33.7k
          break;
1620
33.7k
        case 4:
1621
12.9k
          VALIDATE_BYTE(0xf0, 0x80); /* 0x80 ... 0x8f */
1622
11.3k
          break;
1623
113k
        default:
1624
113k
          VALIDATE_BYTE(0xc0, 0x80); /* 10xxxxxx */
1625
167k
        }
1626
154k
      p++;
1627
154k
      VALIDATE_BYTE(0xc0, 0x80); /* 10xxxxxx */
1628
154k
    }
1629
14.4k
        else
1630
14.4k
    goto error;
1631
269k
      }
1632
1633
281k
    p++;
1634
281k
    VALIDATE_BYTE(0xc0, 0x80); /* 10xxxxxx */
1635
1636
270k
    continue;
1637
1638
270k
  error:
1639
125k
    return last;
1640
281k
  }
1641
301M
    }
1642
1643
19.4M
  return p;
1644
19.5M
}
1645
1646
/**
1647
 * g_utf8_validate:
1648
 * @str: (array length=max_len) (element-type guint8): a pointer to character data
1649
 * @max_len: max bytes to validate, or -1 to go until NUL
1650
 * @end: (out) (optional) (transfer none): return location for end of valid data
1651
 * 
1652
 * Validates UTF-8 encoded text. @str is the text to validate;
1653
 * if @str is nul-terminated, then @max_len can be -1, otherwise
1654
 * @max_len should be the number of bytes to validate.
1655
 * If @end is non-%NULL, then the end of the valid range
1656
 * will be stored there (i.e. the start of the first invalid 
1657
 * character if some bytes were invalid, or the end of the text 
1658
 * being validated otherwise).
1659
 *
1660
 * Note that g_utf8_validate() returns %FALSE if @max_len is 
1661
 * positive and any of the @max_len bytes are nul.
1662
 *
1663
 * Returns %TRUE if all of @str was valid. Many GLib and GTK+
1664
 * routines require valid UTF-8 as input; so data read from a file
1665
 * or the network should be checked with g_utf8_validate() before
1666
 * doing anything else with it.
1667
 * 
1668
 * Returns: %TRUE if the text was valid UTF-8
1669
 */
1670
gboolean
1671
g_utf8_validate (const char   *str,
1672
     gssize        max_len,    
1673
     const gchar **end)
1674
1675
34.3k
{
1676
34.3k
  const gchar *p;
1677
1678
34.3k
  if (max_len >= 0)
1679
0
    return g_utf8_validate_len (str, max_len, end);
1680
1681
34.3k
  p = fast_validate (str);
1682
1683
34.3k
  if (end)
1684
0
    *end = p;
1685
1686
34.3k
  if (*p != '\0')
1687
0
    return FALSE;
1688
34.3k
  else
1689
34.3k
    return TRUE;
1690
34.3k
}
1691
1692
/**
1693
 * g_utf8_validate_len:
1694
 * @str: (array length=max_len) (element-type guint8): a pointer to character data
1695
 * @max_len: max bytes to validate
1696
 * @end: (out) (optional) (transfer none): return location for end of valid data
1697
 *
1698
 * Validates UTF-8 encoded text.
1699
 *
1700
 * As with g_utf8_validate(), but @max_len must be set, and hence this function
1701
 * will always return %FALSE if any of the bytes of @str are nul.
1702
 *
1703
 * Returns: %TRUE if the text was valid UTF-8
1704
 * Since: 2.60
1705
 */
1706
gboolean
1707
g_utf8_validate_len (const char   *str,
1708
                     gsize         max_len,
1709
                     const gchar **end)
1710
1711
19.5M
{
1712
19.5M
  const gchar *p;
1713
1714
19.5M
  p = fast_validate_len (str, max_len);
1715
1716
19.5M
  if (end)
1717
0
    *end = p;
1718
1719
19.5M
  if (p != str + max_len)
1720
125k
    return FALSE;
1721
19.4M
  else
1722
19.4M
    return TRUE;
1723
19.5M
}
1724
1725
/**
1726
 * g_unichar_validate:
1727
 * @ch: a Unicode character
1728
 * 
1729
 * Checks whether @ch is a valid Unicode character. Some possible
1730
 * integer values of @ch will not be valid. 0 is considered a valid
1731
 * character, though it's normally a string terminator.
1732
 * 
1733
 * Returns: %TRUE if @ch is a valid Unicode character
1734
 **/
1735
gboolean
1736
g_unichar_validate (gunichar ch)
1737
0
{
1738
0
  return UNICODE_VALID (ch);
1739
0
}
1740
1741
/**
1742
 * g_utf8_strreverse:
1743
 * @str: a UTF-8 encoded string
1744
 * @len: the maximum length of @str to use, in bytes. If @len < 0,
1745
 *     then the string is nul-terminated.
1746
 *
1747
 * Reverses a UTF-8 string. @str must be valid UTF-8 encoded text. 
1748
 * (Use g_utf8_validate() on all text before trying to use UTF-8 
1749
 * utility functions with it.)
1750
 *
1751
 * This function is intended for programmatic uses of reversed strings.
1752
 * It pays no attention to decomposed characters, combining marks, byte 
1753
 * order marks, directional indicators (LRM, LRO, etc) and similar 
1754
 * characters which might need special handling when reversing a string 
1755
 * for display purposes.
1756
 *
1757
 * Note that unlike g_strreverse(), this function returns
1758
 * newly-allocated memory, which should be freed with g_free() when
1759
 * no longer needed. 
1760
 *
1761
 * Returns: (transfer full): a newly-allocated string which is the reverse of @str
1762
 *
1763
 * Since: 2.2
1764
 */
1765
gchar *
1766
g_utf8_strreverse (const gchar *str,
1767
       gssize       len)
1768
0
{
1769
0
  gchar *r, *result;
1770
0
  const gchar *p;
1771
1772
0
  if (len < 0)
1773
0
    len = strlen (str);
1774
1775
0
  result = g_new (gchar, len + 1);
1776
0
  r = result + len;
1777
0
  p = str;
1778
0
  while (r > result)
1779
0
    {
1780
0
      gchar *m, skip = g_utf8_skip[*(guchar*) p];
1781
0
      r -= skip;
1782
0
      g_assert (r >= result);
1783
0
      for (m = r; skip; skip--)
1784
0
        *m++ = *p++;
1785
0
    }
1786
0
  result[len] = 0;
1787
1788
0
  return result;
1789
0
}
1790
1791
/**
1792
 * g_utf8_make_valid:
1793
 * @str: string to coerce into UTF-8
1794
 * @len: the maximum length of @str to use, in bytes. If @len < 0,
1795
 *     then the string is nul-terminated.
1796
 *
1797
 * If the provided string is valid UTF-8, return a copy of it. If not,
1798
 * return a copy in which bytes that could not be interpreted as valid Unicode
1799
 * are replaced with the Unicode replacement character (U+FFFD).
1800
 *
1801
 * For example, this is an appropriate function to use if you have received
1802
 * a string that was incorrectly declared to be UTF-8, and you need a valid
1803
 * UTF-8 version of it that can be logged or displayed to the user, with the
1804
 * assumption that it is close enough to ASCII or UTF-8 to be mostly
1805
 * readable as-is.
1806
 *
1807
 * Returns: (transfer full): a valid UTF-8 string whose content resembles @str
1808
 *
1809
 * Since: 2.52
1810
 */
1811
gchar *
1812
g_utf8_make_valid (const gchar *str,
1813
                   gssize       len)
1814
0
{
1815
0
  GString *string;
1816
0
  const gchar *remainder, *invalid;
1817
0
  gsize remaining_bytes, valid_bytes;
1818
1819
0
  g_return_val_if_fail (str != NULL, NULL);
1820
1821
0
  if (len < 0)
1822
0
    len = strlen (str);
1823
1824
0
  string = NULL;
1825
0
  remainder = str;
1826
0
  remaining_bytes = len;
1827
1828
0
  while (remaining_bytes != 0) 
1829
0
    {
1830
0
      if (g_utf8_validate (remainder, remaining_bytes, &invalid)) 
1831
0
  break;
1832
0
      valid_bytes = invalid - remainder;
1833
    
1834
0
      if (string == NULL) 
1835
0
  string = g_string_sized_new (remaining_bytes);
1836
1837
0
      g_string_append_len (string, remainder, valid_bytes);
1838
      /* append U+FFFD REPLACEMENT CHARACTER */
1839
0
      g_string_append (string, "\357\277\275");
1840
      
1841
0
      remaining_bytes -= valid_bytes + 1;
1842
0
      remainder = invalid + 1;
1843
0
    }
1844
  
1845
0
  if (string == NULL)
1846
0
    return g_strndup (str, len);
1847
  
1848
0
  g_string_append_len (string, remainder, remaining_bytes);
1849
0
  g_string_append_c (string, '\0');
1850
1851
0
  g_assert (g_utf8_validate (string->str, -1, NULL));
1852
1853
0
  return g_string_free (string, FALSE);
1854
0
}