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; struct sms template;
int offset = 0; int offset = 0;
unsigned char *gsm_encoded = NULL; unsigned char *gsm_encoded = NULL;
char *ucs2_encoded = NULL; void *ucs2_encoded = NULL;
long written; long written;
long left; long left;
guint8 seq; 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, gsm_encoded = convert_utf8_to_gsm_best_lang(utf8, -1, NULL, &written, 0,
alphabet, &used_locking, alphabet, &used_locking,
&used_single); &used_single);
if (gsm_encoded == NULL) { if (!gsm_encoded) {
gsize converted; size_t converted;
ucs2_encoded = g_convert(utf8, -1, "UCS-2BE//TRANSLIT", "UTF-8", ucs2_encoded = l_utf8_to_ucs2be(utf8, &converted);
NULL, &converted, NULL); if (!ucs2_encoded)
written = converted; return NULL;
written = converted - 2;
} }
if (gsm_encoded == NULL && ucs2_encoded == NULL)
return NULL;
if (gsm_encoded != NULL) if (gsm_encoded != NULL)
template.submit.dcs = 0x00; /* Class Unspecified, 7 Bit */ template.submit.dcs = 0x00; /* Class Unspecified, 7 Bit */
else else
@ -3641,7 +3640,7 @@ GSList *sms_text_prepare_with_alphabet(const char *to, const char *utf8,
template.submit.udl = written + offset; template.submit.udl = written + offset;
memcpy(template.submit.ud + offset, ucs2_encoded, written); memcpy(template.submit.ud + offset, ucs2_encoded, written);
g_free(ucs2_encoded); l_free(ucs2_encoded);
return sms_list_append(NULL, &template); 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); g_free(gsm_encoded);
if (ucs2_encoded) if (ucs2_encoded)
g_free(ucs2_encoded); l_free(ucs2_encoded);
if (left > 0) { if (left > 0) {
g_slist_free_full(r, g_free); 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, static gboolean stk_tlv_builder_append_ucs2(struct stk_tlv_builder *iter,
const char *text) const char *text)
{ {
unsigned char *ucs2; L_AUTO_FREE_VAR(uint8_t *, ucs2);
gsize gwritten; size_t ucs2_len;
ucs2 = (unsigned char *) g_convert((const gchar *) text, -1, ucs2 = l_utf8_to_ucs2be(text, &ucs2_len);
"UCS-2BE", "UTF-8//TRANSLIT", if (!ucs2)
NULL, &gwritten, NULL);
if (ucs2 == NULL)
return FALSE; return FALSE;
if (iter->len + gwritten >= iter->max_len) { /* Don't include terminating NULL */
g_free(ucs2); ucs2_len -= 2;
if (iter->len + ucs2_len >= iter->max_len)
return FALSE; return FALSE;
}
iter->value[iter->len++] = 0x08; iter->value[iter->len++] = 0x08;
memcpy(iter->value + iter->len, ucs2, gwritten); memcpy(iter->value + iter->len, ucs2, ucs2_len);
iter->len += gwritten; iter->len += ucs2_len;
g_free(ucs2);
return TRUE; return TRUE;
} }
@ -5307,21 +5304,22 @@ static gboolean build_dataobj_registry_application_data(
{ {
const struct stk_registry_application_data *rad = data; const struct stk_registry_application_data *rad = data;
unsigned char tag = STK_DATA_OBJECT_TYPE_REGISTRY_APPLICATION_DATA; unsigned char tag = STK_DATA_OBJECT_TYPE_REGISTRY_APPLICATION_DATA;
guint8 dcs, *name; uint8_t dcs;
gsize len; L_AUTO_FREE_VAR(uint8_t *, name);
size_t len;
long gsmlen; long gsmlen;
name = convert_utf8_to_gsm(rad->name, -1, NULL, &gsmlen, 0); name = convert_utf8_to_gsm(rad->name, -1, NULL, &gsmlen, 0);
len = gsmlen; len = gsmlen;
dcs = 0x04; dcs = 0x04;
if (name == NULL) { if (!name) {
name = (guint8 *) g_convert((const gchar *) rad->name, -1, name = l_utf8_to_ucs2be(rad->name, &len);
"UCS-2BE", "UTF-8//TRANSLIT", if (!name)
NULL, &len, NULL);
dcs = 0x08;
if (name == NULL)
return FALSE; return FALSE;
/* len includes null terminator, so strip it */
len -= 2;
dcs = 0x08;
} }
return stk_tlv_builder_open_container(tlv, cr, tag, TRUE) && 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) int *out_length)
{ {
unsigned char *result; unsigned char *result;
unsigned char *ucs2; void *ucs2;
long gsm_bytes; long gsm_bytes;
gsize converted; size_t converted;
result = convert_utf8_to_gsm(utf, -1, NULL, &gsm_bytes, 0); result = convert_utf8_to_gsm(utf, -1, NULL, &gsm_bytes, 0);
if (result) { 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 */ /* NOTE: UCS2 formats with an offset are never used */
ucs2 = (guint8 *) g_convert(utf, -1, "UCS-2BE//TRANSLIT", "UTF-8", ucs2 = l_utf8_to_ucs2be(utf, &converted);
NULL, &converted, NULL); if (!ucs2)
if (ucs2 == NULL)
return NULL; return NULL;
/* converted includes null terminator, drop */
converted -= 2;
if (max_length != -1 && (int) converted + 1 > max_length) if (max_length != -1 && (int) converted + 1 > max_length)
converted = (max_length - 1) & ~1; converted = (max_length - 1) & ~1;
result = g_try_malloc(converted + 1); result = l_malloc(converted + 1);
if (result == NULL) {
g_free(ucs2);
return NULL;
}
*out_length = converted + 1;
result[0] = 0x80; result[0] = 0x80;
memcpy(&result[1], ucs2, converted); memcpy(&result[1], ucs2, converted);
g_free(ucs2); *out_length = converted + 1;
l_free(ucs2);
return result; return result;
} }