serialize.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright (C) 2005 iptelorg GmbH
  3. *
  4. * This file is part of ser, a free SIP server.
  5. *
  6. * ser is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * For a license to use the ser software under conditions
  12. * other than those described here, or to purchase support for this
  13. * software, please contact iptel.org by e-mail at the following addresses:
  14. * [email protected]
  15. *
  16. * ser is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include <cds/serialize.h>
  26. #include <cds/logger.h>
  27. #include <stdio.h>
  28. int init_input_sstream(sstream_t *ss, char *data_in, int data_len)
  29. {
  30. if (!ss) return -1;
  31. ss->type = sstream_in;
  32. ss->in.len = data_len;
  33. ss->in.s = data_in;
  34. ss->in_pos = 0;
  35. return 0;
  36. }
  37. int init_output_sstream(sstream_t *ss, int out_buff_resize)
  38. {
  39. if (!ss) return -1;
  40. ss->type = sstream_out;
  41. str_clear(&ss->in);
  42. ss->in_pos = 0;
  43. dstr_init(&ss->out, out_buff_resize);
  44. return 0;
  45. }
  46. int get_serialized_sstream(sstream_t *ss, str_t *dst)
  47. {
  48. if (ss->type == sstream_out) return dstr_get_str(&ss->out, dst);
  49. else return -1; /* no output for input stream */
  50. }
  51. int get_serialized_sstream_data(sstream_t *ss, char *dst)
  52. {
  53. if (ss->type == sstream_out) return dstr_get_data(&ss->out, dst);
  54. else return -1; /* no output for input stream */
  55. }
  56. int get_serialized_sstream_len(sstream_t *ss)
  57. {
  58. if (ss->type == sstream_out) return dstr_get_data_length(&ss->out);
  59. else return 0; /* no output for input stream */
  60. }
  61. int sstream_get(sstream_t *ss, char *c)
  62. {
  63. /* if (!is_input_sstream(ss)) return -1; */ /* optimalization */
  64. /* if (!c) return -1; */ /* dangerous optimalization */
  65. if (ss->in_pos < ss->in.len) {
  66. *c = ss->in.s[ss->in_pos++];
  67. return 0;
  68. }
  69. else return 1;
  70. }
  71. int sstream_put_str(sstream_t *ss, str_t *s)
  72. {
  73. /* if (is_input_sstream(ss)) return -1; */ /* dangerous optimalization */
  74. return dstr_append_str(&ss->out, s);
  75. }
  76. /* returns a part of string of given length - it is NOT a copy !!! */
  77. int sstream_get_str_ex(sstream_t *ss, int len, str_t *dst)
  78. {
  79. int l;
  80. int res = 0;
  81. if (!is_input_sstream(ss)) return -1;
  82. if (!dst) return -1;
  83. if (len == 0) {
  84. str_clear(dst);
  85. return 0;
  86. }
  87. l = ss->in.len - ss->in_pos;
  88. dst->s = ss->in.s + ss->in_pos;
  89. if (len > l) {
  90. dst->len = l;
  91. res = 1; /* not whole requested string is returned ! */
  92. }
  93. else dst->len = len;
  94. ss->in_pos += dst->len;
  95. return 0;
  96. }
  97. /* returns a copy of string from input buffer */
  98. int sstream_get_str(sstream_t *ss, int len, str_t *dst)
  99. {
  100. str_t tmp;
  101. int res = sstream_get_str_ex(ss, len, &tmp);
  102. if (res >= 0) {
  103. res = str_dup(dst, &tmp);
  104. if (res != 0) str_clear(dst);
  105. }
  106. return res;
  107. }
  108. int sstream_put_zt(sstream_t *ss, const char *s)
  109. {
  110. /* if (!is_input_sstream(ss)) return -1; */ /* dangerous optimalization */
  111. return dstr_append_zt(&ss->out, s);
  112. }
  113. int sstream_put(sstream_t *ss, const char *s, int len)
  114. {
  115. /* if (!is_input_sstream(ss)) return -1; */ /* dangerous optimalization */
  116. return dstr_append(&ss->out, s, len);
  117. }
  118. void destroy_sstream(sstream_t *ss)
  119. {
  120. if (ss->type == sstream_out) dstr_destroy(&ss->out);
  121. }
  122. /*****************************************************************/
  123. int serialize_int(sstream_t *ss, int *num)
  124. {
  125. char sep = ':';
  126. if (!num) return -1;
  127. if (is_input_sstream(ss)) {
  128. char c;
  129. int first = 1;
  130. int sign = 1; /* positive */
  131. *num = 0;
  132. while (sstream_get(ss, &c) == 0) {
  133. if (c == sep) break;
  134. if ((c >= '0') && (c <= '9')) *num = 10 * (*num) + (c - '0');
  135. else {
  136. switch (c) {
  137. case '-':
  138. if (first) sign = -1;
  139. else return -1;
  140. case '+':
  141. if (!first) return -1;
  142. default:
  143. return -1; /* unknown character */
  144. }
  145. }
  146. first = 0;
  147. }
  148. *num = sign * (*num);
  149. }
  150. else {
  151. char tmp[32];
  152. sprintf(tmp, "%d%c", *num, sep);
  153. sstream_put_zt(ss, tmp);
  154. }
  155. return 0;
  156. }
  157. int serialize_uint(sstream_t *ss, unsigned int *num)
  158. {
  159. char sep = ':';
  160. if (!num) return -1;
  161. if (is_input_sstream(ss)) {
  162. char c;
  163. *num = 0;
  164. while (sstream_get(ss, &c) == 0) {
  165. if (c == sep) break;
  166. if ((c >= '0') && (c <= '9')) *num = 10 * (*num) + (c - '0');
  167. else return -1; /* unknown character */
  168. }
  169. }
  170. else {
  171. char tmp[32];
  172. sprintf(tmp, "%u%c", *num, sep);
  173. sstream_put_zt(ss, tmp);
  174. }
  175. return 0;
  176. }
  177. int serialize_str(sstream_t *ss, str_t *s)
  178. {
  179. int res = 0;
  180. if (!s) return -1;
  181. if (serialize_int(ss, &s->len) != 0) return -1;
  182. if (is_input_sstream(ss)) {
  183. if (s->len == 0) s->s = NULL;
  184. else res = sstream_get_str(ss, s->len, s); /* duplicates read string */
  185. }
  186. else res = sstream_put(ss, s->s, s->len);
  187. return res;
  188. }
  189. int serialize_str_ex(sstream_t *ss, str_t *s)
  190. {
  191. int res = 0;
  192. if (!s) return -1;
  193. if (serialize_int(ss, &s->len) != 0) return -1;
  194. if (is_input_sstream(ss)) {
  195. if (s->len == 0) s->s = NULL;
  196. else res = sstream_get_str_ex(ss, s->len, s); /* doesn't duplicate read string */
  197. }
  198. else res = sstream_put(ss, s->s, s->len);
  199. return res;
  200. }
  201. int serialize_char(sstream_t *ss, char *c)
  202. {
  203. if (!c) return -1;
  204. if (is_input_sstream(ss)) return sstream_get(ss, c);
  205. else return sstream_put(ss, c, 1);
  206. }
  207. int serialize_uchar(sstream_t *ss, unsigned char *c)
  208. {
  209. if (!c) return -1;
  210. if (is_input_sstream(ss)) return sstream_get(ss, (char *)c);
  211. else return sstream_put(ss, (char *)c, 1);
  212. }