Holger Freyther | 76c9569 | 2009-02-17 20:31:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2008 by Daniel Willmann <daniel@totalueberwachung.de> |
| 3 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 4 | * (C) 2009 by Harald Welte <laforge@gnumonks.org> |
Holger Freyther | 76c9569 | 2009-02-17 20:31:30 +0000 | [diff] [blame] | 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> |
Harald Welte | 12247c6 | 2009-05-21 07:23:02 +0000 | [diff] [blame] | 25 | #include <stdlib.h> |
Holger Freyther | ce66896 | 2009-02-17 23:42:45 +0000 | [diff] [blame] | 26 | #include <string.h> |
Holger Freyther | 76c9569 | 2009-02-17 20:31:30 +0000 | [diff] [blame] | 27 | |
Holger Freyther | ce66896 | 2009-02-17 23:42:45 +0000 | [diff] [blame] | 28 | /* GSM 03.38 6.2.1 Charachter packing */ |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 29 | int gsm_7bit_decode(char *text, const u_int8_t *user_data, u_int8_t length) |
Holger Freyther | 76c9569 | 2009-02-17 20:31:30 +0000 | [diff] [blame] | 30 | { |
| 31 | u_int8_t d_off = 0, b_off = 0; |
| 32 | u_int8_t i; |
Holger Freyther | 76c9569 | 2009-02-17 20:31:30 +0000 | [diff] [blame] | 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 Freyther | 62f4777 | 2009-02-17 20:31:35 +0000 | [diff] [blame] | 42 | text[i] = '\0'; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 43 | return 0; |
Holger Freyther | 76c9569 | 2009-02-17 20:31:30 +0000 | [diff] [blame] | 44 | } |
Holger Freyther | ce66896 | 2009-02-17 23:42:45 +0000 | [diff] [blame] | 45 | |
| 46 | /* GSM 03.38 6.2.1 Charachter packing */ |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 47 | int gsm_7bit_encode(u_int8_t *result, const char *data) |
Holger Freyther | ce66896 | 2009-02-17 23:42:45 +0000 | [diff] [blame] | 48 | { |
| 49 | int i; |
| 50 | u_int8_t d_off = 0, b_off = 0; |
| 51 | const int length = strlen(data); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 52 | int out_length = (length * 8)/7; |
Harald Welte | 93d9303 | 2009-03-30 09:11:45 +0000 | [diff] [blame] | 53 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 54 | memset(result, 0, out_length); |
Holger Freyther | ce66896 | 2009-02-17 23:42:45 +0000 | [diff] [blame] | 55 | |
| 56 | for (i = 0; i < length; ++i) { |
| 57 | u_int8_t first = (data[i] & 0x7f) << b_off; |
Holger Freyther | 3b1f3d0 | 2009-02-23 01:47:15 +0000 | [diff] [blame] | 58 | u_int8_t second = (data[i] & 0x7f) >> (8 - b_off); |
| 59 | |
Holger Freyther | ce66896 | 2009-02-17 23:42:45 +0000 | [diff] [blame] | 60 | result[d_off] |= first; |
Holger Freyther | 59da07b | 2009-02-23 00:50:38 +0000 | [diff] [blame] | 61 | if (second != 0) |
| 62 | result[d_off + 1] = second; |
Holger Freyther | ce66896 | 2009-02-17 23:42:45 +0000 | [diff] [blame] | 63 | |
| 64 | b_off += 7; |
| 65 | |
| 66 | if (b_off >= 8) { |
| 67 | d_off += 1; |
| 68 | b_off -= 8; |
| 69 | } |
| 70 | } |
| 71 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 72 | return out_length; |
Holger Freyther | ce66896 | 2009-02-17 23:42:45 +0000 | [diff] [blame] | 73 | } |