cis_config
serialize.h
1 #include <../tools.h>
2 #include <SerializeBase.h>
3 #include <FormatSerialize.h>
4 #include <AsciiTableSerialize.h>
5 
6 #ifndef CISSERIALIZE_H_
7 #define CISSERIALIZE_H_
8 
9 
14 static inline
15 seri_t empty_serializer() {
16  seri_t s;
17  s.type = DIRECT_SERI; // Can't be -1 (was that used?)
18  s.info = NULL;
19  s.size_info = 0;
20  return s;
21 };
22 
32 static inline
33 int update_serializer(seri_t *s, int type, const void *info) {
34  // Malloc if not initialized
35  if (s == NULL) {
36  cislog_error("update_serializer: Pointer to serializer is NULL.");
37  return -1;
38  }
39  // Copy information
40  if ((type == ASCII_TABLE_SERI) || (type == ASCII_TABLE_ARRAY_SERI)) {
41  asciiTable_t *handle = (asciiTable_t*)malloc(sizeof(asciiTable_t));
42  { // Limit content of format_str so double free not triggered
43  char *format_str;
44  if (info == NULL) {
45  format_str = (char*)(s->info);
46  } else {
47  format_str = (char*)info;
48  }
49  if (handle == NULL) {
50  cislog_error("update_serializer: Failed to allocate for asciiTable.");
51  return -1;
52  }
53  handle[0] = asciiTable("seri", "0", format_str, NULL, NULL, NULL);
54  }
55  if (s->info != NULL) {
56  free(s->info);
57  }
58  s->size_info = sizeof(asciiTable_t);
59  s->info = (void*)handle;
60  } else if (info == NULL) {
61  if (type < 0) {
62  type = DIRECT_SERI;
63  }
64  } else {
65  char *format_str = (char*)info;
66  s->size_info = 2*strlen(format_str) + 1;
67  void *t_sinfo = (void*)realloc(s->info, s->size_info);
68  if (t_sinfo == NULL) {
69  cislog_error("update_serializer: Failed to reallocate for format string.");
70  s->size_info = 0;
71  free(s->info);
72  return -1;
73  }
74  s->info = t_sinfo;
75  strcpy((char*)(s->info), format_str);
76  // size_t len_fmt = strlen(format_str);
77  // memcpy(s->info, format_str, len_fmt + 1);
78  // ((char*)(s->info))[len_fmt] = '\0';
79  if (type < 0) {
80  type = FORMAT_SERI;
81  }
82  }
83  s->type = (seri_type)type;
84  return 0;
85 };
86 
93 static inline
94 seri_t * init_serializer(int type, const void *info) {
95  seri_t *s = (seri_t*)malloc(sizeof(seri_t));
96  if (s == NULL) {
97  cislog_error("init_serializer: Failed to allocate serializer.");
98  return NULL;
99  }
100  s[0] = empty_serializer();
101  int flag = update_serializer(s, type, info);
102  if (flag != 0) {
103  cislog_error("init_serializer: Failed to create serializer.");
104  free(s);
105  s = NULL;
106  }
107  return s;
108 };
109 
110 
115 static inline
116 int free_serializer(seri_t *s) {
117  if (s->info != NULL) {
118  free(s->info);
119  s->info = NULL;
120  }
121  return 0;
122 };
123 
124 
137 static inline
138 int serialize(const seri_t s, char **buf, const size_t buf_siz,
139  const int allow_realloc, int *args_used, va_list ap) {
140  seri_type t = s.type;
141  int ret = -1;
142  va_list ap2;
143  if (allow_realloc) {
144  va_copy(ap2, ap);
145  }
146  if (t == DIRECT_SERI)
147  ret = serialize_direct(s, *buf, buf_siz, args_used, ap);
148  else if (t == FORMAT_SERI)
149  ret = serialize_format(s, *buf, buf_siz, args_used, ap);
150  else if (t == ASCII_TABLE_SERI)
151  ret = serialize_ascii_table(s, *buf, buf_siz, args_used, ap);
152  else if (t == ASCII_TABLE_ARRAY_SERI)
153  ret = serialize_ascii_table_array(s, *buf, buf_siz, args_used, ap);
154  else {
155  cislog_error("serialize: Unsupported seri_type %d", t);
156  }
157  if (ret > (int)buf_siz) {
158  if (allow_realloc) {
159  *buf = (char*)realloc(*buf, ret+1);
160  if (*buf == NULL) {
161  cislog_error("serialize: Failed to realloc buffer.");
162  ret = -1;
163  } else {
164  ret = serialize(s, buf, ret+1, 0, args_used, ap2);
165  }
166  } else {
167  cislog_error("serialize: encoded message too large for the buffer. (buf_siz=%d, len=%d)",
168  buf_siz, ret);
169  ret = -1;
170  }
171  }
172  if (allow_realloc) {
173  va_end(ap2);
174  }
175  return ret;
176 }
177 
186 static inline
187 int deserialize(const seri_t s, const char *buf, const size_t buf_siz, va_list ap) {
188  seri_type t = s.type;
189  int ret = -1;
190  if (t == DIRECT_SERI)
191  ret = deserialize_direct(s, buf, buf_siz, ap);
192  else if (t == FORMAT_SERI)
193  ret = deserialize_format(s, buf, buf_siz, ap);
194  else if (t == ASCII_TABLE_SERI)
195  ret = deserialize_ascii_table(s, buf, buf_siz, ap);
196  else if (t == ASCII_TABLE_ARRAY_SERI)
197  ret = deserialize_ascii_table_array(s, buf, buf_siz, ap);
198  else {
199  cislog_error("deserialize: Unsupported seri_type %d", t);
200  }
201  return ret;
202 };
203 
204 
205 #endif /*CISSERIALIZE_H_*/
Serializer structure.
Definition: SerializeBase.h:15
Structure containing information about an ASCII table.
Definition: AsciiTable.h:76