version 1.7, 2016/09/12 01:22:38 |
version 1.8, 2016/11/25 23:22:04 |
|
|
} |
} |
|
|
int |
int |
sshbuf_reserve(struct sshbuf *buf, size_t len, u_char **dpp) |
sshbuf_allocate(struct sshbuf *buf, size_t len) |
{ |
{ |
size_t rlen, need; |
size_t rlen, need; |
u_char *dp; |
u_char *dp; |
int r; |
int r; |
|
|
if (dpp != NULL) |
SSHBUF_DBG(("allocate buf = %p len = %zu", buf, len)); |
*dpp = NULL; |
|
|
|
SSHBUF_DBG(("reserve buf = %p len = %zu", buf, len)); |
|
if ((r = sshbuf_check_reserve(buf, len)) != 0) |
if ((r = sshbuf_check_reserve(buf, len)) != 0) |
return r; |
return r; |
/* |
/* |
|
|
* then pack the buffer, zeroing buf->off. |
* then pack the buffer, zeroing buf->off. |
*/ |
*/ |
sshbuf_maybe_pack(buf, buf->size + len > buf->max_size); |
sshbuf_maybe_pack(buf, buf->size + len > buf->max_size); |
SSHBUF_TELL("reserve"); |
SSHBUF_TELL("allocate"); |
if (len + buf->size > buf->alloc) { |
if (len + buf->size <= buf->alloc) |
/* |
return 0; /* already have it. */ |
* Prefer to alloc in SSHBUF_SIZE_INC units, but |
|
* allocate less if doing so would overflow max_size. |
/* |
*/ |
* Prefer to alloc in SSHBUF_SIZE_INC units, but |
need = len + buf->size - buf->alloc; |
* allocate less if doing so would overflow max_size. |
rlen = ROUNDUP(buf->alloc + need, SSHBUF_SIZE_INC); |
*/ |
SSHBUF_DBG(("need %zu initial rlen %zu", need, rlen)); |
need = len + buf->size - buf->alloc; |
if (rlen > buf->max_size) |
rlen = ROUNDUP(buf->alloc + need, SSHBUF_SIZE_INC); |
rlen = buf->alloc + need; |
SSHBUF_DBG(("need %zu initial rlen %zu", need, rlen)); |
SSHBUF_DBG(("adjusted rlen %zu", rlen)); |
if (rlen > buf->max_size) |
if ((dp = realloc(buf->d, rlen)) == NULL) { |
rlen = buf->alloc + need; |
SSHBUF_DBG(("realloc fail")); |
SSHBUF_DBG(("adjusted rlen %zu", rlen)); |
if (dpp != NULL) |
if ((dp = realloc(buf->d, rlen)) == NULL) { |
*dpp = NULL; |
SSHBUF_DBG(("realloc fail")); |
return SSH_ERR_ALLOC_FAIL; |
return SSH_ERR_ALLOC_FAIL; |
} |
|
buf->alloc = rlen; |
|
buf->cd = buf->d = dp; |
|
if ((r = sshbuf_check_reserve(buf, len)) < 0) { |
|
/* shouldn't fail */ |
|
if (dpp != NULL) |
|
*dpp = NULL; |
|
return r; |
|
} |
|
} |
} |
|
buf->alloc = rlen; |
|
buf->cd = buf->d = dp; |
|
if ((r = sshbuf_check_reserve(buf, len)) < 0) { |
|
/* shouldn't fail */ |
|
return r; |
|
} |
|
SSHBUF_TELL("done"); |
|
return 0; |
|
} |
|
|
|
int |
|
sshbuf_reserve(struct sshbuf *buf, size_t len, u_char **dpp) |
|
{ |
|
u_char *dp; |
|
int r; |
|
|
|
if (dpp != NULL) |
|
*dpp = NULL; |
|
|
|
SSHBUF_DBG(("reserve buf = %p len = %zu", buf, len)); |
|
if ((r = sshbuf_allocate(buf, len)) != 0) |
|
return r; |
|
|
dp = buf->d + buf->size; |
dp = buf->d + buf->size; |
buf->size += len; |
buf->size += len; |
SSHBUF_TELL("done"); |
|
if (dpp != NULL) |
if (dpp != NULL) |
*dpp = dp; |
*dpp = dp; |
return 0; |
return 0; |