summaryrefslogtreecommitdiffstats
path: root/src/isi-sms.c
blob: 5ee7b7d6023f561a486a78c6cdf2e916a4b6da61 (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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/* isi-ss.c
 * Dissector for ISI's Short Message Service resource
 * Copyright 2010, Sebastian Reichel <sre@ring0.de>
 * Copyright 2011, Tyson Key <tyson.key@gmail.com>
 * 
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <glib.h>
#include <stdlib.h>
#include <epan/prefs.h>
#include <epan/packet.h>

#include "packet-isi.h"
#include "isi-sms.h"

static const value_string isi_sms_message_id[] = {
	{0x00, "SMS_MESSAGE_CAPABILITY_REQ"},
	{0x01, "SMS_MESSAGE_CAPABILITY_RESP"},
	{0x02, "SMS_MESSAGE_SEND_REQ"},
	{0x03, "SMS_MESSAGE_SEND_RESP"},
	{0x04, "SMS_RECEIVED_MT_PP_IND"},
	{0x05, "SMS_RECEIVED_MWI_PP_IND"},
	{0x06, "SMS_PP_ROUTING_REQ"},
	{0x07, "SMS_PP_ROUTING_RESP"},
	{0x08, "SMS_PP_ROUTING_NTF"},
	{0x09, "SMS_GSM_RECEIVED_PP_REPORT_REQ"},
	{0x0A, "SMS_GSM_RECEIVED_PP_REPORT_RESP"},
	{0x0B, "SMS_GSM_CB_ROUTING_REQ"},
	{0x0C, "SMS_GSM_CB_ROUTING_RESP"},
	{0x0D, "SMS_GSM_CB_ROUTING_NTF"},
	{0x0E, "SMS_GSM_TEMP_CB_ROUTING_REQ"},
	{0x0F, "SMS_GSM_TEMP_CB_ROUTING_RESP"},
	{0x10, "SMS_GSM_TEMP_CB_ROUTING_NTF"},
	{0x11, "SMS_GSM_CBCH_PRESENT_IND"},
	{0x12, "SMS_PARAMETERS_UPDATE_REQ"},                
	{0x13, "SMS_PARAMETERS_UPDATE_RESP"},               
	{0x14, "SMS_PARAMETERS_READ_REQ"},                  
	{0x15, "SMS_PARAMETERS_READ_RESP"},                 
	{0x16, "SMS_PARAMETERS_CAPACITY_REQ"},              
	{0x17, "SMS_PARAMETERS_CAPACITY_RESP"},             
	{0x18, "SMS_GSM_SETTINGS_UPDATE_REQ"},              
	{0x19, "SMS_GSM_SETTINGS_UPDATE_RESP"},   
	{0x1A, "SMS_GSM_SETTINGS_READ_REQ"},
	{0x1B, "SMS_GSM_SETTINGS_READ_RESP"},
	{0x1C, "SMS_GSM_MCN_SETTING_CHANGED_IND"},
	{0x1D, "SMS_MEMORY_CAPACITY_EXC_IND"},
	{0x1E, "SMS_STORAGE_STATUS_UPDATE_REQ"},
	{0x1F, "SMS_STORAGE_STATUS_UPDATE_RESP"},
	{0x22, "SMS_MESSAGE_SEND_STATUS_IND"},
	{0x23, "SMS_GSM_RESEND_CANCEL_REQ"},
	{0x24, "SMS_GSM_RESEND_CANCEL_RESP"},
	{0x25, "SMS_SM_CONTROL_ACTIVATE_REQ"},
	{0x26, "SMS_SM_CONTROL_ACTIVATE_RESP"},
	/* 0x29 is undocumented, but appears in traces */
	{0xF0, "COMMON_MESSAGE"},
	{0x00, NULL} 
};

static const value_string isi_sms_routing_command[] = {
	{0x00, "SMS_ROUTING_RELEASE"},
	{0x01, "SMS_ROUTING_SET"},
	{0x02, "SMS_ROUTING_SUSPEND"},
	{0x03, "SMS_ROUTING_RESUME"},
	{0x04, "SMS_ROUTING_UPDATE"},
	{0x05, "SMS_ROUTING_QUERY"},
	{0x06, "SMS_ROUTING_QUERY_ALL"},
	{0x00, NULL}
};

