comparison libutil/plmncodes.c @ 8:34bbb0585cab

libutil: import from previous fc-pcsc-tools version
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 14 Mar 2021 05:42:37 +0000
parents
children
comparison
equal deleted inserted replaced
7:b25d4dfe5798 8:34bbb0585cab
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 return(-1);
37 mcc[0] = decode_hex_digit(asc[0]);
38 mcc[1] = decode_hex_digit(asc[1]);
39 mcc[2] = decode_hex_digit(asc[2]);
40 asc += 3;
41 if (*asc == '-')
42 asc++;
43 if (!isxdigit(asc[0]) || !isxdigit(asc[1]))
44 return(-1);
45 mnc[0] = decode_hex_digit(asc[0]);
46 mnc[1] = decode_hex_digit(asc[1]);
47 asc += 2;
48 if (*asc == '\0')
49 mnc[2] = 0xF;
50 else if (isxdigit(asc[0]) && asc[1] == '\0')
51 mnc[2] = decode_hex_digit(*asc);
52 else
53 return(-1);
54 bin[0] = (mcc[1] << 4) | mcc[0];
55 bin[1] = (mnc[2] << 4) | mcc[2];
56 bin[2] = (mnc[1] << 4) | mnc[0];
57 return(0);
58 }