Module: sip-router
Branch: mariusbucur/dmq
Commit: a0e4c378ff18052848334aa22f40e481fb6db9d0
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=a0e4c37…
Author: Marius Bucur <marius.bucur(a)1and1.ro>
Committer: Marius Bucur <marius.bucur(a)1and1.ro>
Date: Wed Jun 1 17:43:04 2011 +0300
added deserialize function
---
modules_k/htable/ht_serialize.c | 46 +++++++++++++-
modules_k/htable/ht_serialize_test.c | 108 ++++++++++++++++++++++++++++++++++
2 files changed, 150 insertions(+), 4 deletions(-)
diff --git a/modules_k/htable/ht_serialize.c b/modules_k/htable/ht_serialize.c
index e3aeab5..6e046f7 100644
--- a/modules_k/htable/ht_serialize.c
+++ b/modules_k/htable/ht_serialize.c
@@ -3,7 +3,8 @@
#include "ht_serialize.h"
#include "../../basex.h"
-int serialize_ht_pair(pv_value_t* val, str* htname, str* s, int len) {
+/* snprintf - pretty ugly, but cds/serialize is unusable for the moment */
+int serialize_ht_pair(pv_value_t* val, str* htname, str* s) {
str encoded_val = {0, 0};
str encoded_htname = {0, 0};
if (!s) {
@@ -18,21 +19,28 @@ int serialize_ht_pair(pv_value_t* val, str* htname, str* s, int len)
{
encoded_val.len = base64_enc_len(val->rs.len);
encoded_val.s = pkg_malloc(encoded_val.len);
if(base64_enc((unsigned char*)val->rs.s, val->rs.len, (unsigned
char*)encoded_val.s, encoded_val.len) < 0) {
- LM_ERR("cannot base64 value\n");
+ LM_ERR("cannot encode value\n");
goto error;
}
}
encoded_htname.len = base64_enc_len(htname->len);
encoded_htname.s = pkg_malloc(encoded_htname.len);
if(base64_enc((unsigned char*)htname->s, htname->len, (unsigned
char*)encoded_htname.s, encoded_htname.len) < 0) {
- LM_ERR("cannot base64 value\n");
+ LM_ERR("cannot encode htname\n");
goto error;
}
- s->len = snprintf(s->s, len, "%d %d %.*s %.*s", val->flags,
val->ri, STR_FMT(&encoded_htname), STR_FMT(&encoded_val));
+ s->len = snprintf(s->s, s->len, "%d %d %.*s %.*s", val->flags,
val->ri, STR_FMT(&encoded_htname), STR_FMT(&encoded_val));
if(s->len < 0) {
LM_ERR("cannot serialize data - probably an small buffer\n");
goto error;
}
+
+ if(encoded_val.s) {
+ pkg_free(encoded_val.s);
+ }
+ if(encoded_htname.s) {
+ pkg_free(encoded_htname.s);
+ }
return 0;
error:
if(encoded_val.s) {
@@ -42,4 +50,34 @@ error:
pkg_free(encoded_htname.s);
}
return -1;
+}
+
+int deserialize_ht_pair(pv_value_t* val, str* htname, str* src) {
+ str encoded_htname = {0, 0};
+ str encoded_val = {0, 0};
+ encoded_htname.s = pkg_malloc(src->len);
+ memset(encoded_htname.s, 0, src->len);
+ encoded_val.s = pkg_malloc(src->len);
+ memset(encoded_val.s, 0, src->len);
+
+ sscanf(src->s, "%d %d %s %s", &val->flags, &val->ri,
encoded_htname.s, encoded_val.s);
+ encoded_htname.len = strlen(encoded_htname.s);
+ encoded_val.len = strlen(encoded_val.s);
+
+ if(base64_dec((unsigned char*)encoded_htname.s, encoded_htname.len, (unsigned
char*)htname->s, htname->len) < 0) {
+ LM_ERR("cannot decode htname\n");
+ goto error;
+ }
+ if(base64_dec((unsigned char*)encoded_val.s, encoded_val.len, (unsigned
char*)val->rs.s, val->rs.len) < 0) {
+ LM_ERR("cannot decode val\n");
+ goto error;
+ }
+
+ pkg_free(encoded_htname.s);
+ pkg_free(encoded_val.s);
+ return 0;
+error:
+ pkg_free(encoded_htname.s);
+ pkg_free(encoded_val.s);
+ return -1;
}
\ No newline at end of file
diff --git a/modules_k/htable/ht_serialize_test.c b/modules_k/htable/ht_serialize_test.c
new file mode 100644
index 0000000..495cdcf
--- /dev/null
+++ b/modules_k/htable/ht_serialize_test.c
@@ -0,0 +1,108 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#define LM_ERR printf
+#define pkg_malloc malloc
+#define pkg_free free
+#include "../../str.h"
+#include "../../basex.h"
+
+typedef struct _pv_value
+{
+ str rs; /*!< string value */
+ int ri; /*!< integer value */
+ int flags; /*!< flags about the type of value */
+} pv_value_t, *pv_value_p;
+
+/* snprintf - pretty ugly, but cds/serialize is unusable for the moment */
+int serialize_ht_pair(pv_value_t* val, str* htname, str* s) {
+ str encoded_val = {0, 0};
+ str encoded_htname = {0, 0};
+ if (!s) {
+ LM_ERR("no destination string given\n");
+ goto error;
+ }
+ if(!htname || !htname->s || !htname->len) {
+ LM_ERR("no hashtable name given\n");
+ goto error;
+ }
+ if(val->rs.len) {
+ encoded_val.len = base64_enc_len(val->rs.len);
+ encoded_val.s = pkg_malloc(encoded_val.len);
+ if(base64_enc((unsigned char*)val->rs.s, val->rs.len, (unsigned
char*)encoded_val.s, encoded_val.len) < 0) {
+ LM_ERR("cannot encode value\n");
+ goto error;
+ }
+ }
+ encoded_htname.len = base64_enc_len(htname->len);
+ encoded_htname.s = pkg_malloc(encoded_htname.len);
+ if(base64_enc((unsigned char*)htname->s, htname->len, (unsigned
char*)encoded_htname.s, encoded_htname.len) < 0) {
+ LM_ERR("cannot encode htname\n");
+ goto error;
+ }
+ s->len = snprintf(s->s, s->len, "%d %d %.*s %.*s", val->flags,
val->ri, STR_FMT(&encoded_htname), STR_FMT(&encoded_val));
+ if(s->len < 0) {
+ LM_ERR("cannot serialize data - probably an small buffer\n");
+ goto error;
+ }
+
+ if(encoded_val.s) {
+ pkg_free(encoded_val.s);
+ }
+ if(encoded_htname.s) {
+ pkg_free(encoded_htname.s);
+ }
+ return 0;
+error:
+ if(encoded_val.s) {
+ pkg_free(encoded_val.s);
+ }
+ if(encoded_htname.s) {
+ pkg_free(encoded_htname.s);
+ }
+ return -1;
+}
+
+int deserialize_ht_pair(pv_value_t* val, str* htname, str* src) {
+ str encoded_htname = {0, 0};
+ str encoded_val = {0, 0};
+ encoded_htname.s = pkg_malloc(src->len);
+ memset(encoded_htname.s, 0, src->len);
+ encoded_val.s = pkg_malloc(src->len);
+ memset(encoded_val.s, 0, src->len);
+
+ sscanf(src->s, "%d %d %s %s", &val->flags, &val->ri,
encoded_htname.s, encoded_val.s);
+ encoded_htname.len = strlen(encoded_htname.s);
+ encoded_val.len = strlen(encoded_val.s);
+
+ if(base64_dec((unsigned char*)encoded_htname.s, encoded_htname.len, (unsigned
char*)htname->s, htname->len) < 0) {
+ LM_ERR("cannot decode htname\n");
+ goto error;
+ }
+ if(base64_dec((unsigned char*)encoded_val.s, encoded_val.len, (unsigned
char*)val->rs.s, val->rs.len) < 0) {
+ LM_ERR("cannot decode val\n");
+ goto error;
+ }
+
+ pkg_free(encoded_htname.s);
+ pkg_free(encoded_val.s);
+ return 0;
+error:
+ pkg_free(encoded_htname.s);
+ pkg_free(encoded_val.s);
+ return -1;
+}
+
+int main(){
+ pv_value_t pv;
+ str hname = str_init("hashtable name");
+ str src;
+ src.len = 2048;
+ src.s = pkg_malloc(src.len);
+ pv.flags = 2;
+ pv.ri = 0;
+ pv.rs.len = 12;
+ pv.rs.s = "hello world";
+ printf("%d\n", serialize_ht_pair(&pv, &hname, &src));
+ printf("%.*s\n", STR_FMT(&src));
+}
\ No newline at end of file