static const value_string isi_sms_routing_mode[] = {
	{0x00, "SMS_GSM_ROUTING_MODE_CLASS_DISP"},
	{0x01, "SMS_GSM_ROUTING_MODE_CLASS_TE"},
	{0x02, "SMS_GSM_ROUTING_MODE_CLASS_ME"},
	{0x03, "SMS_GSM_ROUTING_MODE_CLASS_SIM"},
	{0x04, "SMS_GSM_ROUTING_MODE_CLASS_UD1"},
	{0x05, "SMS_GSM_ROUTING_MODE_CLASS_UD2"},
	{0x06, "SMS_GSM_ROUTING_MODE_DATACODE_WAP"},
	{0x07, "SMS_GSM_ROUTING_MODE_DATACODE_8BIT"},
	{0x08, "SMS_GSM_ROUTING_MODE_DATACODE_TXT"},
	{0x09, "SMS_GSM_ROUTING_MODE_MWI_DISCARD"},
	{0x0A, "SMS_GSM_ROUTING_MODE_MWI_STORE"},
	{0x0B, "SMS_GSM_ROUTING_MODE_ALL"},
	{0x0C, "SMS_GSM_ROUTING_MODE_CB_DDL"},
	{0x00, NULL}
};

static const value_string isi_sms_route[] = {
	{0x00, "SMS_ROUTE_GPRS_PREF"},
	{0x01, "SMS_ROUTE_CS"},
	{0x02, "SMS_ROUTE_GPRS"},
	{0x03, "SMS_ROUTE_CS_PREF"},
	{0x04, "SMS_ROUTE_DEFAULT"},
	{0x00, NULL}
};

static const value_string isi_sms_sub_id[] = {
	{0x00, "SMS_GSM_DELIVER"},
	{0x01, "SMS_GSM_STATUS_REPORT"},
	{0x02, "SMS_GSM_SUBMIT"},
	{0x03, "SMS_GSM_COMMAND"},
	{0x06, "SMS_GSM_DELIVER_REPORT"},
	{0x0C, "SMS_GSM_REPORT"},
	{0x0D, "SMS_GSM_ROUTING"},
	{0x0E, "SMS_GSM_CB_MESSAGE"},
	{0x11, "SMS_GSM_TPDU"},
	{0x80, "SMS_COMMON_DATA"},
	{0x82, "SMS_ADDRESS"},
	{0x00, NULL}
};

/*
static const value_string isi_sms_subblock[] = {
	{0x00, "SS_FORWARDING"}, 
	{0x01, "SS_STATUS_RESULT"}, 
	{0x03, "SS_GSM_PASSWORD"},
	{0x04, "SS_GSM_FORWARDING_INFO"},
	{0x05, "SS_GSM_FORWARDING_FEATURE"}, 
	{0x08, "SS_GSM_DATA"}, 
	{0x09, "SS_GSM_BSC_INFO"}, 
	{0x0B, "SS_GSM_PASSWORD_INFO"}, 
	{0x0D, "SS_GSM_INDICATE_PASSWORD_ERROR"}, 
	{0x0E, "SS_GSM_INDICATE_ERROR"}, 
	{0x2F, "SS_GSM_ADDITIONAL_INFO"}, 
	{0x32, "SS_GSM_USSD_STRING"}, 
	{0x00, NULL}
};
*/

static const value_string isi_sms_send_status[] = {
	{0x00, "SMS_MSG_REROUTED"},
	{0x01, "SMS_MSG_REPEATED"},
	{0x02, "SMS_MSG_WAITING_NETWORK"},
	{0x03, "SMS_MSG_IDLE"},
	{0x00, NULL},
};

static const value_string isi_sms_common_message_id[] = {
	{0x01, "COMM_SERVICE_NOT_IDENTIFIED_RESP"},
	{0x12, "COMM_ISI_VERSION_GET_REQ"},
	{0x13, "COMM_ISI_VERSION_GET_RESP"},
	{0x14, "COMM_ISA_ENTITY_NOT_REACHABLE_RESP"},
	{0x00, NULL}
};

static const value_string isi_sms_sender_type[] = {
	{0x00, "SMS_SENDER_ANY"},
	{0x01, "SMS_SENDER_SIM_ATK"},
	{0x00, NULL}
};

static const value_string isi_sms_content_type[] = {
	{0x00, "SMS_TYPE_DEFAULT"},
	{0x01, "SMS_TYPE_TEXT_MESSAGE"},
	{0x00, NULL}
};

static const value_string isi_sms_addr_type[] = {
	{0x00, "SMS_ADDR_TYPE_UNICODE"},
	{0x01, "SMS_ADDR_TYPE_GSM_0340"},
	{0x02, "SMS_ADDR_TYPE_GSM_0411"}, /* can also stand for SMS_SMSC_ADDRESS */
	{0x00, NULL}
};

static const value_string isi_sms_tpdu_type[] = {
	{0x01, "STANDARD_SMS"},
	{0x41, "STANDARD_SMS_WITH_UDH"},
	{0x00, NULL}
};

static const value_string isi_sms_iei[] = {
	{0x00, "CONCATED_SHORT_MESSAGES"},
	{0x01, "SPECIAL_SMS_MESSAGE_INDICATION"},
	{0x00, NULL}
};

static dissector_handle_t isi_sms_handle;
static void dissect_isi_sms(tvbuff_t *tvb, packet_info *pinfo, proto_item *tree);

