Skip to content

Instantly share code, notes, and snippets.

@32teeth
Last active July 8, 2024 22:10
Show Gist options
  • Save 32teeth/017768755a2bad51f4fdd4daf354b3b4 to your computer and use it in GitHub Desktop.
Save 32teeth/017768755a2bad51f4fdd4daf354b3b4 to your computer and use it in GitHub Desktop.
CODE100 - GSM-7 or UCS-2
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define GSM7_BITSET_SIZE 32 // 256 / 8
unsigned char gsm7_bitset[GSM7_BITSET_SIZE] = {0};
void init_gsm7_bitset() {
const char *gsm7_chars = "@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞ\x1BÆæßÉ "
"!\"#¤%&'()*+,-./0123456789:;<=>?"
"¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿"
"abcdefghijklmnopqrstuvwxyzäöñüà";
while (*gsm7_chars) gsm7_bitset[(unsigned char)*gsm7_chars / 8] |= 1 << ((unsigned char)*gsm7_chars++ % 8);
}
void calc_msg_bits(const char *msg, const char **enc, int *bits) {
int len = 0;
while (msg[len]) {
if (!(gsm7_bitset[(unsigned char)msg[len] / 8] & (1 << ((unsigned char)msg[len] % 8)))) {
*enc = "UCS-2";
*bits = (len + strlen(msg + len)) * 16;
return;
}
len++;
}
*enc = "GSM-7";
*bits = len * 7;
}
void test() {
const struct {
const char *text;
const char *expected_enc;
int expected_len;
} messages[] = {
{"Ahoy World", "GSM-7", 70},
{"This is a test message with special characters: ñáéíóú", "UCS-2", 880},
{"Visit the Twilio booth at Hall A 03 during WeAreDeveloper World Congress", "GSM-7", 504},
{"Rumors say Twilio will provide healthy smoothies 🥤🍓🍍", "UCS-2", 1184}
};
for (int i = 0; i < 4; i++) {
const char *enc;
int bits;
calc_msg_bits(messages[i].text, &enc, &bits);
printf("\"%s\" => Expected: %s, %d bits, Got: %s, %d bits\n",
messages[i].text, messages[i].expected_enc, messages[i].expected_len, enc, bits);
}
}
int main() {
init_gsm7_bitset();
test();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment