43
43
#define STRING_BUFFER_LENGTH 1024
46
* Gets the length (in characters, not bytes) of a unicode
46
* Gets the length (in characters, not bytes) of a unicode
47
47
* UCS-2 string, eg a string which physically is 0x00 0x41 0x00 0x00
48
48
* will return a value of 1.
50
50
* @param unicstr a UCS-2 Unicode string
51
* @return the length of the string, in number of characters. If you
51
* @return the length of the string, in number of characters. If you
52
52
* want to know the length in bytes, multiply this by two and
53
53
* add two (for zero terminator).
55
55
int ucs2_strlen(uint16_t const * const unicstr)
59
59
/* Unicode strings are terminated with 2 * 0x00 */
60
60
for(length = 0; unicstr[length] != 0x0000U; length ++);
80
80
size_t convlen = (ucs2_strlen(unicstr)+1) * sizeof(uint16_t); // UCS-2 is 16 bit wide, include terminator
81
81
size_t convmax = STRING_BUFFER_LENGTH*3;
84
84
/* Do the conversion. */
85
85
nconv = iconv(params->cd_ucs2_to_locale, &stringp, &convlen, &locp, &convmax);
99
* Converts a UTF-8 string to a big-endian UTF-16 2-byte string
100
* Actually just a UCS-2 internal conversion.
102
* @param device a pointer to the current device.
103
* @param localstr the UTF-8 unicode string to convert
104
* @return a UTF-16 string.
106
uint16_t *utf8_to_utf16(LIBMTP_mtpdevice_t *device, const char *localstr)
108
PTPParams *params = (PTPParams *) device->params;
109
char *stringp = (char *) localstr; // cast away "const"
110
char unicstr[(STRING_BUFFER_LENGTH+1)*2]; // UCS2 encoding is 2 bytes per UTF-8 char.
111
char *unip = unicstr;
113
size_t convlen = strlen(localstr)+1; // utf8 bytes, include terminator
114
size_t convmax = STRING_BUFFER_LENGTH*2;
119
/* Do the conversion. */
120
nconv = iconv(params->cd_locale_to_ucs2, &stringp, &convlen, &unip, &convmax);
122
if (nconv == (size_t) -1) {
123
// Return partial string anyway.
127
// make sure the string is null terminated
128
unicstr[STRING_BUFFER_LENGTH*2] = '\0';
129
unicstr[STRING_BUFFER_LENGTH*2+1] = '\0';
131
// allocate the string to be returned
132
// Note: can't use strdup since every other byte is a null byte
133
int ret_len = ucs2_strlen((uint16_t*)unicstr)*sizeof(uint16_t)+2;
134
uint16_t* ret = malloc(ret_len);
135
memcpy(ret,unicstr,(size_t)ret_len);
99
140
* This helper function simply removes any consecutive chars
100
141
* > 0x7F and replace then with an underscore. In UTF-8
101
142
* consequtive chars > 0x7F represent one single character so