blob: b0594aec3b70f981cd3330646ea5edb690fefd21 [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>
Harald Welte7e310b12009-03-30 20:56:32 +00004 * (C) 2009 by Harald Welte <laforge@gnumonks.org>
Holger Freyther76c95692009-02-17 20:31:30 +00005 *
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 <malloc.h>
Holger Freytherce668962009-02-17 23:42:45 +000026#include <string.h>
Holger Freyther76c95692009-02-17 20:31:30 +000027
Holger Freytherce668962009-02-17 23:42:45 +000028/* GSM 03.38 6.2.1 Charachter packing */
Harald Welte7e310b12009-03-30 20:56:32 +000029int gsm_7bit_decode(char *text, const u_int8_t *user_data, u_int8_t length)
Holger Freyther76c95692009-02-17 20:31:30 +000030{
31 u_int8_t d_off = 0, b_off = 0;
32 u_int8_t i;
Holger Freyther76c95692009-02-17 20:31:30 +000033
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';
Harald Welte7e310b12009-03-30 20:56:32 +000043 return 0;
Holger Freyther76c95692009-02-17 20:31:30 +000044}
Holger Freytherce668962009-02-17 23:42:45 +000045
46/* GSM 03.38 6.2.1 Charachter packing */
Harald Welte7e310b12009-03-30 20:56:32 +000047int gsm_7bit_encode(u_int8_t *result, const char *data)
Holger Freytherce668962009-02-17 23:42:45 +000048{
49 int i;
50 u_int8_t d_off = 0, b_off = 0;
51 const int length = strlen(data);
Harald Welte7e310b12009-03-30 20:56:32 +000052 int out_length = (length * 8)/7;
Harald Welte93d93032009-03-30 09:11:45 +000053
Harald Welte7e310b12009-03-30 20:56:32 +000054 memset(result, 0, out_length);
Holger Freytherce668962009-02-17 23:42:45 +000055
56 for (i = 0; i < length; ++i) {
57 u_int8_t first = (data[i] & 0x7f) << b_off;
Holger Freyther3b1f3d02009-02-23 01:47:15 +000058 u_int8_t second = (data[i] & 0x7f) >> (8 - b_off);
59
Holger Freytherce668962009-02-17 23:42:45 +000060 result[d_off] |= first;
Holger Freyther59da07b2009-02-23 00:50:38 +000061 if (second != 0)
62 result[d_off + 1] = second;
Holger Freytherce668962009-02-17 23:42:45 +000063
64 b_off += 7;
65
66 if (b_off >= 8) {
67 d_off += 1;
68 b_off -= 8;
69 }
70 }
71
Harald Welte7e310b12009-03-30 20:56:32 +000072 return out_length;
Holger Freytherce668962009-02-17 23:42:45 +000073}