summaryrefslogtreecommitdiff
path: root/open-vm-tools/lib/unicode/unicodeICU.c
blob: c0089d9177e4bfd538f2cf781ac96ef9e3008725 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*********************************************************
 * Copyright (C) 2008 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation version 2.1 and no later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *********************************************************/

/*
 * unicodeICU.c --
 *
 *      Unicode functionality that depends on the third-party ICU
 *      library.
 */

#include <unicode/ucasemap.h>
#include <unicode/ucol.h>
#include <unicode/uiter.h>
#include <unicode/ustring.h>
#include <unicode/unorm.h>

#include "util.h"
#include "unicodeBase.h"
#include "unicodeICU.h"


/*
 *-----------------------------------------------------------------------------
 *
 * Unicode_CompareWithLocale --
 *
 *      Compares two strings for equivalence under the collation rules
 *      of the specified locale.
 *
 *      The caller can specify ignoring differences in accents, case,
 *      or punctuation.
 *
 * Results:
 *      -1 if str1 < str2, 0 if str1 == str2, 1 if str1 > str2.
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

int
Unicode_CompareWithLocale(ConstUnicode str1,                  // IN
                          ConstUnicode str2,                  // IN
                          const char *locale,                 // IN
                          UnicodeCompareOption compareOption) // IN
{
   UCollationResult compareResult;
   UColAttributeValue comparisonStrength;
   UErrorCode status = U_ZERO_ERROR;
   int result;
   UCollator *coll;
   UCharIterator str1Iter;
   UCharIterator str2Iter;

   uiter_setUTF8(&str1Iter, (const char *)str1, -1);
   uiter_setUTF8(&str2Iter, (const char *)str2, -1);

   switch (compareOption) {
   case UNICODE_COMPARE_DEFAULT:
      comparisonStrength = UCOL_DEFAULT;
      break;
   case UNICODE_COMPARE_IGNORE_ACCENTS:
      comparisonStrength = UCOL_PRIMARY;
      break;
   case UNICODE_COMPARE_IGNORE_CASE:
      comparisonStrength = UCOL_SECONDARY;
      break;
   case UNICODE_COMPARE_IGNORE_PUNCTUATION:
      comparisonStrength = UCOL_TERTIARY;
      break;
   default:
      NOT_IMPLEMENTED();
   }

   coll = ucol_open(locale, &status);

   ASSERT(U_SUCCESS(status));
   ASSERT(coll);

   if (U_FAILURE(status) || !coll) {
      return -1;
   }

   // Normalize all strings to NFD before comparing.
   ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
   ucol_setAttribute(coll, UCOL_STRENGTH, comparisonStrength, &status);

   ASSERT(U_SUCCESS(status));

   compareResult = ucol_strcollIter(coll, &str1Iter, &str2Iter, &status);

   ucol_close(coll);

   if (U_FAILURE(status)) {
      // We'll probably only get here if the input wasn't UTF-8.
      ASSERT(U_SUCCESS(status));
      return -1;
   }

   switch (compareResult) {
   case UCOL_LESS:
      result = -1;
      break;
   case UCOL_EQUAL:
      result = 0;
      break;
   case UCOL_GREATER:
      result = 1;
      break;
   default:
      NOT_IMPLEMENTED();
   }

   return result;
}


/*
 *-----------------------------------------------------------------------------
 *
 * Unicode_Normalize --
 *
 *      Creates a Unicode string by normalizing the input string
 *      into a Unicode normal form.
 *
 *      Normalization Form C ("precomposed") ensures that accented
 *      characters use as few Unicode code points as possible.  This
 *      form is often used on Windows and Linux hosts.
 *
 *      Example: small letter e with acute -> U+00E9
 *
 *      Normalization Form D ("decomposed") ensures that accented
 *      characters (e with accent acute) use separate Unicode code
 *      points for the base letter and accents.  This form is used on
 *      Mac OS hosts.
 *
 *      Example: small letter e with acute -> U+0065 U+0301
 *
 * Results:
 *      The allocated Unicode string, or NULL on failure.
 *      Caller must free with Unicode_Free().
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

Unicode
Unicode_Normalize(ConstUnicode str,              // IN
                  UnicodeNormalizationForm form) // IN
{
   UNormalizationMode mode;
   UChar *uchars;
   Unicode result;
   int32_t normalizedLen;
   UErrorCode status = U_ZERO_ERROR;
   UCharIterator strIter;
   UBool neededToNormalize = FALSE;

   uiter_setUTF8(&strIter, (const char *)str, -1);

   switch (form) {
   case UNICODE_NORMAL_FORM_C:
      mode = UNORM_NFC;
      break;
   case UNICODE_NORMAL_FORM_D:
      mode = UNORM_NFD;
      break;
   default:
      NOT_REACHED();
   }

   normalizedLen = unorm_next(&strIter,
                              NULL,
                              0,
                              mode,
                              0,
                              TRUE,
                              &neededToNormalize,
                              &status);

   if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR) {
      // We expect U_BUFFER_OVERFLOW_ERROR here. Anything else is a problem.
      ASSERT(U_SUCCESS(status));
      return NULL;
   }

   uchars = Util_SafeMalloc(sizeof *uchars * normalizedLen);

   // Reset back to the beginning of the UTF-8 input.
   (*strIter.move)(&strIter, 0, UITER_START);

   status = U_ZERO_ERROR;
   normalizedLen = unorm_next(&strIter,
                              uchars,
                              normalizedLen,
                              mode,
                              0,
                              TRUE,
                              &neededToNormalize,
                              &status);

   if (U_FAILURE(status)) {
      ASSERT(U_SUCCESS(status));
      return NULL;
   }

   result = Unicode_AllocWithLength(uchars,
                                    normalizedLen * 2,
                                    STRING_ENCODING_UTF16);
   free(uchars);

   return result;
}


/*
 *-----------------------------------------------------------------------------
 *
 * Unicode_ToLower --
 *
 *      Creates a Unicode string by lower-casing the input string using
 *      the rules of the specified locale.
 *
 *      The resulting string may not be the same length as the input
 *      string.
 *
 *      Pass NULL for the locale to use the process's default locale.
 *
 * Results:
 *      The allocated Unicode string, or NULL on failure.
 *      Caller must free with Unicode_Free().
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

Unicode
Unicode_ToLower(ConstUnicode str,   // IN
                const char *locale) // IN
{
   UCaseMap *caseMap;
   UErrorCode status = U_ZERO_ERROR;
   char *utf8Dest;
   const char *utf8Src = (const char *)str;
   int32_t utf8SrcLen = strlen(utf8Src);
   int32_t destCapacity = utf8SrcLen + 1;
   int32_t destLen;
   Unicode result = NULL;

   /*
    * XXX TODO: This and the two following functions are substantially
    * identical.  Refactor them!  (Note that ucasemap_utf8ToTitle
    * takes a non-const UCaseMap, so we can't just use pointers to
    * functions unless we cast.)
    */

   // Most lower-case operations don't change the length of the string.
   utf8Dest = (char *)Util_SafeMalloc(destCapacity);

   caseMap = ucasemap_open(locale, 0, &status);
   if (U_FAILURE(status)) {
      goto out;
   }

   destLen = ucasemap_utf8ToLower(caseMap,
                                  utf8Dest,
                                  destCapacity,
                                  utf8Src,
                                  utf8SrcLen,
                                  &status);

   if (status != U_BUFFER_OVERFLOW_ERROR) {
      goto out;
   }

   // If we need a bigger buffer, then reallocate and retry.
   destCapacity = destLen + 1;
   utf8Dest = (char *)Util_SafeRealloc(utf8Dest, destCapacity);

   status = U_ZERO_ERROR;
   destLen = ucasemap_utf8ToLower(caseMap,
                                  utf8Dest,
                                  destCapacity,
                                  utf8Src,
                                  utf8SrcLen,
                                  &status);

  out:
   ucasemap_close(caseMap);

   if (U_SUCCESS(status) && status != U_STRING_NOT_TERMINATED_WARNING) {
      result = (Unicode)utf8Dest;
   } else {
      ASSERT(U_SUCCESS(status));
      ASSERT(status != U_STRING_NOT_TERMINATED_WARNING);
   }

   return result;
}


