cis_config
serialize.h
1 #ifndef CISSERIALIZE_H_
2 #define CISSERIALIZE_H_
3 
4 #include "../tools.h"
5 #include "SerializeBase.h"
6 #include "FormatSerialize.h"
7 #include "AsciiTableSerialize.h"
8 #include "PlySerialize.h"
9 #include "ObjSerialize.h"
10 
11 #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
12 extern "C" {
13 #endif
14 
19 static inline
20 seri_t empty_serializer() {
21  seri_t s;
22  s.type = DIRECT_SERI; // Can't be -1 (was that used?)
23  s.info = NULL;
24  s.size_info = 0;
25  return s;
26 };
27 
37 static inline
38 int update_serializer(seri_t *s, int type, const void *info) {
39  // Malloc if not initialized
40  if (s == NULL) {
41  cislog_error("update_serializer: Pointer to serializer is NULL.");
42  return -1;
43  }
44  // Copy information
45  if ((type == ASCII_TABLE_SERI) || (type == ASCII_TABLE_ARRAY_SERI)) {
46  asciiTable_t *handle = (asciiTable_t*)malloc(sizeof(asciiTable_t));
47  { // Limit content of format_str so double free not triggered
48  char *format_str;
49  if (info == NULL) {
50  format_str = (char*)(s->info);
51  } else {
52  format_str = (char*)info;
53  }
54  if (handle == NULL) {
55  cislog_error("update_serializer: Failed to allocate for asciiTable.");
56  return -1;
57  }
58  handle[0] = asciiTable("seri", "0", format_str, NULL, NULL, NULL);
59  }
60  if (s->info != NULL) {
61  free(s->info);
62  }
63  s->size_info = sizeof(asciiTable_t);
64  s->info = (void*)handle;
65  } else if (info == NULL) {
66  if (type < 0) {
67  type = DIRECT_SERI;
68  }
69  } else {
70  char *format_str = (char*)info;
71  s->size_info = 2*strlen(format_str) + 1;
72  void *t_sinfo = (void*)realloc(s->info, s->size_info);
73  if (t_sinfo == NULL) {
74  cislog_error("update_serializer: Failed to reallocate for format string.");
75  s->size_info = 0;
76  free(s->info);
77  return -1;
78  }
79  s->info = t_sinfo;
80  strcpy((char*)(s->info), format_str);
81  // size_t len_fmt = strlen(format_str);
82  // memcpy(s->info, format_str, len_fmt + 1);
83  // ((char*)(s->info))[len_fmt] = '\0';
84  if (type < 0) {
85  type = FORMAT_SERI;
86  }
87  }
88  s->type = (seri_type)type;
89  return 0;
90 };
91 
98 static inline
99 seri_t * init_serializer(int type, const void *info) {
100  seri_t *s = (seri_t*)malloc(sizeof(seri_t));
101  if (s == NULL) {
102  cislog_error("init_serializer: Failed to allocate serializer.");
103  return NULL;
104  }
105  s[0] = empty_serializer();
106  int flag = update_serializer(s, type, info);
107  if (flag != 0) {
108  cislog_error("init_serializer: Failed to create serializer.");
109  free(s);
110  s = NULL;
111  }
112  return s;
113 };
114 
115 
120 static inline
121 int free_serializer(seri_t *s) {
122  if (s->info != NULL) {
123  free(s->info);
124  s->info = NULL;
125  }
126  return 0;
127 };
128 
129 
142 static inline
143 int serialize(const seri_t s, char **buf, const size_t buf_siz,
144  const int allow_realloc, int *args_used, va_list ap) {
145  seri_type t = s.type;
146  int ret = -1;
147  va_list ap2;
148  if (allow_realloc) {
149  va_copy(ap2, ap);
150  }
151  if (t == DIRECT_SERI)
152  ret = serialize_direct(s, *buf, buf_siz, args_used, ap);
153  else if (t == FORMAT_SERI)
154  ret = serialize_format(s, *buf, buf_siz, args_used, ap);
155  else if (t == ASCII_TABLE_SERI)
156  ret = serialize_ascii_table(s, *buf, buf_siz, args_used, ap);
157  else if (t == ASCII_TABLE_ARRAY_SERI)
158  ret = serialize_ascii_table_array(s, *buf, buf_siz, args_used, ap);
159  else if (t == PLY_SERI)
160  ret = serialize_ply(s, *buf, buf_siz, args_used, ap);
161  else if (t == OBJ_SERI)
162  ret = serialize_obj(s, *buf, buf_siz, args_used, ap);
163  else {
164  cislog_error("serialize: Unsupported seri_type %d", t);
165  }
166  if (ret > (int)buf_siz) {
167  if (allow_realloc) {
168  *buf = (char*)realloc(*buf, ret+1);
169  if (*buf == NULL) {
170  cislog_error("serialize: Failed to realloc buffer.");
171  ret = -1;
172  } else {
173  ret = serialize(s, buf, ret+1, 1, args_used, ap2);
174  }
175  } else {
176  cislog_error("serialize: encoded message too large for the buffer. (buf_siz=%d, len=%d)",
177  buf_siz, ret);
178  ret = -1;
179  }
180  }
181  if (allow_realloc) {
182  va_end(ap2);
183  }
184  return ret;
185 }
186 
195 static inline
196 int deserialize(const seri_t s, const char *buf, const size_t buf_siz, va_list ap) {
197  seri_type t = s.type;
198  int ret = -1;
199  if (t == DIRECT_SERI)
200  ret = deserialize_direct(s, buf, buf_siz, ap);
201  else if (t == FORMAT_SERI)
202  ret = deserialize_format(s, buf, buf_siz, ap);
203  else if (t == ASCII_TABLE_SERI)
204  ret = deserialize_ascii_table(s, buf, buf_siz, ap);
205  else if (t == ASCII_TABLE_ARRAY_SERI)
206  ret = deserialize_ascii_table_array(s, buf, buf_siz, ap);
207  else if (t == PLY_SERI)
208  ret = deserialize_ply(s, buf, buf_siz, ap);
209  else if (t == OBJ_SERI)
210  ret = deserialize_obj(s, buf, buf_siz, ap);
211  else {
212  cislog_error("deserialize: Unsupported seri_type %d", t);
213  }
214  return ret;
215 };
216 
217 #ifdef __cplusplus /* If this is a C++ compiler, end C linkage */
218 }
219 #endif
220 
221 #endif /*CISSERIALIZE_H_*/
Serializer structure.
Definition: SerializeBase.h:19
Structure containing information about an ASCII table.
Definition: AsciiTable.h:80