summaryrefslogtreecommitdiffstats
path: root/tinySMS/src/tsms_packing.c
blob: 9f9f9169a2c57f2bc8b04fb018694c2f1abde53d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* Copyright (C) 2009 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango.org>
*	
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*	
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*	
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*
*/

/**@file tsms_packing.c
 * @brief SMS Packing (3GPP TS 23.038 subclause 6.1.2).
 *
 * @author Mamadou Diop <diopmamadou(at)doubango.org>
 *

 */
#include "tinysms/tsms_packing.h"

#include "tinysms/tsms_etsi_gsm_03_38.h"

#include "tsk_memory.h"
#include "tsk_string.h"
#include "tsk_debug.h"

#include <string.h> /* strlen() */

/**@defgroup tsms_packing_group SMS Packing
*/

/**@ingroup tsms_packing_group
* Converts ASCII string to GSM 7-bit characters as per 3GPP TS 23.038 v911 section 6.1.2.1.
* @param ascii Null-Terminated ascii string to convert.
* @retval Buffer containing GSM 7-bit characters if succeed and @a Null otherwise.
* @sa @ref tsms_pack_from_7bit
*/
tsk_buffer_t* tsms_pack_to_7bit(const char* ascii)
{
	/* 3GPP TS 23.038 - 6.1.2.1.1	Packing of 7-bit characters
		If a character number $ is noted in the following way:
			b7	b6	b5	b4	b3	b2	b1
			$a	$b	$c	$d	$e	$f	$g
		The packing of the 7-bitscharacters in octets is done by completing the octets with zeros on the left.
		For examples, packing: $
		-	one character in one octet:
		-	bits number:
			7	6	5	4	3	2	1	0
			0	1a	1b	1c	1d	1e	1f	1g

		-	two characters in two octets:
		-	bits number:
			7	6	5	4	3	2	1	0
			2g	1a	1b	1c	1d	1e	1f	1g
			0	0	2a	2b	2c	2d	2e	2f

		-	three characters in three octets:
		-	bits number:	
			7	6	5	4	3	2	1	0
			2g	1a	1b	1c	1d	1e	1f	1g
			3f	3g	2a	2b	2c	2d	2e	2f
			0	0	0	3a	3b	3c	3d	3e

		-	seven characters in seven octets:
		-	bits number:	
			7	6	5	4	3	2	1	0
			2g	1a	1b	1c	1d	1e	1f	1g
			3f	3g	2a	2b	2c	2d	2e	2f
			4e	4f	4g	3a	3b	3c	3d	3e
			5d	5e	5f	5g	4a	4b	4c	4d
			6c	6d	6e	6f	6g	5a	5b	5c
			7b	7c	7d	7e	7f	7g	6a	6b
			0	0	0	0	0	0	0	7a

		-	eight characters in seven octets:
		-	bits number:	
			7	6	5	4	3	2	1	0
			2g	1a	1b	1c	1d	1e	1f	1g
			3f	3g	2a	2b	2c	2d	2e	2f
			4e	4f	4g	3a	3b	3c	3d	3e
			5d	5e	5f	5g	4a	4b	4c	4d
			6c	6d	6e	6f	6g	5a	5b	5c
			7b	7c	7d	7e	7f	7g	6a	6b
			8a	8b	8c	8d	8e	8f	8g	7a

		The bit number zero is always transmitted first.
		Therefore, in 140 octets, it is possible to pack (140x8)/7=160 characters.
	*/
	tsk_buffer_t* ret = tsk_null;
	uint8_t* _ret = tsk_null;
	register tsk_size_t len, bcount = 1/*1-7*/, index = 0, retindex = 0, retlen = 0;
	const uint8_t* pascii = (const uint8_t*)ascii;
	
	if(!ascii || !(len = strlen(ascii))){
		TSK_DEBUG_WARN("Null or Empty ascci string.");
		goto bail;
	}
	
	retlen = len - (len/7) + 1;
	if(!(_ret = tsk_calloc(retlen, sizeof(uint8_t)))){
		goto bail;
	}
	
	while(index < len){
		if(index == 0){
			_ret[retindex] |= (pascii[index] & (0xFF >> bcount)) |
				(pascii[index+1] << (8 - bcount));
		}
		else{
			_ret[retindex] = ((pascii[index] >> (bcount - 1))& (0xFF >> bcount))
							| (pascii[index+1] <<(8-bcount));
		}		
		
		/* how many bytes to use to complete on the left? */
		if(++bcount == 8){
			bcount = 1, ++index;
		}
		/* next */
		retindex++;
		index++;
	}
	
	/* creat ret */
	ret = tsk_buffer_create(_ret, (retlen-1));
bail:
	TSK_FREE(_ret);
	
	return ret;
}