static gint32 hf_isi_sms_message_id = -1;
static gint32 hf_isi_sms_routing_command = -1;
static gint32 hf_isi_sms_routing_mode = -1;
static gint32 hf_isi_sms_moremsg = -1;
static gint32 hf_isi_sms_repeatmsg = -1;
static gint32 hf_isi_sms_route = -1;
static gint32 hf_isi_sms_subblock_count = -1;
static gint32 hf_isi_sms_subsubblock_count = -1;
static gint32 hf_isi_sms_send_status = -1;
static gint32 hf_isi_sms_common_message_id = -1;
static gint32 hf_isi_sms_sender_type = -1;
static gint32 hf_isi_sms_content_type = -1;
static gint32 hf_isi_sms_sub_type = -1;
static gint32 hf_isi_sms_sub_len = -1;
static gint32 hf_isi_sms_sub_sub_type = -1;
static gint32 hf_isi_sms_sub_sub_len = -1;
static gint32 hf_isi_sms_addr_type = -1;
static gint32 hf_isi_sms_addr_len = -1;
static gint32 hf_isi_sms_address = -1;
static gint32 hf_isi_sms_addr_gsm0340_len = -1;
static gint32 hf_isi_sms_addr_gsm0340_fmt = -1;
static gint32 hf_isi_sms_data_bytes = -1;
static gint32 hf_isi_sms_data_chars = -1;
static gint32 hf_isi_sms_parameters = -1;
static gint32 hf_isi_sms_reference = -1;
static gint32 hf_isi_sms_udh_len = -1;
static gint32 hf_isi_sms_udh_iei = -1;
static gint32 hf_isi_sms_udh_ele_len = -1;
static gint32 hf_isi_sms_udh_refid = -1;
static gint32 hf_isi_sms_udh_total_parts = -1;
static gint32 hf_isi_sms_udh_current_part = -1;

void proto_reg_handoff_isi_sms(void) {
	static gboolean initialized=FALSE;

	if (!initialized) {
		isi_sms_handle = create_dissector_handle(dissect_isi_sms, proto_isi);
		dissector_add_uint("isi.resource", 0x02, isi_sms_handle);
	}
}

