[sr-dev] git:master:4b377f07: core: xavp - helper function to shift xavps to left with rotation

Daniel-Constantin Mierla miconda at gmail.com
Tue May 10 19:43:37 CEST 2022


Module: kamailio
Branch: master
Commit: 4b377f07128a5322e7647bd9d492b50e81c5f278
URL: https://github.com/kamailio/kamailio/commit/4b377f07128a5322e7647bd9d492b50e81c5f278

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2022-05-10T19:35:02+02:00

core: xavp - helper function to shift xavps to left with rotation

---

Modified: src/core/xavp.c
Modified: src/core/xavp.h

---

Diff:  https://github.com/kamailio/kamailio/commit/4b377f07128a5322e7647bd9d492b50e81c5f278.diff
Patch: https://github.com/kamailio/kamailio/commit/4b377f07128a5322e7647bd9d492b50e81c5f278.patch

---

diff --git a/src/core/xavp.c b/src/core/xavp.c
index 036a5ffbe1..be92510520 100644
--- a/src/core/xavp.c
+++ b/src/core/xavp.c
@@ -519,6 +519,105 @@ int xavp_count(str *name, sr_xavp_t **start)
 	return n;
 }
 
+/**
+ * Left shift xavps
+ */
+int xavp_lshift(str *name, sr_xavp_t **head, int idx)
+{
+	sr_xavp_t *avp;
+	sr_xavp_t *lhead = NULL;
+	sr_xavp_t *lhead_last = NULL;
+	sr_xavp_t *ltail = NULL;
+	sr_xavp_t *ltail_last = NULL;
+	sr_xavp_t *crt=0;
+	sr_xavp_t *prv=0;
+	unsigned int id;
+	int n=0;
+	int xcnt;
+
+	if(name==NULL || name->s==NULL || name->len<=0) {
+		return 0;
+	}
+
+	if(idx==0) {
+		return 1;
+	}
+	xcnt = xavp_count(name, head);
+	if(xcnt <= 0) {
+		return -2;
+	}
+	while(idx < 0) {
+		idx = xcnt + idx;
+	}
+	if(idx==0) {
+		return 1;
+	}
+	idx = idx % xcnt;
+	if(idx==0) {
+		return 1;
+	}
+
+	id = get_hash1_raw(name->s, name->len);
+	if(head!=NULL)
+		avp = *head;
+	else
+		avp = *_xavp_list_crt;
+	while(avp)
+	{
+		crt = avp;
+		avp=avp->next;
+		if(crt->id==id && crt->name.len==name->len
+				&& strncmp(crt->name.s, name->s, name->len)==0)
+		{
+			if(prv!=NULL)
+				prv->next=crt->next;
+			else if(head!=NULL)
+				*head = crt->next;
+			else
+				*_xavp_list_crt = crt->next;
+			crt->next = NULL;
+			if(n < idx) {
+				if(ltail==NULL) {
+					ltail = crt;
+				}
+				if(ltail_last!=NULL) {
+					ltail_last->next = crt;
+				}
+				ltail_last = crt;
+			} else {
+				if(lhead==NULL) {
+					lhead = crt;
+				}
+				if(lhead_last!=NULL) {
+					lhead_last->next = crt;
+				}
+				lhead_last = crt;
+			}
+			n++;
+		} else {
+			prv = crt;
+		}
+	}
+
+	if(lhead_last) {
+		lhead_last->next = ltail;
+	}
+
+	if(head!=NULL) {
+		if(ltail_last) {
+			ltail_last->next = *head;
+		}
+		*head = lhead;
+	} else {
+		if(ltail_last) {
+			ltail_last->next = *_xavp_list_crt;
+		}
+		*_xavp_list_crt = lhead;
+	}
+
+	return 0;
+}
+
 void xavp_destroy_list_unsafe(sr_xavp_t **head)
 {
 	sr_xavp_t *avp, *foo;
diff --git a/src/core/xavp.h b/src/core/xavp.h
index 446b48eba3..880ea980dc 100644
--- a/src/core/xavp.h
+++ b/src/core/xavp.h
@@ -98,6 +98,7 @@ struct str_list *xavp_get_list_key_names(sr_xavp_t *xavp);
 
 int xavp_insert(sr_xavp_t *xavp, int idx, sr_xavp_t **list);
 sr_xavp_t *xavp_extract(str *name, sr_xavp_t **list);
+int xavp_lshift(str *name, sr_xavp_t **head, int idx);
 
 void xavp_print_list(sr_xavp_t **head);
 




More information about the sr-dev mailing list