annotate libutil/gsm7_encode.c @ 9:b6331ae4eea9

Note-about-padding added
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 22 Feb 2021 02:19:43 +0000
parents 86b4f288862d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module implements functions for parsing quoted string
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * arguments intended for GSM7 string encoding, and actually
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * encoding them into GSM7 binary strings.
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 */
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/types.h>
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <ctype.h>
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdio.h>
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 extern u_char gsm7_encode_table[256];
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 qstring_arg_to_gsm7(arg, record, maxlen)
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 char *arg;
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 u_char *record;
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 unsigned maxlen;
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 {
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 unsigned acclen, nadd;
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 char *cp;
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 int c;
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 cp = arg;
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 for (acclen = 0; *cp; ) {
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 c = *cp++;
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 if (c == '\\') {
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 if (*cp == '\0') {
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 fprintf(stderr,
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 "error: dangling backslash escape\n");
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 return(-1);
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 }
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 c = *cp++;
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 if (c >= '0' && c <= '7' && isxdigit(*cp)) {
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 c = ((c - '0') << 4) | decode_hex_digit(*cp++);
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 goto bypass_encoding;
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 }
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 switch (c) {
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 case 'n':
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 c = '\n';
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 goto bypass_encoding;
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 case 'r':
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 c = '\r';
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 goto bypass_encoding;
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 case 'e':
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 c = 0x1B;
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 goto bypass_encoding;
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 case '"':
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 case '\\':
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 break;
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 default:
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 fprintf(stderr,
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 "error: non-understood backslash escape\n");
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 return(-1);
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 }
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 }
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 c = gsm7_encode_table[c];
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 if (c == 0xFF) {
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 fprintf(stderr,
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 "error: character in alpha tag string cannot be encoded in GSM7\n");
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 return(-1);
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 }
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 bypass_encoding:
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 if (c & 0x80)
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 nadd = 2;
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 else
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 nadd = 1;
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 if (acclen + nadd > maxlen) {
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 fprintf(stderr,
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 "error: alpha tag string is longer than SIM limit\n");
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 return(-1);
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 }
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 if (c & 0x80)
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 record[acclen++] = 0x1B;
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 record[acclen++] = c & 0x7F;
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 }
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 return(acclen);
86b4f288862d libutil: functions for encoding MSISDN record
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 }