aboutsummaryrefslogtreecommitdiffstats
path: root/ext/openssl/ossl.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/openssl/ossl.c')
-rw-r--r--ext/openssl/ossl.c75
1 files changed, 37 insertions, 38 deletions
diff --git a/ext/openssl/ossl.c b/ext/openssl/ossl.c
index 71ef09bc9f..ef03765be1 100644
--- a/ext/openssl/ossl.c
+++ b/ext/openssl/ossl.c
@@ -11,40 +11,6 @@
#include <stdarg.h> /* for ossl_raise */
/*
- * String to HEXString conversion
- */
-int
-string2hex(const unsigned char *buf, int buf_len, char **hexbuf, int *hexbuf_len)
-{
- static const char hex[]="0123456789abcdef";
- int i, len;
-
- if (buf_len < 0 || buf_len > INT_MAX / 2) { /* PARANOIA? */
- return -1;
- }
- len = 2 * buf_len;
- if (!hexbuf) { /* if no buf, return calculated len */
- if (hexbuf_len) {
- *hexbuf_len = len;
- }
- return len;
- }
- if (!(*hexbuf = OPENSSL_malloc(len + 1))) {
- return -1;
- }
- for (i = 0; i < buf_len; i++) {
- (*hexbuf)[2 * i] = hex[((unsigned char)buf[i]) >> 4];
- (*hexbuf)[2 * i + 1] = hex[buf[i] & 0x0f];
- }
- (*hexbuf)[2 * i] = '\0';
-
- if (hexbuf_len) {
- *hexbuf_len = len;
- }
- return len;
-}
-
-/*
* Data Conversion
*/
#define OSSL_IMPL_ARY2SK(name, type, expected_class, dup) \
@@ -130,15 +96,21 @@ ossl_str_new(int size)
return rb_str_new(0, size);
}
+static VALUE
+ossl_str_allocate(int size, int *status)
+{
+ /* This is needed because rb_str_new() may raise. In that case, we can't
+ * free buf. */
+ return rb_protect((VALUE(*)_((VALUE)))ossl_str_new, size, &status);
+}
+
VALUE
ossl_buf2str(char *buf, int len)
{
VALUE str;
- int status = 0;
+ int status;
- /* This is needed because rb_str_new() may raise. In that case, we can't
- * free buf. */
- str = rb_protect((VALUE(*)_((VALUE)))ossl_str_new, len, &status);
+ str = ossl_str_allocate(len, &status);
if(!NIL_P(str)) memcpy(RSTRING_PTR(str), buf, len);
OPENSSL_free(buf);
if(status) rb_jump_tag(status);
@@ -146,6 +118,33 @@ ossl_buf2str(char *buf, int len)
return str;
}
+VALUE
+ossl_buf2hexstr(const unsigned char *buf, int len)
+{
+ static const char hex[] = "0123456789abcdef";
+ int i, status;
+ char *p;
+ VALUE str;
+
+ if (len < 0 || len > LONG_MAX / 2)
+ return Qnil;
+
+ str = ossl_str_allocate(len * 2);
+ if (status) {/* nomem */
+ OPENSSL_free(buf);
+ rb_jump_tag(status);
+ }
+
+ for (i = 0, p = RSTRING_PTR(str); i < len; i++) {
+ unsigned char byte = buf[i];
+ p[i * 2] = hex[byte >> 4];
+ p[i * 2 + 1] = hex[byte & 0x0f];
+ }
+
+ OPENSSL_free(buf);
+ return str;
+}
+
/*
* our default PEM callback
*/