summaryrefslogtreecommitdiffstats
path: root/collection/elapi_util.c
blob: e275703e274edd0b6d12e375e3ac9e71b3dd0420 (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
/*
    ELAPI

    Different serialization methods of the collection.

    Copyright (C) Dmitri Pal <dpal@redhat.com> 2009

    This program 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 3 of the License, or
    (at your option) any later version.
    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.
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include "elapi_collection.h"
#include "elapi_debug.h"
#include "elapi_util.h"

/* Return a static string based on type of the element */
const char *get_type(int type)
{
    switch(type) {
        case ELAPI_TYPE_STRING:     return ELAPI_TYPE_NAME_STRING;
        case ELAPI_TYPE_INTEGER:    return ELAPI_TYPE_NAME_INTEGER;
        case ELAPI_TYPE_UNSIGNED:   return ELAPI_TYPE_NAME_UNSIGNED;
        case ELAPI_TYPE_LONG:       return ELAPI_TYPE_NAME_LONG;
        case ELAPI_TYPE_ULONG:      return ELAPI_TYPE_NAME_ULONG;
        case ELAPI_TYPE_BINARY:     return ELAPI_TYPE_NAME_BINARY;
        default:                    return ELAPI_TYPE_NAME_UNKNOWN;
    }

}

/* Calculate the potential size of the item */ 
int get_data_len(int type, int length)
{
    int len = 0;

    DEBUG_STRING("util_get_item_len","Entry point");

    switch(type) {
        case ELAPI_TYPE_INTEGER:
        case ELAPI_TYPE_UNSIGNED:
        case ELAPI_TYPE_LONG:
        case ELAPI_TYPE_ULONG:
                                len = 15;
                                break;
        case ELAPI_TYPE_STRING: 
        case ELAPI_TYPE_BINARY:
                                len = length * 2 + 2; 
                                break;
        case ELAPI_TYPE_DOUBLE:
                                len = 64;
                                break;
        default:
                                len = 0;
                                break;
    }

    DEBUG_STRING("util_get_item_len","Exit point");

    return len;
}

/* Copy data escaping characters */
static int copy_esc(char *dest,char *source,char esc) 
{
    int i=0;
    int j=0;

    *(dest +j) = esc;
    j++;

    while(*(source+i) != '\0') {
        if((*(source+i) == '\\') ||
           (*(source+i) == esc)) {

            *(dest +j) = '\\';
            j++;
            
        }
        *(dest +j) = *(source +i);
        i++;
        j++;
    }
    *(dest +j) = esc;
    j++;

    return j; 
}



/* Grow buffer to accomodate more space */
int grow_buffer(struct serial_data *buf_data, int len)
{
    void *tmp;

    DEBUG_STRING("grow_buffer","Entry point");
    DEBUG_NUMBER("Current length: ",buf_data->length);
    DEBUG_NUMBER("Increment length: ",len);
    DEBUG_NUMBER("Expected length: ",buf_data->length+len);
    DEBUG_NUMBER("Current size: ",buf_data->size);

    /* Grow buffer if needed */
    while(buf_data->length+len >= buf_data->size) {
        errno = 0;
        tmp = realloc(buf_data->buffer,buf_data->size+BLOCK_SIZE);
        if(tmp == NULL) {
            DEBUG_NUMBER("Error. Failed to allocate memory. Errno: ",errno);
            return errno;
        }
        buf_data->buffer = (char *)(tmp);
        buf_data->size += BLOCK_SIZE;
        DEBUG_NUMBER("New size: ",buf_data->size);

    }

    DEBUG_NUMBER("Final size: ",buf_data->size);
    DEBUG_STRING("grow_buffer","Success Exit.");
    return EOK;
}

/* Specail function to add different formatting symbols to the output */
int put_marker(struct serial_data *buf_data, void *data, int len) 
{
    int error = EOK;

    DEBUG_STRING("put_marker","Entry point");
    DEBUG_NUMBER("Marker length: ",len);

    error = grow_buffer(buf_data, len);
    if(error) {
        DEBUG_NUMBER("grow_buffer failed with: ",error);
        return error; 
    }
    memcpy(buf_data->buffer+buf_data->length,data,len);
    buf_data->length+=len;
    *(buf_data->buffer+buf_data->length) = '\0';

    DEBUG_STRING("put_marker","Success exit");
    return error;
}

/* Add item's data */
int serialize(char *property_in,
              int property_len_in,
              int type,
              void *data_in,
              int length_in,
              void *custom_data,
              int *dummy)
{
    int len;
    struct serial_data *buf_data; 
    char *property;
    void *data;
    int  property_len;
    int length;
    int error = EOK;
    int i;

    DEBUG_STRING("serialize","Entry point");
    
    *dummy = 0;

    /* Check is there is buffer. If not allocate */
    buf_data = (struct serial_data *)(custom_data);
    if(buf_data == (struct serial_data *)(NULL)) {
        DEBUG_STRING("Error.","Storage data is not passed in!");
        return EINVAL;
    }
    if(buf_data->buffer == NULL) {
        DEBUG_STRING("First time use.","Allocating buffer.");
        errno = 0;
        buf_data->buffer = malloc(BLOCK_SIZE);
        if(buf_data->buffer == NULL) {
            DEBUG_NUMBER("Error. Failed to allocate memory. Errno: ",errno);
            return errno;
        }
        *(buf_data->buffer)='\0';
        buf_data->length=0;
        buf_data->size = BLOCK_SIZE;
    }

    DEBUG_NUMBER("Buffer len: ", buf_data->length);
    DEBUG_NUMBER("Buffer size: ", buf_data->size);
    DEBUG_STRING("Buffer: ", buf_data->buffer);

    /* Check the beginning of the collection */
    if(type == ELAPI_TYPE_COLLECTION) {
        DEBUG_STRING("Serializing collection: ", property_in);
        DEBUG_STRING("First header. ", "");
        if((error=put_marker(buf_data,"(",1))) return error;
        property = TEXT_COLLECTION;
        property_len = TEXT_COLLEN;
        data = property_in;
        length = property_len_in+1;
        type=ELAPI_TYPE_STRING;
        buf_data->nest_level++;
    }
    /* Check for subcollections */
    else if(type==ELAPI_TYPE_COLLECTIONREF) {
        /* Skip */
        return EOK;
    }
    /* Check for the end of the collection */
    else if(type==ELAPI_TYPE_END) {
        if((buf_data->length>0) && 
           (*(buf_data->buffer+buf_data->length-1) == ',')) {
            buf_data->length--;
            *(buf_data->buffer+buf_data->length) = '\0';
        }
        if(buf_data->nest_level>0) {
            buf_data->nest_level--;
            if((error=put_marker(buf_data,")",1))) return error;
        }
        return EOK;
    }
    else {
        property = property_in;
        property_len = property_len_in;
        data = data_in;
        length = length_in;
    } 

    DEBUG_STRING("Property: ", property);
    DEBUG_NUMBER("Property length: ", property_len);

    /* Start with property and "=" */
    if((error=put_marker(buf_data,property,property_len)) ||
       (error=put_marker(buf_data,"=",1))) return error;

    /* Get projected length of the item */
    len = get_data_len(type,length);
    DEBUG_NUMBER("Expected data length: ",len);
    DEBUG_STRING("Buffer so far: ", buf_data->buffer);

    /* Make sure we have enough space */ 
    if((error=grow_buffer(buf_data,len))) return error;

    /* Add the value */
    switch(type) {
        case ELAPI_TYPE_STRING:
                                /* No escaping for now */
                                len = copy_esc(buf_data->buffer+buf_data->length,(char *)(data),'"');
                                break;
        case ELAPI_TYPE_BINARY:
                                *(buf_data->buffer+buf_data->length) = '\'';
                                for(i=0;i<length;i++) sprintf(buf_data->buffer+buf_data->length+i*2 + 1,"%02X",*((unsigned char *)(data+i)));
                                len = length * 2 + 1;
                                *(buf_data->buffer+buf_data->length + len) = '\'';
                                len++;
                                break;
        case ELAPI_TYPE_INTEGER:
                                len = sprintf(buf_data->buffer+buf_data->length,"%d",*((int *)(data)));
                                break;
        case ELAPI_TYPE_UNSIGNED:
                                len = sprintf(buf_data->buffer+buf_data->length,"%u",*((unsigned int *)(data)));
                                break;
        case ELAPI_TYPE_LONG:
                                len = sprintf(buf_data->buffer+buf_data->length,"%ld",*((long *)(data)));
                                break;
        case ELAPI_TYPE_ULONG:
                                len = sprintf(buf_data->buffer+buf_data->length,"%lu",*((unsigned long *)(data)));
                                break;
        case ELAPI_TYPE_DOUBLE:
                                len = sprintf(buf_data->buffer+buf_data->length,"%.4f",*((double *)(data)));
                                break;
        default:
                                *(buf_data->buffer+buf_data->length) = '\0';
                                len = 0;
                                break;
    }

    /* Adjust length */
    buf_data->length+=len;
    *(buf_data->buffer+buf_data->length) = '\0';

    /* Always put a comma at the end */
    if((error=put_marker(buf_data,",",1))) return error;


    DEBUG_STRING("Data: ",buf_data->buffer);
    DEBUG_STRING("serialize","Exit point");
    return EOK;

}


/* Add item's data */
int xml_add(char *property,
            int property_len,
            int type,
            void *data,
            int length,
            void *custom_data,
            int *dummy)
{
    int rc;
    struct xml_data *buf_data; 
    int error = EOK;
    char *data_xml;
    char *name;

    DEBUG_STRING("xml_add","Entry point");

    *dummy = 0;

    /* Check is there is buffer. If not allocate */
    buf_data = (struct xml_data *)(custom_data);
    if(buf_data == (struct xml_data *)(NULL)) {
        DEBUG_STRING("Error.","Storage data is not passed in!");
        return EINVAL;
    }

    /* Check if there is buffer allocated */
    if(buf_data->buf == (xmlBufferPtr)(NULL)) {
        DEBUG_STRING("xml_add", "First use - allocating memory");
        buf_data->buf = xmlBufferCreate();
        if (buf_data->buf == NULL) {
            DEBUG_STRING("xml_add", "Error creating the xml buffer");
            return ENOMEM;
        }
        DEBUG_NUMBER("Buffer allocated", buf_data->buf);
        DEBUG_NUMBER("Buffer output", (buf_data->buf)->content);

        /* Create a new XmlWriter for memory, with no compression.
         * Remark: there is no compression for this kind of xmlTextWriter */
        buf_data->writer = xmlNewTextWriterMemory(buf_data->buf, 0);
        if (buf_data->writer == NULL) {
            error = errno;
            DEBUG_STRING("xml_add", "Error creating the xml writer");
            xmlBufferFree(buf_data->buf);
            buf_data->buf = (xmlBufferPtr)(NULL);
            return ENOMEM;
        }

        /* Start the document with the xml default for the version,
         * encoding and the default for the standalone declaration. */
        rc = xmlTextWriterStartDocument(buf_data->writer, NULL, NULL, NULL);
        if (rc < 0) {
            DEBUG_STRING("xml_add", "Error at xmlTextWriterStartDocument");
            xmlFreeTextWriter(buf_data->writer);
            xmlBufferFree(buf_data->buf);
            buf_data->writer = (xmlTextWriterPtr)(NULL);
            buf_data->buf = (xmlBufferPtr)(NULL);
            return EIO;
        }
        buf_data->given_name = NULL;
        buf_data->level = 0;
    }

    DEBUG_STRING("current buffer:", (buf_data->buf)->content);
    DEBUG_NUMBER("Writer", buf_data->writer);


    /* Check the beginning of the collection */
    if(type == ELAPI_TYPE_COLLECTION) {
        DEBUG_STRING("XML collection start: ", property);
        rc = xmlTextWriterStartElement(buf_data->writer, BAD_CAST ELEMENT_COLLECTION);
        if (rc < 0) {
            DEBUG_STRING("xml_add", "Error at xmlTextWriterStartElement");
            xmlFreeTextWriter(buf_data->writer);
            xmlBufferFree(buf_data->buf);
            buf_data->writer = (xmlTextWriterPtr)(NULL);
            buf_data->buf = (xmlBufferPtr)(NULL);
            return EIO;
        }

        if(buf_data->given_name != NULL) name = buf_data->given_name;
        else name = property;
        rc = xmlTextWriterWriteAttribute(buf_data->writer, BAD_CAST ATTRIBUTE_NAME, BAD_CAST property);
        if (rc < 0) {
            DEBUG_STRING("xml_add", "Error at xmlTextWriterWriteAttribute");
            xmlFreeTextWriter(buf_data->writer);
            xmlBufferFree(buf_data->buf);
            buf_data->writer = (xmlTextWriterPtr)(NULL);
            buf_data->buf = (xmlBufferPtr)(NULL);
            return EIO;
        }

        /* Make sure we track the level */
        buf_data->level++;
    }
    /* Check for subcollections */
    else if(type==ELAPI_TYPE_COLLECTIONREF) {
        buf_data->given_name = property;
    }
    /* Check for the end of the collection */
    else if(type==ELAPI_TYPE_END) {
        buf_data->given_name = NULL;
        buf_data->level--;
        /* Check if this is the end of the whole collection */
        if(buf_data->level == 0) {
            rc = xmlTextWriterEndDocument(buf_data->writer);
            if (rc < 0) {
                DEBUG_STRING("xml_add", "Error at xmlTextWriterEndDocument");
                xmlFreeTextWriter(buf_data->writer);
                xmlBufferFree(buf_data->buf);
                buf_data->writer = (xmlTextWriterPtr)(NULL);
                buf_data->buf = (xmlBufferPtr)(NULL);
                return EIO;
            }
        }
        else {
            rc = xmlTextWriterFullEndElement(buf_data->writer);
            if (rc < 0) {
                DEBUG_STRING("xml_add", "Error at xmlTextWriterEndElement");
                xmlFreeTextWriter(buf_data->writer);
                xmlBufferFree(buf_data->buf);
                buf_data->writer = (xmlTextWriterPtr)(NULL);
                buf_data->buf = (xmlBufferPtr)(NULL);
                return EIO;
            }
        }
    }
    else {
        DEBUG_STRING("Property: ", property);
        DEBUG_NUMBER("Property length: ", property_len);

        rc = xmlTextWriterStartElement(buf_data->writer, BAD_CAST ELEMENT_MEMBER);
        if (rc < 0) {
            DEBUG_STRING("xml_add", "Error at xmlTextWriterStartElement");
            xmlFreeTextWriter(buf_data->writer);
            xmlBufferFree(buf_data->buf);
            buf_data->writer = (xmlTextWriterPtr)(NULL);
            buf_data->buf = (xmlBufferPtr)(NULL);
            return EIO;
        }

        rc = xmlTextWriterWriteAttribute(buf_data->writer, BAD_CAST ATTRIBUTE_NAME, BAD_CAST property);
        if (rc < 0) {
            DEBUG_STRING("xml_add", "Error at xmlTextWriterWriteAttribute");
            xmlFreeTextWriter(buf_data->writer);
            xmlBufferFree(buf_data->buf);
            buf_data->writer = (xmlTextWriterPtr)(NULL);
            buf_data->buf = (xmlBufferPtr)(NULL);
            return EIO;
        }

        rc = xmlTextWriterWriteAttribute(buf_data->writer, BAD_CAST ATTRIBUTE_TYPE, BAD_CAST get_type(type));
        if (rc < 0) {
            DEBUG_STRING("xml_add", "Error at xmlTextWriterWriteAttribute");
            xmlFreeTextWriter(buf_data->writer);
            xmlBufferFree(buf_data->buf);
            buf_data->writer = (xmlTextWriterPtr)(NULL);
            buf_data->buf = (xmlBufferPtr)(NULL);
            return EIO;
        }

        /* Add the value */

        switch(type) {
            case ELAPI_TYPE_STRING:
                                data_xml = (char *)(data);
                                if(!xmlCheckUTF8((const unsigned char *)(data_xml))) data_xml = BAD_DATA;
                                rc = xmlTextWriterWriteString(buf_data->writer, BAD_CAST data_xml);
                                break;
            case ELAPI_TYPE_BINARY:
                                rc = xmlTextWriterWriteBase64(buf_data->writer, (const char *)(data),0,length);
                                break;
            case ELAPI_TYPE_INTEGER:
                                rc = xmlTextWriterWriteFormatString(buf_data->writer, "%d", *((int *)(data)));
                                break;
            case ELAPI_TYPE_UNSIGNED:
                                rc = xmlTextWriterWriteFormatString(buf_data->writer, "%u", *((unsigned int *)(data)));
                                break;
            case ELAPI_TYPE_LONG:
                                rc = xmlTextWriterWriteFormatString(buf_data->writer, "%ld", *((long *)(data)));
                                break;
            case ELAPI_TYPE_ULONG:
                                rc = xmlTextWriterWriteFormatString(buf_data->writer, "%lu", *((unsigned long *)(data)));
                                break;
            case ELAPI_TYPE_DOUBLE:
                                rc = xmlTextWriterWriteFormatString(buf_data->writer, "%.4f", *((double *)(data)));
                                break;
            default:
                                rc = xmlTextWriterWriteString(buf_data->writer, BAD_CAST "");
                                break;
        }

        if (rc < 0) {
            DEBUG_STRING("xml_add", "Error trying put data into XML");
            xmlFreeTextWriter(buf_data->writer);
            xmlBufferFree(buf_data->buf);
            buf_data->writer = (xmlTextWriterPtr)(NULL);
            buf_data->buf = (xmlBufferPtr)(NULL);
            return EIO;
        }

        rc = xmlTextWriterFullEndElement(buf_data->writer);
        if (rc < 0) {
            DEBUG_STRING("xml_add", "Error at xmlTextWriterFullEndElement");
            xmlFreeTextWriter(buf_data->writer);
            xmlBufferFree(buf_data->buf);
            buf_data->writer = (xmlTextWriterPtr)(NULL);
            buf_data->buf = (xmlBufferPtr)(NULL);
            return EIO;
        }

    } 

    DEBUG_STRING("xml_add","Exit point");
    return EOK;

}