"Fossies" - the Fresh Open Source Software Archive

Member "burp-2.3.6/src/yajl/yajl_buf.c" (28 Apr 2019, 2553 Bytes) of package /linux/privat/burp-2.3.6.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "yajl_buf.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (c) 2007-2014, Lloyd Hilaiel <me@lloyd.io>
    3  *
    4  * Permission to use, copy, modify, and/or distribute this software for any
    5  * purpose with or without fee is hereby granted, provided that the above
    6  * copyright notice and this permission notice appear in all copies.
    7  *
    8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
   10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
   11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
   12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
   14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   15  */
   16 
   17 #include "../burp.h"
   18 #include "yajl_buf.h"
   19 
   20 #include <assert.h>
   21 #include <stdlib.h>
   22 #include <string.h>
   23 
   24 #define YAJL_BUF_INIT_SIZE 2048
   25 
   26 struct yajl_buf_t {
   27     size_t len;
   28     size_t used;
   29     unsigned char * data;
   30     yajl_alloc_funcs * alloc;
   31 };
   32 
   33 static
   34 void yajl_buf_ensure_available(yajl_buf buf, size_t want)
   35 {
   36     size_t need;
   37     
   38     assert(buf != NULL);
   39 
   40     /* first call */
   41     if (buf->data == NULL) {
   42         buf->len = YAJL_BUF_INIT_SIZE;
   43         buf->data = (unsigned char *) YA_MALLOC(buf->alloc, buf->len);
   44         buf->data[0] = 0;
   45     }
   46 
   47     need = buf->len;
   48 
   49     while (want >= (need - buf->used)) need <<= 1;
   50 
   51     if (need != buf->len) {
   52         buf->data = (unsigned char *) YA_REALLOC(buf->alloc, buf->data, need);
   53         buf->len = need;
   54     }
   55 }
   56 
   57 yajl_buf yajl_buf_alloc(yajl_alloc_funcs * alloc)
   58 {
   59     yajl_buf b = (yajl_buf)YA_MALLOC(alloc, sizeof(struct yajl_buf_t));
   60     memset((void *) b, 0, sizeof(struct yajl_buf_t));
   61     b->alloc = alloc;
   62     return b;
   63 }
   64 
   65 void yajl_buf_free(yajl_buf buf)
   66 {
   67     assert(buf != NULL);
   68     if (buf->data) YA_FREE(buf->alloc, buf->data);
   69     YA_FREE(buf->alloc, buf);
   70 }
   71 
   72 void yajl_buf_append(yajl_buf buf, const void * data, size_t len)
   73 {
   74     yajl_buf_ensure_available(buf, len);
   75     if (len > 0) {
   76         assert(data != NULL);
   77         memcpy(buf->data + buf->used, data, len);
   78         buf->used += len;
   79         buf->data[buf->used] = 0;
   80     }
   81 }
   82 
   83 void yajl_buf_clear(yajl_buf buf)
   84 {
   85     buf->used = 0;
   86     if (buf->data) buf->data[buf->used] = 0;
   87 }
   88 
   89 const unsigned char * yajl_buf_data(yajl_buf buf)
   90 {
   91     return buf->data;
   92 }
   93 
   94 size_t yajl_buf_len(yajl_buf buf)
   95 {
   96     return buf->used;
   97 }
   98 
   99 void
  100 yajl_buf_truncate(yajl_buf buf, size_t len)
  101 {
  102     assert(len <= buf->used);
  103     buf->used = len;
  104 }