aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/mm-charsets.c77
-rw-r--r--src/mm-charsets.h10
-rw-r--r--src/tests/test-charsets.c205
3 files changed, 292 insertions, 0 deletions
diff --git a/src/mm-charsets.c b/src/mm-charsets.c
index 8fea400e..d2b9a667 100644
--- a/src/mm-charsets.c
+++ b/src/mm-charsets.c
@@ -425,3 +425,80 @@ mm_charset_utf8_to_unpacked_gsm (const char *utf8, guint32 *out_len)
return g_byte_array_free (gsm, FALSE);
}
+guint8 *
+gsm_unpack (const guint8 *gsm,
+ guint32 gsm_len,
+ guint8 start_offset, /* in _bits_ */
+ guint32 *out_unpacked_len)
+{
+ GByteArray *unpacked;
+ int i, nchars;
+
+ nchars = ((gsm_len * 8) - start_offset) / 7;
+ unpacked = g_byte_array_sized_new (nchars + 1);
+
+ for (i = 0; i < nchars; i++) {
+ guint8 bits_here, bits_in_next, octet, offset, c;
+ guint32 start_bit;
+
+ start_bit = start_offset + (i * 7); /* Overall bit offset of char in buffer */
+ offset = start_bit % 8; /* Offset to start of char in this byte */
+ bits_here = offset ? (8 - offset) : 7;
+ bits_in_next = 7 - bits_here;
+
+ /* Grab bits in the current byte */
+ octet = gsm[start_bit / 8];
+ c = (octet >> offset) & (0xFF >> (8 - bits_here));
+
+ /* Grab any bits that spilled over to next byte */
+ if (bits_in_next) {
+ octet = gsm[(start_bit / 8) + 1];
+ c |= (octet & (0xFF >> (8 - bits_in_next))) << bits_here;
+ }
+ g_byte_array_append (unpacked, &c, 1);
+ }
+
+ *out_unpacked_len = unpacked->len;
+ return g_byte_array_free (unpacked, FALSE);
+}
+
+guint8 *
+gsm_pack (const guint8 *src,
+ guint32 src_len,
+ guint8 start_offset,
+ guint32 *out_packed_len)
+{
+ GByteArray *packed;
+ guint8 c, add_last = 0;
+ int i;
+
+ packed = g_byte_array_sized_new (src_len);
+
+ for (i = 0, c = 0; i < src_len; i++) {
+ guint8 bits_here, offset;
+ guint32 start_bit;
+
+ start_bit = start_offset + (i * 7); /* Overall bit offset of char in buffer */
+ offset = start_bit % 8; /* Offset to start of char in this byte */
+ bits_here = offset ? (8 - offset) : 7;
+
+ c |= (src[i] & 0x7F) << offset;
+ if (offset) {
+ /* Add this packed byte */
+ g_byte_array_append (packed, &c, 1);
+ c = add_last = 0;
+ }
+
+ /* Pack the rest of this char into the next byte */
+ if (bits_here != 7) {
+ c = (src[i] & 0x7F) >> bits_here;
+ add_last = 1;
+ }
+ }
+ if (add_last)
+ g_byte_array_append (packed, &c, 1);
+
+ *out_packed_len = packed->len;
+ return g_byte_array_free (packed, FALSE);
+}
+
diff --git a/src/mm-charsets.h b/src/mm-charsets.h
index ff39400e..661052da 100644
--- a/src/mm-charsets.h
+++ b/src/mm-charsets.h
@@ -52,5 +52,15 @@ guint8 *mm_charset_utf8_to_unpacked_gsm (const char *utf8, guint32 *out_len);
guint8 *mm_charset_gsm_unpacked_to_utf8 (const guint8 *gsm, guint32 len);
+guint8 *gsm_unpack (const guint8 *gsm,
+ guint32 gsm_len,
+ guint8 start_offset, /* in bits */
+ guint32 *out_unpacked_len);
+
+guint8 *gsm_pack (const guint8 *src,
+ guint32 src_len,
+ guint8 start_offset, /* in bits */
+ guint32 *out_packed_len);
+
#endif /* MM_CHARSETS_H */
diff --git a/src/tests/test-charsets.c b/src/tests/test-charsets.c
index 80518dc5..2727345c 100644
--- a/src/tests/test-charsets.c
+++ b/src/tests/test-charsets.c
@@ -90,6 +90,202 @@ test_mixed_chars (void *f, gpointer d)
g_free (utf8);
}
+static void
+test_unpack_gsm7 (void *f, gpointer d)
+{
+ static const guint8 gsm[] = { 0xC8, 0xF7, 0x1D, 0x14, 0x96, 0x97, 0x41, 0xF9, 0x77, 0xFD, 0x07 };
+ static const guint8 expected[] = { 0x48, 0x6f, 0x77, 0x20, 0x61, 0x72, 0x65, 0x20, 0x79, 0x6f, 0x75, 0x3f };
+ guint8 *unpacked;
+ guint32 unpacked_len = 0;
+
+ unpacked = gsm_unpack (gsm, sizeof (gsm), 0, &unpacked_len);
+ g_assert (unpacked);
+ g_assert_cmpint (unpacked_len, ==, sizeof (expected));
+ g_assert_cmpint (memcmp (unpacked, expected, unpacked_len), ==, 0);
+
+ g_free (unpacked);
+}
+
+static void
+test_unpack_gsm7_7_chars (void *f, gpointer d)
+{
+ static const guint8 gsm[] = { 0xF1, 0x7B, 0x59, 0x4E, 0xCF, 0xD7, 0x01 };
+ static const guint8 expected[] = { 0x71, 0x77, 0x65, 0x72, 0x74, 0x79, 0x75, 0x00 };
+ guint8 *unpacked;
+ guint32 unpacked_len = 0;
+
+ /* Tests the edge case where there are 7 bits left in the packed
+ * buffer but those 7 bits do not contain a character. In this case
+ * we expect a trailing NULL byte and the caller must know enough about
+ * the intended message to remove it when required.
+ */
+
+ unpacked = gsm_unpack (gsm, sizeof (gsm), 0, &unpacked_len);
+ g_assert (unpacked);
+ g_assert_cmpint (unpacked_len, ==, sizeof (expected));
+ g_assert_cmpint (memcmp (unpacked, expected, unpacked_len), ==, 0);
+
+ g_free (unpacked);
+}
+
+static void
+test_unpack_gsm7_all_chars (void *f, gpointer d)
+{
+ /* Packed array of all chars in GSM default and extended charset */
+ static const guint8 gsm[] = {
+ 0x80, 0x80, 0x60, 0x40, 0x28, 0x18, 0x0E, 0x88, 0x84, 0x62, 0xC1, 0x68,
+ 0x38, 0x1E, 0x90, 0x88, 0x64, 0x42, 0xA9, 0x58, 0x2E, 0x98, 0x8C, 0x66,
+ 0xC3, 0xE9, 0x78, 0x3E, 0xA0, 0x90, 0x68, 0x44, 0x2A, 0x99, 0x4E, 0xA8,
+ 0x94, 0x6A, 0xC5, 0x6A, 0xB9, 0x5E, 0xB0, 0x98, 0x6C, 0x46, 0xAB, 0xD9,
+ 0x6E, 0xB8, 0x9C, 0x6E, 0xC7, 0xEB, 0xF9, 0x7E, 0xC0, 0xA0, 0x70, 0x48,
+ 0x2C, 0x1A, 0x8F, 0xC8, 0xA4, 0x72, 0xC9, 0x6C, 0x3A, 0x9F, 0xD0, 0xA8,
+ 0x74, 0x4A, 0xAD, 0x5A, 0xAF, 0xD8, 0xAC, 0x76, 0xCB, 0xED, 0x7A, 0xBF,
+ 0xE0, 0xB0, 0x78, 0x4C, 0x2E, 0x9B, 0xCF, 0xE8, 0xB4, 0x7A, 0xCD, 0x6E,
+ 0xBB, 0xDF, 0xF0, 0xB8, 0x7C, 0x4E, 0xAF, 0xDB, 0xEF, 0xF8, 0xBC, 0x7E,
+ 0xCF, 0xEF, 0xFB, 0xFF, 0x1B, 0xC5, 0x86, 0xB2, 0x41, 0x6D, 0x52, 0x9B,
+ 0xD7, 0x86, 0xB7, 0xE9, 0x6D, 0x7C, 0x1B, 0xE0, 0xA6, 0x0C
+ };
+ static const guint8 ext[] = {
+ 0x1B, 0x0A, 0x1B, 0x14, 0x1B, 0x28, 0x1B, 0x29, 0x1B, 0x2F, 0x1B, 0x3C,
+ 0x1B, 0x3D, 0x1B, 0x3E, 0x1B, 0x40, 0x1B, 0x65
+ };
+ guint8 *unpacked;
+ guint32 unpacked_len = 0;
+ int i;
+
+ unpacked = gsm_unpack (gsm, sizeof (gsm), 0, &unpacked_len);
+ g_assert (unpacked);
+ g_assert_cmpint (unpacked_len, ==, 148);
+
+ /* Test default chars */
+ for (i = 0; i < 128; i++)
+ g_assert_cmpint (unpacked[i], ==, i);
+
+ /* Text extended chars */
+ g_assert_cmpint (memcmp ((guint8 *) (unpacked + 128), &ext[0], sizeof (ext)), ==, 0);
+
+ g_free (unpacked);
+}
+
+static void
+test_pack_gsm7 (void *f, gpointer d)
+{
+ static const guint8 unpacked[] = { 0x48, 0x6f, 0x77, 0x20, 0x61, 0x72, 0x65, 0x20, 0x79, 0x6f, 0x75, 0x3f };
+ static const guint8 expected[] = { 0xC8, 0xF7, 0x1D, 0x14, 0x96, 0x97, 0x41, 0xF9, 0x77, 0xFD, 0x07 };
+ guint8 *packed;
+ guint32 packed_len = 0;
+
+ packed = gsm_pack (unpacked, sizeof (unpacked), 0, &packed_len);
+ g_assert (packed);
+ g_assert_cmpint (packed_len, ==, sizeof (expected));
+ g_assert_cmpint (memcmp (packed, expected, packed_len), ==, 0);
+
+ g_free (packed);
+}
+
+static void
+test_pack_gsm7_7_chars (void *f, gpointer d)
+{
+ static const guint8 unpacked[] = { 0x71, 0x77, 0x65, 0x72, 0x74, 0x79, 0x75 };
+ static const guint8 expected[] = { 0xF1, 0x7B, 0x59, 0x4E, 0xCF, 0xD7, 0x01 };
+ guint8 *packed;
+ guint32 packed_len = 0;
+
+ /* Tests the edge case where there are 7 bits left in the packed
+ * buffer but those 7 bits do not contain a character. In this case
+ * we expect a trailing NULL byte and the caller must know enough about
+ * the intended message to remove it when required.
+ */
+
+ packed = gsm_pack (unpacked, sizeof (unpacked), 0, &packed_len);
+ g_assert (packed);
+ g_assert_cmpint (packed_len, ==, sizeof (expected));
+ g_assert_cmpint (memcmp (packed, expected, packed_len), ==, 0);
+
+ g_free (packed);
+}
+
+#if 0
+static void
+print_array (const guint8 *array, guint32 len)
+{
+ int col, foo;
+ guint8 c;
+
+ g_print ("\n");
+ for (c = 0, col = 0, foo = 0; c < len; c++) {
+ g_print ("0x%02X, ", array[c] & 0xFF);
+ if (col++ == 11) {
+ col = 0;
+ g_print ("\n");
+ }
+ foo = 1;
+ }
+ g_print ("\n");
+}
+#endif
+
+static void
+test_pack_gsm7_all_chars (void *f, gpointer d)
+{
+ /* Packed array of all chars in GSM default and extended charset */
+ static const guint8 expected[] = {
+ 0x80, 0x80, 0x60, 0x40, 0x28, 0x18, 0x0E, 0x88, 0x84, 0x62, 0xC1, 0x68,
+ 0x38, 0x1E, 0x90, 0x88, 0x64, 0x42, 0xA9, 0x58, 0x2E, 0x98, 0x8C, 0x66,
+ 0xC3, 0xE9, 0x78, 0x3E, 0xA0, 0x90, 0x68, 0x44, 0x2A, 0x99, 0x4E, 0xA8,
+ 0x94, 0x6A, 0xC5, 0x6A, 0xB9, 0x5E, 0xB0, 0x98, 0x6C, 0x46, 0xAB, 0xD9,
+ 0x6E, 0xB8, 0x9C, 0x6E, 0xC7, 0xEB, 0xF9, 0x7E, 0xC0, 0xA0, 0x70, 0x48,
+ 0x2C, 0x1A, 0x8F, 0xC8, 0xA4, 0x72, 0xC9, 0x6C, 0x3A, 0x9F, 0xD0, 0xA8,
+ 0x74, 0x4A, 0xAD, 0x5A, 0xAF, 0xD8, 0xAC, 0x76, 0xCB, 0xED, 0x7A, 0xBF,
+ 0xE0, 0xB0, 0x78, 0x4C, 0x2E, 0x9B, 0xCF, 0xE8, 0xB4, 0x7A, 0xCD, 0x6E,
+ 0xBB, 0xDF, 0xF0, 0xB8, 0x7C, 0x4E, 0xAF, 0xDB, 0xEF, 0xF8, 0xBC, 0x7E,
+ 0xCF, 0xEF, 0xFB, 0xFF, 0x1B, 0xC5, 0x86, 0xB2, 0x41, 0x6D, 0x52, 0x9B,
+ 0xD7, 0x86, 0xB7, 0xE9, 0x6D, 0x7C, 0x1B, 0xE0, 0xA6, 0x0C
+ };
+ static const guint8 ext[] = {
+ 0x1B, 0x0A, 0x1B, 0x14, 0x1B, 0x28, 0x1B, 0x29, 0x1B, 0x2F, 0x1B, 0x3C,
+ 0x1B, 0x3D, 0x1B, 0x3E, 0x1B, 0x40, 0x1B, 0x65
+ };
+ guint8 *packed, c;
+ guint32 packed_len = 0;
+ GByteArray *unpacked;
+
+ unpacked = g_byte_array_sized_new (148);
+ for (c = 0; c < 128; c++)
+ g_byte_array_append (unpacked, &c, 1);
+ for (c = 0; c < sizeof (ext); c++)
+ g_byte_array_append (unpacked, &ext[c], 1);
+
+ packed = gsm_pack (unpacked->data, unpacked->len, 0, &packed_len);
+ g_assert (packed);
+ g_assert_cmpint (packed_len, ==, sizeof (expected));
+ g_assert_cmpint (memcmp (packed, expected, packed_len), ==, 0);
+
+ g_free (packed);
+ g_byte_array_free (unpacked, TRUE);
+}
+
+static void
+test_pack_gsm7_24_chars (void *f, gpointer d)
+{
+ static const guint8 unpacked[] = {
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
+ 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
+ };
+ guint8 *packed;
+ guint32 packed_len = 0;
+
+ /* Tests that no empty trailing byte is added when all the 7-bit characters
+ * are packed into an exact number of bytes.
+ */
+
+ packed = gsm_pack (unpacked, sizeof (unpacked), 0, &packed_len);
+ g_assert (packed);
+ g_assert_cmpint (packed_len, ==, 21);
+
+ g_free (packed);
+}
+
#if GLIB_CHECK_VERSION(2,25,12)
typedef GTestFixtureFunc TCFunc;
@@ -112,6 +308,15 @@ int main (int argc, char **argv)
g_test_suite_add (suite, TESTCASE (test_esc_chars, NULL));
g_test_suite_add (suite, TESTCASE (test_mixed_chars, NULL));
+ g_test_suite_add (suite, TESTCASE (test_unpack_gsm7, NULL));
+ g_test_suite_add (suite, TESTCASE (test_unpack_gsm7_7_chars, NULL));
+ g_test_suite_add (suite, TESTCASE (test_unpack_gsm7_all_chars, NULL));
+
+ g_test_suite_add (suite, TESTCASE (test_pack_gsm7, NULL));
+ g_test_suite_add (suite, TESTCASE (test_pack_gsm7_7_chars, NULL));
+ g_test_suite_add (suite, TESTCASE (test_pack_gsm7_all_chars, NULL));
+ g_test_suite_add (suite, TESTCASE (test_pack_gsm7_24_chars, NULL));
+
result = g_test_run ();
return result;