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