virBufferAddChar: Append a single character to the buffer.virBufferURIEncodeString: Append a %-encoded string to the buffer (eg. virBufferEscapeString and the similar function in libxml2).
Rich. -- Emerging Technologies, Red Hat - http://et.redhat.com/~rjones/ Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SL4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 03798903
Index: src/buf.c =================================================================== RCS file: /data/cvs/libvirt/src/buf.c,v retrieving revision 1.8 diff -u -r1.8 buf.c --- src/buf.c 12 Dec 2007 16:27:10 -0000 1.8 +++ src/buf.c 13 Dec 2007 18:15:52 -0000 @@ -16,6 +16,7 @@ #include <stdlib.h> #include <string.h> #include <stdarg.h> + #include "buf.h" /** @@ -49,7 +50,7 @@ /** * virBufferAdd: - * @buf: the buffer to dump + * @buf: the buffer to add to * @str: the string * @len: the number of bytes to add * @@ -78,14 +79,42 @@ return (-1); } } - /* XXX: memmove() is 2x slower than memcpy(), do we really need it? */ - memmove(&buf->content[buf->use], str, len); + + memcpy (&buf->content[buf->use], str, len); buf->use += len; buf->content[buf->use] = 0; return (0); } /** + * virBufferAddChar: + * @buf: the buffer to add to + * @c: the character to add + * + * Add a single character 'c' to a buffer. + * + * Returns 0 if successful, -1 in the case of error. + */ +int +virBufferAddChar (virBufferPtr buf, char c) +{ + unsigned int needSize; + + if (buf == NULL) + return -1; + + needSize = buf->use + 2; + if (needSize > buf->size) + if (!virBufferGrow (buf, needSize - buf->use)) + return -1; + + buf->content[buf->use++] = c; + buf->content[buf->use] = 0; + + return 0; +} + +/** * virBufferNew: * @size: creation size in bytes * @@ -267,6 +296,55 @@ } /** + * virBufferURIEncodeString: + * @buf: the buffer to append to + * @str: the string argument which will be URI-encoded + * + * Append the string to the buffer. The string will be URI-encoded + * during the append (ie any non alpha-numeric characters are replaced + * with '%xx' hex sequences). + * + * Returns 0 successful, -1 in case of internal or API error. + */ +int +virBufferURIEncodeString (virBufferPtr buf, const char *str) +{ + int grow_size = 0; + const char *p; + unsigned char uc; + const char *hex = "0123456789abcdef"; + + for (p = str; *p; ++p) { + /* Want to leave only strict 7 bit ASCII alphanumerics ... */ + if ((*p >= '0' && *p <= '9') || + (*p >= 'a' && *p <= 'z') || + (*p >= 'A' && *p <= 'Z')) + grow_size++; + else + grow_size += 3; /* %ab */ + } + + if (virBufferGrow (buf, grow_size) == -1) + return -1; + + for (p = str; *p; ++p) { + if ((*p >= '0' && *p <= '9') || + (*p >= 'a' && *p <= 'z') || + (*p >= 'A' && *p <= 'Z')) + buf->content[buf->use++] = *p; + else { + uc = (unsigned char) *p; + buf->content[buf->use++] = '%'; + buf->content[buf->use++] = hex[uc >> 4]; + buf->content[buf->use++] = hex[uc & 0xf]; + } + } + + buf->content[buf->use] = '\0'; + return 0; +} + +/** * virBufferStrcat: * @buf: the buffer to dump * @...: the variable list of strings, the last argument must be NULL Index: src/buf.h =================================================================== RCS file: /data/cvs/libvirt/src/buf.h,v retrieving revision 1.2 diff -u -r1.2 buf.h --- src/buf.h 9 Jul 2007 11:24:52 -0000 1.2 +++ src/buf.h 13 Dec 2007 18:15:52 -0000 @@ -30,9 +30,11 @@ void virBufferFree(virBufferPtr buf); char *virBufferContentAndFree(virBufferPtr buf); int virBufferAdd(virBufferPtr buf, const char *str, int len); +int virBufferAddChar(virBufferPtr buf, char c); int virBufferVSprintf(virBufferPtr buf, const char *format, ...) ATTRIBUTE_FORMAT(printf, 2, 3); int virBufferStrcat(virBufferPtr buf, ...); int virBufferEscapeString(virBufferPtr buf, const char *format, const char *str); +int virBufferURIEncodeString (virBufferPtr buf, const char *str); #endif /* __VIR_BUFFER_H__ */
Attachment:
smime.p7s
Description: S/MIME Cryptographic Signature
-- Libvir-list mailing list Libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list