Skip to content

Commit

Permalink
use temporary buffer during decode
Browse files Browse the repository at this point in the history
  • Loading branch information
stesla committed Apr 1, 2011
1 parent 437de32 commit df7ac88
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions ext/ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,25 @@ b32_decode (VALUE self, VALUE value)
if (RSTRING_LEN (value) == 0)
return value;

VALUE result = rb_str_new (0, base32_decode_buffer_size (RSTRING_LEN (value)));
size_t buflen = base32_decode_buffer_size (RSTRING_LEN (value));
char *buffer = (char *) malloc (buflen);
#ifdef TEST
memset(RSTRING_PTR (result), 0xff, RSTRING_LEN (result));
memset(buffer, 0xff, buflen);
#else
memset(buffer, 0x00, buflen);
#endif
size_t length = base32_decode ((uint8_t *) RSTRING_PTR (result), RSTRING_LEN (result),

size_t length = base32_decode ((uint8_t *) buffer, buflen,
(uint8_t *) RSTRING_PTR (value), RSTRING_LEN (value));
if (length == 0)

if (length == 0) {
free(buffer);
rb_raise(rb_eRuntimeError, "Value provided not base32 encoded");
}

length = RSTRING_LEN(result);
VALUE result = rb_str_new (0, length);
memcpy(RSTRING_PTR (result), buffer, length);
free(buffer);
return result;
}

Expand Down

0 comments on commit df7ac88

Please sign in to comment.