/
mms_util.c
118 lines (109 loc) · 3.02 KB
/
mms_util.c
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
/*
* Copyright (C) 2013-2015 Jolla Ltd.
* Contact: Slava Monich <slava.monich@jolla.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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.
*
*/
#include "mms_util.h"
#include "mms_codec.h"
/**
* Strips leading spaces and "/TYPE=" suffix from the string.
*/
char*
mms_strip_address_type(
char* address)
{
if (address) {
char* type = g_strrstr(g_strstrip(address), MMS_ADDRESS_TYPE_SUFFIX);
if (type) *type = 0;
}
return address;
}
/**
* Splits comma-separated list of addresses into an array of string pointers.
* Strips "/TYPE=" suffix from each address. Caller needs to deallocate the
* returned list with g_strfreev.
*/
char**
mms_split_address_list(
const char* addres_list)
{
char** list = NULL;
if (addres_list && addres_list[0]) {
int i;
list = g_strsplit(addres_list, ",", 0);
for (i=0; list[i]; i++) {
list[i] = mms_strip_address_type(list[i]);
}
} else {
list = g_new(char*, 1);
list[0] = NULL;
}
return list;
}
/**
* Removes leading and trailing whitespaces from the address and appends
* the address type if necessary (defaults to phone number). In any case
* the caller must deallocate the returned string. Returns NULL if the
* input string is either NULL or empty.
*/
char*
mms_address_normalize(
const char* address)
{
if (address) {
gssize len;
/* Skip leading whitespaces */
while (*address && g_ascii_isspace(*address)) address++;
/* Calculate the length without traling whitespaces */
len = strlen(address);
while (len > 0 && g_ascii_isspace(address[len-1])) len--;
if (len > 0) {
/* Append the address type if necessary */
char* out;
if (g_strrstr_len(address, len, MMS_ADDRESS_TYPE_SUFFIX)) {
out = g_strndup(address, len);
} else {
out = g_new(char, len+sizeof(MMS_ADDRESS_TYPE_SUFFIX_PHONE));
strncpy(out, address, len);
strcpy(out+len, MMS_ADDRESS_TYPE_SUFFIX_PHONE);
}
return out;
}
}
return NULL;
}
/**
* Allocates and decodes WAP push PDU. Returns NULL if decoding fails.
*/
MMSPdu*
mms_decode_bytes(
GBytes* bytes)
{
MMSPdu* pdu = NULL;
if (bytes) {
gsize len = 0;
const guint8* data = g_bytes_get_data(bytes, &len);
pdu = g_new0(MMSPdu, 1);
if (!mms_message_decode(data, len, pdu)) {
mms_message_free(pdu);
pdu = NULL;
}
}
return pdu;
}
/*
* Local Variables:
* mode: C
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/