- fixed a gotcha in http_chunked_decode (-size_t is always > 0)
[m6w6/ext-http] / phpstr / phpstr.c
index 286a190b3669119dd146118a56ced262b18a9ec3..70673a92ed7c4fd4c8b5f737b75442b98a4d73c9 100644 (file)
@@ -4,10 +4,6 @@
 #include "php.h"
 #include "phpstr.h"
 
-#ifndef PHPSTR_DEFAULT_SIZE
-#define PHPSTR_DEFAULT_SIZE 256
-#endif
-
 PHPSTR_API phpstr *phpstr_init_ex(phpstr *buf, size_t chunk_size, zend_bool pre_alloc)
 {
        if (!buf) {
@@ -45,12 +41,13 @@ PHPSTR_API void phpstr_resize_ex(phpstr *buf, size_t len, size_t override_size)
        }
 }
 
-PHPSTR_API void phpstr_append(phpstr *buf, const char *append, size_t append_len)
+PHPSTR_API size_t phpstr_append(phpstr *buf, const char *append, size_t append_len)
 {
        phpstr_resize(buf, append_len);
        memcpy(buf->data + buf->used, append, append_len);
        buf->used += append_len;
        buf->free -= append_len;
+       return append_len;
 }
 
 PHPSTR_API size_t phpstr_appendf(phpstr *buf, const char *format, ...)
@@ -69,13 +66,14 @@ PHPSTR_API size_t phpstr_appendf(phpstr *buf, const char *format, ...)
        return append_len;
 }
 
-PHPSTR_API void phpstr_insert(phpstr *buf, const char *insert, size_t insert_len, size_t offset)
+PHPSTR_API size_t phpstr_insert(phpstr *buf, const char *insert, size_t insert_len, size_t offset)
 {
        phpstr_resize(buf, insert_len);
        memmove(buf->data + offset + insert_len, buf->data + offset, insert_len);
        memcpy(buf->data + offset, insert, insert_len);
        buf->used += insert_len;
        buf->free -= insert_len;
+       return insert_len;
 }
 
 PHPSTR_API size_t phpstr_insertf(phpstr *buf, size_t offset, const char *format, ...)
@@ -94,13 +92,14 @@ PHPSTR_API size_t phpstr_insertf(phpstr *buf, size_t offset, const char *format,
        return insert_len;
 }
 
-PHPSTR_API void phpstr_prepend(phpstr *buf, const char *prepend, size_t prepend_len)
+PHPSTR_API size_t phpstr_prepend(phpstr *buf, const char *prepend, size_t prepend_len)
 {
        phpstr_resize(buf, prepend_len);
        memmove(buf->data + prepend_len, buf->data, buf->used);
        memcpy(buf->data, prepend, prepend_len);
        buf->used += prepend_len;
        buf->free -= prepend_len;
+       return prepend_len;
 }
 
 PHPSTR_API size_t phpstr_prependf(phpstr *buf, const char *format, ...)
@@ -232,19 +231,17 @@ PHPSTR_API int phpstr_cmp(phpstr *left, phpstr *right)
 
 PHPSTR_API void phpstr_dtor(phpstr *buf)
 {
-       if (buf->data) {
-               efree(buf->data);
-               buf->data = NULL;
-       }
+       STR_SET(buf->data, NULL);
        buf->used = 0;
        buf->free = 0;
 }
 
-PHPSTR_API void phpstr_free(phpstr *buf)
+PHPSTR_API void phpstr_free(phpstr **buf)
 {
-       if (buf) {
-               phpstr_dtor(buf);
-               efree(buf);
+       if (*buf) {
+               phpstr_dtor(*buf);
+               efree(*buf);
+               *buf = NULL;
        }
 }