aboutsummaryrefslogtreecommitdiffstats
path: root/samsung-ipc/sms.c
blob: f0e292509a99431e14ef90c1d11c5986bf63d8a4 (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
/*
 * 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>

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(smsc_length) + smsc_size + pdu_size);

    size = sizeof(struct ipc_sms_send_msg_request_header) + sizeof(smsc_length) + smsc_size + pdu_size;
    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;
}

char *ipc_sms_incoming_msg_pdu_extract(const void *data, size_t size)
{
    struct ipc_sms_incoming_msg_header *header;
    char *string;
    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));

    string = data2string(pdu, header->length);

    return string;
}

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(smsc_length) + smsc_size + pdu_size);

    size = sizeof(struct ipc_sms_save_msg_request_header) + sizeof(smsc_length) + smsc_size + pdu_size;
    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;
}

// vim:ts=4:sw=4:expandtab