FreeCalypso > hg > sms-coding-utils
annotate libcoding/gsmtime_enc.c @ 28:6e925aa54727
libcoding: more sensible naming of GSM time
encoding and decoding modules
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Thu, 13 Jun 2024 02:32:11 +0000 |
parents | libcoding/timestamp.c@b831ee7d512c |
children |
rev | line source |
---|---|
6
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * The function implemented in this module encodes a GSM SMS absolute timestamp |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * from ASCII representation into PDU octets. |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 */ |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <sys/types.h> |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <ctype.h> |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 encode_gsm_sms_abstime(str, bin) |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 char *str; |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 u_char *bin; |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 { |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 u_char digits[14]; |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 if (!isdigit(str[0]) || !isdigit(str[1])) |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 return(-1); |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 digits[0] = str[0] - '0'; |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 digits[1] = str[1] - '0'; |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 if (str[2] != '/') |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 return(-1); |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 if (!isdigit(str[3]) || !isdigit(str[4])) |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 return(-1); |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 digits[2] = str[3] - '0'; |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 digits[3] = str[4] - '0'; |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 if (str[5] != '/') |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 return(-1); |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 if (!isdigit(str[6]) || !isdigit(str[7])) |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 return(-1); |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 digits[4] = str[6] - '0'; |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 digits[5] = str[7] - '0'; |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 if (str[8] != ',') |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 return(-1); |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 if (!isdigit(str[9]) || !isdigit(str[10])) |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 return(-1); |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 digits[6] = str[9] - '0'; |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 digits[7] = str[10] - '0'; |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 if (str[11] != ':') |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 return(-1); |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 if (!isdigit(str[12]) || !isdigit(str[13])) |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 return(-1); |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 digits[8] = str[12] - '0'; |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 digits[9] = str[13] - '0'; |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 if (str[14] != ':') |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 return(-1); |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 if (!isdigit(str[15]) || !isdigit(str[16])) |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 return(-1); |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 digits[10] = str[15] - '0'; |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 digits[11] = str[16] - '0'; |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 if (str[17] != '+' && str[17] != '-') |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 return(-1); |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 if (!isdigit(str[18]) || !isdigit(str[19])) |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 return(-1); |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 digits[12] = str[18] - '0'; |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 digits[13] = str[19] - '0'; |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 if (str[20]) |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 return(-1); |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 if (str[17] == '-') |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 digits[12] |= 8; |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 pack_digit_bytes(digits, bin, 7); |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 return(0); |
b831ee7d512c
libcoding: add absolute time encoding function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 } |