void proto_register_isi_sms(void) {
	static hf_register_info hf[] = {
		{ &hf_isi_sms_message_id,
		  { "Message ID", "isi.sms.msg_id", FT_UINT8, BASE_HEX, isi_sms_message_id, 0x0, "Message ID", HFILL }},
		{ &hf_isi_sms_routing_command,
		  { "SMS Routing Command", "isi.sms.routing.command", FT_UINT8, BASE_HEX, isi_sms_routing_command, 0x0, "SMS Routing Command", HFILL }},
		{ &hf_isi_sms_routing_mode,
		  { "Routing Mode", "isi.sms.routing.mode", FT_UINT8, BASE_HEX, isi_sms_routing_mode, 0x0, "Routing Mode", HFILL }}, 
		{ &hf_isi_sms_route,
		  { "Message Route", "isi.sms.route", FT_UINT8, BASE_HEX, isi_sms_route, 0x0, "Message Route", HFILL }}, 
		{ &hf_isi_sms_subblock_count,
		  { "Subblock Count", "isi.sms.subblock_count", FT_UINT8, BASE_DEC, NULL, 0x0, "Subblock Count", HFILL }},
		{ &hf_isi_sms_subsubblock_count,
		  { "Sub-Subblock Count", "isi.sms.subsubblock_count", FT_UINT8, BASE_DEC, NULL, 0x0, "Sub-Subblock Count", HFILL }},
		{ &hf_isi_sms_send_status,
		  { "Sending Status", "isi.sms.sending_status", FT_UINT8, BASE_HEX, isi_sms_send_status, 0x0, "Sending Status", HFILL }},    
//		{ &hf_isi_sms_subblock,
//		  { "Subblock", "isi.sms.subblock", FT_UINT8, BASE_HEX, isi_sms_subblock, 0x0, "Subblock", HFILL }},
		{ &hf_isi_sms_common_message_id,
		  { "Common Message ID", "isi.sms.common.msg_id", FT_UINT8, BASE_HEX, isi_sms_common_message_id, 0x0, "Common Message ID", HFILL }},
		{ &hf_isi_sms_moremsg,
		  { "More Messages", "isi.sms.more_message_to_send", FT_UINT8, BASE_HEX, NULL, 0x0, "More Messages", HFILL }},
		{ &hf_isi_sms_repeatmsg,
		  { "Repeated Message", "isi.sms.repeated_msg", FT_UINT8, BASE_HEX, NULL, 0x0, "Repeated Message", HFILL }},
		{ &hf_isi_sms_sender_type,
		  { "Sender Type", "isi.sms.sender_type", FT_UINT8, BASE_HEX, isi_sms_sender_type, 0x0, "Sender Type", HFILL }},
		{ &hf_isi_sms_content_type,
		  { "Content Type", "isi.sms.content_type", FT_UINT8, BASE_HEX, isi_sms_content_type, 0x0, "Content Type", HFILL }},
		{ &hf_isi_sms_sub_type,
		  { "Sub Type", "isi.sms.sub_type", FT_UINT8, BASE_HEX, isi_sms_sub_id, 0x0, "Sub Type", HFILL }},
		{ &hf_isi_sms_sub_len,
		  { "Sub Length", "isi.sms.sub_len", FT_UINT8, BASE_DEC, NULL, 0x0, "Sub Length", HFILL }},
		{ &hf_isi_sms_sub_sub_type,
		  { "Sub-Sub Type", "isi.sms.sub_sub_type", FT_UINT8, BASE_HEX, isi_sms_sub_id, 0x0, "Sub-Sub Type", HFILL }},
		{ &hf_isi_sms_sub_sub_len,
		  { "Sub-Sub Length", "isi.sms.sub_sub_len", FT_UINT8, BASE_DEC, NULL, 0x0, "Sub-Sub Length", HFILL }},
		{ &hf_isi_sms_addr_type,
		  { "SMS Address Type", "isi.sms.addr.type", FT_UINT8, BASE_HEX, isi_sms_addr_type, 0x0, "SMS Address Type", HFILL }},
		{ &hf_isi_sms_addr_len,
		  { "SMS Address Length", "isi.sms.addr.len", FT_UINT8, BASE_DEC, NULL, 0x0, "SMS Address Length", HFILL }},
		{ &hf_isi_sms_address,
		  { "SMS Address", "isi.sms.addr.address", FT_STRING, BASE_NONE, NULL, 0x0, "SMS Address", HFILL }},
		{ &hf_isi_sms_addr_gsm0340_len,
		  { "GSM 03.40 Address Length", "isi.sms.addr.gsm0340_len", FT_UINT8, BASE_DEC, NULL, 0x0, "GSM 03.40 Address Length", HFILL }},
		{ &hf_isi_sms_addr_gsm0340_fmt,
		  { "GSM 03.40 Address Format", "isi.sms.addr.gsm0340_fmt", FT_UINT8, BASE_HEX, NULL, 0x0, "GSM 03.40 Address Format", HFILL }},
		{ &hf_isi_sms_data_bytes,
		  { "Common Data Bytes", "isi.sms.data.bytes", FT_UINT8, BASE_HEX, NULL, 0x0, "Common Data Bytes", HFILL }},
		{ &hf_isi_sms_data_chars,
		  { "Common Data Characters", "isi.sms.data.chars", FT_UINT8, BASE_HEX, NULL, 0x0, "Common Data Characters", HFILL }},
		{ &hf_isi_sms_parameters,
		  { "Message Parameters", "isi.sms.tp.type", FT_UINT8, BASE_HEX, isi_sms_tpdu_type, 0x0, "TPDU Type", HFILL }},
		{ &hf_isi_sms_reference,
		  { "Message Reference", "isi.sms.tp.mr", FT_UINT8, BASE_HEX, NULL, 0x0, "TPDU Reference", HFILL }},
		{ &hf_isi_sms_udh_len,
		  { "UDH Length", "isi.sms.udh.len", FT_UINT8, BASE_DEC, NULL, 0x0, "UDH Length", HFILL }},
		{ &hf_isi_sms_udh_iei,
		  { "Information Element Identifier (IEI)", "isi.sms.udh.ele.iei", FT_UINT8, BASE_HEX, isi_sms_iei, 0x0, "Information Element Identifier (IEI)", HFILL }},
		{ &hf_isi_sms_udh_ele_len,
		  { "Information Element Length", "isi.sms.udh.ele.len", FT_UINT8, BASE_DEC, NULL, 0x0, "Information Element Length", HFILL }},
		{ &hf_isi_sms_udh_refid,
		  { "CSMS Reference ID", "isi.sms.udh.refid", FT_UINT8, BASE_DEC, NULL, 0x0, "CSMS Reference ID", HFILL }},
		{ &hf_isi_sms_udh_total_parts,
		  { "CSMS Total Number of Parts", "isi.sms.udh.total_parts", FT_UINT8, BASE_DEC, NULL, 0x0, "CSMS Total Number of Parts", HFILL }},
		{ &hf_isi_sms_udh_current_part,
		  { "Current Part", "isi.sms.udh.current_part", FT_UINT8, BASE_DEC, NULL, 0x0, "Current Part", HFILL }},
	};

	proto_register_field_array(proto_isi, hf, array_length(hf));
	register_dissector("isi.sms", dissect_isi_sms, proto_isi);
}

#define GET_MASK(x) (0xFF >> (8-x))

