From: Ben Pfaff Date: Tue, 19 Aug 2008 18:12:45 +0000 (-0700) Subject: New function ds_put_printable(). X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=0415ee1ac948dbedf76ce58dcd98aa934c2923f7;p=sliver-openvswitch.git New function ds_put_printable(). Use the function in dhcp_option_to_string(). --- diff --git a/include/dynamic-string.h b/include/dynamic-string.h index ea69c6af6..c85756dbe 100644 --- a/include/dynamic-string.h +++ b/include/dynamic-string.h @@ -56,6 +56,7 @@ void ds_put_cstr(struct ds *, const char *); void ds_put_format(struct ds *, const char *, ...) PRINTF_FORMAT(2, 3); void ds_put_format_valist(struct ds *, const char *, va_list) PRINTF_FORMAT(2, 0); +void ds_put_printable(struct ds *, const char *, size_t); void ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size, uintptr_t ofs, bool ascii); char *ds_cstr(struct ds *); diff --git a/lib/dhcp.c b/lib/dhcp.c index 0d3cf5f61..1e6628ad2 100644 --- a/lib/dhcp.c +++ b/lib/dhcp.c @@ -426,14 +426,15 @@ dhcp_option_to_string(const struct dhcp_option *opt, int code, struct ds *ds) if (class->type == DHCP_ARG_STRING) { ds_put_char(ds, '"'); + ds_put_printable(ds, opt->data, opt->n); + ds_put_char(ds, '"'); + return ds_cstr(ds); } for (offset = 0; offset + type->size <= opt->n; offset += type->size) { const void *p = (const char *) opt->data + offset; const uint8_t *uint8 = p; const uint32_t *uint32 = p; const uint16_t *uint16 = p; - const char *cp = p; - unsigned char c; if (offset && class->type != DHCP_ARG_STRING) { ds_put_cstr(ds, class->type == DHCP_ARG_UINT8 ? ":" : ", "); @@ -457,13 +458,7 @@ dhcp_option_to_string(const struct dhcp_option *opt, int code, struct ds *ds) put_duration(ds, ntohl(*uint32)); break; case DHCP_ARG_STRING: - c = *cp; - if (isprint(c) && (!isspace(c) || c == ' ') && c != '\\') { - ds_put_char(ds, *cp); - } else { - ds_put_format(ds, "\\%03o", (int) c); - } - break; + NOT_REACHED(); case DHCP_ARG_BOOLEAN: if (*uint8 == 0) { ds_put_cstr(ds, "false"); @@ -475,9 +470,6 @@ dhcp_option_to_string(const struct dhcp_option *opt, int code, struct ds *ds) break; } } - if (class->type == DHCP_ARG_STRING) { - ds_put_char(ds, '"'); - } if (offset != opt->n) { if (offset) { ds_put_cstr(ds, ", "); diff --git a/lib/dynamic-string.c b/lib/dynamic-string.c index d12739b75..ca141783e 100644 --- a/lib/dynamic-string.c +++ b/lib/dynamic-string.c @@ -118,6 +118,20 @@ ds_put_format_valist(struct ds *ds, const char *format, va_list args_) } } +void +ds_put_printable(struct ds *ds, const char *s, size_t n) +{ + ds_reserve(ds, ds->length + n); + while (n-- > 0) { + unsigned char c = *s++; + if (c < 0x20 || c > 0x7e || c == '\\' || c == '"') { + ds_put_format(ds, "\\%03o", (int) c); + } else { + ds_put_char(ds, c); + } + } +} + char * ds_cstr(struct ds *ds) {