mirror of
https://github.com/openssl/openssl.git
synced 2024-11-21 01:15:20 +08:00
crypto: factorize to hex chars conversion code.
Reviewed-by: Tom Cosgrove <tom.cosgrove@arm.com> Reviewed-by: Tomas Mraz <tomas@openssl.org> (Merged from https://github.com/openssl/openssl/pull/24968)
This commit is contained in:
parent
668fdb593a
commit
ca3c6f3829
@ -234,15 +234,14 @@ static int do_buf(unsigned char *buf, int buflen,
|
||||
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++;
|
||||
|
@ -16,7 +16,6 @@
|
||||
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)
|
||||
@ -39,8 +38,7 @@ int i2a_ASN1_INTEGER(BIO *bp, const ASN1_INTEGER *a)
|
||||
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;
|
||||
|
@ -16,7 +16,6 @@
|
||||
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)
|
||||
@ -33,8 +32,7 @@ int i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *a, int type)
|
||||
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;
|
||||
|
@ -11,8 +11,6 @@
|
||||
#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)
|
||||
{
|
||||
@ -33,8 +31,7 @@ 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;
|
||||
}
|
||||
}
|
||||
|
@ -9,18 +9,17 @@
|
||||
|
||||
#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);
|
||||
|
||||
@ -28,21 +27,16 @@ char *EC_POINT_point2hex(const EC_GROUP *group,
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -14,6 +14,7 @@
|
||||
#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'
|
||||
@ -286,12 +287,9 @@ static int buf2hexstr_sep(char *str, size_t str_n, size_t *strlength,
|
||||
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;
|
||||
@ -306,9 +304,8 @@ static int buf2hexstr_sep(char *str, size_t str_n, size_t *strlength,
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
@ -428,3 +425,10 @@ int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n)
|
||||
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);
|
||||
}
|
||||
|
@ -32,7 +32,6 @@ char *X509_NAME_oneline(const X509_NAME *a, char *buf, int len)
|
||||
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
|
||||
@ -147,8 +146,7 @@ char *X509_NAME_oneline(const X509_NAME *a, char *buf, int len)
|
||||
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++) = '\\';
|
||||
@ -159,8 +157,7 @@ char *X509_NAME_oneline(const X509_NAME *a, char *buf, int len)
|
||||
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++) = '\\';
|
||||
|
@ -162,6 +162,12 @@ char *ossl_buf2hexstr_sep(const unsigned char *buf, long buflen, char sep);
|
||||
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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user