comparison libutil/plmncodes.c @ 157:f064dbcc5f41

libutil split from libcommon
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 26 Feb 2021 20:19:58 +0000
parents libcommon/plmncodes.c@d2e800abd257
children 3ddbc3fea5f0
comparison
equal deleted inserted replaced
156:5f1f3f6fd865 157:f064dbcc5f41
1 /*
2 * This module implements some functions for working with MCC-MNC PLMN codes.
3 */
4
5 #include <sys/types.h>
6 #include <ctype.h>
7 #include <stdio.h>
8
9 decode_plmn_3bytes(bin, asc, space_pad)
10 u_char *bin;
11 char *asc;
12 {
13 asc[0] = encode_hex_digit(bin[0] & 0xF);
14 asc[1] = encode_hex_digit(bin[0] >> 4);
15 asc[2] = encode_hex_digit(bin[1] & 0xF);
16 asc[3] = '-';
17 asc[4] = encode_hex_digit(bin[2] & 0xF);
18 asc[5] = encode_hex_digit(bin[2] >> 4);
19 asc[6] = encode_hex_digit(bin[1] >> 4);
20 asc[7] = '\0';
21 if (asc[6] == 'F') {
22 if (space_pad)
23 asc[6] = ' ';
24 else
25 asc[6] = '\0';
26 }
27 }
28
29 encode_plmn_3bytes(asc, bin)
30 char *asc;
31 u_char *bin;
32 {
33 u_char mcc[3], mnc[3];
34
35 if (!isxdigit(asc[0]) || !isxdigit(asc[1]) || !isxdigit(asc[2])) {
36 inv: fprintf(stderr, "error: invalid MCC-MNC argument\n");
37 return(-1);
38 }
39 mcc[0] = decode_hex_digit(asc[0]);
40 mcc[1] = decode_hex_digit(asc[1]);
41 mcc[2] = decode_hex_digit(asc[2]);
42 asc += 3;
43 if (*asc == '-')
44 asc++;
45 if (!isxdigit(asc[0]) || !isxdigit(asc[1]))
46 goto inv;
47 mnc[0] = decode_hex_digit(asc[0]);
48 mnc[1] = decode_hex_digit(asc[1]);
49 asc += 2;
50 if (*asc == '\0')
51 mnc[2] = 0xF;
52 else if (isxdigit(asc[0]) && asc[1] == '\0')
53 mnc[2] = decode_hex_digit(*asc);
54 else
55 goto inv;
56 bin[0] = (mcc[1] << 4) | mcc[0];
57 bin[1] = (mnc[2] << 4) | mcc[2];
58 bin[2] = (mnc[1] << 4) | mnc[0];
59 return(0);
60 }