aboutsummaryrefslogtreecommitdiffstats
path: root/samsung-ipc/sms.c
blob: ba4c159d0d9bbdcccd9ee6500f3573e2d7f769b7 (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
/*
 * This file is part of libsamsung-ipc.
 *
 * Copyright (C) 2011 Simon Busch <morphis@gravedo.de>
 * Copyright (C) 2014 Paul Kocialkowski <contact@paulk.fr>
 *
 * libsamsung-ipc 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 2 of the License, or
 * (at your option) any later version.
 *
 * libsamsung-ipc 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 libsamsung-ipc.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <string.h>

#include <samsung-ipc.h>

size_t ipc_sms_send_msg_size_setup(
	struct ipc_sms_send_msg_request_header *header,
	const void *smsc, size_t smsc_size, const void *pdu, size_t pdu_size)
{
	size_t size;

	if (header == NULL || smsc == NULL || smsc_size == 0 || pdu == NULL ||
	    pdu_size == 0) {
		return 0;
	}

	size = sizeof(struct ipc_sms_send_msg_request_header) +
		sizeof(unsigned char) + smsc_size + pdu_size;

	return size;
}

void *ipc_sms_send_msg_setup(struct ipc_sms_send_msg_request_header *header,
			     const void *smsc, size_t smsc_size,
			     const void *pdu, size_t pdu_size)
{
	void *data;
	size_t size;
	unsigned char smsc_length;
	unsigned char *p;

	if (header == NULL || smsc == NULL || smsc_size == 0 || pdu == NULL ||
	    pdu_size == 0) {
		return NULL;
	}

	smsc_length = (unsigned char) smsc_size;

	header->length = (unsigned char) (
		sizeof(unsigned char) + smsc_size + pdu_size);

	size = ipc_sms_send_msg_size_setup(header, smsc, smsc_size, pdu,
					   pdu_size);
	if (size == 0)
		return NULL;

	data = calloc(1, size);

	p = (unsigned char *) data;

	memcpy(p, header, sizeof(struct ipc_sms_send_msg_request_header));
	p += sizeof(struct ipc_sms_send_msg_request_header);

	memcpy(p, &smsc_length, sizeof(smsc_length));
	p += sizeof(smsc_length);

	memcpy(p, smsc, smsc_size);
	p += smsc_size;

	memcpy(p, pdu, pdu_size);
	p += pdu_size;

	return data;
}

size_t ipc_sms_incoming_msg_pdu_size_extract(const void *data, size_t size)
{
	struct ipc_sms_incoming_msg_header *header;

	header = (struct ipc_sms_incoming_msg_header *) data;
	if (header->length == 0 ||
	    header->length > size -
	    sizeof(struct ipc_sms_incoming_msg_header)) {
		return 0;
	}

	return (size_t) header->length;
}

void *ipc_sms_incoming_msg_pdu_extract(const void *data, size_t size)
{
	struct ipc_sms_incoming_msg_header *header;
	void *pdu;

	if (data == NULL || size < sizeof(struct ipc_sms_incoming_msg_header))
		return NULL;

	header = (struct ipc_sms_incoming_msg_header *) data;
	if (header->length == 0 ||
	    header->length > size -
	    sizeof(struct ipc_sms_incoming_msg_header)) {
		return NULL;
	}

	pdu = (void *) ((unsigned char *) data +
			sizeof(struct ipc_sms_incoming_msg_header));

	return pdu;
}

size_t ipc_sms_save_msg_size_setup(
	struct ipc_sms_save_msg_request_header *header,
	const void *smsc, size_t smsc_size, const void *pdu, size_t pdu_size)
{
	size_t size;

	if (header == NULL || pdu == NULL || pdu_size == 0)
		return 0;

	if (smsc == NULL)
		smsc_size = 0;

	size = sizeof(struct ipc_sms_save_msg_request_header) +
		sizeof(unsigned char) + smsc_size + pdu_size;

	return size;
}

void *ipc_sms_save_msg_setup(struct ipc_sms_save_msg_request_header *header,
			     const void *smsc, size_t smsc_size,
			     const void *pdu, size_t pdu_size)
{
	void *data;
	size_t size;
	unsigned char smsc_length;
	unsigned char *p;

	if (header == NULL || pdu == NULL || pdu_size == 0)
		return NULL;

	if (smsc == NULL)
		smsc_size = 0;

	smsc_length = (unsigned char) smsc_size;

	header->magic = 2;
	header->index = 12 - 1,
		header->length = (unsigned char) (
			sizeof(unsigned char) + smsc_size + pdu_size);

	size = ipc_sms_save_msg_size_setup(header, smsc, smsc_size, pdu,
					   pdu_size);
	if (size == 0)
		return NULL;

	data = calloc(1, size);

	p = (unsigned char *) data;

	memcpy(p, header, sizeof(struct ipc_sms_save_msg_request_header));
	p += sizeof(struct ipc_sms_save_msg_request_header);

	memcpy(p, &smsc_length, sizeof(smsc_length));
	p += sizeof(smsc_length);

	if (smsc != NULL && smsc_size > 0) {
		memcpy(p, smsc, smsc_size);
		p += smsc_size;
	}

	memcpy(p, pdu, pdu_size);
	p += pdu_size;

	return data;
}

int ipc_sms_del_msg_setup(struct ipc_sms_del_msg_request_data *data,
			  unsigned short index)
{
	if (data == NULL)
		return -1;

	memset(data, 0, sizeof(struct ipc_sms_del_msg_request_data));
	data->magic = 2;
	data->index = index;

	return 0;
}

size_t ipc_sms_svc_center_addr_smsc_size_extract(const void *data, size_t size)
{
	struct ipc_sms_svc_center_addr_header *header;

	if (data == NULL ||
	    size < sizeof(struct ipc_sms_svc_center_addr_header)) {
		return 0;
	}

	header = (struct ipc_sms_svc_center_addr_header *) data;
	if (header->length == 0 ||
	    header->length > size -
	    sizeof(struct ipc_sms_svc_center_addr_header)) {
		return 0;
	}

	return (size_t) header->length;
}

void *ipc_sms_svc_center_addr_smsc_extract(const void *data, size_t size)
{
	struct ipc_sms_svc_center_addr_header *header;
	void *smsc;

	if (data == NULL ||
	    size < sizeof(struct ipc_sms_svc_center_addr_header)) {
		return NULL;
	}

	header = (struct ipc_sms_svc_center_addr_header *) data;
	if (header->length == 0 ||
	    header->length > size -
	    sizeof(struct ipc_sms_svc_center_addr_header)) {
		return NULL;
	}

	smsc = (void *) ((unsigned char *) data +
			 sizeof(struct ipc_sms_svc_center_addr_header));

	return smsc;
}