Module: kamailio
Branch: 5.1
Commit: 37ffe9bcb52751f6f2aed76074784872af3f94c5
URL: 
https://github.com/kamailio/kamailio/commit/37ffe9bcb52751f6f2aed76074784872af3f94c5

Author: Daniel-Constantin Mierla <[email protected]>
Committer: Daniel-Constantin Mierla <[email protected]>
Date: 2020-02-10T13:28:04+01:00

core: pv - function to get the size of the value for strings with vars

- added silent mode for printing strings with vars

(cherry picked from commit 6ee9e9c9dc2d818a5ce6c6cf7828a68a1c8cb291)

---

Modified: src/core/pvapi.c
Modified: src/core/pvar.h

---

Diff:  
https://github.com/kamailio/kamailio/commit/37ffe9bcb52751f6f2aed76074784872af3f94c5.diff
Patch: 
https://github.com/kamailio/kamailio/commit/37ffe9bcb52751f6f2aed76074784872af3f94c5.patch

---

diff --git a/src/core/pvapi.c b/src/core/pvapi.c
index ebc66ef148..6e09bb406b 100644
--- a/src/core/pvapi.c
+++ b/src/core/pvapi.c
@@ -1402,18 +1402,22 @@ int pv_set_spec_value(struct sip_msg* msg, pv_spec_p 
sp, int op,
 /**
  *
  */
-int pv_printf(struct sip_msg* msg, pv_elem_p list, char *buf, int *len)
+int pv_printf_mode(sip_msg_t* msg, pv_elem_t *list, int mode, char *buf, int 
*len)
 {
        int n;
        pv_value_t tok;
        pv_elem_p it;
        char *cur;
 
-       if(msg==NULL || list==NULL || buf==NULL || len==NULL)
+       if(msg==NULL || list==NULL || buf==NULL || len==NULL) {
+               LM_DBG("invalid parameters\n");
                return -1;
+       }
 
-       if(*len <= 0)
+       if(*len <= 0) {
+               LM_DBG("invalid value for output buffer size\n");
                return -1;
+       }
 
        *buf = '\0';
        cur = buf;
@@ -1430,8 +1434,10 @@ int pv_printf(struct sip_msg* msg, pv_elem_p list, char 
*buf, int *len)
                                n += it->text.len;
                                cur += it->text.len;
                        } else {
-                               LM_ERR("no more space for text value - 
printed:%d token:%d buffer:%d\n",
+                               if(likely(mode)) {
+                                       LM_ERR("no more space for text value - 
printed:%d token:%d buffer:%d\n",
                                                n, it->text.len, *len);
+                               }
                                goto overflow;
                        }
                }
@@ -1450,8 +1456,10 @@ int pv_printf(struct sip_msg* msg, pv_elem_p list, char 
*buf, int *len)
                                        cur += tok.rs.len;
                                }
                        } else {
-                               LM_ERR("no more space for spec value - 
printed:%d token:%d buffer:%d\n",
+                               if(likely(mode)) {
+                                       LM_ERR("no more space for spec value - 
printed:%d token:%d buffer:%d\n",
                                                n, tok.rs.len, *len);
+                               }
                                goto overflow;
                        }
                }
@@ -1460,8 +1468,10 @@ int pv_printf(struct sip_msg* msg, pv_elem_p list, char 
*buf, int *len)
        goto done;
 
 overflow:
-       LM_ERR("buffer overflow -- increase the buffer size...\n");
-       return -1;
+       if(likely(mode)) {
+               LM_ERR("buffer overflow -- increase the buffer size...\n");
+       }
+       return -2;
 
 done:
 #ifdef EXTRA_DEBUG
@@ -1472,6 +1482,47 @@ int pv_printf(struct sip_msg* msg, pv_elem_p list, char 
*buf, int *len)
        return 0;
 }
 
+/**
+ *
+ */
+int pv_printf(sip_msg_t* msg, pv_elem_t *list, char *buf, int *len)
+{
+       return pv_printf_mode(msg, list, 1, buf, len);
+}
+
+/**
+ *
+ */
+int pv_printf_size(sip_msg_t* msg, pv_elem_t *list)
+{
+       int n;
+       pv_value_t tok;
+       pv_elem_t *it;
+
+       if(msg==NULL || list==NULL) {
+               return -1;
+       }
+
+       n = 0;
+       for (it=list; it; it=it->next) {
+               /* count the static text */
+               if(it->text.s && it->text.len>0) {
+                       n += it->text.len;
+               }
+               /* count the value of the specifier */
+               if(it->spec!=NULL && it->spec->type!=PVT_NONE
+                               && pv_get_spec_value(msg, it->spec, &tok)==0)
+               {
+                       if(tok.flags&PV_VAL_NULL) {
+                               tok.rs = pv_str_null;
+                       }
+                       n += tok.rs.len;
+               }
+       }
+
+       return n;
+}
+
 /**
  *
  */
diff --git a/src/core/pvar.h b/src/core/pvar.h
index ce275cd4b4..d46b544ebc 100644
--- a/src/core/pvar.h
+++ b/src/core/pvar.h
@@ -173,7 +173,9 @@ char* pv_parse_spec2(str *in, pv_spec_p sp, int silent);
 int pv_get_spec_value(struct sip_msg* msg, pv_spec_p sp, pv_value_t *value);
 int pv_set_spec_value(struct sip_msg* msg, pv_spec_p sp, int op,
                pv_value_t *value);
-int pv_printf(struct sip_msg* msg, pv_elem_p list, char *buf, int *len);
+int pv_printf_mode(sip_msg_t* msg, pv_elem_t* list, int mode, char *buf, int 
*len);
+int pv_printf(sip_msg_t* msg, pv_elem_t* list, char *buf, int *len);
+int pv_printf_size(sip_msg_t* msg, pv_elem_t *list);
 int pv_elem_free_all(pv_elem_p log);
 void pv_value_destroy(pv_value_t *val);
 void pv_spec_destroy(pv_spec_t *spec);


_______________________________________________
Kamailio (SER) - Development Mailing List
[email protected]
https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-dev

Reply via email to