/*
 *-----------------------------------------------------------------------------
 *
 * Unicode_ToUpper --
 *
 *      Creates a Unicode string by upper-casing the input string using
 *      the rules of the specified locale.
 *
 *      The resulting string may not be the same length as the input
 *      string.
 *
 *      Pass NULL for the locale to use the process's default locale.
 *
 * Results:
 *      The allocated Unicode string, or NULL on failure.
 *      Caller must free with Unicode_Free().
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

Unicode
Unicode_ToUpper(ConstUnicode str,   // IN
                const char *locale) // IN
{
   UCaseMap *caseMap;
   UErrorCode status = U_ZERO_ERROR;
   char *utf8Dest;
   const char *utf8Src = (const char *)str;
   int32_t utf8SrcLen = strlen(utf8Src);
   int32_t destCapacity = utf8SrcLen + 1;
   int32_t destLen;
   Unicode result = NULL;

   // Most upper-case operations don't change the length of the string.
   utf8Dest = (char *)Util_SafeMalloc(destCapacity);

   caseMap = ucasemap_open(locale, 0, &status);
   if (U_FAILURE(status)) {
      goto out;
   }

   destLen = ucasemap_utf8ToUpper(caseMap,
                                  utf8Dest,
                                  destCapacity,
                                  utf8Src,
                                  utf8SrcLen,
                                  &status);

   if (status != U_BUFFER_OVERFLOW_ERROR) {
      goto out;
   }

   // If we need a bigger buffer, then reallocate and retry.
   destCapacity = destLen + 1;
   utf8Dest = (char *)Util_SafeRealloc(utf8Dest, destCapacity);

   status = U_ZERO_ERROR;
   destLen = ucasemap_utf8ToUpper(caseMap,
                                  utf8Dest,
                                  destCapacity,
                                  utf8Src,
                                  utf8SrcLen,
                                  &status);

  out:
   ucasemap_close(caseMap);

   if (U_SUCCESS(status) && status != U_STRING_NOT_TERMINATED_WARNING) {
      result = (Unicode)utf8Dest;
   } else {
      ASSERT(U_SUCCESS(status));
      ASSERT(status != U_STRING_NOT_TERMINATED_WARNING);
   }

   return result;
}

/*
 * "ucasemap_utf8ToTitle" is not in version 3.6 of the ICU library,
 * which appears to be the default on many systems...
 *
 * XXX Currently HAVE_ICU_38 is only set by the open-source tools
 * build (based on what it detects on the current system) because that
 * is the only entity currently capable of compiling with USE_ICU.
 */

