Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit b51d0f0

Browse files
[[ Bug 14413 ]] Make ConvertCStringFromNativeToUTF8 (and opposite) part of ExternalV0 rev4
1 parent 9b0a03a commit b51d0f0

File tree

6 files changed

+86
-159
lines changed

6 files changed

+86
-159
lines changed

engine/src/externalv0.cpp

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ extern MCExecContext *MCECptr;
6363
// IM-2014-03-06: [[ revBrowserCEF ]] Add revision number to v0 external interface
6464
// SN-2014-07-08: [[ UnicodeExternalsV0 ]] Bump revision number after unicode update
6565
// AL-2015-02-06: [[ SB Inclusions ]] Increment revision number of v0 external interface
66-
#define EXTERNAL_INTERFACE_VERSION 3
66+
// SN-2015-03-12: [[ Bug 14413 ]] Increment revision number, for the addition of
67+
// UTF-8 <-> native string functions.
68+
#define EXTERNAL_INTERFACE_VERSION 4
6769

6870
typedef struct _Xternal
6971
{
@@ -1718,6 +1720,46 @@ static char *resolve_symbol_in_module(const char *arg1, const char *arg2,
17181720
return nil;
17191721
}
17201722

1723+
////////////////////////////////////////////////////////////////////////////////
1724+
// V4: UTF-8 <-> Native string conversion
1725+
// arg1 contains the string to convert, for both of the functions.
1726+
static char *convert_from_native_to_utf8(const char *arg1, const char *arg2,
1727+
const char *arg3, int *retval)
1728+
{
1729+
MCAutoStringRef t_input;
1730+
char* t_utf8_string;
1731+
if (arg1 == NULL
1732+
|| !MCStringCreateWithNativeChars((char_t*)arg1, strlen(arg1), &t_input)
1733+
|| !MCStringConvertToUTF8String(*t_input, t_utf8_string))
1734+
{
1735+
*retval = xresFail;
1736+
return nil;
1737+
}
1738+
1739+
*retval = xresSucc;
1740+
return t_utf8_string;
1741+
}
1742+
1743+
static char *convert_to_native_from_utf8(const char *arg1, const char *arg2,
1744+
const char *arg3, int *retval)
1745+
{
1746+
MCAutoStringRef t_input;
1747+
char *t_native_string;
1748+
1749+
if (arg1 == NULL
1750+
|| !MCStringCreateWithBytes((byte_t*)arg1, strlen(arg1), kMCStringEncodingUTF8, false, &t_input)
1751+
|| !MCStringConvertToCString(*t_input, t_native_string))
1752+
{
1753+
*retval = xresFail;
1754+
return nil;
1755+
}
1756+
1757+
*retval = xresSucc;
1758+
return t_native_string;
1759+
}
1760+
1761+
////////////////////////////////////////////////////////////////////////////////
1762+
17211763
// IM-2014-03-06: [[ revBrowserCEF ]] Add externals extension to the callback list
17221764
// SN-2014-07-08: [[ UnicodeExternalsV0 ]] Add externals extension to handle UTF8-encoded parameters
17231765
// AL-2015-02-06: [[ SB Inclusions ]] Add new callbacks for resource loading.
@@ -1788,6 +1830,9 @@ XCB MCcbs[] =
17881830
/* V3 */ unload_module,
17891831
/* V3 */ resolve_symbol_in_module,
17901832

1833+
/* V4 */ convert_from_native_to_utf8,
1834+
/* V4 */ convert_to_native_from_utf8,
1835+
17911836
NULL
17921837
};
17931838

libexternal/include/revolution/external.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,11 @@ extern Bool SecurityCanAccessLibrary(const char *p_library);
511511
extern Bool SecurityCanAccessFileUTF8(const char *p_file);
512512
extern Bool SecurityCanAccessHostUTF8(const char *p_host);
513513
extern Bool SecurityCanAccessLibraryUTF8(const char *p_library);
514+
515+
516+
// SN-2015-03-12: [[ Bug 14413 ]] Added UTF-8 <-> native string conversion
517+
extern char *ConvertCStringFromNativeToUTF8(const char *p_native, int *r_success);
518+
extern char *ConvertCStringToNativeFromUTF8(const char *p_utf8, int *r_success);
514519

515520
#ifdef __cplusplus
516521
};

