2 * Copyright 2007-2013, Axel Dörfler, axeld@pinc-software.de.
3 * Copyright 2009, Michael Lotz, mmlr@mlotz.ch. All rights reserved.
5 * Distributed under the terms of the MIT License.
14 # include <utf8_functions.h>
17 #include "gpt_known_guids.h"
20 const guid_t kEmptyGUID
= {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}};
24 put_utf8_byte(char*& to
, size_t& left
, char c
)
38 to_utf8(const uint16
* from
, size_t maxFromLength
, char* to
, size_t toSize
)
40 const char* start
= to
;
41 for (uint32 i
= 0; i
< maxFromLength
; i
++) {
44 uint16 w1
= B_LENDIAN_TO_HOST_INT16(from
[i
]);
49 if (w1
< 0xD800 || w1
> 0xDFFF) {
54 if (!valid
&& (w1
>= 0xD800 && w1
<= 0xDBFF)) {
55 if (i
+ 1 < maxFromLength
) {
56 uint16 w2
= B_LENDIAN_TO_HOST_INT16(from
[i
+ 1]);
57 if (w2
>= 0xDC00 && w2
<= 0xDFFF) {
58 c
= ((w1
& 0x3FF) << 10) | (w2
& 0x3FF);
69 put_utf8_byte(to
, toSize
, c
);
71 put_utf8_byte(to
, toSize
, 0xc0 | (c
>> 6));
72 put_utf8_byte(to
, toSize
, 0x80 | (c
& 0x3f));
73 } else if (c
< 0x10000) {
74 put_utf8_byte(to
, toSize
, 0xe0 | (c
>> 12));
75 put_utf8_byte(to
, toSize
, 0x80 | ((c
>> 6) & 0x3f));
76 put_utf8_byte(to
, toSize
, 0x80 | (c
& 0x3f));
77 } else if (c
<= 0x10ffff) {
78 put_utf8_byte(to
, toSize
, 0xf0 | (c
>> 18));
79 put_utf8_byte(to
, toSize
, 0x80 | ((c
>> 12) & 0x3f));
80 put_utf8_byte(to
, toSize
, 0x80 | ((c
>> 6) & 0x3f));
81 put_utf8_byte(to
, toSize
, 0x80 | (c
& 0x3f));
94 to_ucs2(const char* from
, size_t fromLength
, uint16
* to
, size_t maxToLength
)
97 while (from
[0] != '\0' && index
< maxToLength
) {
98 uint32 c
= UTF8ToCharCode(&from
);
101 if (c
> 0x10FFFF) break; // invalid
103 to
[index
++] = B_HOST_TO_LENDIAN_INT16(c
);
105 if (index
+ 1 >= maxToLength
) break;
106 uint32 c2
= c
- 0x10000;
107 uint16 w1
= 0xD800, w2
= 0xDC00;
108 w1
= w1
+ ((c2
>> 10) & 0x3FF);
109 w2
= w2
+ (c2
& 0x3FF);
110 to
[index
++] = B_HOST_TO_LENDIAN_INT16(w1
);
111 to
[index
++] = B_HOST_TO_LENDIAN_INT16(w2
);
115 if (index
< maxToLength
)
120 #endif // !_BOOT_MODE
124 get_partition_type(const guid_t
& guid
)
126 for (uint32 i
= 0; i
< sizeof(kTypeMap
) / sizeof(kTypeMap
[0]); i
++) {
127 if (kTypeMap
[i
].guid
== guid
)
128 return kTypeMap
[i
].type
;
137 get_guid_for_partition_type(const char* type
, guid_t
& guid
)
139 for (uint32 i
= 0; i
< sizeof(kTypeMap
) / sizeof(kTypeMap
[0]); i
++) {
140 if (strcmp(kTypeMap
[i
].type
, type
) == 0) {
141 guid
= kTypeMap
[i
].guid
;
148 #endif // !_BOOT_MODE