]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
subtraction is the inverse of addition
authorAlan T. DeKok <aland@freeradius.org>
Sat, 20 Nov 2021 16:09:35 +0000 (11:09 -0500)
committerAlan T. DeKok <aland@freeradius.org>
Sat, 20 Nov 2021 16:19:16 +0000 (11:19 -0500)
src/lib/util/calc.c
src/tests/unit/calc.txt

index 8f65ab0f05b4cf1eb8bba235f6d8dfa3c35c222b..a9a0806313b4faece032d5de2ad9b9992beb3a8d 100644 (file)
@@ -379,6 +379,31 @@ static int calc_octets(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t cons
                fr_value_box_memdup_shallow(dst, dst->enumv, buf, len, a->tainted | b->tainted);
                break;
 
+       case T_SUB:
+               /*
+                *  The inverse of add!
+                */
+               if (a->vb_length < b->vb_length) {
+                       fr_strerror_const("Suffix to remove is longer than input string");
+                       return -1;
+               }
+
+               if (memcmp(a->vb_octets + a->vb_length - b->vb_length, b->vb_strvalue, b->vb_length) != 0) {
+                       fr_strerror_const("Right side is not a suffix of the input string");
+                       return -1;
+               }
+
+               len = a->vb_length - b->vb_length;
+               buf = talloc_array(ctx, uint8_t, len);
+               if (!buf) goto oom;
+
+               memcpy(buf, a->vb_strvalue, len);
+               buf[len] = '\0';
+
+               fr_value_box_clear_value(dst);
+               fr_value_box_memdup_shallow(dst, dst->enumv, buf, len, a->tainted | b->tainted);
+               break;
+
        default:
                return ERR_INVALID;     /* invalid operator */
        }
@@ -438,6 +463,31 @@ static int calc_string(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t cons
                fr_value_box_strdup_shallow(dst, dst->enumv, buf, a->tainted | b->tainted);
                break;
 
+       case T_SUB:
+               /*
+                *  The inverse of add!
+                */
+               if (a->vb_length < b->vb_length) {
+                       fr_strerror_const("Suffix to remove is longer than input string");
+                       return -1;
+               }
+
+               if (memcmp(a->vb_strvalue + a->vb_length - b->vb_length, b->vb_strvalue, b->vb_length) != 0) {
+                       fr_strerror_const("Right side is not a suffix of the input string");
+                       return -1;
+               }
+
+               len = a->vb_length - b->vb_length;
+               buf = talloc_array(ctx, char, len + 1);
+               if (!buf) goto oom;
+
+               memcpy(buf, a->vb_strvalue, len);
+               buf[len] = '\0';
+
+               fr_value_box_clear_value(dst);
+               fr_value_box_strdup_shallow(dst, dst->enumv, buf, a->tainted | b->tainted);
+               break;
+
        default:
                return ERR_INVALID;     /* invalid operator */
        }
index 0efe2323ad81d79b10a0b0de66fd2373dc54954c..25537ca25cc4b47a8367e1991274bb33df49d7b4 100644 (file)
@@ -30,9 +30,22 @@ match 1
 calc string "a" . string "b" -> string
 match ab
 
-# string prepend
-calc string "a" ^ string "b" -> string
-match ba
+# string subtraction is the inverse of addition!
+calc string "ab" - string "b" -> string
+match a
+
+# octets prepend
+calc octets "a" ^ octets "b" -> octets
+match 0x6261
+
+# octets append
+calc octets "a" . octets "b" -> octets
+match 0x6162
+
+# octets subtraction is the inverse of addition!
+calc octets "ab" - octets "b" -> octets
+match 0x61
+
 
 # time deltas
 calc time_delta 1 + time_delta 2 -> time_delta
@@ -94,4 +107,4 @@ calc string "2" += string "test"
 match 2test
 
 count
-match 42
+match 48