-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathunirec.c
1772 lines (1681 loc) · 53.8 KB
/
unirec.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
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* \file unirec.c
* \brief Definition of UniRec structures and functions
* \author Vaclav Bartos <ibartosv@fit.vutbr.cz>
* \author Zdenek Rosa <rosazden@fit.cvut.cz>
* \date 2015
*/
/*
* Copyright (C) 2015 CESNET
*
* LICENSE TERMS
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of the Company nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* ALTERNATIVELY, provided that this notice is retained in full, this
* product may be distributed under the terms of the GNU General Public
* License (GPL) version 2 or later, in which case the provisions
* of the GPL apply INSTEAD OF those given above.
*
* This software is provided ``as is'', and any express or implied
* warranties, including, but not limited to, the implied warranties of
* merchantability and fitness for a particular purpose are disclaimed.
* In no event shall the company or contributors be liable for any
* direct, indirect, incidental, special, exemplary, or consequential
* damages (including, but not limited to, procurement of substitute
* goods or services; loss of use, data, or profits; or business
* interruption) however caused and on any theory of liability, whether
* in contract, strict liability, or tort (including negligence or
* otherwise) arising in any way out of the use of this software, even
* if advised of the possibility of such damage.
*
*/
#define _DEFAULT_SOURCE
#define _BSD_SOURCE
#define _XOPEN_SOURCE
#define __USE_XOPEN
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <time.h>
#include <string.h>
#include <regex.h>
#include <assert.h>
#include <ctype.h>
#include <unirec/unirec.h>
#include <unirec/inline.h>
#include <libtrap/trap.h>
#include <inttypes.h>
#include "include/unirec/ur_values.c"
#ifndef MAX
#define MAX(A, B) ((A >= B) ? (A) : (B))
#endif
// All inline functions from ipaddr.h must be declared again with "extern"
// in exactly one translation unit (it generates externally linkable code of
// these function)
// See this for explanation (Nemo's answer):
// http://stackoverflow.com/questions/6312597/is-inline-without-static-or-extern-ever-useful-in-c99
INLINE_IMPL int ip_is4(const ip_addr_t *addr);
INLINE_IMPL int ip_is6(const ip_addr_t *addr);
INLINE_IMPL uint32_t ip_get_v4_as_int(const ip_addr_t *addr);
INLINE_IMPL char *ip_get_v4_as_bytes(const ip_addr_t *addr);
INLINE_IMPL ip_addr_t ip_from_int(uint32_t i);
INLINE_IMPL ip_addr_t ip_from_4_bytes_be(const char b[4]);
INLINE_IMPL ip_addr_t ip_from_4_bytes_le(const char b[4]);
INLINE_IMPL ip_addr_t ip_from_16_bytes_be(const char b[16]);
INLINE_IMPL ip_addr_t ip_from_16_bytes_le(const char b[16]);
INLINE_IMPL int ip_cmp(const ip_addr_t *addr1, const ip_addr_t *addr2);
INLINE_IMPL int ip_from_str(const char *str, ip_addr_t *addr);
INLINE_IMPL void ip_to_str(const ip_addr_t *addr, char *str);
INLINE_IMPL mac_addr_t mac_from_bytes(const uint8_t *array);
INLINE_IMPL int mac_from_str(const char *str, mac_addr_t *addr);
INLINE_IMPL int mac_cmp(const mac_addr_t *addr1, const mac_addr_t *addr2);
INLINE_IMPL void mac_to_str(const mac_addr_t *addr, char *str);
INLINE_IMPL void mac_to_bytes(const mac_addr_t *addr, uint8_t *array);
/**
* \brief Sizes of UniRec data types.
*
* Data types are defined in the #ur_field_type_str array.
*/
const int ur_field_type_size[] = {
-1, /*UR_TYPE_STRING*/
-1, /*UR_TYPE_BYTES*/
1, /*UR_TYPE_CHAR*/
1, /*UR_TYPE_UINT8*/
1, /*UR_TYPE_INT8*/
2, /*UR_TYPE_UINT16*/
2, /*UR_TYPE_INT16*/
4, /*UR_TYPE_UINT32*/
4, /*UR_TYPE_INT32*/
8, /*UR_TYPE_UINT64*/
8, /*UR_TYPE_INT64*/
4, /*UR_TYPE_FLOAT*/
8, /*UR_TYPE_DOUBLE*/
16, /*UR_TYPE_IP*/
6, /*UR_TYPE_MAC*/
8, /*UR_TYPE_TIME*/
// arrays
-1, /*UR_TYPE_A_UINT8*/
-1, /*UR_TYPE_A_INT8*/
-2, /*UR_TYPE_A_UINT16*/
-2, /*UR_TYPE_A_INT16*/
-4, /*UR_TYPE_A_UINT32*/
-4, /*UR_TYPE_A_INT32*/
-8, /*UR_TYPE_A_UINT64*/
-8, /*UR_TYPE_A_INT64*/
-4, /*UR_TYPE_A_FLOAT*/
-8, /*UR_TYPE_A_DOUBLE*/
-16, /*UR_TYPE_A_IP*/
-6, /*UR_TYPE_A_MAC*/
-8, /*UR_TYPE_A_TIME*/
};
/**
* \brief UniRec data types.
*
* Sizes of data types are defined in the #ur_field_type_size array.
*/
const char *ur_field_type_str[] = {
"string", /*UR_TYPE_STRING*/
"bytes", /*UR_TYPE_BYTES*/
"char", /*UR_TYPE_CHAR*/
"uint8", /*UR_TYPE_UINT8*/
"int8", /*UR_TYPE_INT8*/
"uint16", /*UR_TYPE_UINT16*/
"int16", /*UR_TYPE_INT16*/
"uint32", /*UR_TYPE_UINT32*/
"int32", /*UR_TYPE_INT32*/
"uint64", /*UR_TYPE_UINT64*/
"int64", /*UR_TYPE_INT64*/
"float", /*UR_TYPE_FLOAT*/
"double", /*UR_TYPE_DOUBLE*/
"ipaddr", /*UR_TYPE_IP*/
"macaddr", /*UR_TYPE_MAC*/
"time", /*UR_TYPE_TIME*/
"uint8*", /*UR_TYPE_A_UINT8*/
"int8*", /*UR_TYPE_A_INT8*/
"uint16*", /*UR_TYPE_A_UINT16*/
"int16*", /*UR_TYPE_A_INT16*/
"uint32*", /*UR_TYPE_A_UINT32*/
"int32*", /*UR_TYPE_A_INT32*/
"uint64*", /*UR_TYPE_A_UINT64*/
"int64*", /*UR_TYPE_A_INT64*/
"float*", /*UR_TYPE_A_FLOAT*/
"double*", /*UR_TYPE_A_DOUBLE*/
"ipaddr*", /*UR_TYPE_A_IP*/
"macaddr*", /*UR_TYPE_A_MAC*/
"time*", /*UR_TYPE_A_TIME*/
};
/**
* \brief UniRec array element data types.
*/
int ur_field_array_elem_type[] = {
UR_TYPE_STRING, /* UR_TYPE_STRING */
UR_TYPE_BYTES, /* UR_TYPE_BYTES */
UR_TYPE_CHAR, /* UR_TYPE_CHAR */
UR_TYPE_UINT8, /* UR_TYPE_UINT8 */
UR_TYPE_INT8, /* UR_TYPE_INT8 */
UR_TYPE_UINT16, /* UR_TYPE_UINT16 */
UR_TYPE_INT16, /* UR_TYPE_INT16 */
UR_TYPE_UINT32, /* UR_TYPE_UINT32 */
UR_TYPE_INT32, /* UR_TYPE_INT32 */
UR_TYPE_UINT64, /* UR_TYPE_UINT64 */
UR_TYPE_INT64, /* UR_TYPE_INT64 */
UR_TYPE_FLOAT, /* UR_TYPE_FLOAT */
UR_TYPE_DOUBLE, /* UR_TYPE_DOUBLE */
UR_TYPE_IP, /* UR_TYPE_IP */
UR_TYPE_MAC, /* UR_TYPE_MAC */
UR_TYPE_TIME, /* UR_TYPE_TIME */
UR_TYPE_UINT8, /* UR_TYPE_A_UINT8 */
UR_TYPE_INT8, /* UR_TYPE_A_INT8 */
UR_TYPE_UINT16, /* UR_TYPE_A_UINT16 */
UR_TYPE_INT16, /* UR_TYPE_A_INT16 */
UR_TYPE_UINT32, /* UR_TYPE_A_UINT32 */
UR_TYPE_INT32, /* UR_TYPE_A_INT32 */
UR_TYPE_UINT64, /* UR_TYPE_A_UINT64 */
UR_TYPE_INT64, /* UR_TYPE_A_INT64 */
UR_TYPE_FLOAT, /* UR_TYPE_A_FLOAT */
UR_TYPE_DOUBLE, /* UR_TYPE_A_DOUBLE */
UR_TYPE_IP, /* UR_TYPE_A_IP */
UR_TYPE_MAC, /* UR_TYPE_A_MAC */
UR_TYPE_TIME, /* UR_TYPE_A_TIME */
};
ur_field_specs_t ur_field_specs;
ur_static_field_specs_t UR_FIELD_SPECS_STATIC;
const char UR_MEMORY_ERROR[] = "Memory allocation error";
int ur_init(ur_static_field_specs_t field_specs_static)
{
int i, j;
if (ur_field_specs.intialized == UR_INITIALIZED) {
return UR_OK;
}
//copy size
ur_field_specs.ur_last_statically_defined_id = field_specs_static.ur_last_id;
ur_field_specs.ur_last_id = field_specs_static.ur_last_id;
ur_field_specs.ur_allocated_fields = field_specs_static.ur_last_id + UR_INITIAL_SIZE_FIELDS_TABLE;
//copy field type
ur_field_specs.ur_field_types = (ur_field_type_t *) calloc(sizeof(ur_field_type_t), ur_field_specs.ur_allocated_fields);
if (ur_field_specs.ur_field_types == NULL) {
return UR_E_MEMORY;
}
memcpy(ur_field_specs.ur_field_types, field_specs_static.ur_field_types, sizeof(ur_field_type_t) * field_specs_static.ur_last_id);
//copy field sizes
ur_field_specs.ur_field_sizes = (short *) calloc(sizeof(short), ur_field_specs.ur_allocated_fields);
if (ur_field_specs.ur_field_sizes == NULL) {
free(ur_field_specs.ur_field_types);
return UR_E_MEMORY;
}
memcpy(ur_field_specs.ur_field_sizes, field_specs_static.ur_field_sizes, sizeof(short) * field_specs_static.ur_last_id);
//copy field names
ur_field_specs.ur_field_names = (char **) calloc(sizeof(char *), ur_field_specs.ur_allocated_fields);
if (ur_field_specs.ur_field_names == NULL) {
free(ur_field_specs.ur_field_types);
free(ur_field_specs.ur_field_sizes);
return UR_E_MEMORY;
}
for (i = 0; i < field_specs_static.ur_last_id; i++) {
ur_field_specs.ur_field_names[i] = (char *) calloc(sizeof(char), strlen(field_specs_static.ur_field_names[i]) + 1);
if (ur_field_specs.ur_field_names[i] == NULL) {
free(ur_field_specs.ur_field_types);
free(ur_field_specs.ur_field_sizes);
for (j = 0; j < i; j++) {
free(ur_field_specs.ur_field_names[j]);
}
free(ur_field_specs.ur_field_names);
return UR_E_MEMORY;
}
strcpy(ur_field_specs.ur_field_names[i], field_specs_static.ur_field_names[i]);
}
ur_field_specs.intialized = UR_INITIALIZED;
return UR_OK;
}
char *ur_template_string_delimiter(const ur_template_t *tmplt, int delimiter)
{
char *str = NULL, *strmove = NULL, *str_new = NULL;
int len = UR_DEFAULT_LENGTH_OF_TEMPLATE, act_len = 0;
if (tmplt == NULL) {
return NULL;
}
str = (char *) calloc(sizeof(char), len);
if (str == NULL) {
return NULL;
}
strmove = str;
for (int i = 0; i < tmplt->count; i++) {
act_len += strlen(ur_field_type_str[ur_get_type(tmplt->ids[i])]) + strlen(ur_get_name(tmplt->ids[i])) + 2;
if (act_len >= len) {
len *= 2;
str_new = (char *) realloc(str, sizeof(char) * len);
if (str_new == NULL) {
free(str);
return NULL;
}
strmove = str_new + (strmove - str);
str = str_new;
}
sprintf(strmove, "%s %s%c", ur_field_type_str[ur_get_type(tmplt->ids[i])], ur_get_name(tmplt->ids[i]), delimiter);
strmove += strlen(strmove);
}
if (tmplt->count != 0) {
strmove[-1] = '\0';
}
return str;
}
int ur_get_empty_id()
{
ur_field_id_linked_list_t * first;
//check if UniRec is initialized, if not initialize it
if (ur_field_specs.intialized != UR_INITIALIZED) {
int init_val = ur_init(UR_FIELD_SPECS_STATIC);
if (init_val != UR_OK) {
return init_val;
}
}
//check undefined fields
if (ur_field_specs.ur_undefine_fields != NULL) {
//resuse old undefined fields
int id;
first = ur_field_specs.ur_undefine_fields;
ur_field_specs.ur_undefine_fields = ur_field_specs.ur_undefine_fields->next;
id = first->id;
free(first);
return id;
} else {
//take new id
if (ur_field_specs.ur_last_id < ur_field_specs.ur_allocated_fields) {
//take value from remaining space
return ur_field_specs.ur_last_id++;
} else if (ur_field_specs.ur_last_id < UR_FIELD_ID_MAX) {
//increase space for fields
int new_size;
char **ur_field_names_new;
short *ur_field_sizes_new;
ur_field_type_t *ur_field_types_new;
new_size = ur_field_specs.ur_allocated_fields * 2 < UR_FIELD_ID_MAX ? ur_field_specs.ur_allocated_fields * 2 : UR_FIELD_ID_MAX;
//copy field type
ur_field_types_new = (ur_field_type_t *) realloc(ur_field_specs.ur_field_types, sizeof(ur_field_type_t) * new_size);
if (ur_field_types_new == NULL) {
return UR_E_MEMORY;
}
//copy field sizes
ur_field_sizes_new = (short *) realloc(ur_field_specs.ur_field_sizes, sizeof(short) * new_size);
if (ur_field_sizes_new == NULL) {
free(ur_field_types_new);
return UR_E_MEMORY;
}
//copy field names
ur_field_names_new = (char **) realloc(ur_field_specs.ur_field_names, sizeof(char *) * new_size);
if (ur_field_names_new == NULL) {
free(ur_field_types_new);
free(ur_field_sizes_new);
return UR_E_MEMORY;
}
//replace for new values
ur_field_specs.ur_field_names = ur_field_names_new;
ur_field_specs.ur_field_sizes = ur_field_sizes_new;
ur_field_specs.ur_field_types = ur_field_types_new;
ur_field_specs.ur_allocated_fields = new_size;
return ur_field_specs.ur_last_id++;
} else {
//no more space for new fields
return UR_E_MEMORY;
}
}
}
int ur_get_field_type_from_str(const char *type)
{
if (type == NULL) {
return UR_E_INVALID_TYPE;
}
for (int i = 0; i < UR_COUNT_OF_TYPES; i++) {
if (strcmp(type, ur_field_type_str[i]) == 0) {
return i;
}
}
return UR_E_INVALID_TYPE;
}
const char *ur_get_type_and_name_from_string(const char *source, char **name, char **type, int *length_name, int *length_type)
{
int length_type_2 = 0, length_name_2 = 0;
const char *source_cpy;
/* skip white spaces */
while (*source != 0 && isspace(*source)) {
source++;
}
/* start of type */
source_cpy = source;
while (*source != 0 && !isspace(*source)) {
length_type_2++;
source++;
}
/* end of type */
/* copy "type" string (realloc destination if needed) */
if (length_type_2 >= *length_type) {
if (*type != NULL) {
free(*type);
}
*type = (char *) malloc(sizeof(char) * (length_type_2 + 1));
if (*type == NULL) {
return NULL;
}
*length_type = length_type_2 + 1;
}
memcpy(*type, source_cpy, length_type_2);
(*type)[length_type_2] = 0;
/* skip white spaces */
while (*source != 0 && isspace(*source)) {
source++;
}
/* start of name */
source_cpy = source;
while (*source != 0 && !isspace(*source) && *source != ',') {
length_name_2++;
source++;
}
/* end of name */
/* copy "name" string (realloc destination if needed) */
if (length_name_2 >= *length_name) {
if (*name != NULL) {
free(*name);
}
*name = (char *) malloc(sizeof(char) * (length_name_2 + 1));
if (*name == NULL) {
return NULL;
}
*length_name = length_name_2 + 1;
}
memcpy(*name, source_cpy, length_name_2);
(*name)[length_name_2] = 0;
/* skip white spaces */
while (*source != 0 && isspace(*source)) {
source++;
}
/* skip comma */
if (*source == ',') {
source++;
}
return source;
}
char *ur_ifc_data_fmt_to_field_names(const char *ifc_data_fmt)
{
const char *source_cpy = NULL, *p = ifc_data_fmt;
char *out_str;
int name_len = 0, act_len = 0, str_len;
str_len = strlen(ifc_data_fmt);
out_str = (char *) calloc(str_len + 1, sizeof(char));
if (out_str == NULL) {
return NULL;
}
while (*p != 0) {
/* skip white spaces */
while (*p != 0 && isspace(*p)) {
p++;
}
/* field type */
while (*p != 0 && *p != ' ') {
p++;
}
/* skip white spaces */
while (*p != 0 && isspace(*p)) {
p++;
}
//copy name
source_cpy = p;
name_len = 0;
while (*p != 0 && *p != ',' && !isspace(*p)) {
name_len++;
p++;
}
assert(name_len + act_len + 1 <= str_len);
memcpy(out_str + act_len, source_cpy, name_len);
act_len += name_len;
/* skip white spaces */
while (*p != 0 && isspace(*p)) {
p++;
}
if (*p == ',') {
p++;
} else if (*p == 0) {
break;
} else {
free(out_str);
return NULL; /* name must be followed by a comma or end of string */
}
out_str[act_len] = ',';
act_len++;
}
return out_str;
}
ur_template_t *ur_expand_template(const char *ifc_data_fmt, ur_template_t *tmplt)
{
int name_len = 0, act_len = 0, concat_str_len = strlen(ifc_data_fmt);
char *concat_str;
const char *source_cpy, *p = ifc_data_fmt;
ur_tmplt_direction direction = UR_TMPLT_DIRECTION_NO;
uint32_t ifc_out = 0;
concat_str = (char *) malloc(sizeof(char) * concat_str_len);
if (concat_str == NULL) {
return NULL;
}
while (*p != 0) {
while (*p != 0 && !isspace(*p)) {
p++;
}
p++;
//copy name
source_cpy = p;
name_len = 0;
while (*p != 0 && *p != ',') {
name_len++;
p++;
}
if (name_len + act_len + 1 > concat_str_len) {
char *str_new;
size_t req_size = MAX(name_len + act_len + 1, (concat_str_len * 2));
str_new = (char *) realloc(concat_str, sizeof(char) * req_size);
if (str_new == NULL) {
/* XXX memory leak original concat_str? */
return NULL;
}
concat_str_len = req_size;
concat_str = str_new;
}
memcpy(concat_str + act_len, source_cpy, name_len);
act_len += name_len;
concat_str[act_len] = ',';
act_len++;
}
if (tmplt != NULL) {
direction = tmplt->direction;
ifc_out = tmplt->ifc_out;
for (int i = 0; i < tmplt->count; i++) {
const char *f_name = ur_get_name(tmplt->ids[i]);
name_len = strlen(f_name);
if (name_len + act_len + 1 > concat_str_len) {
char *str_new;
size_t req_size = MAX(name_len + act_len + 1, (concat_str_len * 2));
str_new = (char *) realloc(concat_str, sizeof(char) * req_size);
if (str_new == NULL) {
/* XXX memory leak original concat_str? */
return NULL;
}
concat_str_len = req_size;
concat_str = str_new;
}
memcpy(concat_str + act_len, f_name, name_len);
act_len += name_len;
*(concat_str + act_len) = ',';
act_len++;
}
ur_free_template(tmplt);
}
if (act_len > 0) {
act_len--;
concat_str[act_len] = 0;
}
tmplt = ur_create_template(concat_str, NULL);
tmplt->direction = direction;
tmplt->ifc_out = ifc_out;
free(concat_str);
return tmplt;
}
int ur_define_set_of_fields(const char *ifc_data_fmt)
{
const char *new_fields_move;
new_fields_move = ifc_data_fmt;
char *field_name, *field_type;
int field_name_length = UR_DEFAULT_LENGTH_OF_FIELD_NAME, field_type_length = UR_DEFAULT_LENGTH_OF_FIELD_TYPE;
int field_id = 0, field_type_id = 0;
field_name = (char *) malloc(sizeof(char) * field_name_length);
if (field_name == NULL) {
return UR_E_MEMORY;
}
field_type = (char *) malloc(sizeof(char) * field_type_length);
if (field_type == NULL) {
free(field_name);
return UR_E_MEMORY;
}
while (*new_fields_move != 0) {
new_fields_move = ur_get_type_and_name_from_string(new_fields_move, &field_name, &field_type, &field_name_length, &field_type_length);
if (new_fields_move == NULL) {
if (field_name != NULL) {
free(field_name);
}
if (field_type != NULL) {
free(field_type);
}
return UR_E_MEMORY;
}
//through all fields of receiver
field_type_id = ur_get_field_type_from_str(field_type);
if (field_type_id < 0) {
if (field_name != NULL) {
free(field_name);
}
free(field_type);
return field_type_id;
}
field_id = ur_define_field(field_name, field_type_id);
if (field_id < 0) {
if (field_name != NULL) {
free(field_name);
}
free(field_type);
return field_id;
}
}
if (field_name != NULL) {
free(field_name);
}
free(field_type);
return UR_OK;
}
ur_template_t *ur_define_fields_and_update_template(const char *ifc_data_fmt, ur_template_t *tmplt)
{
ur_template_t *new_tmplt;
if (ur_define_set_of_fields(ifc_data_fmt) < 0) {
return NULL;
}
new_tmplt = ur_create_template_from_ifc_spec(ifc_data_fmt);
if (new_tmplt != NULL && tmplt != NULL) {
new_tmplt->ifc_out = tmplt->ifc_out;
new_tmplt->direction = tmplt->direction;
ur_free_template(tmplt);
}
return new_tmplt;
}
ur_template_t *ur_create_template_from_ifc_spec(const char *ifc_data_fmt)
{
char *field_names = ur_ifc_data_fmt_to_field_names(ifc_data_fmt);
if (field_names == NULL) {
return NULL;
}
ur_template_t *new_tmplt = ur_create_template(field_names, NULL);
free(field_names);
return new_tmplt;
}
int ur_define_field(const char *name, ur_field_type_t type)
{
int insert_id;
char * name_copy;
int name_len;
if (name == NULL) {
return UR_E_INVALID_NAME;
}
//check the regural expression of a name
name_len = strlen(name);
if (name_len == 0) {
return UR_E_INVALID_NAME;
}
if (!((name[0] >= 'A' && name[0] <= 'Z') || (name[0] >= 'a' && name[0] <= 'z'))) {
return UR_E_INVALID_NAME;
}
for (int i = 1; i < name_len; i++) {
if (!((name[i] >= 'A' && name[i] <= 'Z') || (name[i] >= 'a' && name[i] <= 'z') || (name[i] >= '0' && name[i] <= '9') || name[i] == '_')) {
return UR_E_INVALID_NAME;
}
}
// If this is the first dynamically allocated field, call ur_init
if (ur_field_specs.ur_allocated_fields == ur_field_specs.ur_last_statically_defined_id) {
int init_val = ur_init(UR_FIELD_SPECS_STATIC);
if (init_val != 0) {
return init_val;
}
}
//check if the field is already defined
for (int i = 0; i < ur_field_specs.ur_last_id; i++) {
if (ur_field_specs.ur_field_names[i] != NULL && strcmp(name, ur_field_specs.ur_field_names[i]) == 0) {
if (type == ur_field_specs.ur_field_types[i]) {
//name exists and type is equal
return i;
} else {
//name exists, but type is different
return UR_E_TYPE_MISMATCH;
}
}
}
//create new field
name_copy = (char *) calloc(sizeof(char), strlen(name) + 1);
if (name_copy == NULL) {
//error during allocation
return UR_E_MEMORY;
}
strcpy(name_copy, name);
insert_id = ur_get_empty_id();
if (insert_id < 0) {
//error
free(name_copy);
return insert_id;
}
ur_field_specs.ur_field_names[insert_id] = name_copy;
ur_field_specs.ur_field_sizes[insert_id] = ur_size_of(type);
ur_field_specs.ur_field_types[insert_id] = type;
return insert_id;
}
int ur_undefine_field_by_id(ur_field_id_t field_id)
{
if (field_id < ur_field_specs.ur_last_statically_defined_id || field_id >= ur_field_specs.ur_last_id) {
//id is invalid
return UR_E_INVALID_PARAMETER;
} else if (ur_field_specs.ur_field_names[field_id] == NULL) {
//ID is already undefined
return UR_E_INVALID_PARAMETER;
} else {
//undefine field
ur_field_id_linked_list_t *undefined_item;
undefined_item = (ur_field_id_linked_list_t *) calloc(sizeof(ur_field_id_linked_list_t), 1);
if (undefined_item == NULL) {
//error during allocation
return UR_E_MEMORY;
}
free(ur_field_specs.ur_field_names[field_id]);
ur_field_specs.ur_field_names[field_id] = NULL;
undefined_item->id = field_id;
undefined_item->next = ur_field_specs.ur_undefine_fields;
ur_field_specs.ur_undefine_fields = undefined_item;
}
return UR_OK;
}
int ur_undefine_field(const char *name)
{
int i;
//find id of field
for (i = ur_field_specs.ur_last_statically_defined_id; i < ur_field_specs.ur_last_id; i++) {
if (ur_field_specs.ur_field_names[i] != NULL && strcmp(name, ur_field_specs.ur_field_names[i]) == 0) {
return ur_undefine_field_by_id(i);
}
}
//field with given name was not found
return UR_E_INVALID_NAME;
}
void ur_finalize()
{
if (ur_field_specs.intialized != UR_INITIALIZED) {
//there is no need for deallocation, because nothing has been allocated.
return;
}
if (ur_field_specs.ur_field_names != NULL) {
for (int i=0; i < ur_field_specs.ur_last_id; i++) {
if (ur_field_specs.ur_field_names[i] != NULL) {
free(ur_field_specs.ur_field_names[i]);
}
}
free(ur_field_specs.ur_field_names);
}
if (ur_field_specs.ur_undefine_fields != NULL) {
ur_field_id_linked_list_t *next, * act_del;
act_del = ur_field_specs.ur_undefine_fields;
while (act_del != NULL) {
next = act_del->next;
free(act_del);
act_del = next;
}
}
if (ur_field_specs.ur_field_sizes != NULL) {
free(ur_field_specs.ur_field_sizes);
}
if (ur_field_specs.ur_field_types != NULL) {
free(ur_field_specs.ur_field_types);
}
ur_field_specs.ur_field_names = UR_FIELD_SPECS_STATIC.ur_field_names;
ur_field_specs.ur_field_sizes = UR_FIELD_SPECS_STATIC.ur_field_sizes;
ur_field_specs.ur_field_types = UR_FIELD_SPECS_STATIC.ur_field_types;
ur_field_specs.ur_last_statically_defined_id = UR_FIELD_SPECS_STATIC.ur_last_id;
ur_field_specs.ur_last_id = UR_FIELD_SPECS_STATIC.ur_last_id;
ur_field_specs.ur_allocated_fields = UR_FIELD_SPECS_STATIC.ur_last_id;
ur_field_specs.ur_undefine_fields = NULL;
ur_field_specs.intialized = UR_UNINITIALIZED;
}
// Find field ID given its name
int ur_get_id_by_name(const char *name)
{
for (int id = 0; id < ur_field_specs.ur_last_id; id++) {
if (ur_field_specs.ur_field_names[id] != NULL && strcmp(name, ur_field_specs.ur_field_names[id]) == 0) {
return id;
}
}
return UR_E_INVALID_NAME;
}
// Return -1 if f1 should go before f2, 0 if f1 is the same as f2, 1 otherwise
int compare_fields(const void *field1, const void *field2)
{
const field_spec_t *f1 = field1;
const field_spec_t *f2 = field2;
if (f1->size > f2->size) {
return -1;
} else if (f1->size < f2->size) {
return 1;
} else {
return strcmp(f1->name, f2->name);
}
}
ur_template_t *ur_ctx_create_input_template(trap_ctx_t *ctx, int ifc, const char *fields, char **errstr)
{
ur_template_t *tmplt = ur_create_template(fields, errstr);
if (tmplt == NULL) {
return NULL;
}
if (ur_ctx_set_input_template(ctx, ifc, tmplt) != UR_OK) {
if (errstr != NULL) {
*errstr = (char *) malloc(strlen(UR_MEMORY_ERROR) + 1);
if (*errstr != NULL) {
strcpy(*errstr, UR_MEMORY_ERROR);
}
}
ur_free_template(tmplt);
return NULL;
}
return tmplt;
}
ur_template_t *ur_ctx_create_output_template(trap_ctx_t *ctx, int ifc, const char *fields, char **errstr)
{
ur_template_t *tmplt = ur_create_template(fields, errstr);
if (tmplt == NULL) {
return NULL;
}
if (ur_ctx_set_output_template(ctx, ifc, tmplt) != UR_OK) {
if (errstr != NULL) {
*errstr = (char *) malloc(strlen(UR_MEMORY_ERROR) + 1);
if (*errstr != NULL) {
strcpy(*errstr, UR_MEMORY_ERROR);
}
}
ur_free_template(tmplt);
return NULL;
}
return tmplt;
}
int ur_ctx_set_output_template(trap_ctx_t *ctx, int ifc, ur_template_t *tmplt)
{
if (tmplt == NULL) {
return UR_OK;
}
if (tmplt->direction == UR_TMPLT_DIRECTION_IN) {
tmplt->direction = UR_TMPLT_DIRECTION_BI;
} else {
tmplt->direction = UR_TMPLT_DIRECTION_OUT;
}
tmplt->ifc_out = ifc;
char * tmplt_str = ur_template_string(tmplt);
if (tmplt_str == NULL) {
return UR_E_MEMORY;
}
trap_ctx_set_data_fmt(ctx, ifc, TRAP_FMT_UNIREC, tmplt_str);
free(tmplt_str);
return UR_OK;
}
int ur_ctx_set_input_template(trap_ctx_t *ctx, int ifc, ur_template_t *tmplt)
{
if (tmplt == NULL) {
return UR_OK;
}
if (tmplt->direction == UR_TMPLT_DIRECTION_OUT) {
tmplt->direction = UR_TMPLT_DIRECTION_BI;
} else {
tmplt->direction = UR_TMPLT_DIRECTION_IN;
}
char * tmplt_str = ur_template_string(tmplt);
if (tmplt_str == NULL) {
return UR_E_MEMORY;
}
trap_ctx_set_required_fmt(ctx, ifc, TRAP_FMT_UNIREC, tmplt_str);
free(tmplt_str);
return UR_OK;
}
ur_template_t *ur_ctx_create_bidirectional_template(trap_ctx_t *ctx, int ifc_in, int ifc_out, const char *fields, char **errstr)
{
ur_template_t *tmplt = ur_create_template(fields, errstr);
if (tmplt == NULL) {
return NULL;
}
tmplt->direction = UR_TMPLT_DIRECTION_BI;
tmplt->ifc_out = ifc_out;
char * tmplt_str = ur_template_string(tmplt);
if (tmplt_str == NULL) {
if (errstr != NULL) {
*errstr = (char *) malloc(strlen(UR_MEMORY_ERROR) + 1);
if (*errstr != NULL) {
strcpy(*errstr, UR_MEMORY_ERROR);
}
}
ur_free_template(tmplt);
return NULL;
}
trap_ctx_set_required_fmt(ctx, ifc_in, TRAP_FMT_UNIREC, tmplt_str);
trap_ctx_set_data_fmt(ctx, ifc_out, TRAP_FMT_UNIREC, tmplt_str);
free(tmplt_str);
return tmplt;
}
ur_template_t *ur_create_template(const char *fields, char **errstr)
{
// Count number of fields
int n_fields = 0, written_fields = 0;
if (fields) {
/* skip leading spaces */
while (*fields != '\0' && isspace(*fields)) {
fields++;
}
/* Count number of fields */
if (*fields != '\0') {
n_fields = 1;
const char *tmp = fields;
while (*tmp != '\0') {
if (*(tmp++) == ',') {
n_fields++;
}
}
}
}
// Allocate array of field_spec structs
field_spec_t *fields_spec = malloc(n_fields * sizeof(field_spec_t));
if (fields_spec == NULL && n_fields > 0) {
if (errstr != NULL) {
*errstr = (char *) malloc(strlen(UR_MEMORY_ERROR) + 1);
if (*errstr != NULL) {
strcpy(*errstr, UR_MEMORY_ERROR);
}
}
return NULL;
}
// Parse fields and fill the array
const char *start_ptr = fields;
const char *end_ptr;
for (int i = 0; i < n_fields; i++) {
// Get field name
end_ptr = start_ptr;
/* go to the first space / comma / end-of-string */
while (!isspace(*end_ptr) && *end_ptr != ',' && *end_ptr != '\0') {
end_ptr++;
}
int len = end_ptr - start_ptr;
fields_spec[written_fields].name = malloc(len + 1);
if (fields_spec[written_fields].name == NULL) {
if (errstr != NULL) {
*errstr = (char *) malloc(strlen(UR_MEMORY_ERROR) + 1);
if (*errstr != NULL) {
strcpy(*errstr, UR_MEMORY_ERROR);
}
}
for (int j = 0; j < i; j++) {
free(fields_spec[j].name);
}
free(fields_spec);
return NULL;
}
memcpy(fields_spec[written_fields].name, start_ptr, len);
fields_spec[written_fields].name[len] = 0;
start_ptr = end_ptr;
while ((isspace(*start_ptr) || *start_ptr == ',') && *start_ptr != '\0') {
start_ptr++;
}
// Get field ID
int id_by_name = ur_get_id_by_name(fields_spec[written_fields].name);
if (id_by_name == UR_E_INVALID_NAME) {
// Unknown field name
if (errstr != NULL) {
*errstr = (char *) malloc(100);
if (*errstr != NULL) {
int n;
n = snprintf(*errstr, 100, "field: %s is not defined.", fields_spec[written_fields].name);
if (n >= 100) {
strcpy(*errstr, "given field is not defined");
}
}
}
for (int j = 0; j <= written_fields; j++) {
free(fields_spec[j].name);
}
free(fields_spec);
return NULL;
}
//check if the field is not in the template.
int in_the_template = 0;
for (int j = 0; j < written_fields; j++) {
if (fields_spec[j].id == id_by_name) {
in_the_template = 1;
break;
}
}
//if the field is not already int the template, copy values and move the index, otherwise just free the string with name.
if (in_the_template == 0) {
fields_spec[written_fields].id = id_by_name;
// Get field size
fields_spec[written_fields].size = ur_get_size(fields_spec[written_fields].id);