Exemple #1
0
/**
 *  \brief  Append a NUL-terminated string to a string object
 *
 *  Append the NUL-terminated string \p buf to the ::u_string_t object \p s
 *
 *  \param  s   an ::u_string_t object
 *  \param  buf NUL-terminated string that will be appended to \p s
 *  \param  len length of \p buf
 *
 *  \retval  0  on success
 *  \retval ~0  on failure
 */
int u_string_append (u_string_t *s, const char *buf, size_t len)
{
    size_t nsz, min;

    dbg_return_if (s == NULL, ~0);
    dbg_return_if (buf == NULL, ~0);
    nop_return_if (len == 0, 0);    /* nothing to do */

    /* if there's not enough space on s->data alloc a bigger buffer */
    if (s->data_len + len + 1 > s->data_sz)
    {
        min = s->data_len + len + 1; /* min required buffer length */
        nsz = s->data_sz;
        while (nsz <= min)
            nsz += (BLOCK_SIZE << s->shift_cnt++);

        dbg_err_if (u_string_reserve(s, nsz));
    }

    /* append this chunk to the data buffer */
    strncpy(s->data + s->data_len, buf, len);
    s->data_len += len;
    s->data[s->data_len] = '\0';
    
    return 0;
err:
    return ~0;
}
Exemple #2
0
static int gzip_free(codec_t *codec)
{
    codec_gzip_t *iz;
    int err;

    nop_return_if (codec == NULL, 0);
    
    iz = (codec_gzip_t*)codec;
    dbg_err_if((err = iz->opEnd(&iz->zstr)) != Z_OK);
    U_FREE(iz);

    return 0;
err:
    u_dbg("%s", zError(err));
    return ~0;
}
Exemple #3
0
/**
 *  \brief  Enlarge the underlying memory block of the given buffer
 *
 *  Enlarge the buffer data block of the supplied ::u_string_t object \p s 
 *  to (at least) \p size bytes.
 *
 *  \param  s       an ::u_string_t object
 *  \param  size    the requested new size
 *
 *  \retval  0  on success
 *  \retval ~0  on failure
 */
int u_string_reserve (u_string_t *s, size_t size)
{
    char *nbuf = NULL;

    dbg_return_if (s == NULL, ~0);
    nop_return_if (size <= s->data_sz, 0);
   
    /* realloc requested size plus 1 char to store the terminating '\0' */
    nbuf = u_realloc(((s->data == null) ? NULL : s->data), size + 1);
    dbg_err_sif (nbuf == NULL);

    /* zero terminate it */
    nbuf[size] = '\0';

    s->data = (void *) nbuf;
    s->data_sz = size;

    return 0;
err:
    return ~0;
}
Exemple #4
0
/**
 *  \brief  Enlarge the memory allocated to a given buffer
 *
 *  Enlarge the buffer data block to (at least) \p size bytes.
 *
 *  \param  ubuf    an already allocated ::u_buf_t object
 *  \param  size    the requested size in bytes
 *
 *  \retval  0  on success
 *  \retval ~0  on failure
 */
int u_buf_reserve (u_buf_t *ubuf, size_t size)
{
    char *nbuf;

    dbg_err_if (ubuf == NULL);

    nop_return_if (size <= ubuf->size, 0);  /* nothing to do */
   
    /* size plus 1 char to store a '\0' */
    dbg_err_sif ((nbuf = u_realloc(ubuf->data, size + 1)) == NULL);

    /* buffer data will always be zero-terminated (but the len field will not
     * count the last '\0') */
    nbuf[size] = '\0';

    ubuf->data = nbuf;
    ubuf->size = size;

    return 0;
err:
    return ~0;
}