#ifdef HAVE_ICU_38

/*
 *-----------------------------------------------------------------------------
 *
 * Unicode_ToTitle --
 *
 *      Creates a Unicode string by title-casing the input string using
 *      the rules of the specified locale.
 *
 *      The resulting string may not be the same length as the input
 *      string.
 *
 *      Pass NULL for the locale to use the process's default locale.
 *
 * Results:
 *      The allocated Unicode string, or NULL on failure.
 *      Caller must free with Unicode_Free().
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

Unicode
Unicode_ToTitle(ConstUnicode str,   // IN
                const char *locale) // IN
{
   UCaseMap *caseMap;
   UErrorCode status = U_ZERO_ERROR;
   char *utf8Dest;
   const char *utf8Src = (const char *)str;
   int32_t utf8SrcLen = strlen(utf8Src);
   int32_t destCapacity = utf8SrcLen + 1;
   int32_t destLen;
   Unicode result = NULL;

   // Most title-case operations don't change the length of the string.
   utf8Dest = (char *)Util_SafeMalloc(destCapacity);

   caseMap = ucasemap_open(locale, 0, &status);
   if (U_FAILURE(status)) {
      goto out;
   }

   destLen = ucasemap_utf8ToTitle(caseMap,
                                  utf8Dest,
                                  destCapacity,
                                  utf8Src,
                                  utf8SrcLen,
                                  &status);

   if (status != U_BUFFER_OVERFLOW_ERROR) {
      goto out;
   }

   // If we need a bigger buffer, then reallocate and retry.
   destCapacity = destLen + 1;
   utf8Dest = (char *)Util_SafeRealloc(utf8Dest, destCapacity);

   status = U_ZERO_ERROR;
   destLen = ucasemap_utf8ToTitle(caseMap,
                                  utf8Dest,
                                  destCapacity,
                                  utf8Src,
                                  utf8SrcLen,
                                  &status);

  out:
   ucasemap_close(caseMap);

   if (U_SUCCESS(status) && status != U_STRING_NOT_TERMINATED_WARNING) {
      result = (Unicode)utf8Dest;
   } else {
      ASSERT(U_SUCCESS(status));
      ASSERT(status != U_STRING_NOT_TERMINATED_WARNING);
   }

   return result;
}

#endif // HAVE_ICU_38