Browse code

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)

Daniel-Constantin Mierla authored on 30/10/2019 17:26:34
Showing 2 changed files
... ...
@@ -1426,18 +1426,22 @@ int pv_set_spec_value(struct sip_msg* msg, pv_spec_p sp, int op,
1426 1426
 /**
1427 1427
  *
1428 1428
  */
1429
-int pv_printf(struct sip_msg* msg, pv_elem_p list, char *buf, int *len)
1429
+int pv_printf_mode(sip_msg_t* msg, pv_elem_t *list, int mode, char *buf, int *len)
1430 1430
 {
1431 1431
 	int n;
1432 1432
 	pv_value_t tok;
1433 1433
 	pv_elem_p it;
1434 1434
 	char *cur;
1435 1435
 
1436
-	if(msg==NULL || list==NULL || buf==NULL || len==NULL)
1436
+	if(msg==NULL || list==NULL || buf==NULL || len==NULL) {
1437
+		LM_DBG("invalid parameters\n");
1437 1438
 		return -1;
1439
+	}
1438 1440
 
1439
-	if(*len <= 0)
1441
+	if(*len <= 0) {
1442
+		LM_DBG("invalid value for output buffer size\n");
1440 1443
 		return -1;
1444
+	}
1441 1445
 
1442 1446
 	*buf = '\0';
1443 1447
 	cur = buf;
... ...
@@ -1454,8 +1458,10 @@ int pv_printf(struct sip_msg* msg, pv_elem_p list, char *buf, int *len)
1454 1458
 				n += it->text.len;
1455 1459
 				cur += it->text.len;
1456 1460
 			} else {
1457
-				LM_ERR("no more space for text value - printed:%d token:%d buffer:%d\n",
1461
+				if(likely(mode)) {
1462
+					LM_ERR("no more space for text value - printed:%d token:%d buffer:%d\n",
1458 1463
 						n, it->text.len, *len);
1464
+				}
1459 1465
 				goto overflow;
1460 1466
 			}
1461 1467
 		}
... ...
@@ -1474,8 +1480,10 @@ int pv_printf(struct sip_msg* msg, pv_elem_p list, char *buf, int *len)
1474 1480
 					cur += tok.rs.len;
1475 1481
 				}
1476 1482
 			} else {
1477
-				LM_ERR("no more space for spec value - printed:%d token:%d buffer:%d\n",
1483
+				if(likely(mode)) {
1484
+					LM_ERR("no more space for spec value - printed:%d token:%d buffer:%d\n",
1478 1485
 						n, tok.rs.len, *len);
1486
+				}
1479 1487
 				goto overflow;
1480 1488
 			}
1481 1489
 		}
... ...
@@ -1484,8 +1492,10 @@ int pv_printf(struct sip_msg* msg, pv_elem_p list, char *buf, int *len)
1484 1492
 	goto done;
1485 1493
 
1486 1494
 overflow:
1487
-	LM_ERR("buffer overflow -- increase the buffer size...\n");
1488
-	return -1;
1495
+	if(likely(mode)) {
1496
+		LM_ERR("buffer overflow -- increase the buffer size...\n");
1497
+	}
1498
+	return -2;
1489 1499
 
1490 1500
 done:
1491 1501
 #ifdef EXTRA_DEBUG
... ...
@@ -1496,6 +1506,47 @@ done:
1496 1506
 	return 0;
1497 1507
 }
1498 1508
 
1509
+/**
1510
+ *
1511
+ */
1512
+int pv_printf(sip_msg_t* msg, pv_elem_t *list, char *buf, int *len)
1513
+{
1514
+	return pv_printf_mode(msg, list, 1, buf, len);
1515
+}
1516
+
1517
+/**
1518
+ *
1519
+ */
1520
+int pv_printf_size(sip_msg_t* msg, pv_elem_t *list)
1521
+{
1522
+	int n;
1523
+	pv_value_t tok;
1524
+	pv_elem_t *it;
1525
+
1526
+	if(msg==NULL || list==NULL) {
1527
+		return -1;
1528
+	}
1529
+
1530
+	n = 0;
1531
+	for (it=list; it; it=it->next) {
1532
+		/* count the static text */
1533
+		if(it->text.s && it->text.len>0) {
1534
+			n += it->text.len;
1535
+		}
1536
+		/* count the value of the specifier */
1537
+		if(it->spec!=NULL && it->spec->type!=PVT_NONE
1538
+				&& pv_get_spec_value(msg, it->spec, &tok)==0)
1539
+		{
1540
+			if(tok.flags&PV_VAL_NULL) {
1541
+				tok.rs = pv_str_null;
1542
+			}
1543
+			n += tok.rs.len;
1544
+		}
1545
+	}
1546
+
1547
+	return n;
1548
+}
1549
+
1499 1550
 /**
1500 1551
  *
1501 1552
  */
... ...
@@ -173,7 +173,9 @@ char* pv_parse_spec2(str *in, pv_spec_p sp, int silent);
173 173
 int pv_get_spec_value(struct sip_msg* msg, pv_spec_p sp, pv_value_t *value);
174 174
 int pv_set_spec_value(struct sip_msg* msg, pv_spec_p sp, int op,
175 175
 		pv_value_t *value);
176
-int pv_printf(struct sip_msg* msg, pv_elem_p list, char *buf, int *len);
176
+int pv_printf_mode(sip_msg_t* msg, pv_elem_t* list, int mode, char *buf, int *len);
177
+int pv_printf(sip_msg_t* msg, pv_elem_t* list, char *buf, int *len);
178
+int pv_printf_size(sip_msg_t* msg, pv_elem_t *list);
177 179
 int pv_elem_free_all(pv_elem_p log);
178 180
 void pv_value_destroy(pv_value_t *val);
179 181
 void pv_spec_destroy(pv_spec_t *spec);