/* source: https://en.wikipedia.org/wiki/GSM_03.38 */
static const char* charset[] = {
	"@", "£", "$", "¥", "è", "é", "ù", "ì", "ò", "Ç", "\n", "Ø", "ø", "\r", "Å", "å",
	"Δ", "_", "Φ", "Γ", "Λ", "Ω", "Π", "Ψ", "Σ", "Θ", "Ξ", "\b", "Æ", "æ", "ß", "É",
	" ", "!", "\"", "#", "¤", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/",
	"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?",
	"¡", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
	"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "Ä", "Ö", "Ñ", "Ü", "§",
	"¿", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
	"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "ä", "ö", "ñ", "ü", "à"
};

static int LONGEST_CHAR() {
	int max=0, cur, i;
	for(i=0; i < 0x7f; i++) {
		cur = strlen(charset[i]);
		max = cur > max ? cur : max;
	}
	return max;
}

static char* sms_translate_charset(char *str) {
	int inlen = strlen(str);
	int xlen;
	unsigned char o = 0, i, c;
	char *out = malloc(inlen*LONGEST_CHAR());

	for(i=0; i < inlen; i++) {
		c = str[i];
		xlen = strlen(charset[c]);
		if(c > 0x7f) {
			continue;
		}
		memcpy(out+o, charset[c], xlen);
		o += xlen;
	}
	out[o] = '\0';

	return out;
};

static void dissect_isi_sms_iei(tvbuff_t *tvb, packet_info *pinfo, proto_item *item, proto_tree *tree, guint32 offset, guint8 iei, guint8 len) {
	proto_tree_add_item(tree, hf_isi_sms_udh_iei, tvb, offset, 1, FALSE);
	proto_tree_add_item(tree, hf_isi_sms_udh_ele_len, tvb, offset+1, 1, FALSE);

	switch(iei) {
		case 0x00:
			proto_tree_add_item(tree, hf_isi_sms_udh_refid, tvb, offset+2, 1, FALSE);
			proto_tree_add_item(tree, hf_isi_sms_udh_total_parts, tvb, offset+3, 1, FALSE);
			proto_tree_add_item(tree, hf_isi_sms_udh_current_part, tvb, offset+4, 1, FALSE);
			break;
		default:
			col_set_str(pinfo->cinfo, COL_INFO, "Unknown type");
			break;
	}
}

static void dissect_isi_sms_data(tvbuff_t *tvb, packet_info *pinfo, proto_item *item, proto_tree *tree, guint32 offset, guint8 udhi) {
	guint8 b = 0, c = 0, old = 0;
	guint8 udh_size = 0, udh_padding = 0;

	proto_tree_add_item(tree, hf_isi_sms_data_bytes, tvb, offset+0, 1, FALSE);
	proto_tree_add_item(tree, hf_isi_sms_data_chars, tvb, offset+1, 1, FALSE);

	/* byte-align the 7-bit characters */
	guint8 bytes = tvb_get_guint8(tvb, offset+0);
	guint8 chars = tvb_get_guint8(tvb, offset+1);

	char *msg = malloc(chars+1);

	if(udhi) {
		udh_size = tvb_get_guint8(tvb, offset+2)+1; /* UDH Length*/

		proto_item *subitem = proto_tree_add_text(tree, tvb, offset+2, udh_size, "User Data Header");
		proto_tree *subtree = proto_item_add_subtree(subitem, ett_isi_msg);

		proto_tree_add_item(subtree, hf_isi_sms_udh_len, tvb, offset+2, 1, FALSE);

		for(b=1; b < udh_size; b++) {
			guint8 udh_iei = tvb_get_guint8(tvb, offset+2+b); /* Information Element Identifier */
			guint8 udh_sub_len = tvb_get_guint8(tvb, offset+2+b+1)+2; /* subpkg len (excluding iei + len) */

			proto_item *elesubitem = proto_tree_add_text(subtree, tvb, offset+2+b, udh_sub_len, "Information Element");
			proto_tree *elesubtree = proto_item_add_subtree(elesubitem, ett_isi_msg);

			dissect_isi_sms_iei(tvb, pinfo, elesubitem, elesubtree, offset+2+b, udh_iei, udh_sub_len);

			b += udh_sub_len;
		}

		/* udh_size bytes have already been handled */
		b = udh_size;
		bytes -= udh_size;

		udh_padding = 7 - (udh_size*8 % 7);
		if(udh_padding > 1) {
			old = tvb_get_guint8(tvb, offset+2+b);
			b++;
		} else if(udh_padding == 1) {
			msg[c] = tvb_get_guint8(tvb, offset+2+b) >> 1;
			b++;
			c++;
		}
	}

	for(; b<bytes && c < chars; b++, c++) {
		guint8 new = tvb_get_guint8(tvb, offset+2+b);

		switch(b % 7) {
			case 0:
				msg[c] = ((new & GET_MASK(7)) << 0) | old >> 8; // 0 old
				break;
			case 1:
				msg[c] = ((new & GET_MASK(6)) << 1) | old >> 7; // 1 old
				break;
			case 2:
				msg[c] = ((new & GET_MASK(5)) << 2) | old >> 6; // 2 old
				break;
			case 3:
				msg[c] = ((new & GET_MASK(4)) << 3) | old >> 5; // 3 old
				break;
			case 4:
				msg[c] = ((new & GET_MASK(3)) << 4) | old >> 4; // 4 old
				break;
			case 5:
				msg[c] = ((new & GET_MASK(2)) << 5) | old >> 3; // 5 old
				break;
			case 6:
				msg[c] = ((new & GET_MASK(1)) << 6) | old >> 2; // 6 old
				if(++c < chars)
					msg[c] = ((new & GET_MASK(8)) >> 1);
				break;
		}

		old = new;
	}
	msg[c] = '\0';

	/* decode character set */
	char *message = sms_translate_charset(msg);
	free(msg);
	proto_tree_add_text(tree, tvb, offset+2+udh_size, bytes, "Message: %s", message);
}

