/** * virBufferEscapeSexpr: * @buf: the buffer to dump * @format: a printf like format string but with only one %s parameter * @str: the string argument which need to be escaped * * Do a formatted print with a single string to an sexpr buffer. The string * is escaped to avoid generating a sexpr that xen will choke on. This * doesn't fully escape the sexpr, just enough for our code to work. */ void virBufferEscapeSexpr(const virBufferPtr buf, const char *format, const char *str) { int len; char *escaped, *out; const char *cur; if ((format == NULL) || (buf == NULL) || (str == NULL)) return; if (buf->error) return; len = strlen(str); if (strcspn(str, "\\'") == len) { virBufferVSprintf(buf, format, str); return; } if (VIR_ALLOC_N(escaped, 2 * len + 1) < 0) { virBufferNoMemory(buf); return; } cur = str; out = escaped; while (*cur != 0) { switch (*cur) { case '\\': case '\'': *out++ = '\\'; /* fallthrough */ default: *out++ = *cur; } cur++; } *out = 0; virBufferVSprintf(buf, format, escaped); VIR_FREE(escaped); }
/** * virBufferGrow: * @buf: the buffer * @len: the minimum free size to allocate on top of existing used space * * Grow the available space of a buffer to at least @len bytes. * * Returns zero on success or -1 on error */ static int virBufferGrow(virBufferPtr buf, unsigned int len) { int size; if (buf->error) return -1; if ((len + buf->use) < buf->size) return 0; size = buf->use + len + 1000; if (VIR_REALLOC_N(buf->content, size) < 0) { virBufferNoMemory(buf); return -1; } buf->size = size; return 0; }
/** * virBufferEscapeString: * @buf: the buffer to dump * @format: a printf like format string but with only one %s parameter * @str: the string argument which need to be escaped * * Do a formatted print with a single string to an XML buffer. The string * is escaped to avoid generating a not well-formed XML instance. */ void virBufferEscapeString(const virBufferPtr buf, const char *format, const char *str) { int len; char *escaped, *out; const char *cur; if ((format == NULL) || (buf == NULL) || (str == NULL)) return; if (buf->error) return; len = strlen(str); if (strcspn(str, "<>&'\"") == len) { virBufferVSprintf(buf, format, str); return; } if (VIR_ALLOC_N(escaped, 6 * len + 1) < 0) { virBufferNoMemory(buf); return; } cur = str; out = escaped; while (*cur != 0) { if (*cur == '<') { *out++ = '&'; *out++ = 'l'; *out++ = 't'; *out++ = ';'; } else if (*cur == '>') { *out++ = '&'; *out++ = 'g'; *out++ = 't'; *out++ = ';'; } else if (*cur == '&') { *out++ = '&'; *out++ = 'a'; *out++ = 'm'; *out++ = 'p'; *out++ = ';'; } else if (*cur == '"') { *out++ = '&'; *out++ = 'q'; *out++ = 'u'; *out++ = 'o'; *out++ = 't'; *out++ = ';'; } else if (*cur == '\'') { *out++ = '&'; *out++ = 'a'; *out++ = 'p'; *out++ = 'o'; *out++ = 's'; *out++ = ';'; } else if (((unsigned char)*cur >= 0x20) || (*cur == '\n') || (*cur == '\t') || (*cur == '\r')) { /* * default case, just copy ! * Note that character over 0x80 are likely to give problem * with UTF-8 XML, but since our string don't have an encoding * it's hard to handle properly we have to assume it's UTF-8 too */ *out++ = *cur; } cur++; } *out = 0; virBufferVSprintf(buf, format, escaped); VIR_FREE(escaped); }
/** * virBufferEscapeString: * @buf: the buffer to dump * @format: a printf like format string but with only one %s parameter * @str: the string argument which need to be escaped * * Do a formatted print with a single string to an XML buffer. The string * is escaped to avoid generating a not well-formed XML instance. */ void virBufferEscapeString(const virBufferPtr buf, const char *format, const char *str) { int size, count, len, grow_size; char *escaped, *out; const char *cur; if ((format == NULL) || (buf == NULL) || (str == NULL)) return; if (buf->error) return; len = strlen(str); if (VIR_ALLOC_N(escaped, 6 * len + 1) < 0) { virBufferNoMemory(buf); return; } cur = str; out = escaped; while (*cur != 0) { if (*cur == '<') { *out++ = '&'; *out++ = 'l'; *out++ = 't'; *out++ = ';'; } else if (*cur == '>') { *out++ = '&'; *out++ = 'g'; *out++ = 't'; *out++ = ';'; } else if (*cur == '&') { *out++ = '&'; *out++ = 'a'; *out++ = 'm'; *out++ = 'p'; *out++ = ';'; } else if (*cur == '"') { *out++ = '&'; *out++ = 'q'; *out++ = 'u'; *out++ = 'o'; *out++ = 't'; *out++ = ';'; } else if (*cur == '\'') { *out++ = '&'; *out++ = 'a'; *out++ = 'p'; *out++ = 'o'; *out++ = 's'; *out++ = ';'; } else if (((unsigned char)*cur >= 0x20) || (*cur == '\n') || (*cur == '\t') || (*cur == '\r')) { /* * default case, just copy ! * Note that character over 0x80 are likely to give problem * with UTF-8 XML, but since our string don't have an encoding * it's hard to handle properly we have to assume it's UTF-8 too */ *out++ = *cur; } cur++; } *out = 0; if ((buf->use >= buf->size) && virBufferGrow(buf, 100) < 0) { VIR_FREE(escaped); return; } size = buf->size - buf->use - 1; while (((count = snprintf(&buf->content[buf->use], size, format, (char *)escaped)) < 0) || (count >= size - 1)) { buf->content[buf->use] = 0; grow_size = (count > 1000) ? count : 1000; if (virBufferGrow(buf, grow_size) < 0) { VIR_FREE(escaped); return; } size = buf->size - buf->use - 1; } buf->use += count; buf->content[buf->use] = '\0'; VIR_FREE(escaped); }