util: Use l_utf8_to_ucs2be instead of g_convert

This commit is contained in:
Denis Kenzior 2018-12-21 14:49:02 -06:00
parent 351088e185
commit 9c7a87673a
3 changed files with 40 additions and 47 deletions

View File

@ -3562,7 +3562,7 @@ GSList *sms_text_prepare_with_alphabet(const char *to, const char *utf8,
struct sms template;
int offset = 0;
unsigned char *gsm_encoded = NULL;
char *ucs2_encoded = NULL;
void *ucs2_encoded = NULL;
long written;
long left;
guint8 seq;
@ -3587,17 +3587,16 @@ GSList *sms_text_prepare_with_alphabet(const char *to, const char *utf8,
gsm_encoded = convert_utf8_to_gsm_best_lang(utf8, -1, NULL, &written, 0,
alphabet, &used_locking,
&used_single);
if (gsm_encoded == NULL) {
gsize converted;
if (!gsm_encoded) {
size_t converted;
ucs2_encoded = g_convert(utf8, -1, "UCS-2BE//TRANSLIT", "UTF-8",
NULL, &converted, NULL);
written = converted;
ucs2_encoded = l_utf8_to_ucs2be(utf8, &converted);
if (!ucs2_encoded)
return NULL;
written = converted - 2;
}
if (gsm_encoded == NULL && ucs2_encoded == NULL)
return NULL;
if (gsm_encoded != NULL)
template.submit.dcs = 0x00; /* Class Unspecified, 7 Bit */
else
@ -3641,7 +3640,7 @@ GSList *sms_text_prepare_with_alphabet(const char *to, const char *utf8,
template.submit.udl = written + offset;
memcpy(template.submit.ud + offset, ucs2_encoded, written);
g_free(ucs2_encoded);
l_free(ucs2_encoded);
return sms_list_append(NULL, &template);
}
@ -3716,7 +3715,7 @@ GSList *sms_text_prepare_with_alphabet(const char *to, const char *utf8,
g_free(gsm_encoded);
if (ucs2_encoded)
g_free(ucs2_encoded);
l_free(ucs2_encoded);
if (left > 0) {
g_slist_free_full(r, g_free);

View File

@ -4088,26 +4088,23 @@ static gboolean stk_tlv_builder_append_gsm_unpacked(
static gboolean stk_tlv_builder_append_ucs2(struct stk_tlv_builder *iter,
const char *text)
{
unsigned char *ucs2;
gsize gwritten;
L_AUTO_FREE_VAR(uint8_t *, ucs2);
size_t ucs2_len;
ucs2 = (unsigned char *) g_convert((const gchar *) text, -1,
"UCS-2BE", "UTF-8//TRANSLIT",
NULL, &gwritten, NULL);
if (ucs2 == NULL)
ucs2 = l_utf8_to_ucs2be(text, &ucs2_len);
if (!ucs2)
return FALSE;
if (iter->len + gwritten >= iter->max_len) {
g_free(ucs2);
/* Don't include terminating NULL */
ucs2_len -= 2;
if (iter->len + ucs2_len >= iter->max_len)
return FALSE;
}
iter->value[iter->len++] = 0x08;
memcpy(iter->value + iter->len, ucs2, gwritten);
iter->len += gwritten;
g_free(ucs2);
memcpy(iter->value + iter->len, ucs2, ucs2_len);
iter->len += ucs2_len;
return TRUE;
}
@ -5307,21 +5304,22 @@ static gboolean build_dataobj_registry_application_data(
{
const struct stk_registry_application_data *rad = data;
unsigned char tag = STK_DATA_OBJECT_TYPE_REGISTRY_APPLICATION_DATA;
guint8 dcs, *name;
gsize len;
uint8_t dcs;
L_AUTO_FREE_VAR(uint8_t *, name);
size_t len;
long gsmlen;
name = convert_utf8_to_gsm(rad->name, -1, NULL, &gsmlen, 0);
len = gsmlen;
dcs = 0x04;
if (name == NULL) {
name = (guint8 *) g_convert((const gchar *) rad->name, -1,
"UCS-2BE", "UTF-8//TRANSLIT",
NULL, &len, NULL);
dcs = 0x08;
if (name == NULL)
if (!name) {
name = l_utf8_to_ucs2be(rad->name, &len);
if (!name)
return FALSE;
/* len includes null terminator, so strip it */
len -= 2;
dcs = 0x08;
}
return stk_tlv_builder_open_container(tlv, cr, tag, TRUE) &&

View File

@ -3673,9 +3673,9 @@ unsigned char *utf8_to_sim_string(const char *utf, int max_length,
int *out_length)
{
unsigned char *result;
unsigned char *ucs2;
void *ucs2;
long gsm_bytes;
gsize converted;
size_t converted;
result = convert_utf8_to_gsm(utf, -1, NULL, &gsm_bytes, 0);
if (result) {
@ -3691,25 +3691,21 @@ unsigned char *utf8_to_sim_string(const char *utf, int max_length,
/* NOTE: UCS2 formats with an offset are never used */
ucs2 = (guint8 *) g_convert(utf, -1, "UCS-2BE//TRANSLIT", "UTF-8",
NULL, &converted, NULL);
if (ucs2 == NULL)
ucs2 = l_utf8_to_ucs2be(utf, &converted);
if (!ucs2)
return NULL;
/* converted includes null terminator, drop */
converted -= 2;
if (max_length != -1 && (int) converted + 1 > max_length)
converted = (max_length - 1) & ~1;
result = g_try_malloc(converted + 1);
if (result == NULL) {
g_free(ucs2);
return NULL;
}
*out_length = converted + 1;
result = l_malloc(converted + 1);
result[0] = 0x80;
memcpy(&result[1], ucs2, converted);
g_free(ucs2);
*out_length = converted + 1;
l_free(ucs2);
return result;
}