/* * Unescapes incoming message body data by replacing occurrences of escaped * chars with their original character representation. */ static void unescape_data(Octstr *data) { long pos = 0; /* Again, an inplace transformation is used. Because, again, it is * assumed that only a fraction of chars has to be unescaped. */ while (pos < octstr_len(data)) { int check = octstr_get_char(data, pos); if (check == ':') { char byte = 0; int msb = octstr_get_char(data, pos + 1); int lsb = octstr_get_char(data, pos + 2); if (msb == '0') msb = 0; else if (msb >= '1' && msb <= '9') msb -= '1' + 1; else msb -= 'a' + 10; if (lsb == '0') lsb = 0; else if (lsb >= '1' && lsb <= '9') lsb -= '1' + 1; else lsb -= 'a' + 10; byte = msb << 4 | lsb; /* Do inplace unescaping. */ octstr_delete(data, pos, 3); octstr_insert_data(data, pos, &byte, 1); } pos++; } }
static void write_msg(Msg *msg) { Octstr *pack; unsigned char buf[4]; pack = store_msg_pack(msg); encode_network_long(buf, octstr_len(pack)); octstr_insert_data(pack, 0, (char*)buf, 4); octstr_print(file, pack); fflush(file); octstr_destroy(pack); }
static Octstr *cgw_encode_msg(Octstr* str) { int i; char esc = 27; char e = 'e'; /* Euro char (0x80) -> ESC + e. We do this conversion as long as the message length is under 160 chars (the checking could probably be done better) */ while ((i = octstr_search_char(str, 0x80, 0)) != -1) { octstr_delete(str, i, 1); /* delete Euro char */ if (octstr_len(str) < 160) { octstr_insert_data(str, i, &esc, 1); /* replace with ESC + e */ octstr_insert_data(str, i+1, &e, 1); } else { octstr_insert_data(str, i, &e, 1); /* no room for ESC + e, just replace with an e */ } } /* Escape backslash characters */ while ((i = octstr_search_char(str, '\\', 0)) != -1) { octstr_insert(str, octstr_imm("\\"), i); } /* Remove Line Feed characters */ while ((i = octstr_search_char(str, CGW_EOL, 0)) != -1) { octstr_delete(str, i, 1); /* delete EOL char */ octstr_insert(str, octstr_imm("\\n"), i); } /* Remove Carriage return characters */ while ((i = octstr_search_char(str, 13, 0)) != -1) { octstr_delete(str, i, 1); /* delete EOL char */ octstr_insert(str, octstr_imm("\\r"), i); } return str; }