libexternal/src/external.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ enum
7474
/* V3 */ OPERATION_LOAD_MODULE,
7575
/* V3 */ OPERATION_UNLOAD_MODULE,
7676
/* V3 */ OPERATION_RESOLVE_SYMBOL_IN_MODULE,
77+
78+
// SN-2015-03-12: [[ Bug 14413 ]] Add new UTF-8 <-> native conversion functions
79+
/* V4 */ OPERATION_CONVERT_FROM_NATIVE_TO_UTF8,
80+
/* V4 */ OPERATION_CONVERT_TO_NATIVE_FROM_UTF8,
7781
};
7882

7983
enum
@@ -905,6 +909,37 @@ void ResolveSymbolInModule(void *p_handle, const char *p_symbol, void **r_resolv
905909
}
906910

907911
////////////////////////////////////////////////////////////////////////////////
912+
// V4: UTF-8 <-> native string conversion
913+
914+
char *ConvertCStringFromNativeToUTF8(const char *p_native, int *r_success)
915+
{
916+
char *t_result;
917+
918+
if (s_external_interface_version < 4)
919+
{
920+
*r_success = EXTERNAL_FAILURE;
921+
return;
922+
}
923+
924+
t_result = (s_operations[OPERATION_CONVERT_FROM_NATIVE_TO_UTF8])(p_native, NULL, NULL, r_success);
925+
926+
return t_result;
927+
}
928+
929+
char *ConvertCStringToNativeFromUTF8(const char *p_utf8, int *r_success)
930+
{
931+
char *t_result;
932+
933+
if (s_external_interface_version < 4)
934+
{
935+
*r_success = EXTERNAL_FAILURE;
936+
return;
937+
}
938+
939+
t_result = (s_operations[OPERATION_CONVERT_TO_NATIVE_FROM_UTF8])(p_utf8, NULL, NULL, r_success);
940+
941+
return t_result;
942+
}
908943

909944
#ifdef TARGET_SUBPLATFORM_IPHONE
910945
struct LibExport

libexternal/src/osxsupport.cpp

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -237,35 +237,3 @@ char *os_path_resolve(const char *p_native_path)
237237
}
238238
return newname;
239239
}
240-
241-
// SN-2015-03-10:[[ Bug 14413 ]] Added UTF-8 conversion functions
242-
243-
// Parameters:
244-
// p_utf8_string : pointer to UTF-8 encoded string.
245-
// Returns:
246-
// a pointer to the native-encoded string. Must be freed by the caller
247-
// Semantics:
248-
// Converts a UTF-8 encoded srting into a Native string
249-
char *ConvertCStringFromUTF8ToNative(const char* p_utf8_path, int *r_success)
250-
{
251-
char *t_native_string;
252-
t_native_string = string_from_utf8(p_utf8_path);
253-
254-
*r_success = t_native_string != NULL;
255-
return t_native_string;
256-
}
257-
258-
// Parameters:
259-
// p_native_string : pointer to native-encoded string.
260-
// Returns:
261-
// a pointer to the UTF-8 encoded string. Must be freed by the caller
262-
// Semantics:
263-
// Converts a native srting into a UTF-8 encoded string
264-
char *ConvertCStringFromNativeToUTF8(const char* p_native_string, int *r_success)
265-
{
266-
char *t_utf8_string;
267-
t_utf8_string = string_to_utf8(p_native_string);
268-
269-
*r_success = t_utf8_string != NULL;
270-
return t_utf8_string;
271-
}

libexternal/src/unxsupport.cpp

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -50,42 +50,6 @@ char *string_to_utf8(const char *p_string)
5050
return t_utf8_string;
5151
}
5252

