buf.c 9.5 KB
Newer Older
D
Daniel Veillard 已提交
1
/*
D
Daniel P. Berrange 已提交
2
 * buf.c: buffers for libvirt
D
Daniel Veillard 已提交
3
 *
4
 * Copyright (C) 2005-2008, 2010 Red Hat, Inc.
D
Daniel Veillard 已提交
5 6 7 8 9 10
 *
 * See COPYING.LIB for the License of this software
 *
 * Daniel Veillard <veillard@redhat.com>
 */

11
#include <config.h>
J
Jim Meyering 已提交
12

D
Daniel Veillard 已提交
13 14 15 16
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
J
Jim Meyering 已提交
17
#include "c-ctype.h"
18

19 20
#define __VIR_BUFFER_C__

D
Daniel Veillard 已提交
21
#include "buf.h"
22
#include "memory.h"
D
Daniel Veillard 已提交
23

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

/* If adding more fields, ensure to edit buf.h to match
   the number of fields */
struct _virBuffer {
    unsigned int size;
    unsigned int use;
    unsigned int error;
    char *content;
};

/**
 * virBufferFail
 * @buf: the buffer
 *
 * Mark the buffer has having failed a memory allocation,
 * freeing the content and setting the error flag.
 */
static void
virBufferNoMemory(const virBufferPtr buf)
{
44
    VIR_FREE(buf->content);
45 46 47 48 49
    buf->size = 0;
    buf->use = 0;
    buf->error = 1;
}

D
Daniel Veillard 已提交
50
/**
D
Daniel P. Berrange 已提交
51
 * virBufferGrow:
D
Daniel Veillard 已提交
52
 * @buf:  the buffer
53
 * @len:  the minimum free size to allocate on top of existing used space
D
Daniel Veillard 已提交
54
 *
55
 * Grow the available space of a buffer to at least @len bytes.
D
Daniel Veillard 已提交
56
 *
57
 * Returns zero on success or -1 on error
D
Daniel Veillard 已提交
58 59
 */
static int
D
Daniel P. Berrange 已提交
60
virBufferGrow(virBufferPtr buf, unsigned int len)
D
Daniel Veillard 已提交
61 62 63
{
    int size;

64 65 66 67 68
    if (buf->error)
        return -1;

    if ((len + buf->use) < buf->size)
        return 0;
D
Daniel Veillard 已提交
69 70 71

    size = buf->use + len + 1000;

72
    if (VIR_REALLOC_N(buf->content, size) < 0) {
73 74 75
        virBufferNoMemory(buf);
        return -1;
    }
D
Daniel Veillard 已提交
76
    buf->size = size;
77
    return 0;
D
Daniel Veillard 已提交
78 79 80
}

/**
D
Daniel P. Berrange 已提交
81
 * virBufferAdd:
82
 * @buf:  the buffer to add to
D
Daniel Veillard 已提交
83 84 85 86 87 88 89
 * @str:  the string
 * @len:  the number of bytes to add
 *
 * Add a string range to an XML buffer. if len == -1, the length of
 * str is recomputed to the full string.
 *
 */
90
void
D
Daniel P. Berrange 已提交
91
virBufferAdd(const virBufferPtr buf, const char *str, int len)
D
Daniel Veillard 已提交
92 93 94
{
    unsigned int needSize;

95 96 97 98 99
    if ((str == NULL) || (buf == NULL) || (len == 0))
        return;

    if (buf->error)
        return;
D
Daniel Veillard 已提交
100 101 102 103 104

    if (len < 0)
        len = strlen(str);

    needSize = buf->use + len + 2;
105 106 107
    if (needSize > buf->size &&
        virBufferGrow(buf, needSize - buf->use) < 0)
        return;
108 109

    memcpy (&buf->content[buf->use], str, len);
D
Daniel Veillard 已提交
110
    buf->use += len;
111
    buf->content[buf->use] = '\0';
D
Daniel Veillard 已提交
112 113
}

114 115 116 117 118 119 120 121
/**
 * virBufferAddChar:
 * @buf: the buffer to add to
 * @c: the character to add
 *
 * Add a single character 'c' to a buffer.
 *
 */