/**@ingroup tsms_packing_group
* Converts ASCII string to UCS-2(2-byte Universal Character Set) characters.
* @param ascii Null-Terminated ascii string to convert.
* @retval Buffer containing UCS-2 characters if succeed and @a Null otherwise.
* @sa @ref tsms_pack_from_ucs2
*/
tsk_buffer_t* tsms_pack_to_ucs2(const char* ascii)
{
	register tsk_size_t i, j;
	tsk_size_t len, retlen = 0;
	uint8_t* str = tsk_null;
	tsk_buffer_t* ret = tsk_null;

	if(!ascii || !(len=strlen(ascii))){
		TSK_DEBUG_WARN("Null or Empty gsm7bit buffer.");
		goto bail;
	}

	if(!(str = tsk_calloc(len, sizeof(uint8_t)*2))){
		goto bail;
	}

	/* very bas way to do conversion ==> to be fixed */
	for(i=0; i<len; i++){
		for(j=0; j<TSMS_ETSI_GSM_03_38_COUNT; j++){
			if((uint8_t)ascii[i] == (uint8_t)(TSMS_ETSI_GSM_03_38[j][1] & 0xFF)){
				retlen++; /* 00 */
				*(str + retlen++) = (TSMS_ETSI_GSM_03_38[j][0] & 0xFF);
			}
		}
	}

	/* create buffer */
	ret = tsk_buffer_create(str, retlen);

bail:
	TSK_FREE(str);
	return ret;
}

/**@ingroup tsms_packing_group
* Converts ASCII string to 8-bit characters.
* @param ascii Null-terminated ascii string to convert.
* @retval Buffer containing 8-bit characters if succeed and @a Null otherwise.
* @sa @ref tsms_pack_from_8bit
*/
tsk_buffer_t* tsms_pack_to_8bit(const char* ascii)
{
	register tsk_size_t i, j;
	tsk_size_t len, retlen = 0;
	uint8_t* str = tsk_null;
	tsk_buffer_t* ret = tsk_null;

	if(!ascii || !(len=strlen(ascii))){
		TSK_DEBUG_WARN("Null or Empty gsm7bit buffer.");
		goto bail;
	}

	if(!(str = tsk_calloc(len, sizeof(uint8_t)))){
		goto bail;
	}

	/* very bas way to do conversion ==> to be fixed */
	for(i = 0; i<len; i++){
		for(j = 0; j<TSMS_ETSI_GSM_03_38_COUNT; j++){
			if((uint8_t)ascii[i] == (uint8_t)(TSMS_ETSI_GSM_03_38[j][1] & 0xFF)){
				*(str + retlen++) = (TSMS_ETSI_GSM_03_38[j][0] & 0xFF);
				continue;
			}
		}
	}
	
	/* create buffer */
	ret = tsk_buffer_create(str, retlen);

bail:
	TSK_FREE(str);
	return ret;
}

