BaseUcs2Utf8Lib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /** @file
  2. UCS2 to UTF8 manipulation library.
  3. Copyright (c) 2018 - 2019, Intel Corporation. All rights reserved.<BR>
  4. (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include <Uefi.h>
  8. #include <Library/BaseLib.h>
  9. #include <Library/BaseMemoryLib.h>
  10. #include <Library/BaseUcs2Utf8Lib.h>
  11. #include <Library/DebugLib.h>
  12. #include <Library/MemoryAllocationLib.h>
  13. /**
  14. Since each UCS2 character can be represented by 1-3 UTF8 encoded characters,
  15. this function is used to retrieve the UTF8 encoding size for a UCS2 character.
  16. @param[in] Utf8Buffer The buffer for UTF8 encoded data.
  17. @retval Return the size of UTF8 encoding string or 0 if it is not for
  18. UCS2 format.
  19. **/
  20. UINT8
  21. GetUTF8SizeForUCS2 (
  22. IN CHAR8 *Utf8Buffer
  23. )
  24. {
  25. CHAR8 TempChar;
  26. UINT8 Utf8Size;
  27. ASSERT (Utf8Buffer != NULL);
  28. TempChar = *Utf8Buffer;
  29. if ((TempChar & 0xF0) == 0xF0) {
  30. //
  31. // This format is not for UCS2.
  32. //
  33. return 0;
  34. }
  35. Utf8Size = 1;
  36. if ((TempChar & 0x80) == 0x80) {
  37. if ((TempChar & 0xC0) == 0xC0) {
  38. Utf8Size++;
  39. if ((TempChar & 0xE0) == 0xE0) {
  40. Utf8Size++;
  41. }
  42. }
  43. }
  44. return Utf8Size;
  45. }
  46. /**
  47. Since each UCS2 character can be represented by the format: \uXXXX, this function
  48. is used to retrieve the UCS2 character from a Unicode format.
  49. Call MUST make sure there are at least 6 Bytes in the input UTF8 buffer.
  50. @param[in] Utf8Buffer The buffer for UTF8 encoded data.
  51. @param[out] Ucs2Char The converted UCS2 character.
  52. @retval EFI_INVALID_PARAMETER Non-Ascii characters found in the hexadecimal
  53. digits string, and can't be converted to a UCS2
  54. character.
  55. @retval EFI_SUCCESS The UCS2 character has been retrieved.
  56. **/
  57. EFI_STATUS
  58. GetUCS2CharByFormat (
  59. IN CHAR8 *Utf8Buffer,
  60. OUT CHAR16 *Ucs2Char
  61. )
  62. {
  63. UINT8 Num1;
  64. UINT8 Num2;
  65. UINT8 Index;
  66. CHAR8 Ucs2CharFormat[UNICODE_FORMAT_CHAR_SIZE]; /// two Hexadecimal digits Ascii string, like "3F"
  67. for (Index = 0; Index < 4; Index++) {
  68. if ((*(Utf8Buffer + 2 + Index) & 0x80) != 0x00) {
  69. return EFI_INVALID_PARAMETER;
  70. }
  71. }
  72. ZeroMem (Ucs2CharFormat, UNICODE_FORMAT_CHAR_SIZE);
  73. //
  74. // Get the First Number, Offset is 2
  75. //
  76. CopyMem (Ucs2CharFormat, Utf8Buffer + 2, UNICODE_FORMAT_CHAR_LEN);
  77. Num1 = (UINT8)AsciiStrHexToUintn (Ucs2CharFormat);
  78. //
  79. // Get the Second Number, Offset is 4
  80. //
  81. CopyMem (Ucs2CharFormat, Utf8Buffer + 4, UNICODE_FORMAT_CHAR_LEN);
  82. Num2 = (UINT8)AsciiStrHexToUintn (Ucs2CharFormat);
  83. //
  84. // Ucs2Char is Little-Endian
  85. //
  86. *((CHAR8 *)Ucs2Char) = Num2;
  87. *(((CHAR8 *)Ucs2Char) + 1) = Num1;
  88. return EFI_SUCCESS;
  89. }
  90. /**
  91. Convert a UCS2 character to UTF8 encoding string.
  92. @param[in] Ucs2Char The provided UCS2 character.
  93. @param[out] Utf8Buffer The converted UTF8 encoded data.
  94. @retval Return the size of UTF8 encoding data for this UCS2 character.
  95. **/
  96. UINT8
  97. UCS2CharToUTF8 (
  98. IN CHAR16 Ucs2Char,
  99. OUT CHAR8 *Utf8Buffer
  100. )
  101. {
  102. UINT16 Ucs2Number;
  103. ASSERT (Utf8Buffer != NULL);
  104. Ucs2Number = (UINT16)Ucs2Char;
  105. if (Ucs2Number <= 0x007F) {
  106. //
  107. // UTF8 format: 0xxxxxxx
  108. //
  109. *Utf8Buffer = Ucs2Char & 0x7F;
  110. return 1;
  111. } else if ((Ucs2Number >= 0x0080) && (Ucs2Number <= 0x07FF)) {
  112. //
  113. // UTF8 format: 110xxxxx 10xxxxxx
  114. //
  115. *(Utf8Buffer + 1) = (Ucs2Char & 0x3F) | 0x80;
  116. *Utf8Buffer = ((Ucs2Char >> 6) & 0x1F) | 0xC0;
  117. return 2;
  118. } else {
  119. /// Ucs2Number >= 0x0800 && Ucs2Number <= 0xFFFF
  120. //
  121. // UTF8 format: 1110xxxx 10xxxxxx 10xxxxxx
  122. //
  123. *(Utf8Buffer + 2) = (Ucs2Char & 0x3F) | 0x80;
  124. *(Utf8Buffer + 1) = ((Ucs2Char >> 6) & 0x3F) | 0x80;
  125. *Utf8Buffer = ((Ucs2Char >> 12) & 0x0F) | 0xE0;
  126. return 3;
  127. }
  128. }
  129. /**
  130. Convert a UTF8 encoded data to a UCS2 character.
  131. @param[in] Utf8Buffer The provided UTF8 encoded data.
  132. @param[out] Ucs2Char The converted UCS2 character.
  133. @retval EFI_INVALID_PARAMETER The UTF8 encoded string is not valid or
  134. not for UCS2 character.
  135. @retval EFI_SUCCESS The converted UCS2 character.
  136. **/
  137. EFI_STATUS
  138. UTF8ToUCS2Char (
  139. IN CHAR8 *Utf8Buffer,
  140. OUT CHAR16 *Ucs2Char
  141. )
  142. {
  143. UINT8 Utf8Size;
  144. CHAR8 *Ucs2Buffer;
  145. CHAR8 TempChar1;
  146. CHAR8 TempChar2;
  147. CHAR8 TempChar3;
  148. ASSERT (Utf8Buffer != NULL && Ucs2Char != NULL);
  149. ZeroMem (Ucs2Char, sizeof (CHAR16));
  150. Ucs2Buffer = (CHAR8 *)Ucs2Char;
  151. Utf8Size = GetUTF8SizeForUCS2 (Utf8Buffer);
  152. switch (Utf8Size) {
  153. case 1:
  154. //
  155. // UTF8 format: 0xxxxxxx
  156. //
  157. TempChar1 = *Utf8Buffer;
  158. if ((TempChar1 & 0x80) != 0x00) {
  159. return EFI_INVALID_PARAMETER;
  160. }
  161. *Ucs2Buffer = TempChar1;
  162. *(Ucs2Buffer + 1) = 0;
  163. break;
  164. case 2:
  165. //
  166. // UTF8 format: 110xxxxx 10xxxxxx
  167. //
  168. TempChar1 = *Utf8Buffer;
  169. if ((TempChar1 & 0xE0) != 0xC0) {
  170. return EFI_INVALID_PARAMETER;
  171. }
  172. TempChar2 = *(Utf8Buffer + 1);
  173. if ((TempChar2 & 0xC0) != 0x80) {
  174. return EFI_INVALID_PARAMETER;
  175. }
  176. *Ucs2Buffer = (TempChar1 << 6) + (TempChar2 & 0x3F);
  177. *(Ucs2Buffer + 1) = (TempChar1 >> 2) & 0x07;
  178. break;
  179. case 3:
  180. //
  181. // UTF8 format: 1110xxxx 10xxxxxx 10xxxxxx
  182. //
  183. TempChar1 = *Utf8Buffer;
  184. if ((TempChar1 & 0xF0) != 0xE0) {
  185. return EFI_INVALID_PARAMETER;
  186. }
  187. TempChar2 = *(Utf8Buffer + 1);
  188. if ((TempChar2 & 0xC0) != 0x80) {
  189. return EFI_INVALID_PARAMETER;
  190. }
  191. TempChar3 = *(Utf8Buffer + 2);
  192. if ((TempChar3 & 0xC0) != 0x80) {
  193. return EFI_INVALID_PARAMETER;
  194. }
  195. *Ucs2Buffer = (TempChar2 << 6) + (TempChar3 & 0x3F);
  196. *(Ucs2Buffer + 1) = (TempChar1 << 4) + ((TempChar2 >> 2) & 0x0F);
  197. break;
  198. default:
  199. return EFI_INVALID_PARAMETER;
  200. }
  201. return EFI_SUCCESS;
  202. }
  203. /**
  204. Convert a UCS2 string to a UTF8 encoded string.
  205. @param[in] Ucs2Str The provided UCS2 string.
  206. @param[out] Utf8StrAddr The converted UTF8 string address. Caller
  207. is responsible for Free this string.
  208. @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
  209. @retval EFI_OUT_OF_RESOURCES System runs out of resources.
  210. @retval EFI_SUCCESS The UTF8 encoded string has been converted.
  211. **/
  212. EFI_STATUS
  213. UCS2StrToUTF8 (
  214. IN CHAR16 *Ucs2Str,
  215. OUT CHAR8 **Utf8StrAddr
  216. )
  217. {
  218. UINTN Ucs2StrIndex;
  219. UINTN Ucs2StrLength;
  220. CHAR8 *Utf8Str;
  221. UINTN Utf8StrLength;
  222. UINTN Utf8StrIndex;
  223. CHAR8 Utf8Buffer[UTF8_BUFFER_FOR_UCS2_MAX_SIZE];
  224. UINT8 Utf8BufferSize;
  225. if ((Ucs2Str == NULL) || (Utf8StrAddr == NULL)) {
  226. return EFI_INVALID_PARAMETER;
  227. }
  228. Ucs2StrLength = StrLen (Ucs2Str);
  229. Utf8StrLength = 0;
  230. for (Ucs2StrIndex = 0; Ucs2StrIndex < Ucs2StrLength; Ucs2StrIndex++) {
  231. ZeroMem (Utf8Buffer, sizeof (Utf8Buffer));
  232. Utf8BufferSize = UCS2CharToUTF8 (Ucs2Str[Ucs2StrIndex], Utf8Buffer);
  233. Utf8StrLength += Utf8BufferSize;
  234. }
  235. Utf8Str = AllocateZeroPool (Utf8StrLength + 1);
  236. if (Utf8Str == NULL) {
  237. return EFI_OUT_OF_RESOURCES;
  238. }
  239. Utf8StrIndex = 0;
  240. for (Ucs2StrIndex = 0; Ucs2StrIndex < Ucs2StrLength; Ucs2StrIndex++) {
  241. ZeroMem (Utf8Buffer, sizeof (Utf8Buffer));
  242. Utf8BufferSize = UCS2CharToUTF8 (Ucs2Str[Ucs2StrIndex], Utf8Buffer);
  243. CopyMem (Utf8Str + Utf8StrIndex, Utf8Buffer, Utf8BufferSize);
  244. Utf8StrIndex += Utf8BufferSize;
  245. }
  246. Utf8Str[Utf8StrIndex] = '\0';
  247. *Utf8StrAddr = Utf8Str;
  248. return EFI_SUCCESS;
  249. }
  250. /**
  251. Convert a UTF8 encoded string to a UCS2 string.
  252. @param[in] Utf8Str The provided UTF8 encoded string.
  253. @param[out] Ucs2StrAddr The converted UCS2 string address. Caller
  254. is responsible for Free this string.
  255. @retval EFI_INVALID_PARAMETER The UTF8 encoded string is not valid to
  256. convert to UCS2 string.
  257. One or more parameters are invalid.
  258. @retval EFI_OUT_OF_RESOURCES System runs out of resources.
  259. @retval EFI_SUCCESS The UCS2 string has been converted.
  260. **/
  261. EFI_STATUS
  262. UTF8StrToUCS2 (
  263. IN CHAR8 *Utf8Str,
  264. OUT CHAR16 **Ucs2StrAddr
  265. )
  266. {
  267. EFI_STATUS Status;
  268. UINTN Utf8StrIndex;
  269. UINTN Utf8StrLength;
  270. UINTN Ucs2StrIndex;
  271. UINT8 Utf8BufferSize;
  272. CHAR16 *Ucs2StrTemp;
  273. if ((Utf8Str == NULL) || (Ucs2StrAddr == NULL)) {
  274. return EFI_INVALID_PARAMETER;
  275. }
  276. //
  277. // It is not an Ascii string, calculate string length.
  278. //
  279. Utf8StrLength = 0;
  280. while (*(Utf8Str + Utf8StrLength) != '\0') {
  281. Utf8StrLength++;
  282. }
  283. //
  284. // UCS2 string shall not be longer than the UTF8 string.
  285. //
  286. Ucs2StrTemp = AllocateZeroPool ((Utf8StrLength + 1) * sizeof (CHAR16));
  287. if (Ucs2StrTemp == NULL) {
  288. return EFI_OUT_OF_RESOURCES;
  289. }
  290. Utf8StrIndex = 0;
  291. Ucs2StrIndex = 0;
  292. while (Utf8Str[Utf8StrIndex] != '\0') {
  293. if ((CompareMem (Utf8Str + Utf8StrIndex, "\\u", 2) == 0) &&
  294. (Utf8StrLength - Utf8StrIndex >= UNICODE_FORMAT_LEN))
  295. {
  296. Status = GetUCS2CharByFormat (Utf8Str + Utf8StrIndex, Ucs2StrTemp + Ucs2StrIndex);
  297. if (!EFI_ERROR (Status)) {
  298. Utf8StrIndex += UNICODE_FORMAT_LEN;
  299. Ucs2StrIndex++;
  300. } else {
  301. StrCpyS (Ucs2StrTemp + Ucs2StrIndex, 3, L"\\u");
  302. Ucs2StrIndex += 2;
  303. Utf8StrIndex += 2;
  304. }
  305. } else {
  306. Utf8BufferSize = GetUTF8SizeForUCS2 (Utf8Str + Utf8StrIndex);
  307. if ((Utf8BufferSize == 0) || (Utf8StrLength - Utf8StrIndex < Utf8BufferSize)) {
  308. FreePool (Ucs2StrTemp);
  309. return EFI_INVALID_PARAMETER;
  310. }
  311. Status = UTF8ToUCS2Char (Utf8Str + Utf8StrIndex, Ucs2StrTemp + Ucs2StrIndex);
  312. if (EFI_ERROR (Status)) {
  313. FreePool (Ucs2StrTemp);
  314. return EFI_INVALID_PARAMETER;
  315. }
  316. Ucs2StrIndex++;
  317. Utf8StrIndex += Utf8BufferSize;
  318. }
  319. }
  320. *Ucs2StrAddr = AllocateZeroPool ((Ucs2StrIndex + 1) * sizeof (CHAR16));
  321. if (*Ucs2StrAddr == NULL) {
  322. FreePool (Ucs2StrTemp);
  323. return EFI_OUT_OF_RESOURCES;
  324. }
  325. StrCpyS (*Ucs2StrAddr, Ucs2StrIndex + 1, Ucs2StrTemp);
  326. *(*Ucs2StrAddr + Ucs2StrIndex) = L'\0';
  327. FreePool (Ucs2StrTemp);
  328. return EFI_SUCCESS;
  329. }