summaryrefslogtreecommitdiffstats
path: root/RedfishPkg/Library/BaseUcs2Utf8Lib/BaseUcs2Utf8Lib.c
blob: 891423734bac619028f25f154c27fc4dc09de2b1 (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
/** @file
  UCS2 to UTF8 manipulation library.

  Copyright (c) 2018 - 2019, Intel Corporation. All rights reserved.<BR>
  (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>

    SPDX-License-Identifier: BSD-2-Clause-Patent

**/
#include <Uefi.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/BaseUcs2Utf8Lib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>

/**
  Since each UCS2 character can be represented by 1-3 UTF8 encoded characters,
  this function is used to retrieve the UTF8 encoding size for a UCS2 character.

  @param[in]   Utf8Buffer       The buffer for UTF8 encoded data.

  @retval      Return the size of UTF8 encoding string or 0 if it is not for
               UCS2 format.

**/
UINT8
GetUTF8SizeForUCS2 (
  IN    CHAR8      *Utf8Buffer
  )
{
  CHAR8    TempChar;
  UINT8    Utf8Size;

  ASSERT (Utf8Buffer != NULL);

  TempChar = *Utf8Buffer;
  if ((TempChar & 0xF0) == 0xF0) {

    //
    // This format is not for UCS2.
    //
    return 0;
  }

  Utf8Size = 1;
  if ((TempChar & 0x80) == 0x80) {
    if ((TempChar & 0xC0) == 0xC0) {

      Utf8Size ++;
      if ((TempChar & 0xE0) == 0xE0) {

        Utf8Size ++;
      }
    }
  }

  return Utf8Size;
}

/**
  Since each UCS2 character can be represented by the format: \uXXXX, this function
  is used to retrieve the UCS2 character from a Unicode format.
  Call MUST make sure there are at least 6 Bytes in the input UTF8 buffer.

  @param[in]    Utf8Buffer             The buffer for UTF8 encoded data.
  @param[out]   Ucs2Char               The converted UCS2 character.

  @retval       EFI_INVALID_PARAMETER  Non-Ascii characters found in the hexadecimal
                                       digits string, and can't be converted to a UCS2
                                       character.
  @retval       EFI_SUCCESS            The UCS2 character has been retrieved.

**/
EFI_STATUS
GetUCS2CharByFormat (
  IN    CHAR8      *Utf8Buffer,
  OUT   CHAR16     *Ucs2Char
  )
{
  UINT8     Num1;
  UINT8     Num2;
  UINT8     Index;
  CHAR8     Ucs2CharFormat[UNICODE_FORMAT_CHAR_SIZE];  /// two Hexadecimal digits Ascii string, like "3F"

  for (Index = 0; Index < 4; Index ++) {
    if ((*(Utf8Buffer + 2 + Index) & 0x80) != 0x00) {
      return EFI_INVALID_PARAMETER;
    }
  }

  ZeroMem (Ucs2CharFormat, UNICODE_FORMAT_CHAR_SIZE);

  //
  // Get the First Number, Offset is 2
  //
  CopyMem (Ucs2CharFormat, Utf8Buffer + 2, UNICODE_FORMAT_CHAR_LEN);
  Num1 = (UINT8) AsciiStrHexToUintn (Ucs2CharFormat);

  //
  // Get the Second Number, Offset is 4
  //
  CopyMem (Ucs2CharFormat, Utf8Buffer + 4, UNICODE_FORMAT_CHAR_LEN);
  Num2 = (UINT8) AsciiStrHexToUintn (Ucs2CharFormat);

  //
  // Ucs2Char is Little-Endian
  //
  *((CHAR8 *) Ucs2Char)        = Num2;
  *(((CHAR8 *) Ucs2Char) + 1) = Num1;

  return EFI_SUCCESS;
}

/**
  Convert a UCS2 character to UTF8 encoding string.

  @param[in]    Ucs2Char               The provided UCS2 character.
  @param[out]   Utf8Buffer             The converted UTF8 encoded data.

  @retval      Return the size of UTF8 encoding data for this UCS2 character.

**/
UINT8
UCS2CharToUTF8 (
  IN  CHAR16     Ucs2Char,
  OUT CHAR8      *Utf8Buffer
  )
{
  UINT16    Ucs2Number;

  ASSERT (Utf8Buffer != NULL);

  Ucs2Number = (UINT16) Ucs2Char;
  if (Ucs2Number <= 0x007F) {

    //
    // UTF8 format: 0xxxxxxx
    //
    *Utf8Buffer = Ucs2Char & 0x7F;
    return 1;

  } else if (Ucs2Number >= 0x0080 && Ucs2Number <= 0x07FF) {

    //
    // UTF8 format: 110xxxxx 10xxxxxx
    //
    *(Utf8Buffer + 1) = (Ucs2Char & 0x3F) | 0x80;
    *Utf8Buffer       = ((Ucs2Char >> 6) & 0x1F) | 0xC0;
    return 2;

  } else {  /// Ucs2Number >= 0x0800 && Ucs2Number <= 0xFFFF

    //
    // UTF8 format: 1110xxxx 10xxxxxx 10xxxxxx
    //
    *(Utf8Buffer + 2) = (Ucs2Char & 0x3F) | 0x80;
    *(Utf8Buffer + 1) = ((Ucs2Char >> 6) & 0x3F) | 0x80;
    *Utf8Buffer       = ((Ucs2Char >> 12) & 0x0F) | 0xE0;
    return 3;
  }
}

/**
  Convert a UTF8 encoded data to a UCS2 character.

  @param[in]    Utf8Buffer             The provided UTF8 encoded data.
  @param[out]   Ucs2Char               The converted UCS2 character.

  @retval       EFI_INVALID_PARAMETER  The UTF8 encoded string is not valid or
                                       not for UCS2 character.
  @retval       EFI_SUCCESS            The converted UCS2 character.

**/
EFI_STATUS
UTF8ToUCS2Char (
  IN   CHAR8      *Utf8Buffer,
  OUT  CHAR16     *Ucs2Char
  )
{
  UINT8    Utf8Size;
  CHAR8    *Ucs2Buffer;
  CHAR8    TempChar1;
  CHAR8    TempChar2;
  CHAR8    TempChar3;

  ASSERT (Utf8Buffer != NULL && Ucs2Char != NULL);
  ZeroMem (Ucs2Char, sizeof (CHAR16));
  Ucs2Buffer = (CHAR8 *) Ucs2Char;

  Utf8Size = GetUTF8SizeForUCS2 (Utf8Buffer);
  switch (Utf8Size) {

    case 1:

      //
      // UTF8 format: 0xxxxxxx
      //
      TempChar1 = *Utf8Buffer;
      if ((TempChar1 & 0x80) != 0x00) {
        return EFI_INVALID_PARAMETER;
      }

      *Ucs2Buffer       = TempChar1;
      *(Ucs2Buffer + 1) = 0;
      break;

    case 2:

      //
      // UTF8 format: 110xxxxx 10xxxxxx
      //
      TempChar1 = *Utf8Buffer;
      if ((TempChar1 & 0xE0) != 0xC0) {
        return EFI_INVALID_PARAMETER;
      }

      TempChar2 = *(Utf8Buffer + 1);
      if ((TempChar2 & 0xC0) != 0x80) {
        return EFI_INVALID_PARAMETER;
      }

      *Ucs2Buffer       = (TempChar1 << 6) + (TempChar2 & 0x3F);
      *(Ucs2Buffer + 1) = (TempChar1 >> 2) & 0x07;
      break;

    case 3:

      //
      // UTF8 format: 1110xxxx 10xxxxxx 10xxxxxx
      //
      TempChar1 = *Utf8Buffer;
      if ((TempChar1 & 0xF0) != 0xE0) {
        return EFI_INVALID_PARAMETER;
      }

      TempChar2 = *(Utf8Buffer + 1);
      if ((TempChar2 & 0xC0) != 0x80) {
        return EFI_INVALID_PARAMETER;
      }

      TempChar3 = *(Utf8Buffer + 2);
      if ((TempChar3 & 0xC0) != 0x80) {
        return EFI_INVALID_PARAMETER;
      }

      *Ucs2Buffer       = (TempChar2 << 6) + (TempChar3 & 0x3F);
      *(Ucs2Buffer + 1) = (TempChar1 << 4) + ((TempChar2 >> 2) & 0x0F);

      break;

    default:

      return EFI_INVALID_PARAMETER;
  }

  return EFI_SUCCESS;
}

/**
  Convert a UCS2 string to a UTF8 encoded string.

  @param[in]    Ucs2Str                The provided UCS2 string.
  @param[out]   Utf8StrAddr            The converted UTF8 string address. Caller
                                       is responsible for Free this string.

  @retval       EFI_INVALID_PARAMETER  One or more parameters are invalid.
  @retval       EFI_OUT_OF_RESOURCES   System runs out of resources.
  @retval       EFI_SUCCESS            The UTF8 encoded string has been converted.

**/
EFI_STATUS
UCS2StrToUTF8 (
  IN  CHAR16     *Ucs2Str,
  OUT CHAR8      **Utf8StrAddr
  )
{
  UINTN    Ucs2StrIndex;
  UINTN    Ucs2StrLength;
  CHAR8    *Utf8Str;
  UINTN    Utf8StrLength;
  UINTN    Utf8StrIndex;
  CHAR8    Utf8Buffer[UTF8_BUFFER_FOR_UCS2_MAX_SIZE];
  UINT8    Utf8BufferSize;

  if (Ucs2Str == NULL || Utf8StrAddr == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  Ucs2StrLength = StrLen (Ucs2Str);
  Utf8StrLength = 0;

  for (Ucs2StrIndex = 0; Ucs2StrIndex < Ucs2StrLength; Ucs2StrIndex ++) {

    ZeroMem (Utf8Buffer, sizeof (Utf8Buffer));
    Utf8BufferSize = UCS2CharToUTF8 (Ucs2Str[Ucs2StrIndex], Utf8Buffer);
    Utf8StrLength += Utf8BufferSize;
  }

  Utf8Str = AllocateZeroPool (Utf8StrLength + 1);
  if (Utf8Str == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  Utf8StrIndex = 0;
  for (Ucs2StrIndex = 0; Ucs2StrIndex < Ucs2StrLength; Ucs2StrIndex ++) {

    ZeroMem (Utf8Buffer, sizeof (Utf8Buffer));
    Utf8BufferSize = UCS2CharToUTF8 (Ucs2Str[Ucs2StrIndex], Utf8Buffer);

    CopyMem (Utf8Str + Utf8StrIndex, Utf8Buffer, Utf8BufferSize);
    Utf8StrIndex += Utf8BufferSize;
  }

  Utf8Str[Utf8StrIndex] = '\0';
  *Utf8StrAddr = Utf8Str;

  return EFI_SUCCESS;
}

/**
  Convert a UTF8 encoded string to a UCS2 string.

  @param[in]    Utf8Str                The provided UTF8 encoded string.
  @param[out]   Ucs2StrAddr            The converted UCS2 string address. Caller
                                       is responsible for Free this string.

  @retval       EFI_INVALID_PARAMETER  The UTF8 encoded string is not valid to
                                       convert to UCS2 string.
                                       One or more parameters are invalid.
  @retval       EFI_OUT_OF_RESOURCES   System runs out of resources.
  @retval       EFI_SUCCESS            The UCS2 string has been converted.

**/
EFI_STATUS
UTF8StrToUCS2 (
  IN  CHAR8      *Utf8Str,
  OUT CHAR16     **Ucs2StrAddr
  )
{
  EFI_STATUS    Status;
  UINTN         Utf8StrIndex;
  UINTN         Utf8StrLength;
  UINTN         Ucs2StrIndex;
  UINT8         Utf8BufferSize;
  CHAR16        *Ucs2StrTemp;

  if (Utf8Str == NULL || Ucs2StrAddr == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  //
  // It is not an Ascii string, calculate string length.
  //
  Utf8StrLength = 0;
  while (*(Utf8Str + Utf8StrLength) != '\0') {
    Utf8StrLength ++;
  }

  //
  // UCS2 string shall not be longer than the UTF8 string.
  //
  Ucs2StrTemp = AllocateZeroPool ((Utf8StrLength + 1) * sizeof (CHAR16));
  if (Ucs2StrTemp == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  Utf8StrIndex = 0;
  Ucs2StrIndex = 0;
  while (Utf8Str[Utf8StrIndex] != '\0') {

    if (CompareMem (Utf8Str + Utf8StrIndex, "\\u", 2) == 0 &&
      Utf8StrLength - Utf8StrIndex >= UNICODE_FORMAT_LEN) {

      Status = GetUCS2CharByFormat (Utf8Str + Utf8StrIndex, Ucs2StrTemp + Ucs2StrIndex);
      if (!EFI_ERROR (Status)) {

        Utf8StrIndex += UNICODE_FORMAT_LEN;
        Ucs2StrIndex ++;
      } else {

        StrCpyS (Ucs2StrTemp + Ucs2StrIndex, 3, L"\\u");

        Ucs2StrIndex += 2;
        Utf8StrIndex += 2;
      }
    } else {

      Utf8BufferSize = GetUTF8SizeForUCS2 (Utf8Str + Utf8StrIndex);
      if (Utf8BufferSize == 0 || Utf8StrLength - Utf8StrIndex < Utf8BufferSize) {

        FreePool (Ucs2StrTemp);
        return EFI_INVALID_PARAMETER;
      }

      Status = UTF8ToUCS2Char (Utf8Str + Utf8StrIndex, Ucs2StrTemp + Ucs2StrIndex);
      if (EFI_ERROR (Status)) {

        FreePool (Ucs2StrTemp);
        return EFI_INVALID_PARAMETER;
      }

      Ucs2StrIndex ++;
      Utf8StrIndex += Utf8BufferSize;
    }
  }

  *Ucs2StrAddr = AllocateZeroPool ((Ucs2StrIndex + 1) * sizeof (CHAR16));
  if (*Ucs2StrAddr == NULL) {

    FreePool (Ucs2StrTemp);
    return EFI_OUT_OF_RESOURCES;
  }

  StrCpyS (*Ucs2StrAddr, Ucs2StrIndex + 1, Ucs2StrTemp);
  *(*Ucs2StrAddr + Ucs2StrIndex) = L'\0';
  FreePool (Ucs2StrTemp);

  return EFI_SUCCESS;
}