122
void
D
Daniel P. Berrange 已提交
123
virBufferAddChar (virBufferPtr buf, char c)
124 125 126 127
{
    unsigned int needSize;

    if (buf == NULL)
128 129 130 131
        return;

    if (buf->error)
        return;
132 133

    needSize = buf->use + 2;
134 135 136
    if (needSize > buf->size &&
        virBufferGrow (buf, needSize - buf->use) < 0)
        return;
137 138

    buf->content[buf->use++] = c;
139
    buf->content[buf->use] = '\0';
140 141
}

142
/**
143 144
 * virBufferContentAndReset:
 * @buf: Buffer
145
 *
146 147 148
 * Get the content from the buffer and free (only) the buffer structure.
 * The caller owns the returned string & should free it when no longer
 * required. The buffer object is reset to its initial state.
149
 *
150
 * Returns the buffer content or NULL in case of error.
151
 */
152
char *
D
Daniel P. Berrange 已提交
153
virBufferContentAndReset(const virBufferPtr buf)
D
Daniel Veillard 已提交
154
{
155 156 157
    char *str;
    if (buf == NULL)
        return NULL;
D
Daniel Veillard 已提交
158

159 160
    if (buf->error) {
        memset(buf, 0, sizeof(*buf));
D
Daniel Veillard 已提交
161 162 163
        return NULL;
    }

164 165 166
    str = buf->content;
    memset(buf, 0, sizeof(*buf));
    return str;
D
Daniel Veillard 已提交
167 168
}

169 170 171 172 173 174 175 176 177 178 179 180 181
/**
 * virBufferFreeAndReset:
 * @buf: the buffer to free and reset
 *
 * Frees the buffer content and resets the buffer structure.
 */
void virBufferFreeAndReset(const virBufferPtr buf)
{
    char *str = virBufferContentAndReset(buf);

    VIR_FREE(str);
}

182
/**
183 184
 * virBufferError:
 * @buf: the buffer
185
 *
186 187 188 189
 * Check to see if the buffer is in an error state due
 * to failed memory allocation
 *
 * Return true if in error, 0 if normal
190
 */
191
int
D
Daniel P. Berrange 已提交
192
virBufferError(const virBufferPtr buf)
D
Daniel Veillard 已提交
193
{
194 195 196 197
    if (buf == NULL)
        return 1;

    return buf->error;
D
Daniel Veillard 已提交
198 199 200
}

/**
201 202
 * virBufferUse:
 * @buf: the usage of the string in the buffer
D
Daniel Veillard 已提交
203
 *
204
 * Return the string usage in bytes
D
Daniel Veillard 已提交
205
 */
206 207
unsigned int
virBufferUse(const virBufferPtr buf)
D
Daniel Veillard 已提交
208
{
209
    if (buf == NULL)
210
        return 0;
D
Daniel Veillard 已提交
211

212
    return buf->use;
D
Daniel Veillard 已提交
213 214 215
}

/**
D
Daniel P. Berrange 已提交
216
 * virBufferVSprintf:
D
Daniel Veillard 已提交
217 218
 * @buf:  the buffer to dump
 * @format:  the format
219
 * @...:  the variable list of arguments
D
Daniel Veillard 已提交
220 221 222
 *
 * Do a formatted print to an XML buffer.
 */
223
void
D
Daniel P. Berrange 已提交
224
virBufferVSprintf(const virBufferPtr buf, const char *format, ...)
D
Daniel Veillard 已提交
225
{
226
    int size, count, grow_size;
D
Daniel Veillard 已提交
227 228
    va_list locarg, argptr;

229 230 231 232 233
    if ((format == NULL) || (buf == NULL))
        return;

    if (buf->error)
        return;
234 235 236

    if (buf->size == 0 &&
        virBufferGrow(buf, 100) < 0)
237
        return;
238

D
Daniel Veillard 已提交
239 240 241 242 243 244 245
    size = buf->size - buf->use - 1;
    va_start(argptr, format);
    va_copy(locarg, argptr);
    while (((count = vsnprintf(&buf->content[buf->use], size, format,
                               locarg)) < 0) || (count >= size - 1)) {
        buf->content[buf->use] = 0;
        va_end(locarg);
246

247
        grow_size = (count > 1000) ? count : 1000;
248 249 250
        if (virBufferGrow(buf, grow_size) < 0)
            return;

D
Daniel Veillard 已提交
251 252 253 254 255
        size = buf->size - buf->use - 1;
        va_copy(locarg, argptr);
    }
    va_end(locarg);
    buf->use += count;
256
    buf->content[buf->use] = '\0';
D
Daniel Veillard 已提交
257 258
}

