sms: sanitize 8-bit data so that it is UTF8-clean

When receiving a SMS message with raw 8-bit data, sanitize it by
replacing non-ASCII characters with \xNN escape sequences. This
prevents a problem further down the line where the body of the message
is passed into DBus as a string, and DBus requires strings to be UTF-8.

BUG=chrome-os-partner:5953
TEST=Run network_ModemManagerSMS.py with the PDU from this bug.

Change-Id: Ic33a365f9a065c49a325e047e4c3f5e81450fa1f
Reviewed-on: http://gerrit.chromium.org/gerrit/8232
Reviewed-by: Eric Shienbrood <ers@chromium.org>
Tested-by: Nathan J. Williams <njw@chromium.org>
Commit-Ready: Nathan J. Williams <njw@chromium.org>
This commit is contained in:
Nathan Williams
2011-09-23 17:21:15 -04:00
committed by Dan Williams
parent 18fc92ef73
commit 00670456ff

View File

@@ -13,6 +13,9 @@
* Copyright (C) 2011 Red Hat, Inc.
*/
#include <ctype.h>
#include <stdio.h>
#include <glib.h>
#include "mm-charsets.h"
@@ -200,8 +203,22 @@ sms_decode_text (const guint8 *text, int len, SmsEncoding encoding, int bit_offs
g_free (unpacked);
} else if (encoding == MM_SMS_ENCODING_UCS2)
utf8 = g_convert ((char *) text, len, "UTF8", "UCS-2BE", NULL, NULL, NULL);
else if (encoding == MM_SMS_ENCODING_8BIT)
utf8 = g_strndup ((const char *)text, len);
else if (encoding == MM_SMS_ENCODING_8BIT) {
/* DBus requires UTF-8 strings, so we have some sanitizing to do */
char *p;
int i;
utf8 = g_malloc0 (4*len+1); /* Worst case: Every byte becomes "\xFF" */
p = utf8;
for (i = 0 ; i < len ; i++) {
if (isascii (text[i]) && text[i] != '\0')
*p++ = text[i];
else {
sprintf(p, "\\x%02x", text[i]);
p += 4;
}
}
*p = '\0';
}
else
utf8 = g_strdup ("");