Browse code

posops: handle negative index parameter for find functions

Daniel-Constantin Mierla authored on 23/09/2021 07:06:51
Showing 2 changed files
... ...
@@ -246,7 +246,8 @@ $var(pos) = pos_body_end();
246 246
 			val is at index 0, it returns the value specified by modparam idx0.
247 247
 			</para>
248 248
 			<para>
249
-			The idx can be an integer value or a variable holding an integer.
249
+			The idx can be an integer value or a variable holding an integer. If
250
+			the value is negative, the position is counted from the end of the buffer.
250 251
 			</para>
251 252
 			<para>
252 253
 			The val can be a static string or variables.
... ...
@@ -274,7 +275,8 @@ $var(idx) = pos_find_str("100", "kamailio");
274 275
 			by modparam idx0.
275 276
 			</para>
276 277
 			<para>
277
-			The idx can be an integer value or a variable holding an integer.
278
+			The idx can be an integer value or a variable holding an integer. If
279
+			the value is negative, the position is counted from the end of the buffer.
278 280
 			</para>
279 281
 			<para>
280 282
 			The val can be a static string or variables.
... ...
@@ -427,13 +427,15 @@ static int ki_posops_pos_find_str(sip_msg_t *msg, int idx, str *val)
427 427
 	char *p;
428 428
 	str text;
429 429
 
430
-	if(idx<0 || val==NULL || val->s==NULL || val->len<=0) {
430
+	if(val==NULL || val->s==NULL || val->len<=0) {
431 431
 		return -1;
432 432
 	}
433
-	if(idx > msg->len - val->len) {
433
+	if(idx<0) {
434
+		idx += msg->len;
435
+	}
436
+	if(idx<0 || idx > msg->len - val->len) {
434 437
 		return -1;
435 438
 	}
436
-
437 439
 	text.s = msg->buf + idx;
438 440
 	text.len = msg->len - idx;
439 441
 	p = str_search(&text, val);
... ...
@@ -476,10 +478,13 @@ static int ki_posops_pos_findi_str(sip_msg_t *msg, int idx, str *val)
476 478
 	char *p;
477 479
 	str text;
478 480
 
479
-	if(idx<0 || val==NULL || val->s==NULL || val->len<=0) {
481
+	if(val==NULL || val->s==NULL || val->len<=0) {
480 482
 		return -1;
481 483
 	}
482
-	if(idx > msg->len - val->len) {
484
+	if(idx<0) {
485
+		idx += msg->len;
486
+	}
487
+	if(idx<0 || idx > msg->len - val->len) {
483 488
 		return -1;
484 489
 	}
485 490