53-
char *string_from_utf8(const char* p_utf8_string)
54-
{
55-
char *t_iso_string;
56-
t_iso_string = (char*)malloc(strlen(p_utf8_string) + 1);
57-
58-
int i, j, t_error;
59-
t_error = 0;
60-
61-
for (i = 0, j = 0; p_utf8_string[i] != '\0' && !t_error;)
62-
{
63-
unsigned char t_first_char;
64-
t_first_char = ((unsigned char *)p_utf8_string)[i++];
65-
66-
if (t_first_char < 128)
67-
t_iso_string[j++] = t_first_char;
68-
else if (p_utf8_string[i] != '\0')
69-
{
70-
unsigned char t_second_char;
71-
t_second_char = p_utf8_string[i++];
72-
73-
// SN-2015-03-11: [[ Bug 14413 ]] Take the 6 right bit of the first char.
74-
t_iso_string[j++] = ((t_first_char & 0x3F) << 6)
75-
| (t_second_char & 0x63);
76-
}
77-
else
78-
t_error = true;
79-
}
80-
81-
if (t_error)
82-
return NULL;
83-
else
84-
{
85-
t_iso_string[j] = '\0';
86-
return t_iso_string;
87-
}
88-
}
8953
// LINUX implimentation of externals.
9054
char *os_path_to_native(const char *p_path)
9155
{
@@ -189,37 +153,3 @@ char *os_path_resolve(const char *p_native_path)
189153
}
190154
return newname;
191155
}
192-
193-
194-
// SN-2015-03-10:[[ Bug 14413 ]] Added UTF-8 conversion functions
195-
196-
// Parameters:
197-
// p_utf8_string : pointer to UTF-8 encoded string.
198-
// Returns:
199-
// a pointer to the native-encoded string. Must be freed by the caller
200-
// Semantics:
201-
// Converts a UTF-8 encoded srting into a Native string
202-
char *ConvertCStringFromUTF8ToNative(const char* p_utf8_path, int *r_success)
203-
{
204-
char *t_native_string;
205-
t_native_string = string_from_utf8(p_utf8_path);
206-
207-
*r_success = t_native_string != NULL;
208-
return t_native_string;
209-
}
210-
211-
// Parameters:
212-
// p_native_string : pointer to native-encoded string.
213-
// Returns:
214-
// a pointer to the UTF-8 encoded string. Must be freed by the caller
215-
// Semantics:
216-
// Converts a native srting into a UTF-8 encoded string
217-
char *ConvertCStringFromNativeToUTF8(const char* p_native_string, int *r_success)
218-
{
219-
char *t_utf8_string;
220-
t_utf8_string = string_to_utf8(p_native_string);
221-
222-
*r_success = t_utf8_string != NULL;
223-
return t_utf8_string;
224-
}
225-

libexternal/src/w32support.cpp

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,6 @@ char *string_to_utf8(const char *p_native_path)
3636
return t_utf8_path;
3737
}
3838

39-
char *string_from_utf8(const char *p_utf8_string)
40-
{
41-
int t_length;
42-
// Make sure that the length includes the NULL terminating char
43-
t_length = strlen(p_utf8_string) + 1;
44-
45-
WCHAR *t_utf16_path;
46-
t_utf16_path = (WCHAR *)malloc(sizeof(WCHAR) * t_length);
47-
MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED, p_utf8_string, t_length, t_utf16_path, t_length);
48-
49-
int t_native_length;
50-
t_native_length = WideCharToMultiByte(CP_ACP, 0, t_utf16_path, t_length, NULL, 0, NULL, NULL);
51-
52-
char *t_native_string;
53-
t_native_string = (char *)malloc(t_native_length);
54-
55-
WideCharToMultiByte(CP_ACP, 0, t_utf16_path, t_length, t_native_string, t_native_length, NULL, NULL);
56-
57-
free(t_utf16_path);
58-
59-
return t_native_string;
60-
}
61-
6239
char *os_path_to_native_utf8(const char *p_path)
6340
{
6441
char *t_native_path;
@@ -142,36 +119,3 @@ char *os_path_resolve(const char *p_native_path)
142119
cstr = os_path_to_native(cstr);
143120
return cstr;
144121
}
145-
146-
147-
// SN-2015-03-10:[[ Bug 14413 ]] Added UTF-8 conversion functions
148-
149-
// Parameters:
150-
// p_utf8_string : pointer to UTF-8 encoded string.
151-
// Returns:
152-
// a pointer to the native-encoded string. Must be freed by the caller
153-
// Semantics:
154-
// Converts a UTF-8 encoded srting into a Native string
155-
char *ConvertCStringFromUTF8ToNative(const char* p_utf8_path, int *r_success)
156-
{
157-
char *t_native_string;
158-
t_native_string = string_from_utf8(p_utf8_path);
159-
160-
*r_success = t_native_string != NULL;
161-
return t_native_string;
162-
}
163-
164-
// Parameters:
165-
// p_native_string : pointer to native-encoded string.
166-
// Returns:
167-
// a pointer to the UTF-8 encoded string. Must be freed by the caller
168-
// Semantics:
169-
// Converts a native srting into a UTF-8 encoded string
170-
char *ConvertCStringFromNativeToUTF8(const char* p_native_string, int *r_success)
171-
{
172-
char *t_utf8_string;
173-
t_utf8_string = string_to_utf8(p_native_string);
174-
175-
*r_success = t_utf8_string != NULL;
176-
return t_utf8_string;
177-
}

0 commit comments

Comments
 (0)