259 260 261 262 263 264 265 266 267
/**
 * 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.
 */
268 269
void
virBufferEscapeString(const virBufferPtr buf, const char *format, const char *str)
270
{
271
    int size, count, len, grow_size;
272 273 274
    char *escaped, *out;
    const char *cur;

275 276 277 278 279
    if ((format == NULL) || (buf == NULL) || (str == NULL))
        return;

    if (buf->error)
        return;
280 281

    len = strlen(str);
282
    if (VIR_ALLOC_N(escaped, 6 * len + 1) < 0) {
283 284
        virBufferNoMemory(buf);
        return;
285
    }
286

287 288 289 290
    cur = str;
    out = escaped;
    while (*cur != 0) {
        if (*cur == '<') {
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
            *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++ = ';';
306 307 308 309 310 311 312 313 314 315 316 317 318 319
        } 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++ = ';';
320
        } else if (((unsigned char)*cur >= 0x20) || (*cur == '\n') || (*cur == '\t') ||
321 322 323 324 325 326 327 328 329 330
                   (*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++;
331 332 333
    }
    *out = 0;

L
Laine Stump 已提交
334 335 336 337 338 339
    if ((buf->use >= buf->size) &&
        virBufferGrow(buf, 100) < 0) {
        VIR_FREE(escaped);
        return;
    }

340 341 342 343
    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;
344 345
        grow_size = (count > 1000) ? count : 1000;
        if (virBufferGrow(buf, grow_size) < 0) {
346
            VIR_FREE(escaped);
347
            return;
348 349 350 351
        }
        size = buf->size - buf->use - 1;
    }
    buf->use += count;
352
    buf->content[buf->use] = '\0';
353
    VIR_FREE(escaped);
354 355
}

356 357 358 359 360 361 362 363 364
/**
 * 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).
 */
365
void
366 367 368 369 370 371 372
virBufferURIEncodeString (virBufferPtr buf, const char *str)
{
    int grow_size = 0;
    const char *p;
    unsigned char uc;
    const char *hex = "0123456789abcdef";

373 374 375 376 377 378
    if ((buf == NULL) || (str == NULL))
        return;

    if (buf->error)
        return;

379
    for (p = str; *p; ++p) {
J
Jim Meyering 已提交
380
        if (c_isalnum(*p))
381 382 383 384 385
            grow_size++;
        else
            grow_size += 3; /* %ab */
    }

386 387
    if (virBufferGrow (buf, grow_size) < 0)
        return;
388 389

    for (p = str; *p; ++p) {
J
Jim Meyering 已提交
390
        if (c_isalnum(*p))
391 392 393 394 395 396 397 398 399 400 401 402
            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';
}

D
Daniel Veillard 已提交
403
/**
D
Daniel P. Berrange 已提交
404
 * virBufferStrcat:
D
Daniel Veillard 已提交
405
 * @buf:  the buffer to dump
406
 * @...:  the variable list of strings, the last argument must be NULL
D
Daniel Veillard 已提交
407 408 409
 *
 * Concatenate strings to an XML buffer.
 */
410
void
D
Daniel P. Berrange 已提交
411
virBufferStrcat(virBufferPtr buf, ...)
D
Daniel Veillard 已提交
412 413 414 415
{
    va_list ap;
    char *str;

416 417 418
    if (buf->error)
        return;

D
Daniel Veillard 已提交
419 420 421 422 423 424 425
    va_start(ap, buf);

    while ((str = va_arg(ap, char *)) != NULL) {
        unsigned int len = strlen(str);
        unsigned int needSize = buf->use + len + 2;

        if (needSize > buf->size) {
426
            if (virBufferGrow(buf, needSize - buf->use) < 0)
427
                break;
D
Daniel Veillard 已提交
428 429 430 431 432 433 434
        }
        memcpy(&buf->content[buf->use], str, len);
        buf->use += len;
        buf->content[buf->use] = 0;
    }
    va_end(ap);
}