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

Commit 01bc0a1

Browse files
[[ Bug 14413 ]] Add UTF-8 conversion if the archive items are native, not UTF-8 encoded.
Add ConvertCStringFromNativeToUTF8 and ConvertCStringToNativeFromUTF8 in libexternal/support.h
1 parent 2311ac2 commit 01bc0a1

File tree

5 files changed

+218
-7
lines changed

5 files changed

+218
-7
lines changed

libexternal/include/revolution/support.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,23 @@ char *os_path_from_native(const char *p_native_path);
4949
// Resolves a native path into an absolute path, e.g. by expanding "~" etc.
5050
char *os_path_resolve(const char *p_native_path);
5151

52+
53+
// SN-2015-03-10:[[ Bug 14413 ]] Added UTF-8 conversion functions
54+
55+
// Parameters:
56+
// p_utf8_string : pointer to UTF-8 encoded string.
57+
// Returns:
58+
// a pointer to the native-encoded string. Must be freed by the caller
59+
// Semantics:
60+
// Converts a UTF-8 encoded srting into a Native string
61+
char *ConvertCStringFromUTF8ToNative(const char* p_utf8_path, int *r_success);
62+
63+
// Parameters:
64+
// p_native_string : pointer to native-encoded string.
65+
// Returns:
66+
// a pointer to the UTF-8 encoded string. Must be freed by the caller
67+
// Semantics:
68+
// Converts a native srting into a UTF-8 encoded string
69+
char *ConvertCStringFromNativeToUTF8(const char* p_native_string, int *r_success);
70+
5271
#endif

libexternal/src/osxsupport.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,36 @@ char *os_path_resolve(const char *p_native_path)
236236
free(fullpath);
237237
}
238238
return newname;
239-
}
239+
}
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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,41 @@ char *string_to_utf8(const char *p_string)
4949
return t_utf8_string;
5050
}
5151

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

libexternal/src/w32support.cpp

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,29 @@ 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+
3962
char *os_path_to_native_utf8(const char *p_path)
4063
{
4164
char *t_native_path;
@@ -118,4 +141,37 @@ char *os_path_resolve(const char *p_native_path)
118141
char *cstr = strclone(p_native_path);
119142
cstr = os_path_to_native(cstr);
120143
return cstr;
121-
}
144+
}
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+
}

revzip/src/revzip.cpp

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,15 +1267,50 @@ void revZipEnumerateItems(char *p_arguments[], int p_argument_count, char **r_re
12671267
for( int i = 0; i < t_num_files; ++i )
12681268
{
12691269
const char* t_line = NULL;
1270-
t_line = zip_get_name(t_archive, i, 0);
1271-
if( t_line )
1270+
struct zip_stat t_stat;
1271+
1272+
// SN-2015-03-11: [[ Bug 14413 ]] We want to get the bitflags
1273+
// alongside the name: zip_stat_index provides this.
1274+
if (zip_stat_index(t_archive, i, 0, &t_stat) != 0)
12721275
{
1273-
t_str_names += std::string(t_line);
1274-
t_str_names += "\n";
1276+
std::string t_outerr = "ziperr," + std::string((zip_strerror(t_archive)));
1277+
t_result = strdup(t_outerr.c_str());
1278+
t_error = False;
1279+
}
1280+
else
1281+
{
1282+
char *t_converted_name;
1283+
int t_success;
1284+
1285+
// SN-2015-03-10: [[ Bug 14413 ]] We convert the string to UTF-8
1286+
// in case it was natively encoded, as revZipEnumerateItems is
1287+
// meant to return a UTF-8 encoded string.
1288+
if (t_stat.bitflags && ZIP_UTF8_FLAG)
1289+
{
1290+
t_success = 1;
1291+
t_converted_name = strdup(t_stat.name);
1292+
}
1293+
else
1294+
t_converted_name = ConvertCStringFromNativeToUTF8(t_stat.name, &t_success);
1295+
1296+
if (t_success)
1297+
{
1298+
t_str_names += std::string(t_converted_name);
1299+
t_str_names += "\n";
1300+
}
1301+
else
1302+
{
1303+
t_result = strdup("");
1304+
t_error = True;
1305+
break;
1306+
}
1307+
1308+
// Free the allocated memory.
1309+
free(t_converted_name);
12751310
}
12761311
}
12771312

1278-
if( !t_str_names.empty() )
1313+
if( !t_str_names.empty() && t_error == False)
12791314
{
12801315
t_result = strdup(t_str_names.c_str());
12811316
t_error = False;

0 commit comments

Comments
 (0)