static void dissect_isi_sms_addr(tvbuff_t *tvb, packet_info *pinfo, proto_item *item, proto_tree *tree, guint32 offset) {
	int i;

	proto_tree_add_item(tree, hf_isi_sms_addr_type, tvb, offset+0, 1, FALSE);
	proto_tree_add_item(tree, hf_isi_sms_addr_len, tvb, offset+1, 1, FALSE);

	guint8 addrtype = tvb_get_guint8(tvb, offset+0);

	switch(addrtype) {
		case 0x00: /* UNICODE */
			col_set_str(pinfo->cinfo, COL_INFO, "Unicode address dissection not supported");
			break;
		case 0x01: /* GSM_0340 */
		{
			guint8 len = tvb_get_guint8(tvb, offset+2);
			guint8 bytelen = (len + 1) / 2;
			guint8 format = tvb_get_guint8(tvb, offset+3);

			char *number = malloc(len+2);
			guint8 international = 0;

			if(format == 0x91) {
				international=1;
				number[0] = '+';
			}

			for(i=0; i < bytelen; i++) {
				guint8 b = tvb_get_guint8(tvb, offset+4+i);
				number[(i*2)+international] = '0' + ((b & 0xF0) >> 4);

				if ((i+1)*2 <= len)
					number[(i*2)+1+international] = '0' + (b & 0x0F);
			}
			number[len+international] = '\0';

			proto_tree_add_item(tree, hf_isi_sms_addr_gsm0340_len, tvb, offset+2, 1, FALSE);
			proto_tree_add_item(tree, hf_isi_sms_addr_gsm0340_fmt, tvb, offset+3, 1, FALSE);
			proto_tree_add_string(tree, hf_isi_sms_address, tvb, offset+4, bytelen, number);
			break;
		}
		case 0x02: /* GSM_0411 */
		{
			guint8 bytelen = tvb_get_guint8(tvb, offset+2)-1;
			guint8 format = tvb_get_guint8(tvb, offset+3);

			char *number = malloc(bytelen*2+2);
			guint8 international = 0;

			if(format == 0x91) {
				international=1;
				number[0] = '+';
			}

			for(i=0; i < bytelen; i++) {
				guint8 b = tvb_get_guint8(tvb, offset+4+i);
				number[(i*2)+international] = '0' + (b & 0x0F);

				if (((b & 0xF0) >> 4 != 0xF))
					number[(i*2)+1+international] = '0' + ((b & 0xF0) >> 4);
			}
			number[(i*2)+international] = '\0';

			proto_tree_add_item(tree, hf_isi_sms_addr_gsm0340_len, tvb, offset+2, 1, FALSE);
			proto_tree_add_item(tree, hf_isi_sms_addr_gsm0340_fmt, tvb, offset+3, 1, FALSE);
			proto_tree_add_string(tree, hf_isi_sms_address, tvb, offset+4, bytelen, number);
			break;

			break;
		}
		default:
			col_set_str(pinfo->cinfo, COL_INFO, "Unknown type");
			break;
	}
}