/**@ingroup tsms_packing_group
* Converts GSM 7-bit characters to ASCII string.
* @param gsm7bit Buffer containing GSM 7-bit characters to convert.
* @param size The size of the buffer.
* @retval Null-terminated ASCII string, ready to be shown to the screen.
* @sa @ref tsms_pack_to_7bit
*/
char* tsms_pack_from_7bit(const void* gsm7bit, tsk_size_t size)
{
	char* ret = tsk_null;
	register tsk_size_t bcount = 1/*1-7*/, index = 0, retindex = 0, retsize = 0;
	const uint8_t* pgsm7bit = gsm7bit;
	
	if(!gsm7bit || !size){
		TSK_DEBUG_WARN("Null or Empty gsm7bit buffer.");
		goto bail;
	}
	
	// dup the gsm7bit buffer
	retsize = size + (size/7) + 1; // variable used for debug
	if(!(ret = tsk_calloc(retsize, sizeof(uint8_t)))){
		goto bail;
	}
	
	while(index < size){
		ret[retindex] = (pgsm7bit[index] & (0xFF >>bcount));
		if(index){
			ret[retindex] = (ret[retindex] << (bcount - 1)) 
				| (pgsm7bit[index-1] >> (8 - bcount + 1));
		}

		/* how many bytes to use to complete on the right? */
		if(bcount++ == 8){
			bcount = 1, --index;
		}
		/* next */
		index++;
		retindex++;
	}
	
	/* special one */
	if(!(size%7)){
		ret[retindex] = pgsm7bit[size-1]>>1;
	}
	
bail:
	
	return ret;
}

/**@ingroup tsms_packing_group
* Converts UCS-2(2-byte Universal Character Set) characters to ASCII string.
* @param ucs2 Buffer containing UCS-2 characters to convert.
* @param size The size of the buffer.
* @retval Null-terminated ASCII string, ready to be shown to the screen.
* @sa @ref tsms_pack_to_ucs2
*/
char* tsms_pack_from_ucs2(const void* ucs2, tsk_size_t size)
{
	register tsk_size_t i, j;
	char* ret = tsk_null;
	
	const uint16_t* pucs2 = ucs2;

	if(!ucs2 || !size){
		TSK_DEBUG_WARN("Null or Empty gsm8bit buffer.");
		goto bail;
	}

	if(!(ret = tsk_calloc(size+1, sizeof(uint8_t)))){
		goto bail;
	}

	for(i=0; i<size; i++){
		for(j=0; j<TSMS_ETSI_GSM_03_38_COUNT; j++){
			if(*(pucs2 + i) == (TSMS_ETSI_GSM_03_38[j][0])){
				*(ret+i) = (TSMS_ETSI_GSM_03_38[j][1] & 0xFF);
				continue;
			}
		}
	}

bail:
	return ret;
}

/**@ingroup tsms_packing_group
* Converts 8-bit characters to ASCII string.
* @param gsm8bit Buffer containing GSM 8-bit characters to convert.
* @param size The size of the buffer.
* @retval Null-terminated ASCII string, ready to be shown to the screen.
* @sa @ref tsms_pack_to_8bit
*/
char* tsms_pack_from_8bit(const void* gsm8bit, tsk_size_t size)
{
	register tsk_size_t i, j;
	char* ret = tsk_null;
	
	const uint8_t* pgsm8bit = gsm8bit;

	if(!gsm8bit || !size){
		TSK_DEBUG_WARN("Null or Empty gsm8bit buffer.");
		goto bail;
	}

	if(!(ret = tsk_calloc(size+1, sizeof(uint8_t)))){
		goto bail;
	}

	/* Very bad way to do convertion ==> to be changed */
	for(i=0; i<size; i++){
		for(j=0; j<TSMS_ETSI_GSM_03_38_COUNT; j++){
			if(*(pgsm8bit + i) == (TSMS_ETSI_GSM_03_38[j][0] & 0xFF)){
				*(ret+i) = (TSMS_ETSI_GSM_03_38[j][1] & 0xFF);
				continue;
			}
		}
	}

bail:
	return ret;
}
OpenPOWER on IntegriCloud