blob: 0c25b28c6417619c38ca632a8bceba5d34dd606b [file] [log] [blame]
Harald Welte59b04682009-06-10 05:40:52 +08001/*
2 * (C) 2008 by Daniel Willmann <daniel@totalueberwachung.de>
3 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
4 * (C) 2009 by Harald Welte <laforge@gnumonks.org>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24#include <openbsc/gsm_utils.h>
25#include <stdlib.h>
26#include <string.h>
27
28/* GSM 03.38 6.2.1 Charachter packing */
29int gsm_7bit_decode(char *text, const u_int8_t *user_data, u_int8_t length)
30{
31 u_int8_t d_off = 0, b_off = 0;
32 u_int8_t i;
33
34 for (i=0;i<length;i++) {
35 text[i] = ((user_data[d_off] + (user_data[d_off+1]<<8)) & (0x7f<<b_off))>>b_off;
36 b_off += 7;
37 if (b_off >= 8) {
38 d_off += 1;
39 b_off -= 8;
40 }
41 }
42 text[i] = '\0';
43 return 0;
44}
45
46/* GSM 03.38 6.2.1 Charachter packing */
47int gsm_7bit_encode(u_int8_t *result, const char *data)
48{
49 int i;
50 u_int8_t d_off = 0, b_off = 0;
51 const int length = strlen(data);
52 int out_length = (length * 8)/7;
53
54 memset(result, 0, out_length);
55
56 for (i = 0; i < length; ++i) {
57 u_int8_t first = (data[i] & 0x7f) << b_off;
58 u_int8_t second = (data[i] & 0x7f) >> (8 - b_off);
59
60 result[d_off] |= first;
61 if (second != 0)
62 result[d_off + 1] = second;
63
64 b_off += 7;
65
66 if (b_off >= 8) {
67 d_off += 1;
68 b_off -= 8;
69 }
70 }
71
72 return out_length;
73}