From: Philippe Nunes <philippe.nunes(a)linux.intel.com>
---
src/smsutil.c | 142 +++++++++++++++++++++++++++++++++++++-------------------
1 files changed, 94 insertions(+), 48 deletions(-)
diff --git a/src/smsutil.c b/src/smsutil.c
index a541964..347c9d2 100644
--- a/src/smsutil.c
+++ b/src/smsutil.c
@@ -3996,6 +3996,96 @@ gboolean iso639_2_from_language(enum cbs_language lang, char
*iso639)
return FALSE;
}
+static unsigned char *decode_7bits_sms_charset(int taken, int *bufsize,
+ unsigned char *buf,
+ const guint8 *ud,
+ gboolean iso639)
+{
+ unsigned char unpacked[CBS_MAX_GSM_CHARS];
+ long written;
+ int max_chars;
+ int i, j;
+ int buf_size = *bufsize;
+
+ max_chars = sms_text_capacity_gsm(CBS_MAX_GSM_CHARS, taken);
+
+ unpack_7bit_own_buf(ud + taken, 82 - taken, taken, FALSE, max_chars,
+ &written, 0, unpacked);
+
+ i = iso639 ? 3 : 0;
+
+ /*
+ * CR can be used as a padding character, which means
+ * we can safely discard everything afterwards
+ */
+
+ for (; i < written; i++, buf_size++) {
+ if (unpacked[i] == '\r') {
+ /*
+ * check if this is a padding character
+ * or if it is a wanted <CR>
+ */
+ for (j = i + 1; j < written; j++)
+ if (unpacked[j] != '\r')
+ break;
+
+ if (j == written)
+ break;
+ }
+
+
+ buf[buf_size] = unpacked[i];
+ }
+
+ *bufsize = buf_size;
+
+ return buf;
+}
+
+static unsigned char *decode_default_sms_charset(int taken, int *bufsize,
+ unsigned char *buf,
+ const guint8 *ud,
+ gboolean iso639)
+{
+ int num_ucs2_chars = (82 - taken) >> 1;
+ int i = taken;
+ int max_offset = taken + num_ucs2_chars * 2;
+ int buf_size = *bufsize;
+
+ /*
+ * It is completely unclear how UCS2 chars are handled
+ * especially across pages or when the UDH is present.
+ * For now do the best we can.
+ */
+ if (iso639) {
+ i += 2;
+ num_ucs2_chars -= 1;
+ }
+
+ while (i < max_offset) {
+ if (ud[i] == 0x00 && ud[i+1] == '\r') {
+ int j = i + 2;
+
+ for (; j < max_offset; j = j + 2)
+ if (ud[j] != 0x00 || ud[j + 1] != '\r')
+ break;
+
+ if (j == max_offset)
+ break;
+ }
+
+ buf[buf_size] = ud[i];
+ buf[buf_size + 1] = ud[i+1];
+
+ buf_size += 2;
+ i += 2;
+ }
+
+ *bufsize = buf_size;
+
+ return buf;
+}
+
char *cbs_decode_text(GSList *cbs_list, char *iso639_lang)
{
GSList *l;
@@ -4087,30 +4177,8 @@ char *cbs_decode_text(GSList *cbs_list, char *iso639_lang)
taken = sms_udh_iter_get_udh_length(&iter) + 1;
if (charset == SMS_CHARSET_7BIT) {
- unsigned char unpacked[CBS_MAX_GSM_CHARS];
- long written;
- int max_chars;
- int i;
-
- max_chars =
- sms_text_capacity_gsm(CBS_MAX_GSM_CHARS, taken);
-
- unpack_7bit_own_buf(ud + taken, 82 - taken,
- taken, FALSE, max_chars,
- &written, 0, unpacked);
-
- i = iso639 ? 3 : 0;
-
- /*
- * CR is a padding character, which means we can
- * safely discard everything afterwards
- */
- for (; i < written; i++, bufsize++) {
- if (unpacked[i] == '\r')
- break;
-
- buf[bufsize] = unpacked[i];
- }
+ decode_7bits_sms_charset(taken, &bufsize, buf, ud,
+ iso639);
/*
* It isn't clear whether extension sequences
@@ -4120,30 +4188,8 @@ char *cbs_decode_text(GSList *cbs_list, char *iso639_lang)
* the check here since the specification isn't clear
*/
} else {
- int num_ucs2_chars = (82 - taken) >> 1;
- int i = taken;
- int max_offset = taken + num_ucs2_chars * 2;
-
- /*
- * It is completely unclear how UCS2 chars are handled
- * especially across pages or when the UDH is present.
- * For now do the best we can.
- */
- if (iso639) {
- i += 2;
- num_ucs2_chars -= 1;
- }
-
- while (i < max_offset) {
- if (ud[i] == 0x00 && ud[i+1] == '\r')
- break;
-
- buf[bufsize] = ud[i];
- buf[bufsize + 1] = ud[i+1];
-
- bufsize += 2;
- i += 2;
- }
+ decode_default_sms_charset(taken, &bufsize, buf, ud,
+ iso639);
}
}
--
1.7.5.4