blob: d8e0859471a878951dfc4c7afacea18c2b45c15e [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);
Harald Welte93d93032009-03-30 09:11:45 +000033 if (!text)
34 return NULL;
Holger Freyther76c95692009-02-17 20:31:30 +000035
36 for (i=0;i<length;i++) {
37 text[i] = ((user_data[d_off] + (user_data[d_off+1]<<8)) & (0x7f<<b_off))>>b_off;
38 b_off += 7;
39 if (b_off >= 8) {
40 d_off += 1;
41 b_off -= 8;
42 }
43 }
Holger Freyther62f47772009-02-17 20:31:35 +000044 text[i] = '\0';
Holger Freyther76c95692009-02-17 20:31:30 +000045 return text;
46}
Holger Freytherce668962009-02-17 23:42:45 +000047
48/* GSM 03.38 6.2.1 Charachter packing */
Holger Freyther59da07b2009-02-23 00:50:38 +000049u_int8_t *gsm_7bit_encode(const char *data, u_int8_t *out_length)
Holger Freytherce668962009-02-17 23:42:45 +000050{
51 int i;
52 u_int8_t d_off = 0, b_off = 0;
53 const int length = strlen(data);
Holger Freyther0d4df322009-02-23 04:03:33 +000054 *out_length = (length * 8)/7;
55 u_int8_t *result = malloc(*out_length);
Harald Welte93d93032009-03-30 09:11:45 +000056 if (!result)
57 return NULL;
58
Holger Freyther0d4df322009-02-23 04:03:33 +000059 memset(result, 0, *out_length);
Holger Freytherce668962009-02-17 23:42:45 +000060
61 for (i = 0; i < length; ++i) {
62 u_int8_t first = (data[i] & 0x7f) << b_off;
Holger Freyther3b1f3d02009-02-23 01:47:15 +000063 u_int8_t second = (data[i] & 0x7f) >> (8 - b_off);
64
Holger Freytherce668962009-02-17 23:42:45 +000065 result[d_off] |= first;
Holger Freyther59da07b2009-02-23 00:50:38 +000066 if (second != 0)
67 result[d_off + 1] = second;
Holger Freytherce668962009-02-17 23:42:45 +000068
69 b_off += 7;
70
71 if (b_off >= 8) {
72 d_off += 1;
73 b_off -= 8;
74 }
75 }
76
77 return result;
78}