]> git.ozlabs.org Git - ccan/commitdiff
ccan/rune: add explicit-length variants.
authorRusty Russell <rusty@rustcorp.com.au>
Sat, 2 Jul 2022 01:01:14 +0000 (10:31 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Sat, 2 Jul 2022 01:01:14 +0000 (10:31 +0930)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/rune/_info
ccan/rune/coding.c
ccan/rune/rune.h

index e3601560b94f08a6b85d6db00bde9206dd5fa5c0..aaed15d871eb21ea37b521c94e8943a1a2c14ad8 100644 (file)
@@ -73,7 +73,7 @@
  *             rune = rune_from_base64(NULL, argv[2]);
  *             if (!rune)
  *                     errx(1, "Bad rune");
- *             restr = rune_restr_from_string(NULL, argv[3]);
+ *             restr = rune_restr_from_string(NULL, argv[3], strlen(argv[3]));
  *             if (!restr)
  *                     errx(1, "Bad restriction string");
  *             rune_add_restr(rune, restr);
index 201553d8b5e65239cdfb1260b2a7d684cecce384..b9255b0b5b02f9f39eea564954a3f811ebfaee3b 100644 (file)
@@ -306,9 +306,9 @@ static struct rune *from_string(const tal_t *ctx,
 }
 
 struct rune_restr *rune_restr_from_string(const tal_t *ctx,
-                                         const char *str)
+                                         const char *str,
+                                         size_t len)
 {
-       size_t len = strlen(str);
        struct rune_restr *restr;
 
        restr = rune_restr_decode(NULL, &str, &len);
@@ -334,26 +334,26 @@ static void to_string(struct wbuf *wbuf, const struct rune *rune, u8 *hash32)
        to_wbuf("", 1, wbuf);
 }
 
-struct rune *rune_from_base64(const tal_t *ctx, const char *str)
+struct rune *rune_from_base64n(const tal_t *ctx, const char *str, size_t len)
 {
-       size_t len;
+       size_t blen;
        u8 *data;
        struct rune *rune;
 
-       data = tal_arr(NULL, u8, base64_decoded_length(strlen(str)) + 1);
+       data = tal_arr(NULL, u8, base64_decoded_length(len) + 1);
 
-       len = base64_decode_using_maps(&base64_maps_urlsafe,
+       blen = base64_decode_using_maps(&base64_maps_urlsafe,
                                       (char *)data, tal_bytelen(data),
-                                      str, strlen(str));
-       if (len == -1)
+                                      str, len);
+       if (blen == -1)
                goto fail;
-                       
-       if (len < 32)
+
+       if (blen < 32)
                goto fail;
 
-       data[len] = '\0';
+       data[blen] = '\0';
        /* Sanity check that it's a valid string! */
-       if (strlen((char *)data + 32) != len - 32)
+       if (strlen((char *)data + 32) != blen - 32)
                goto fail;
 
        rune = from_string(ctx, (const char *)data + 32, data);
@@ -365,6 +365,11 @@ fail:
        return NULL;
 }
 
+struct rune *rune_from_base64(const tal_t *ctx, const char *str)
+{
+       return rune_from_base64n(ctx, str, strlen(str));
+}
+
 char *rune_to_base64(const tal_t *ctx, const struct rune *rune)
 {
        u8 hash32[32];
index a284b733423ee643304ac6139dd9ca921d25c321..59406db3637754032f365ae31d37cbcf4e730305 100644 (file)
@@ -321,6 +321,16 @@ const char *rune_test_(const tal_t *ctx,
  */
 struct rune *rune_from_base64(const tal_t *ctx, const char *str);
 
+/**
+ * rune_from_base64n - convert base64 string to rune.
+ * @ctx: context to allocate rune off.
+ * @str: base64 string.
+ * @len: length of @str.
+ *
+ * Returns NULL if it's malformed.
+ */
+struct rune *rune_from_base64n(const tal_t *ctx, const char *str, size_t len);
+
 /**
  * rune_to_base64 - convert run to base64 string.
  * @ctx: context to allocate rune off.
@@ -340,10 +350,11 @@ char *rune_to_string(const tal_t *ctx, const struct rune *rune);
  * rune_restr_from_string - convenience routine to parse a single restriction.
  * @ctx: context to allocate rune off.
  * @str: the string of form "<field><cond><val>[|<field><cond><val>]*"
+ * @len: the length of @str.
  *
- * This is really an internal routine, exposed for simple examples.
+ * This is useful for writing simple tests and making simple runes.
  */
 struct rune_restr *rune_restr_from_string(const tal_t *ctx,
-                                         const char *str);
-
+                                         const char *str,
+                                         size_t len);
 #endif /* CCAN_RUNE_RUNE_H */