# HG changeset patch # User Mychaela Falconia # Date 1613954472 0 # Node ID 86b4f288862d844e139a1e018b8279b835b0c586 # Parent acb4d86e6ed7cdb3b6ed1483a384bfb10e4ff365 libutil: functions for encoding MSISDN record diff -r acb4d86e6ed7 -r 86b4f288862d libutil/Makefile --- a/libutil/Makefile Mon Feb 22 00:40:14 2021 +0000 +++ b/libutil/Makefile Mon Feb 22 00:41:12 2021 +0000 @@ -1,6 +1,6 @@ CC= gcc CFLAGS= -O2 -OBJS= hexstdin.o hexstr.o +OBJS= gsm7_encode.o gsm7_encode_table.o hexstdin.o hexstr.o number_encode.o LIB= libutil.a all: ${LIB} diff -r acb4d86e6ed7 -r 86b4f288862d libutil/gsm7_encode.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libutil/gsm7_encode.c Mon Feb 22 00:41:12 2021 +0000 @@ -0,0 +1,76 @@ +/* + * This module implements functions for parsing quoted string + * arguments intended for GSM7 string encoding, and actually + * encoding them into GSM7 binary strings. + */ + +#include +#include +#include + +extern u_char gsm7_encode_table[256]; + +qstring_arg_to_gsm7(arg, record, maxlen) + char *arg; + u_char *record; + unsigned maxlen; +{ + unsigned acclen, nadd; + char *cp; + int c; + + cp = arg; + for (acclen = 0; *cp; ) { + c = *cp++; + if (c == '\\') { + if (*cp == '\0') { + fprintf(stderr, + "error: dangling backslash escape\n"); + return(-1); + } + c = *cp++; + if (c >= '0' && c <= '7' && isxdigit(*cp)) { + c = ((c - '0') << 4) | decode_hex_digit(*cp++); + goto bypass_encoding; + } + switch (c) { + case 'n': + c = '\n'; + goto bypass_encoding; + case 'r': + c = '\r'; + goto bypass_encoding; + case 'e': + c = 0x1B; + goto bypass_encoding; + case '"': + case '\\': + break; + default: + fprintf(stderr, + "error: non-understood backslash escape\n"); + return(-1); + } + } + c = gsm7_encode_table[c]; + if (c == 0xFF) { + fprintf(stderr, + "error: character in alpha tag string cannot be encoded in GSM7\n"); + return(-1); + } +bypass_encoding: + if (c & 0x80) + nadd = 2; + else + nadd = 1; + if (acclen + nadd > maxlen) { + fprintf(stderr, + "error: alpha tag string is longer than SIM limit\n"); + return(-1); + } + if (c & 0x80) + record[acclen++] = 0x1B; + record[acclen++] = c & 0x7F; + } + return(acclen); +} diff -r acb4d86e6ed7 -r 86b4f288862d libutil/gsm7_encode_table.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libutil/gsm7_encode_table.c Mon Feb 22 00:41:12 2021 +0000 @@ -0,0 +1,43 @@ +/* + * This library module contains the table for encoding from ISO 8859-1 + * into the GSM 7-bit default alphabet (03.38 or 23.038). High bit set + * in the output indicates escape encoding, used for ASCII characters + * [\]^ and {|}~. 0xFF indicates invalid chars. + */ + +#include + +u_char gsm7_encode_table[256] = { + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 0x00 */ + 0xFF, 0xFF, '\n', 0xFF, 0xFF, '\r', 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 0x10 */ + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + ' ', '!', '"', '#', 0x02, '%', '&', 0x27, /* 0x20 */ + '(', ')', '*', '+', ',', '-', '.', '/', + '0', '1', '2', '3', '4', '5', '6', '7', /* 0x30 */ + '8', '9', ':', ';', '<', '=', '>', '?', + 0x00, 'A', 'B', 'C', 'D', 'E', 'F', 'G', /* 0x40 */ + 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', + 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', /* 0x50 */ + 'X', 'Y', 'Z', 0xBC, 0xAF, 0xBE, 0x94, 0x11, + 0xFF, 'a', 'b', 'c', 'd', 'e', 'f', 'g', /* 0x60 */ + 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', + 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', /* 0x70 */ + 'x', 'y', 'z', 0xA8, 0xC0, 0xA9, 0xBD, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 0x80 */ + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 0x90 */ + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0x40, 0xFF, 0x01, 0x24, 0x03, 0xFF, 0x5F, /* 0xA0 */ + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 0xB0 */ + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x60, + 0xFF, 0xFF, 0xFF, 0xFF, 0x5B, 0x0E, 0x1C, 0x09, /* 0xC0 */ + 0xFF, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0x5D, 0xFF, 0xFF, 0xFF, 0xFF, 0x5C, 0xFF, /* 0xD0 */ + 0x0B, 0xFF, 0xFF, 0xFF, 0x5E, 0xFF, 0xFF, 0x1E, + 0x7F, 0xFF, 0xFF, 0xFF, 0x7B, 0x0F, 0x1D, 0xFF, /* 0xE0 */ + 0x04, 0x05, 0xFF, 0xFF, 0x07, 0xFF, 0xFF, 0xFF, + 0xFF, 0x7D, 0x08, 0xFF, 0xFF, 0xFF, 0x7C, 0xFF, /* 0xF0 */ + 0x0C, 0x06, 0xFF, 0xFF, 0x7E, 0xFF, 0xFF, 0xFF +}; diff -r acb4d86e6ed7 -r 86b4f288862d libutil/number_encode.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libutil/number_encode.c Mon Feb 22 00:41:12 2021 +0000 @@ -0,0 +1,104 @@ +/* + * This module implements functions for encoding phone numbers. + */ + +#include +#include +#include +#include + +digit_char_to_gsm(ch) +{ + switch (ch) { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + return (ch - '0'); + case '*': + return 0xA; + case '#': + return 0xB; + case 'a': + case 'b': + case 'c': + return (ch - 'a' + 0xC); + case 'A': + case 'B': + case 'C': + return (ch - 'A' + 0xC); + } + return (-1); +} + +void +pack_digit_bytes(digits, dest, num_digit_bytes) + u_char *digits, *dest; + unsigned num_digit_bytes; +{ + u_char *sp, *dp; + unsigned n; + + sp = digits; + dp = dest; + for (n = 0; n < num_digit_bytes; n++) { + *dp++ = sp[0] | (sp[1] << 4); + sp += 2; + } +} + +encode_phone_number_arg(arg, fixp, mode) + char *arg; + u_char *fixp; +{ + u_char digits[20]; + unsigned ndigits, num_digit_bytes; + char *cp, *endp; + int c; + + cp = arg; + if (*cp == '+') { + fixp[1] = 0x91; + cp++; + } else + fixp[1] = 0x81; + if (digit_char_to_gsm(*cp) < 0) { +inv_arg: fprintf(stderr, "error: invalid phone number argument\n"); + return(-1); + } + for (ndigits = 0; ; ndigits++) { + c = digit_char_to_gsm(*cp); + if (c < 0) + break; + cp++; + if (ndigits >= 20) { + fprintf(stderr, "error: too many number digits\n"); + return(-1); + } + digits[ndigits] = c; + } + if (mode) + fixp[0] = ndigits; + if (ndigits & 1) + digits[ndigits++] = 0xF; + num_digit_bytes = ndigits >> 1; + if (!mode) + fixp[0] = num_digit_bytes + 1; + pack_digit_bytes(digits, fixp + 2, num_digit_bytes); + if (*cp == ',') { + cp++; + if (!isdigit(*cp)) + goto inv_arg; + fixp[1] = strtoul(cp, &endp, 0); + if (*endp) + goto inv_arg; + } else if (*cp) + goto inv_arg; + return(0); +}