]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/jansson/strbuffer.c
2 * Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org>
4 * Jansson is free software; you can redistribute it and/or modify
5 * it under the terms of the MIT license. See LICENSE for details.
14 #include "jansson_private.h"
15 #include "strbuffer.h"
17 #define STRBUFFER_MIN_SIZE 16
18 #define STRBUFFER_FACTOR 2
19 #define STRBUFFER_SIZE_MAX ((size_t)-1)
21 int strbuffer_init(strbuffer_t
*strbuff
)
23 strbuff
->size
= STRBUFFER_MIN_SIZE
;
26 strbuff
->value
= jsonp_malloc(strbuff
->size
);
30 /* initialize to empty */
31 strbuff
->value
[0] = '\0';
35 void strbuffer_close(strbuffer_t
*strbuff
)
38 jsonp_free(strbuff
->value
);
42 strbuff
->value
= NULL
;
45 void strbuffer_clear(strbuffer_t
*strbuff
)
48 strbuff
->value
[0] = '\0';
51 const char *strbuffer_value(const strbuffer_t
*strbuff
)
53 return strbuff
->value
;
56 char *strbuffer_steal_value(strbuffer_t
*strbuff
)
58 char *result
= strbuff
->value
;
59 strbuff
->value
= NULL
;
63 int strbuffer_append_byte(strbuffer_t
*strbuff
, char byte
)
65 return strbuffer_append_bytes(strbuff
, &byte
, 1);
68 int strbuffer_append_bytes(strbuffer_t
*strbuff
, const char *data
, size_t size
)
70 if(size
>= strbuff
->size
- strbuff
->length
)
75 /* avoid integer overflow */
76 if (strbuff
->size
> STRBUFFER_SIZE_MAX
/ STRBUFFER_FACTOR
77 || size
> STRBUFFER_SIZE_MAX
- 1
78 || strbuff
->length
> STRBUFFER_SIZE_MAX
- 1 - size
)
81 new_size
= max(strbuff
->size
* STRBUFFER_FACTOR
,
82 strbuff
->length
+ size
+ 1);
84 new_value
= jsonp_malloc(new_size
);
88 memcpy(new_value
, strbuff
->value
, strbuff
->length
);
90 jsonp_free(strbuff
->value
);
91 strbuff
->value
= new_value
;
92 strbuff
->size
= new_size
;
95 memcpy(strbuff
->value
+ strbuff
->length
, data
, size
);
96 strbuff
->length
+= size
;
97 strbuff
->value
[strbuff
->length
] = '\0';
102 char strbuffer_pop(strbuffer_t
*strbuff
)
104 if(strbuff
->length
> 0) {
105 char c
= strbuff
->value
[--strbuff
->length
];
106 strbuff
->value
[strbuff
->length
] = '\0';