diff options
Diffstat (limited to 'common')
| -rw-r--r-- | common/buffer.c | 41 | 
1 files changed, 25 insertions, 16 deletions
diff --git a/common/buffer.c b/common/buffer.c index db23d79..0cb045c 100644 --- a/common/buffer.c +++ b/common/buffer.c @@ -525,12 +525,13 @@ char* ha_bufenc64(ha_buffer_t* buf, const void* source, size_t len)    return buf->_pp;  } -void* ha_bufdec64(ha_buffer_t* buf, const char* src, size_t bytes) +void* ha_bufdec64(ha_buffer_t* buf, const char* src, size_t* bytes)  {    int state = 0;    int ch;    char* pos;    void* ret; +  size_t todo = 0;    size_t done = 0;    ASSERT(buf && src); @@ -540,10 +541,12 @@ void* ha_bufdec64(ha_buffer_t* buf, const char* src, size_t bytes)    BUF_NEW_BLOCK(buf); -  if(bytes == 0) -    bytes = ~0; +  if(!bytes || *bytes == 0) +    todo = ~0; +  else +    todo = *bytes; -  while((ch = *src++) != '\0' && done < bytes) +  while((ch = *src++) != '\0' && done < todo)    {      if(isspace(ch))        /* Skip whitespace anywhere. */        continue; @@ -594,9 +597,11 @@ void* ha_bufdec64(ha_buffer_t* buf, const char* src, size_t bytes)    /* TODO: Validate ending and return error if invalid somehow */ -  /* If we were asked for a specific amount of bytes, then return null */ -  if(bytes != ~0 && bytes != done) -    return NULL; +  /* We always null terminate anyway */ +  *(buf->_rp++) = 0; + +  if(bytes) +    *bytes = done;    ret = (void*)buf->_pp;    buf->_pp = buf->_rp; @@ -635,23 +640,25 @@ char* ha_bufenchex(ha_buffer_t* buf, const void* source, size_t len)    return buf->_pp;  } -void* ha_bufdechex(ha_buffer_t* buf, const char* src, size_t bytes) +void* ha_bufdechex(ha_buffer_t* buf, const char* src, size_t* bytes)  {    unsigned short j; -  size_t done = 0;    int state = 0;    char* pos;    void* ret; +  size_t done = 0; +  size_t todo = 0;    ASSERT(buf && src); -  if(bytes != 0) +  if(bytes && *bytes != 0)    { -    buffer_bump(buf, bytes + 1); +    buffer_bump(buf, *bytes + 1); +    todo = *bytes;    }    else    { -    bytes = ~0; +    todo = ~0;      buffer_bump(buf, (strlen(src) / 2) + 1);    } @@ -660,7 +667,7 @@ void* ha_bufdechex(ha_buffer_t* buf, const char* src, size_t bytes)    BUF_NEW_BLOCK(buf); -  while(src[0] && done < bytes) +  while(src[0] && done < todo)    {      /* Find the position */      pos = strchr(HEXC, tolower(src[0])); @@ -688,9 +695,11 @@ void* ha_bufdechex(ha_buffer_t* buf, const char* src, size_t bytes)    if(state != 0)      return NULL; -  /* If we were asked for a specific amount of bytes, then return null */ -  if(bytes != ~0 && bytes != done) -    return NULL; +  /* We always null terminate anyway */ +  *(buf->_rp++) = 0; + +  if(bytes) +    *bytes = done;    ret = (void*)buf->_pp;    buf->_pp = buf->_rp;  | 
