summaryrefslogtreecommitdiff
path: root/lcmodule/source/serialization.c
blob: 717f847301988dc2d9afc8c33d6d690f5e18a7ea (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
/*******************************************************************************
 * Copyright (C) ST-Ericsson SA 2011
 * License terms: 3-clause BSD license
 ******************************************************************************/
/*
 *  @addtogroup ldr_utilities
 *  @{
 *    @addtogroup serialization
 *    @{
 */

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include "r_serialization.h"
#include <string.h>
#include <stdlib.h>
#include "r_debug_macro.h"

/*******************************************************************************
 * Definition of external functions
 ******************************************************************************/

void get_block(const void **data_pp, void *target_p, uint32 length)
{
    memcpy(target_p, *data_pp, length);
    *(const uint8 **)data_pp += length;
}

uint8 get_uint8(void **data_pp)
{
    const uint8 *d = *(const uint8 **)data_pp;
    *(const uint8 **)data_pp += sizeof(uint8);
    return d[0];
}

uint16 get_uint16_le(void **data_pp)
{
    const uint8 *d = *(const uint8 **)data_pp;
    *(const uint8 **)data_pp += sizeof(uint16);
    return (d[1] << 8) | d[0];
}

uint16 get_uint16_be(void **data_pp)
{
    const uint8 *d = *(const uint8 **)data_pp;
    *(const uint8 **)data_pp += sizeof(uint16);
    return (d[0] << 8) | d[1];
}

uint32 get_uint32_le(void **data_pp)
{
    const uint8 *d = *(const uint8 **) data_pp;
    *(const uint8 **)data_pp += sizeof(uint32);
    return ((d[3] << 8 | d[2]) << 8 | d[1]) << 8 | d[0];
}

uint64 get_uint64_le(void **data_pp)
{
    const uint8 *d = *(const uint8 **) data_pp;
    *(const uint8 **)data_pp += sizeof(uint64);
    return (((((((uint64)d[7] << 8 | d[6]) << 8 | d[5]) << 8 | d[4]) << 8 | d[3]) << 8 | d[2]) << 8 | d[1]) << 8 | d[0];
}

uint32 get_uint32_string_le(void **data_pp)
{
    const uint8 *d = *(const uint8 **) data_pp;
    //*(const uint8 **)data_pp += sizeof(uint32);
    return ((d[3] << 8 | d[2]) << 8 | d[1]) << 8 | (d[0] + sizeof(uint32));
}

uint32 get_uint32_be(void **data_pp)
{
    const uint8 *d = *(const uint8 **) data_pp;
    *(const uint8 **)data_pp += sizeof(uint32);
    return ((d[0] << 8 | d[1]) << 8 | d[2]) << 8 | d[3];
}

void put_block(void **data_pp, const void *source_p, uint32 length)
{
    if (length > 0) {
        memcpy(*data_pp, source_p, length);
        *(const uint8 **)data_pp += length;
    }
}

void put_string(void **data_pp, const void *source_p, uint32 length)
{
    if (length > 0) {
        memcpy(*data_pp, &length, sizeof(uint32));
        *(const uint8 **)data_pp += sizeof(uint32);
        memcpy(*data_pp, source_p, length);
        *(const uint8 **)data_pp += length;
    }
}

void put_uint8(void **data_pp, uint8 v)
{
    **(uint8 **) data_pp = v;
    *(uint8 **) data_pp += sizeof(uint8);
}

void put_uint16_le(void **data_pp, uint16 v)
{
    uint8 *d = *(uint8 **) data_pp;
    d[0] = v & 0xff;
    d[1] = v >> 8;
    *(uint8 **)data_pp += sizeof(uint16);
}

void put_uint16_be(void **data_pp, uint16 v)
{
    uint8 *d = *(uint8 **) data_pp;
    d[1] = v & 0xff;
    d[0] = v >> 8;
    *(uint8 **)data_pp += sizeof(uint16);
}

void put_uint32_le(void **data_pp, uint32 v)
{
    uint8 *d = *(uint8 **) data_pp;
    uint8 *v_p = (uint8 *)&v;
    d[0] = v_p[0];
    d[1] = v_p[1];
    d[2] = v_p[2];
    d[3] = v_p[3];
    *(uint8 **)data_pp += sizeof(uint32);
}

void put_uint32_be(void **data_pp, uint32 v)
{
    uint8 *d = *(uint8 **) data_pp;
    uint8 *v_p = (uint8 *)&v;
    d[3] = v_p[0];
    d[2] = v_p[1];
    d[1] = v_p[2];
    d[0] = v_p[3];
    *(uint8 **)data_pp += sizeof(uint32);
}

void put_uint64_le(void **data_pp, uint64 v)
{
    uint8 *d = *(uint8 **) data_pp;
    uint8 *v_p = (uint8 *)&v;
    d[0] = v_p[0];
    d[1] = v_p[1];
    d[2] = v_p[2];
    d[3] = v_p[3];
    d[4] = v_p[4];
    d[5] = v_p[5];
    d[6] = v_p[6];
    d[7] = v_p[7];
    *(uint8 **)data_pp += sizeof(uint32);
    *(uint8 **)data_pp += sizeof(uint32);
}

void put_uint64_be(void **data_pp, uint64 v)
{
    uint8 *d = *(uint8 **) data_pp;
    uint8 *v_p = (uint8 *)&v;
    d[7] = v_p[0];
    d[6] = v_p[1];
    d[5] = v_p[2];
    d[4] = v_p[3];
    d[3] = v_p[4];
    d[2] = v_p[5];
    d[1] = v_p[6];
    d[0] = v_p[7];
    *(uint8 **)data_pp += sizeof(uint32);
    *(uint8 **)data_pp += sizeof(uint32);
}

void skip_uint8(void **data_pp)
{
    *(const uint8 **)data_pp += sizeof(uint8);
}

void skip_uint16(void **data_pp)
{
    *(const uint8 **)data_pp += sizeof(uint16);
}

void skip_uint32(void **data_pp)
{
    *(const uint8 **)data_pp += sizeof(uint32);
}

void skip_block(void **data_pp, uint32 length)
{
    *(const uint8 **)data_pp += length;
}

char *skip_str(void **data_pp)
{
    char *Result_p = NULL;
    uint32 StrLength;

    ASSERT(NULL != data_pp);
    Result_p = *(char **)data_pp;
    ASSERT(NULL != Result_p);
    StrLength = get_uint32_le(data_pp);
    *(char **)data_pp += StrLength;
    return Result_p;
}

char *get_strdup(void *String_p)
{
    char *Result_p;
    uint32 StrLength = strlen((char *)String_p) + 1;

    Result_p = (char *)malloc(StrLength);
    ASSERT(Result_p != NULL);

    strncpy(Result_p, (char *)String_p, StrLength);
    Result_p[StrLength - 1] = 0;
    return Result_p;
}

void insert_string(char **data_pp, const char *source_p, uint32 length)
{
    *data_pp = NULL;

    *data_pp = (char *)malloc(sizeof(uint32) + length);
    ASSERT(NULL != *data_pp);

    memcpy(*data_pp, &length, sizeof(uint32));
    memcpy(*data_pp + sizeof(uint32), source_p, length);
}

#if defined(CFG_ENABLE_LOADER_SERIALIZATION)
uint32 get_directory_entries_len(const DirEntry_t *source_p, uint32 DirectoryEntriesCount)
{
    uint32 EntriesLen = 0;
    uint32 i = 0;

    for (i = 0; i < DirectoryEntriesCount; i++) {
        EntriesLen += get_uint32_string_le((void **)&source_p[i].Name_p);
        EntriesLen += sizeof(uint32) + sizeof(uint32) + sizeof(uint64);
    }

    return EntriesLen;
}

void serialize_directory_entries(void **data_pp, const DirEntry_t *source_p, uint32 DirectoryEntriesCount)
{
    uint32 NameLength = 0;
    uint32 i = 0;

    for (i = 0; i < DirectoryEntriesCount; i++) {
        NameLength = get_uint32_string_le((void **)&source_p[i].Name_p);
        memcpy(*data_pp, source_p[i].Name_p, NameLength);
        *(const uint8 **)data_pp += NameLength;
        memcpy(*data_pp, &source_p[i].Size, sizeof(uint64));
        *(const uint8 **)data_pp += sizeof(uint64);
        memcpy(*data_pp, &source_p[i].Mode, sizeof(uint32));
        *(const uint8 **)data_pp += sizeof(uint32);
        memcpy(*data_pp, &source_p[i].Time, sizeof(uint32));
        *(const uint8 **)data_pp += sizeof(uint32);
    }
}

uint32 get_device_entries_len(const ListDevice_t *source_p, uint32 DeviceEntriesCount)
{
    uint32 EntriesLen = 0;
    uint32 i = 0;

    for (i = 0; i < DeviceEntriesCount; i++) {
        if (NULL != source_p[i].Path_p) {
            EntriesLen += get_uint32_string_le((void **)&source_p[i].Path_p);
        } else {
            EntriesLen += sizeof(uint32);
        }

        if (NULL != source_p[i].Type_p) {
            EntriesLen += get_uint32_string_le((void **)&source_p[i].Type_p);
        } else {
            EntriesLen += sizeof(uint32);
        }

        EntriesLen += sizeof(uint64) + sizeof(uint64) + sizeof(uint64);
    }

    return EntriesLen;
}

void serialize_device_entries(void **data_pp, const ListDevice_t *source_p, uint32 DeviceEntriesCount)
{
    uint32 PathLen = 0;
    uint32 TypeLen = 0;
    uint32 i = 0;

    for (i = 0; i < DeviceEntriesCount; i++) {
        if (NULL != source_p[i].Path_p) {
            PathLen = get_uint32_string_le((void **)&source_p[i].Path_p);
            memcpy(*data_pp, source_p[i].Path_p, PathLen);
            *(const uint8 **)data_pp += PathLen;
        } else {
            memset(*data_pp, 0x00, sizeof(char *));
            *(const uint8 **)data_pp += sizeof(char *);
        }

        if (NULL != source_p[i].Type_p) {
            TypeLen = get_uint32_string_le((void **)&source_p[i].Type_p);
            memcpy(*data_pp, source_p[i].Type_p, TypeLen);
            *(const uint8 **)data_pp += TypeLen;
        } else {
            memset(*data_pp, 0x00, sizeof(char *));
            *(const uint8 **)data_pp += sizeof(char *);
        }

        memcpy(*data_pp, &source_p[i].BlockSize, sizeof(uint64));
        *(const uint8 **)data_pp += sizeof(uint64);
        memcpy(*data_pp, &source_p[i].Start, sizeof(uint64));
        *(const uint8 **)data_pp += sizeof(uint64);
        memcpy(*data_pp, &source_p[i].Length, sizeof(uint64));
        *(const uint8 **)data_pp += sizeof(uint64);
    }
}
#endif

/* @} */
/* @} */