static void dissect_isi_sms_submit(tvbuff_t *tvb, packet_info *pinfo, proto_item *item, proto_tree *tree, guint32 offset) {
	guint8 pkgcount, i;
	guint8 udh = 0; /* user data header */

	/* First Byte of TDPU: TP-MTI, TP-RD, TP-VPF, TP-RP, TP-UDHI, TP-SRR*/
	proto_tree_add_item(tree, hf_isi_sms_parameters, tvb, offset + 0x02, 1, FALSE);
	udh = !!(tvb_get_guint8(tvb, offset + 0x02) & 0x40);

	/* TP-Message-Reference */
	proto_tree_add_item(tree, hf_isi_sms_reference, tvb, offset + 0x03, 1, FALSE);

	/* Next 3 bytes are unknown, since they were always 0x00 for me. Likely candidates: */
	/* TP-Protocol-Identifier (TP-PID) */
	/* SMS Data Coding Scheme */
	/* Padding */

	pkgcount = tvb_get_guint8(tvb, offset + 0x07);
	proto_tree_add_item(tree, hf_isi_sms_subsubblock_count, tvb, offset + 0x07, 1, FALSE);

	offset += 0x08;
	for(i=0; i<pkgcount; i++) {
		guint8 sptype = tvb_get_guint8(tvb, offset+0);
		guint8 splen = tvb_get_guint8(tvb, offset+1);

		proto_item *subitem = proto_tree_add_text(tree, tvb, offset, splen, "Sub-Subpacket (%s)", val_to_str(sptype, isi_sms_sub_id, "unknown: 0x%x"));
		proto_tree *subtree = proto_item_add_subtree(subitem, ett_isi_msg);

		proto_tree_add_item(subtree, hf_isi_sms_sub_sub_type, tvb, offset+0, 1, FALSE);
		proto_tree_add_item(subtree, hf_isi_sms_sub_sub_len, tvb, offset+1, 1, FALSE);

		switch(sptype) {
			case 0x80: /* SMS_COMMON_DATA*/
				dissect_isi_sms_data(tvb, pinfo, item, subtree, offset+2, udh);
				break;
			case 0x82: /* SMS_ADDRESS */
				dissect_isi_sms_addr(tvb, pinfo, item, subtree, offset+2);
				break;
			default:
				col_set_str(pinfo->cinfo, COL_INFO, "Unknown type");
				break;
		}

		offset += splen;
	}
}

