From fa4aad5b509946f947d9b2553d09f5f35be24915 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Sun, 14 Aug 2011 20:47:08 -0500 Subject: decode_signature: guess signature data length for efficiency We may end up allocating 1 or 2 extra bytes this way, but it is worth it to simplify the method and not have to call base64_decode() a second time. Use the hueristic that base64 encoding produces 3 bytes of decoded data for every 4 bytes of encoded data. Signed-off-by: Dan McGee --- lib/libalpm/signing.c | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/lib/libalpm/signing.c b/lib/libalpm/signing.c index 6ca32fd5..7521e3ad 100644 --- a/lib/libalpm/signing.c +++ b/lib/libalpm/signing.c @@ -169,22 +169,12 @@ error: */ static int decode_signature(const char *base64_data, unsigned char **data, size_t *data_len) { - unsigned char *usline; - size_t len; - - len = strlen(base64_data); - usline = (unsigned char *)base64_data; - int ret; - size_t destlen = 0; - /* get the necessary size for the buffer by passing 0 */ - ret = base64_decode(NULL, &destlen, usline, len); - if(ret != 0 && ret != POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL) { - goto error; - } - /* alloc our memory and repeat the call to decode */ + size_t len = strlen(base64_data); + unsigned char *usline = (unsigned char *)base64_data; + /* reasonable allocation of expected length is 3/4 of encoded length */ + size_t destlen = len * 3 / 4; MALLOC(*data, destlen, goto error); - ret = base64_decode(*data, &destlen, usline, len); - if(ret != 0) { + if(base64_decode(*data, &destlen, usline, len)) { goto error; } *data_len = destlen; -- cgit v1.2.3-70-g09d2