[ovs-dev] [trace 2/3] ofpbuf: New function ofpbuf_put_hex().

Ethan Jackson ethan at nicira.com
Thu Dec 9 21:55:53 UTC 2010


Looks Good.

On Wed, Dec 8, 2010 at 5:10 PM, Ben Pfaff <blp at nicira.com> wrote:
> This commit converts nx_match_from_string() to use this new function.  The
> new function will also have another user in an upcoming commit.
> ---
>  lib/nx-match.c |   30 +++++++++---------------------
>  lib/ofpbuf.c   |   27 +++++++++++++++++++++++++++
>  lib/ofpbuf.h   |    1 +
>  3 files changed, 37 insertions(+), 21 deletions(-)
>
> diff --git a/lib/nx-match.c b/lib/nx-match.c
> index 4821c62..276093e 100644
> --- a/lib/nx-match.c
> +++ b/lib/nx-match.c
> @@ -743,25 +743,6 @@ parse_nxm_field_name(const char *name, int name_len)
>
>     return 0;
>  }
> -
> -static const char *
> -parse_hex_bytes(struct ofpbuf *b, const char *s, unsigned int n)
> -{
> -    while (n--) {
> -        uint8_t byte;
> -        bool ok;
> -
> -        s += strspn(s, " ");
> -        byte = hexits_value(s, 2, &ok);
> -        if (!ok) {
> -            ovs_fatal(0, "%.2s: hex digits expected", s);
> -        }
> -
> -        ofpbuf_put(b, &byte, 1);
> -        s += 2;
> -    }
> -    return s;
> -}
>
>  /* nx_match_from_string(). */
>
> @@ -782,6 +763,7 @@ nx_match_from_string(const char *s, struct ofpbuf *b)
>         const char *name;
>         uint32_t header;
>         int name_len;
> +        size_t n;
>
>         name = s;
>         name_len = strcspn(s, "(");
> @@ -797,14 +779,20 @@ nx_match_from_string(const char *s, struct ofpbuf *b)
>         s += name_len + 1;
>
>         nxm_put_header(b, header);
> -        s = parse_hex_bytes(b, s, nxm_field_bytes(header));
> +        s = ofpbuf_put_hex(b, s, &n);
> +        if (n != nxm_field_bytes(header)) {
> +            ovs_fatal(0, "%.2s: hex digits expected", s);
> +        }
>         if (NXM_HASMASK(header)) {
>             s += strspn(s, " ");
>             if (*s != '/') {
>                 ovs_fatal(0, "%s: missing / in masked field %.*s",
>                           full_s, name_len, name);
>             }
> -            s = parse_hex_bytes(b, s + 1, nxm_field_bytes(header));
> +            s = ofpbuf_put_hex(b, s + 1, &n);
> +            if (n != nxm_field_bytes(header)) {
> +                ovs_fatal(0, "%.2s: hex digits expected", s);
> +            }
>         }
>
>         s += strspn(s, " ");
> diff --git a/lib/ofpbuf.c b/lib/ofpbuf.c
> index 77595e0..91ea363 100644
> --- a/lib/ofpbuf.c
> +++ b/lib/ofpbuf.c
> @@ -242,6 +242,33 @@ ofpbuf_put(struct ofpbuf *b, const void *p, size_t size)
>     return dst;
>  }
>
> +/* Parses as many pairs of hex digits as possible (possibly separated by
> + * spaces) from the beginning of 's', appending bytes for their values to 'b'.
> + * Returns the first character of 's' that is not the first of a pair of hex
> + * digits.  If 'n' is nonnull, stores the number of bytes added to 'b' in
> + * '*n'. */
> +char *
> +ofpbuf_put_hex(struct ofpbuf *b, const char *s, size_t *n)
> +{
> +    size_t initial_size = b->size;
> +    for (;;) {
> +        uint8_t byte;
> +        bool ok;
> +
> +        s += strspn(s, " ");
> +        byte = hexits_value(s, 2, &ok);
> +        if (!ok) {
> +            if (n) {
> +                *n = b->size - initial_size;
> +            }
> +            return (char *) s;
> +        }
> +
> +        ofpbuf_put(b, &byte, 1);
> +        s += 2;
> +    }
> +}
> +
>  /* Reserves 'size' bytes of headroom so that they can be later allocated with
>  * ofpbuf_push_uninit() without reallocating the ofpbuf. */
>  void
> diff --git a/lib/ofpbuf.h b/lib/ofpbuf.h
> index a7b5ded..bd668c1 100644
> --- a/lib/ofpbuf.h
> +++ b/lib/ofpbuf.h
> @@ -65,6 +65,7 @@ void *ofpbuf_end(const struct ofpbuf *);
>  void *ofpbuf_put_uninit(struct ofpbuf *, size_t);
>  void *ofpbuf_put_zeros(struct ofpbuf *, size_t);
>  void *ofpbuf_put(struct ofpbuf *, const void *, size_t);
> +char *ofpbuf_put_hex(struct ofpbuf *, const char *s, size_t *n);
>  void ofpbuf_reserve(struct ofpbuf *, size_t);
>  void *ofpbuf_push_uninit(struct ofpbuf *b, size_t);
>  void *ofpbuf_push_zeros(struct ofpbuf *, size_t);
> --
> 1.7.1
>
>
> _______________________________________________
> dev mailing list
> dev at openvswitch.org
> http://openvswitch.org/mailman/listinfo/dev_openvswitch.org
>




More information about the dev mailing list