static int do_hex_dump(char_io *io_ch, void *arg, unsigned char *buf,
int buflen)
{
- static const char hexdig[] = "0123456789ABCDEF";
unsigned char *p, *q;
char hextmp[2];
+
if (arg) {
p = buf;
q = buf + buflen;
while (p != q) {
- hextmp[0] = hexdig[*p >> 4];
- hextmp[1] = hexdig[*p & 0xf];
+ ossl_to_hex(hextmp, *p);
if (!io_ch(arg, hextmp, 2))
return -1;
p++;
int i2a_ASN1_INTEGER(BIO *bp, const ASN1_INTEGER *a)
{
int i, n = 0;
- static const char *h = "0123456789ABCDEF";
char buf[2];
if (a == NULL)
goto err;
n += 2;
}
- buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f];
- buf[1] = h[((unsigned char)a->data[i]) & 0x0f];
+ ossl_to_hex(buf, a->data[i]);
if (BIO_write(bp, buf, 2) != 2)
goto err;
n += 2;
int i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *a, int type)
{
int i, n = 0;
- static const char *h = "0123456789ABCDEF";
char buf[2];
if (a == NULL)
goto err;
n += 2;
}
- buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f];
- buf[1] = h[((unsigned char)a->data[i]) & 0x0f];
+ ossl_to_hex(buf, a->data[i]);
if (BIO_write(bp, buf, 2) != 2)
goto err;
n += 2;
#include "crypto/ctype.h"
#include "bn_local.h"
-static const char Hex[] = "0123456789ABCDEF";
-
/* Must 'OPENSSL_free' the returned data */
char *BN_bn2hex(const BIGNUM *a)
{
/* strip leading zeros */
v = (int)((a->d[i] >> j) & 0xff);
if (z || v != 0) {
- *p++ = Hex[v >> 4];
- *p++ = Hex[v & 0x0f];
+ p += ossl_to_hex(p, v);
z = 1;
}
}
#include <string.h> /* strlen */
#include <openssl/crypto.h>
+#include "internal/cryptlib.h"
#include "ec_local.h"
-static const char *HEX_DIGITS = "0123456789ABCDEF";
-
/* the return value must be freed (using OPENSSL_free()) */
char *EC_POINT_point2hex(const EC_GROUP *group,
const EC_POINT *point,
point_conversion_form_t form, BN_CTX *ctx)
{
char *ret, *p;
- size_t buf_len = 0, i;
- unsigned char *buf = NULL, *pbuf;
+ size_t buf_len, i;
+ unsigned char *buf = NULL;
buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
return NULL;
ret = OPENSSL_malloc(buf_len * 2 + 2);
- if (ret == NULL) {
- OPENSSL_free(buf);
- return NULL;
- }
+ if (ret == NULL)
+ goto err;
+
p = ret;
- pbuf = buf;
- for (i = buf_len; i > 0; i--) {
- int v = (int)*(pbuf++);
- *(p++) = HEX_DIGITS[v >> 4];
- *(p++) = HEX_DIGITS[v & 0x0F];
- }
+ for (i = 0; i < buf_len; ++i)
+ p += ossl_to_hex(p, buf[i]);
*p = '\0';
+ err:
OPENSSL_free(buf);
-
return ret;
}
#include "crypto/ctype.h"
#include "internal/cryptlib.h"
#include "internal/thread_once.h"
+#include "internal/to_hex.h"
#define DEFAULT_SEPARATOR ':'
#define CH_ZERO '\0'
const unsigned char *buf, size_t buflen,
const char sep)
{
- static const char hexdig[] = "0123456789ABCDEF";
- const unsigned char *p;
char *q;
- size_t i;
int has_sep = (sep != CH_ZERO);
- size_t len = has_sep ? buflen * 3 : 1 + buflen * 2;
+ size_t i, len = has_sep ? buflen * 3 : 1 + buflen * 2;
if (len == 0)
++len;
}
q = str;
- for (i = 0, p = buf; i < buflen; i++, p++) {
- *q++ = hexdig[(*p >> 4) & 0xf];
- *q++ = hexdig[*p & 0xf];
+ for (i = 0; i < buflen; i++) {
+ q += ossl_to_hex(q, buf[i]);
if (has_sep)
*q++ = sep;
}
return 0;
return 0;
}
+
+size_t ossl_to_hex(char *buf, uint8_t n)
+{
+ static const char hexdig[] = "0123456789ABCDEF";
+
+ return to_hex(buf, n, hexdig);
+}
char *p;
unsigned char *q;
BUF_MEM *b = NULL;
- static const char hex[17] = "0123456789ABCDEF";
int gs_doit[4];
char tmp_buf[80];
#ifdef CHARSET_EBCDIC
if ((n < ' ') || (n > '~')) {
*(p++) = '\\';
*(p++) = 'x';
- *(p++) = hex[(n >> 4) & 0x0f];
- *(p++) = hex[n & 0x0f];
+ p += ossl_to_hex(p, n);
} else {
if (n == '/' || n == '+')
*(p++) = '\\';
if ((n < os_toascii[' ']) || (n > os_toascii['~'])) {
*(p++) = '\\';
*(p++) = 'x';
- *(p++) = hex[(n >> 4) & 0x0f];
- *(p++) = hex[n & 0x0f];
+ p += ossl_to_hex(p, n);
} else {
if (n == os_toascii['/'] || n == os_toascii['+'])
*(p++) = '\\';
unsigned char *ossl_hexstr2buf_sep(const char *str, long *buflen,
const char sep);
+/**
+ * Writes |n| value in hex format into |buf|,
+ * and returns the number of bytes written
+ */
+size_t ossl_to_hex(char *buf, uint8_t n);
+
STACK_OF(SSL_COMP) *ossl_load_builtin_compressions(void);
void ossl_free_compression_methods_int(STACK_OF(SSL_COMP) *methods);