Module: kamailio Branch: master Commit: 5c83f2ea04cfc02dc53532b53888a33be295a997 URL: https://github.com/kamailio/kamailio/commit/5c83f2ea04cfc02dc53532b53888a33be295a997
Author: Daniel-Constantin Mierla <mico...@gmail.com> Committer: Daniel-Constantin Mierla <mico...@gmail.com> Date: 2025-07-08T17:04:38+02:00 core: ut - convenience function to decode hexa with whitespaces --- Modified: src/core/ut.c Modified: src/core/ut.h --- Diff: https://github.com/kamailio/kamailio/commit/5c83f2ea04cfc02dc53532b53888a33be295a997.diff Patch: https://github.com/kamailio/kamailio/commit/5c83f2ea04cfc02dc53532b53888a33be295a997.patch --- diff --git a/src/core/ut.c b/src/core/ut.c index cac9fcaf00b..317a0384a12 100644 --- a/src/core/ut.c +++ b/src/core/ut.c @@ -498,3 +498,79 @@ void *ser_memrmem(const void *b1, const void *b2, size_t len1, size_t len2) return NULL; } + +/** + * decode hexa value in shex, storing in sraw + * - on success sraw->s is pkg-allocated and has to be pkg-freed + * - return 0 on success, -1 on failure + */ +int ksr_hex_decode_ws(str *shex, str *sraw) +{ + int i; + char v; + + if(shex == NULL || shex->s == NULL || shex->len == 0) { + LM_ERR("invalid body parameter\n"); + return -1; + } + + sraw->len = shex->len / 2 + 2; + sraw->s = pkg_malloc(sraw->len * sizeof(char)); + if(sraw->s == NULL) { + LM_ERR("no more pkg memory\n"); + sraw->len = 0; + return -1; + } + memset(sraw->s, 0, sraw->len * sizeof(char)); + + sraw->len = 0; + for(i = 0; i < shex->len; i++) { + if(shex->s[i] == ' ' || shex->s[i] == '\t') { + continue; + } + if(i + 1 == shex->len) { + LM_ERR("invalid input hex data [%.*s] (%d/%d)\n", shex->len, + shex->s, shex->len, i); + goto error; + } + v = 0; + if(shex->s[i] >= '0' && shex->s[i] <= '9') { + v = (shex->s[i] - '0') << 4; + } else if(shex->s[i] >= 'A' && shex->s[i] <= 'F') { + v = (shex->s[i] - 'A' + 10) << 4; + } else if(shex->s[i] >= 'a' && shex->s[i] <= 'f') { + v = (shex->s[i] - 'a' + 10) << 4; + } else { + LM_ERR("invalid input hex data [%.*s] (%d/%d)\n", shex->len, + shex->s, shex->len, i); + goto error; + } + i++; + if(shex->s[i] >= '0' && shex->s[i] <= '9') { + v += (shex->s[i] - '0'); + } else if(shex->s[i] >= 'A' && shex->s[i] <= 'F') { + v += (shex->s[i] - 'A' + 10); + } else if(shex->s[i] >= 'a' && shex->s[i] <= 'f') { + v += (shex->s[i] - 'a' + 10); + } else { + LM_ERR("invalid input hex data [%.*s] (%d/%d)\n", shex->len, + shex->s, shex->len, i); + goto error; + } + sraw->s[sraw->len++] = v; + } + if(sraw->len == 0) { + /* only white spaces */ + LM_ERR("invalid input hex data [%.*s] (%d/%d)\n", shex->len, shex->s, + shex->len, i); + goto error; + } + + return 0; + +error: + pkg_free(sraw->s); + sraw->s = NULL; + sraw->len = 0; + return -1; +} diff --git a/src/core/ut.h b/src/core/ut.h index 3a3d66202b6..a0b01e23dfb 100644 --- a/src/core/ut.h +++ b/src/core/ut.h @@ -1294,4 +1294,6 @@ void *ser_memmem(const void *b1, const void *b2, size_t len1, size_t len2); */ void *ser_memrmem(const void *b1, const void *b2, size_t len1, size_t len2); +int ksr_hex_decode_ws(str *shex, str *sraw); + #endif _______________________________________________ Kamailio - Development Mailing List -- sr-dev@lists.kamailio.org To unsubscribe send an email to sr-dev-le...@lists.kamailio.org Important: keep the mailing list in the recipients, do not reply only to the sender!