mirror of https://github.com/openssl/openssl.git
crypto/evp/bio_b64.c: improve coding style
Reviewed-by: Hugo Landau <hlandau@openssl.org> Reviewed-by: Tomas Mraz <tomas@openssl.org> Reviewed-by: Paul Dale <pauli@openssl.org> (Merged from https://github.com/openssl/openssl/pull/18787)
This commit is contained in:
parent
ca6286c382
commit
f95fec2938
|
@ -39,8 +39,8 @@ typedef struct b64_struct {
|
||||||
int start; /* have we started decoding yet? */
|
int start; /* have we started decoding yet? */
|
||||||
int cont; /* <= 0 when finished */
|
int cont; /* <= 0 when finished */
|
||||||
EVP_ENCODE_CTX *base64;
|
EVP_ENCODE_CTX *base64;
|
||||||
char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE) + 10];
|
unsigned char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE) + 10];
|
||||||
char tmp[B64_BLOCK_SIZE];
|
unsigned char tmp[B64_BLOCK_SIZE];
|
||||||
} BIO_B64_CTX;
|
} BIO_B64_CTX;
|
||||||
|
|
||||||
static const BIO_METHOD methods_b64 = {
|
static const BIO_METHOD methods_b64 = {
|
||||||
|
@ -58,7 +58,6 @@ static const BIO_METHOD methods_b64 = {
|
||||||
b64_callback_ctrl,
|
b64_callback_ctrl,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const BIO_METHOD *BIO_f_base64(void)
|
const BIO_METHOD *BIO_f_base64(void)
|
||||||
{
|
{
|
||||||
return &methods_b64;
|
return &methods_b64;
|
||||||
|
@ -90,6 +89,7 @@ static int b64_new(BIO *bi)
|
||||||
static int b64_free(BIO *a)
|
static int b64_free(BIO *a)
|
||||||
{
|
{
|
||||||
BIO_B64_CTX *ctx;
|
BIO_B64_CTX *ctx;
|
||||||
|
|
||||||
if (a == NULL)
|
if (a == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
@ -117,7 +117,7 @@ static int b64_read(BIO *b, char *out, int outl)
|
||||||
ctx = (BIO_B64_CTX *)BIO_get_data(b);
|
ctx = (BIO_B64_CTX *)BIO_get_data(b);
|
||||||
|
|
||||||
next = BIO_next(b);
|
next = BIO_next(b);
|
||||||
if ((ctx == NULL) || (next == NULL))
|
if (ctx == NULL || next == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
BIO_clear_retry_flags(b);
|
BIO_clear_retry_flags(b);
|
||||||
|
@ -185,11 +185,10 @@ static int b64_read(BIO *b, char *out, int outl)
|
||||||
* We need to scan, a line at a time until we have a valid line if we
|
* We need to scan, a line at a time until we have a valid line if we
|
||||||
* are starting.
|
* are starting.
|
||||||
*/
|
*/
|
||||||
if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)) {
|
if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) != 0) {
|
||||||
/* ctx->start=1; */
|
|
||||||
ctx->tmp_len = 0;
|
ctx->tmp_len = 0;
|
||||||
} else if (ctx->start) {
|
} else if (ctx->start) {
|
||||||
q = p = (unsigned char *)ctx->tmp;
|
q = p = ctx->tmp;
|
||||||
num = 0;
|
num = 0;
|
||||||
for (j = 0; j < i; j++) {
|
for (j = 0; j < i; j++) {
|
||||||
if (*(q++) != '\n')
|
if (*(q++) != '\n')
|
||||||
|
@ -206,16 +205,12 @@ static int b64_read(BIO *b, char *out, int outl)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
k = EVP_DecodeUpdate(ctx->base64,
|
k = EVP_DecodeUpdate(ctx->base64, ctx->buf, &num, p, q - p);
|
||||||
(unsigned char *)ctx->buf,
|
if (k <= 0 && num == 0 && ctx->start) {
|
||||||
&num, p, q - p);
|
|
||||||
if ((k <= 0) && (num == 0) && (ctx->start))
|
|
||||||
EVP_DecodeInit(ctx->base64);
|
EVP_DecodeInit(ctx->base64);
|
||||||
else {
|
} else {
|
||||||
if (p != (unsigned char *)
|
if (p != ctx->tmp) {
|
||||||
&(ctx->tmp[0])) {
|
i -= p - ctx->tmp;
|
||||||
i -= (p - (unsigned char *)
|
|
||||||
&(ctx->tmp[0]));
|
|
||||||
for (x = 0; x < i; x++)
|
for (x = 0; x < i; x++)
|
||||||
ctx->tmp[x] = p[x];
|
ctx->tmp[x] = p[x];
|
||||||
}
|
}
|
||||||
|
@ -227,12 +222,12 @@ static int b64_read(BIO *b, char *out, int outl)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* we fell off the end without starting */
|
/* we fell off the end without starting */
|
||||||
if ((j == i) && (num == 0)) {
|
if (j == i && num == 0) {
|
||||||
/*
|
/*
|
||||||
* Is this is one long chunk?, if so, keep on reading until a
|
* Is this is one long chunk?, if so, keep on reading until a
|
||||||
* new line.
|
* new line.
|
||||||
*/
|
*/
|
||||||
if (p == (unsigned char *)&(ctx->tmp[0])) {
|
if (p == ctx->tmp) {
|
||||||
/* Check buffer full */
|
/* Check buffer full */
|
||||||
if (i == B64_BLOCK_SIZE) {
|
if (i == B64_BLOCK_SIZE) {
|
||||||
ctx->tmp_nl = 1;
|
ctx->tmp_nl = 1;
|
||||||
|
@ -249,7 +244,7 @@ static int b64_read(BIO *b, char *out, int outl)
|
||||||
} else {
|
} else {
|
||||||
ctx->tmp_len = 0;
|
ctx->tmp_len = 0;
|
||||||
}
|
}
|
||||||
} else if ((i < B64_BLOCK_SIZE) && (ctx->cont > 0)) {
|
} else if (i < B64_BLOCK_SIZE && ctx->cont > 0) {
|
||||||
/*
|
/*
|
||||||
* If buffer isn't full and we can retry then restart to read in
|
* If buffer isn't full and we can retry then restart to read in
|
||||||
* more data.
|
* more data.
|
||||||
|
@ -257,12 +252,11 @@ static int b64_read(BIO *b, char *out, int outl)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
|
if ((BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) != 0) {
|
||||||
int z, jj;
|
int z, jj;
|
||||||
|
|
||||||
jj = i & ~3; /* process per 4 */
|
jj = i & ~3; /* process per 4 */
|
||||||
z = EVP_DecodeBlock((unsigned char *)ctx->buf,
|
z = EVP_DecodeBlock(ctx->buf, ctx->tmp, jj);
|
||||||
(unsigned char *)ctx->tmp, jj);
|
|
||||||
if (jj > 2) {
|
if (jj > 2) {
|
||||||
if (ctx->tmp[jj - 1] == '=') {
|
if (ctx->tmp[jj - 1] == '=') {
|
||||||
z--;
|
z--;
|
||||||
|
@ -283,9 +277,8 @@ static int b64_read(BIO *b, char *out, int outl)
|
||||||
}
|
}
|
||||||
i = z;
|
i = z;
|
||||||
} else {
|
} else {
|
||||||
i = EVP_DecodeUpdate(ctx->base64,
|
i = EVP_DecodeUpdate(ctx->base64, ctx->buf, &ctx->buf_len,
|
||||||
(unsigned char *)ctx->buf, &ctx->buf_len,
|
ctx->tmp, i);
|
||||||
(unsigned char *)ctx->tmp, i);
|
|
||||||
ctx->tmp_len = 0;
|
ctx->tmp_len = 0;
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
|
@ -320,7 +313,7 @@ static int b64_read(BIO *b, char *out, int outl)
|
||||||
}
|
}
|
||||||
/* BIO_clear_retry_flags(b); */
|
/* BIO_clear_retry_flags(b); */
|
||||||
BIO_copy_next_retry(b);
|
BIO_copy_next_retry(b);
|
||||||
return ((ret == 0) ? ret_code : ret);
|
return ret == 0 ? ret_code : ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int b64_write(BIO *b, const char *in, int inl)
|
static int b64_write(BIO *b, const char *in, int inl)
|
||||||
|
@ -333,7 +326,7 @@ static int b64_write(BIO *b, const char *in, int inl)
|
||||||
|
|
||||||
ctx = (BIO_B64_CTX *)BIO_get_data(b);
|
ctx = (BIO_B64_CTX *)BIO_get_data(b);
|
||||||
next = BIO_next(b);
|
next = BIO_next(b);
|
||||||
if ((ctx == NULL) || (next == NULL))
|
if (ctx == NULL || next == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
BIO_clear_retry_flags(b);
|
BIO_clear_retry_flags(b);
|
||||||
|
@ -366,13 +359,13 @@ static int b64_write(BIO *b, const char *in, int inl)
|
||||||
ctx->buf_off = 0;
|
ctx->buf_off = 0;
|
||||||
ctx->buf_len = 0;
|
ctx->buf_len = 0;
|
||||||
|
|
||||||
if ((in == NULL) || (inl <= 0))
|
if (in == NULL || inl <= 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
while (inl > 0) {
|
while (inl > 0) {
|
||||||
n = (inl > B64_BLOCK_SIZE) ? B64_BLOCK_SIZE : inl;
|
n = inl > B64_BLOCK_SIZE ? B64_BLOCK_SIZE : inl;
|
||||||
|
|
||||||
if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
|
if ((BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) != 0) {
|
||||||
if (ctx->tmp_len > 0) {
|
if (ctx->tmp_len > 0) {
|
||||||
OPENSSL_assert(ctx->tmp_len <= 3);
|
OPENSSL_assert(ctx->tmp_len <= 3);
|
||||||
n = 3 - ctx->tmp_len;
|
n = 3 - ctx->tmp_len;
|
||||||
|
@ -387,8 +380,7 @@ static int b64_write(BIO *b, const char *in, int inl)
|
||||||
if (ctx->tmp_len < 3)
|
if (ctx->tmp_len < 3)
|
||||||
break;
|
break;
|
||||||
ctx->buf_len =
|
ctx->buf_len =
|
||||||
EVP_EncodeBlock((unsigned char *)ctx->buf,
|
EVP_EncodeBlock(ctx->buf, ctx->tmp, ctx->tmp_len);
|
||||||
(unsigned char *)ctx->tmp, ctx->tmp_len);
|
|
||||||
OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
|
OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
|
||||||
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
|
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
|
||||||
/*
|
/*
|
||||||
|
@ -405,17 +397,15 @@ static int b64_write(BIO *b, const char *in, int inl)
|
||||||
}
|
}
|
||||||
n -= n % 3;
|
n -= n % 3;
|
||||||
ctx->buf_len =
|
ctx->buf_len =
|
||||||
EVP_EncodeBlock((unsigned char *)ctx->buf,
|
EVP_EncodeBlock(ctx->buf, (unsigned char *)in, n);
|
||||||
(const unsigned char *)in, n);
|
|
||||||
OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
|
OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
|
||||||
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
|
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
|
||||||
ret += n;
|
ret += n;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!EVP_EncodeUpdate(ctx->base64,
|
if (!EVP_EncodeUpdate(ctx->base64, ctx->buf, &ctx->buf_len,
|
||||||
(unsigned char *)ctx->buf, &ctx->buf_len,
|
(unsigned char *)in, n))
|
||||||
(unsigned char *)in, n))
|
return ret == 0 ? -1 : ret;
|
||||||
return ((ret == 0) ? -1 : ret);
|
|
||||||
OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
|
OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
|
||||||
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
|
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
|
||||||
ret += n;
|
ret += n;
|
||||||
|
@ -429,7 +419,7 @@ static int b64_write(BIO *b, const char *in, int inl)
|
||||||
i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
|
i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
|
||||||
if (i <= 0) {
|
if (i <= 0) {
|
||||||
BIO_copy_next_retry(b);
|
BIO_copy_next_retry(b);
|
||||||
return ((ret == 0) ? i : ret);
|
return ret == 0 ? i : ret;
|
||||||
}
|
}
|
||||||
OPENSSL_assert(i <= n);
|
OPENSSL_assert(i <= n);
|
||||||
n -= i;
|
n -= i;
|
||||||
|
@ -452,7 +442,7 @@ static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
|
||||||
|
|
||||||
ctx = (BIO_B64_CTX *)BIO_get_data(b);
|
ctx = (BIO_B64_CTX *)BIO_get_data(b);
|
||||||
next = BIO_next(b);
|
next = BIO_next(b);
|
||||||
if ((ctx == NULL) || (next == NULL))
|
if (ctx == NULL || next == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
|
@ -471,8 +461,8 @@ static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
|
||||||
case BIO_CTRL_WPENDING: /* More to write in buffer */
|
case BIO_CTRL_WPENDING: /* More to write in buffer */
|
||||||
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
|
OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
|
||||||
ret = ctx->buf_len - ctx->buf_off;
|
ret = ctx->buf_len - ctx->buf_off;
|
||||||
if ((ret == 0) && (ctx->encode != B64_NONE)
|
if (ret == 0 && ctx->encode != B64_NONE
|
||||||
&& (EVP_ENCODE_CTX_num(ctx->base64) != 0))
|
&& EVP_ENCODE_CTX_num(ctx->base64) != 0)
|
||||||
ret = 1;
|
ret = 1;
|
||||||
else if (ret <= 0)
|
else if (ret <= 0)
|
||||||
ret = BIO_ctrl(next, cmd, num, ptr);
|
ret = BIO_ctrl(next, cmd, num, ptr);
|
||||||
|
@ -493,9 +483,8 @@ static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
|
||||||
}
|
}
|
||||||
if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
|
if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
|
||||||
if (ctx->tmp_len != 0) {
|
if (ctx->tmp_len != 0) {
|
||||||
ctx->buf_len = EVP_EncodeBlock((unsigned char *)ctx->buf,
|
ctx->buf_len = EVP_EncodeBlock(ctx->buf,
|
||||||
(unsigned char *)ctx->tmp,
|
ctx->tmp, ctx->tmp_len);
|
||||||
ctx->tmp_len);
|
|
||||||
ctx->buf_off = 0;
|
ctx->buf_off = 0;
|
||||||
ctx->tmp_len = 0;
|
ctx->tmp_len = 0;
|
||||||
goto again;
|
goto again;
|
||||||
|
@ -503,8 +492,7 @@ static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
|
||||||
} else if (ctx->encode != B64_NONE
|
} else if (ctx->encode != B64_NONE
|
||||||
&& EVP_ENCODE_CTX_num(ctx->base64) != 0) {
|
&& EVP_ENCODE_CTX_num(ctx->base64) != 0) {
|
||||||
ctx->buf_off = 0;
|
ctx->buf_off = 0;
|
||||||
EVP_EncodeFinal(ctx->base64,
|
EVP_EncodeFinal(ctx->base64, ctx->buf, &(ctx->buf_len));
|
||||||
(unsigned char *)ctx->buf, &(ctx->buf_len));
|
|
||||||
/* push out the bytes */
|
/* push out the bytes */
|
||||||
goto again;
|
goto again;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue