[sr-dev] git:master:6ee9e9c9: core: pv - function to get the size of the value for strings with vars

Daniel-Constantin Mierla miconda at gmail.com
Wed Oct 30 18:28:44 CET 2019


Module: kamailio
Branch: master
Commit: 6ee9e9c9dc2d818a5ce6c6cf7828a68a1c8cb291
URL: https://github.com/kamailio/kamailio/commit/6ee9e9c9dc2d818a5ce6c6cf7828a68a1c8cb291

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2019-10-30T18:28:18+01:00

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

- added silent mode for printing strings with vars

---

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

---

Diff:  https://github.com/kamailio/kamailio/commit/6ee9e9c9dc2d818a5ce6c6cf7828a68a1c8cb291.diff
Patch: https://github.com/kamailio/kamailio/commit/6ee9e9c9dc2d818a5ce6c6cf7828a68a1c8cb291.patch

---

diff --git a/src/core/pvapi.c b/src/core/pvapi.c
index 1830ba0a2d..0148578315 100644
--- a/src/core/pvapi.c
+++ b/src/core/pvapi.c
@@ -1426,18 +1426,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;
@@ -1454,8 +1458,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;
 			}
 		}
@@ -1474,8 +1480,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;
 			}
 		}
@@ -1484,8 +1492,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
@@ -1496,6 +1506,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 f225cbc93a..2a249ea118 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);




More information about the sr-dev mailing list