static void dissect_isi_sms(tvbuff_t *tvb, packet_info *pinfo, proto_item *isitree) {
	proto_item *item = NULL;
	proto_tree *tree = NULL;
	guint8 cmd, code, i, pkgcount;
	size_t offset;

	if(isitree) {
		item = proto_tree_add_text(isitree, tvb, 0, -1, "Payload");
		tree = proto_item_add_subtree(item, ett_isi_msg);

		proto_tree_add_item(tree, hf_isi_sms_message_id, tvb, 0, 1, FALSE);
		cmd = tvb_get_guint8(tvb, 0);

		switch(cmd) {
			case 0x02: /* SMS_MESSAGE_SEND_REQ */
				col_set_str(pinfo->cinfo, COL_INFO, "SMS Message Send Request");
				pkgcount = tvb_get_guint8(tvb, 0x06);

				proto_tree_add_item(tree, hf_isi_sms_moremsg, tvb, 1, 1, FALSE);
				proto_tree_add_item(tree, hf_isi_sms_route, tvb, 2, 1, FALSE);
				proto_tree_add_item(tree, hf_isi_sms_repeatmsg, tvb, 3, 1, FALSE);
				proto_tree_add_item(tree, hf_isi_sms_sender_type, tvb, 4, 1, FALSE);
				proto_tree_add_item(tree, hf_isi_sms_content_type, tvb, 5, 1, FALSE);
				proto_tree_add_item(tree, hf_isi_sms_subblock_count, tvb, 6, 1, FALSE);

				offset = 0x07;
				for(i=0; i<pkgcount; i++) {
					guint8 sptype = tvb_get_guint8(tvb, offset+0);
					guint8 splen = tvb_get_guint8(tvb, offset+1);

					proto_item *subitem = proto_tree_add_text(tree, tvb, offset, splen, "Subpacket (%s)", val_to_str(sptype, isi_sms_sub_id, "unknown: 0x%x"));
					proto_tree *subtree = proto_item_add_subtree(subitem, ett_isi_msg);

					proto_tree_add_item(subtree, hf_isi_sms_sub_type, tvb, offset+0, 1, FALSE);
					proto_tree_add_item(subtree, hf_isi_sms_sub_len, tvb,  offset+1, 1, FALSE);

					switch(sptype) {
						case 0x02: /* SMS_GSM_SUBMIT */
							dissect_isi_sms_submit(tvb, pinfo, item, subtree, offset);
							break;
						default:
							col_set_str(pinfo->cinfo, COL_INFO, "Unknown type");
							break;
					}

					offset += splen;
				}

				break;

			case 0x03: /* SMS_MESSAGE_SEND_RESP */
				col_set_str(pinfo->cinfo, COL_INFO, "SMS Message Send Response");
				proto_tree_add_item(tree, hf_isi_sms_subblock_count, tvb, 2, 1, FALSE);
				pkgcount = tvb_get_guint8(tvb, 2);
				offset = 0x03;

				for(i=0; i<pkgcount; i++) {
					guint8 sptype = tvb_get_guint8(tvb, offset+0);
					guint8 splen = tvb_get_guint8(tvb, offset+1);

					proto_item *subitem = proto_tree_add_text(tree, tvb, offset, splen, "Subpacket (%s)", val_to_str(sptype, isi_sms_sub_id, "unknown: 0x%x"));
					proto_tree *subtree = proto_item_add_subtree(subitem, ett_isi_msg);

					proto_tree_add_item(subtree, hf_isi_sms_sub_type, tvb, offset+0, 1, FALSE);
					proto_tree_add_item(subtree, hf_isi_sms_sub_len, tvb,  offset+1, 1, FALSE);

					switch(sptype) {
						case 0x0C: /* SMS_GSM_REPORT */
							/* TODO */
							break;
						default:
							break;
					}

					offset += splen;
				}
				break; 

			case 0x06: /* SMS_PP_ROUTING_REQ */
				proto_tree_add_item(tree, hf_isi_sms_routing_command, tvb, 1, 1, FALSE);
				proto_tree_add_item(tree, hf_isi_sms_subblock_count, tvb, 2, 1, FALSE);
				code = tvb_get_guint8(tvb, 1);
				switch(code) {
//					case 0x05:
//						col_set_str(pinfo->cinfo, COL_INFO, "Service Request: Interrogation");
//						break;
//				case 0x06:
//						col_set_str(pinfo->cinfo, COL_INFO, "Service Request: GSM Password Registration");
//						break;
					default:
						col_set_str(pinfo->cinfo, COL_INFO, "SMS Point-to-Point Routing Request");
						break;
				}
				break; 
				
			case 0x07: /* SMS_PP_ROUTING_RESP */
//				//proto_tree_add_item(tree, hf_isi_sms_service_type, tvb, 1, 1, FALSE);
				code = tvb_get_guint8(tvb, 1);
				switch(code) {
//					//case 0x2F:
//					//	col_set_str(pinfo->cinfo, COL_INFO, "Network Information Request: Read Home PLMN");
//					//	break;
					default:
						col_set_str(pinfo->cinfo, COL_INFO, "SMS Point-to-Point Routing Response");
						break;
				}
				break; 
				
			case 0x0B: /* SMS_GSM_CB_ROUTING_REQ */
				proto_tree_add_item(tree, hf_isi_sms_routing_command, tvb, 1, 1, FALSE);
				proto_tree_add_item(tree, hf_isi_sms_routing_mode, tvb, 2, 1, FALSE);
//				proto_tree_add_item(tree, hf_isi_sms_cb_subject_list_type, tvb, 3, 1, FALSE);
//				proto_tree_add_item(tree, hf_isi_sms_cb_subject_count, tvb, 4, 1, FALSE);
//				proto_tree_add_item(tree, hf_isi_sms_cb_language_count, tvb, 5, 1, FALSE);
//				proto_tree_add_item(tree, hf_isi_sms_cb_range, tvb, 6, 1, FALSE);
				code = tvb_get_guint8(tvb, 1);
				switch(code) {
					case 0x00:
						col_set_str(pinfo->cinfo, COL_INFO, "SMS GSM Cell Broadcast Routing Release");
						break;
					case 0x01:
						col_set_str(pinfo->cinfo, COL_INFO, "SMS GSM Cell Broadcast Routing Set");
						break;
					default:
						col_set_str(pinfo->cinfo, COL_INFO, "SMS GSM Cell Broadcast Routing Request");
						break;
				}
				break; 
								
			case 0x0C: /* SMS_GSM_CB_ROUTING_RESP */
//				proto_tree_add_item(tree, hf_isi_sms_operation, tvb, 1, 1, FALSE);
//				proto_tree_add_item(tree, hf_isi_sms_service_code, tvb, 2, 1, FALSE);
				code = tvb_get_guint8(tvb, 1);
				switch(code) {
//					case 0x05:
//						col_set_str(pinfo->cinfo, COL_INFO, "Service Completed Response: Interrogation");
//						break;
					default:
						col_set_str(pinfo->cinfo, COL_INFO, "SMS GSM Cell Broadcast Routing Response");
						break;
				}
				break; 

			case 0x22: /* SMS_MESSAGE_SEND_STATUS_IND */
				proto_tree_add_item(tree, hf_isi_sms_send_status, tvb, 1, 1, FALSE);
				/* The second byte is a "segment" identifier/"Message Reference" */
				proto_tree_add_item(tree, hf_isi_sms_route, tvb, 3, 1, FALSE);
				code = tvb_get_guint8(tvb, 1);
				switch(code) {
					case 0x02:
						col_set_str(pinfo->cinfo, COL_INFO, "SMS Message Sending Status: Waiting for Network");
						break;
					case 0x03:
						col_set_str(pinfo->cinfo, COL_INFO, "SMS Message Sending Status: Idle");
						break;
					default:
						col_set_str(pinfo->cinfo, COL_INFO, "SMS Message Sending Status Indication");
						break;
				}
				break; 	

			case 0xF0: /* COMMON_MESSAGE */
				dissect_isi_common("SMS", tvb, pinfo, tree);
				break; 

			default:
				col_set_str(pinfo->cinfo, COL_INFO, "Unknown type");
				break;
		}
	}
}