summaryrefslogtreecommitdiff
path: root/lib/libcrypto/asn1/bio_asn1.c
diff options
context:
space:
mode:
authorJoel Sing <jsing@cvs.openbsd.org>2014-04-19 07:21:00 +0000
committerJoel Sing <jsing@cvs.openbsd.org>2014-04-19 07:21:00 +0000
commit258d452e925eea9fbd0a6b764407f6b32bd8b733 (patch)
treebd1a848dce503019d39c6055ae25c9d8f430bd01 /lib/libcrypto/asn1/bio_asn1.c
parent3269778c659c2d9d1683263535bd099ad8a779dd (diff)
More KNF.
Diffstat (limited to 'lib/libcrypto/asn1/bio_asn1.c')
-rw-r--r--lib/libcrypto/asn1/bio_asn1.c152
1 files changed, 80 insertions, 72 deletions
diff --git a/lib/libcrypto/asn1/bio_asn1.c b/lib/libcrypto/asn1/bio_asn1.c
index 8ebac02a00a..36b82758ed3 100644
--- a/lib/libcrypto/asn1/bio_asn1.c
+++ b/lib/libcrypto/asn1/bio_asn1.c
@@ -10,7 +10,7 @@
* are met:
*
* 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
+ * notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
@@ -68,8 +68,7 @@
/* Must be large enough for biggest tag+length */
#define DEFAULT_ASN1_BUF_SIZE 20
-typedef enum
-{
+typedef enum {
ASN1_STATE_START,
ASN1_STATE_PRE_COPY,
ASN1_STATE_HEADER,
@@ -79,14 +78,12 @@ typedef enum
ASN1_STATE_DONE
} asn1_bio_state_t;
-typedef struct BIO_ASN1_EX_FUNCS_st
-{
+typedef struct BIO_ASN1_EX_FUNCS_st {
asn1_ps_func *ex_func;
asn1_ps_func *ex_free_func;
} BIO_ASN1_EX_FUNCS;
-typedef struct BIO_ASN1_BUF_CTX_t
-{
+typedef struct BIO_ASN1_BUF_CTX_t {
/* Internal state */
asn1_bio_state_t state;
/* Internal buffer */
@@ -110,7 +107,7 @@ typedef struct BIO_ASN1_BUF_CTX_t
} BIO_ASN1_BUF_CTX;
-static int asn1_bio_write(BIO *h, const char *buf,int num);
+static int asn1_bio_write(BIO *h, const char *buf, int num);
static int asn1_bio_read(BIO *h, char *buf, int size);
static int asn1_bio_puts(BIO *h, const char *str);
static int asn1_bio_gets(BIO *h, char *str, int size);
@@ -121,14 +118,12 @@ static long asn1_bio_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size);
static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
- asn1_ps_func *cleanup, asn1_bio_state_t next);
+ asn1_ps_func *cleanup, asn1_bio_state_t next);
static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
- asn1_ps_func *setup,
- asn1_bio_state_t ex_state,
- asn1_bio_state_t other_state);
+ asn1_ps_func *setup, asn1_bio_state_t ex_state,
+ asn1_bio_state_t other_state);
-static BIO_METHOD methods_asn1=
-{
+static BIO_METHOD methods_asn1 = {
BIO_TYPE_ASN1,
"asn1",
asn1_bio_write,
@@ -141,13 +136,14 @@ static BIO_METHOD methods_asn1=
asn1_bio_callback_ctrl,
};
-BIO_METHOD *BIO_f_asn1(void)
+BIO_METHOD *
+BIO_f_asn1(void)
{
- return(&methods_asn1);
+ return (&methods_asn1);
}
-
-static int asn1_bio_new(BIO *b)
+static int
+asn1_bio_new(BIO *b)
{
BIO_ASN1_BUF_CTX *ctx;
ctx = malloc(sizeof(BIO_ASN1_BUF_CTX));
@@ -156,14 +152,15 @@ static int asn1_bio_new(BIO *b)
if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE)) {
free(ctx);
return 0;
-}
+ }
b->init = 1;
b->ptr = (char *)ctx;
b->flags = 0;
return 1;
}
-static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
+static int
+asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
{
ctx->buf = malloc(size);
if (!ctx->buf)
@@ -181,9 +178,11 @@ static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
return 1;
}
-static int asn1_bio_free(BIO *b)
+static int
+asn1_bio_free(BIO *b)
{
BIO_ASN1_BUF_CTX *ctx;
+
ctx = (BIO_ASN1_BUF_CTX *) b->ptr;
if (ctx == NULL)
return 0;
@@ -196,11 +195,13 @@ static int asn1_bio_free(BIO *b)
return 1;
}
-static int asn1_bio_write(BIO *b, const char *in , int inl)
+static int
+asn1_bio_write(BIO *b, const char *in , int inl)
{
BIO_ASN1_BUF_CTX *ctx;
int wrmax, wrlen, ret;
unsigned char *p;
+
if (!in || (inl < 0) || (b->next_bio == NULL))
return 0;
ctx = (BIO_ASN1_BUF_CTX *) b->ptr;
@@ -210,42 +211,38 @@ static int asn1_bio_write(BIO *b, const char *in , int inl)
wrlen = 0;
ret = -1;
- for(;;) {
+ for (;;) {
switch (ctx->state) {
/* Setup prefix data, call it */
case ASN1_STATE_START:
if (!asn1_bio_setup_ex(b, ctx, ctx->prefix,
- ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER))
+ ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER))
return 0;
break;
/* Copy any pre data first */
case ASN1_STATE_PRE_COPY:
-
ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free,
- ASN1_STATE_HEADER);
-
+ ASN1_STATE_HEADER);
if (ret <= 0)
goto done;
-
break;
case ASN1_STATE_HEADER:
ctx->buflen =
- ASN1_object_size(0, inl, ctx->asn1_tag) - inl;
+ ASN1_object_size(0, inl, ctx->asn1_tag) - inl;
OPENSSL_assert(ctx->buflen <= ctx->bufsize);
p = ctx->buf;
ASN1_put_object(&p, 0, inl,
- ctx->asn1_tag, ctx->asn1_class);
+ ctx->asn1_tag, ctx->asn1_class);
ctx->copylen = inl;
ctx->state = ASN1_STATE_HEADER_COPY;
-
break;
- case ASN1_STATE_HEADER_COPY:
+ case ASN1_STATE_HEADER_COPY:
ret = BIO_write(b->next_bio,
- ctx->buf + ctx->bufpos, ctx->buflen);
+ ctx->buf + ctx->bufpos, ctx->buflen);
if (ret <= 0)
goto done;
@@ -256,7 +253,6 @@ static int asn1_bio_write(BIO *b, const char *in , int inl)
ctx->bufpos = 0;
ctx->state = ASN1_STATE_DATA_COPY;
}
-
break;
case ASN1_STATE_DATA_COPY:
@@ -275,46 +271,44 @@ static int asn1_bio_write(BIO *b, const char *in , int inl)
if (ctx->copylen == 0)
ctx->state = ASN1_STATE_HEADER;
-
if (inl == 0)
goto done;
-
break;
- default:
+ default:
BIO_clear_retry_flags(b);
return 0;
-
}
}
- done:
+done:
BIO_clear_retry_flags(b);
BIO_copy_next_retry(b);
return (wrlen > 0) ? wrlen : ret;
-
}
-static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
- asn1_ps_func *cleanup, asn1_bio_state_t next)
+static int
+asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, asn1_ps_func *cleanup,
+ asn1_bio_state_t next)
{
int ret;
+
if (ctx->ex_len <= 0)
return 1;
- for(;;) {
+ for (;;) {
ret = BIO_write(b->next_bio, ctx->ex_buf + ctx->ex_pos,
- ctx->ex_len);
+ ctx->ex_len);
if (ret <= 0)
break;
ctx->ex_len -= ret;
if (ctx->ex_len > 0)
ctx->ex_pos += ret;
else {
- if(cleanup)
+ if (cleanup)
cleanup(b, &ctx->ex_buf, &ctx->ex_len,
- &ctx->ex_arg);
+ &ctx->ex_arg);
ctx->state = next;
ctx->ex_pos = 0;
break;
@@ -323,10 +317,9 @@ static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
return ret;
}
-static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
- asn1_ps_func *setup,
- asn1_bio_state_t ex_state,
- asn1_bio_state_t other_state)
+static int
+asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, asn1_ps_func *setup,
+ asn1_bio_state_t ex_state, asn1_bio_state_t other_state)
{
if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg)) {
BIO_clear_retry_flags(b);
@@ -339,45 +332,52 @@ static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
return 1;
}
-static int asn1_bio_read(BIO *b, char *in , int inl)
+static int
+asn1_bio_read(BIO *b, char *in , int inl)
{
if (!b->next_bio)
return 0;
return BIO_read(b->next_bio, in , inl);
}
-static int asn1_bio_puts(BIO *b, const char *str)
+static int
+asn1_bio_puts(BIO *b, const char *str)
{
return asn1_bio_write(b, str, strlen(str));
}
-static int asn1_bio_gets(BIO *b, char *str, int size)
+static int
+asn1_bio_gets(BIO *b, char *str, int size)
{
if (!b->next_bio)
return 0;
return BIO_gets(b->next_bio, str , size);
}
-static long asn1_bio_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
+static long
+asn1_bio_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
{
- if (b->next_bio == NULL) return(0);
- return BIO_callback_ctrl(b->next_bio,cmd,fp);
+ if (b->next_bio == NULL)
+ return (0);
+ return BIO_callback_ctrl(b->next_bio, cmd, fp);
}
-static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
+static long
+asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
{
BIO_ASN1_BUF_CTX *ctx;
BIO_ASN1_EX_FUNCS *ex_func;
long ret = 1;
+
ctx = (BIO_ASN1_BUF_CTX *) b->ptr;
if (ctx == NULL)
return 0;
- switch(cmd) {
+ switch (cmd) {
case BIO_C_SET_PREFIX:
ex_func = arg2;
- ctx->prefix = ex_func->ex_func;
- ctx->prefix_free = ex_func->ex_free_func;
+ ctx->prefix = ex_func->ex_func;
+ ctx->prefix_free = ex_func->ex_free_func;
break;
case BIO_C_GET_PREFIX:
@@ -388,8 +388,8 @@ static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
case BIO_C_SET_SUFFIX:
ex_func = arg2;
- ctx->suffix = ex_func->ex_func;
- ctx->suffix_free = ex_func->ex_free_func;
+ ctx->suffix = ex_func->ex_func;
+ ctx->suffix_free = ex_func->ex_free_func;
break;
case BIO_C_GET_SUFFIX:
@@ -413,13 +413,13 @@ static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
/* Call post function if possible */
if (ctx->state == ASN1_STATE_HEADER) {
if (!asn1_bio_setup_ex(b, ctx, ctx->suffix,
- ASN1_STATE_POST_COPY, ASN1_STATE_DONE))
+ ASN1_STATE_POST_COPY, ASN1_STATE_DONE))
return 0;
}
if (ctx->state == ASN1_STATE_POST_COPY) {
ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free,
- ASN1_STATE_DONE);
+ ASN1_STATE_DONE);
if (ret <= 0)
return ret;
}
@@ -443,20 +443,24 @@ static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
return ret;
}
-static int asn1_bio_set_ex(BIO *b, int cmd,
- asn1_ps_func *ex_func, asn1_ps_func *ex_free_func)
+static int
+asn1_bio_set_ex(BIO *b, int cmd, asn1_ps_func *ex_func, asn1_ps_func
+ *ex_free_func)
{
BIO_ASN1_EX_FUNCS extmp;
+
extmp.ex_func = ex_func;
extmp.ex_free_func = ex_free_func;
return BIO_ctrl(b, cmd, 0, &extmp);
}
-static int asn1_bio_get_ex(BIO *b, int cmd,
- asn1_ps_func **ex_func, asn1_ps_func **ex_free_func)
+static int
+asn1_bio_get_ex(BIO *b, int cmd, asn1_ps_func **ex_func,
+ asn1_ps_func **ex_free_func)
{
BIO_ASN1_EX_FUNCS extmp;
int ret;
+
ret = BIO_ctrl(b, cmd, 0, &extmp);
if (ret > 0) {
*ex_func = extmp.ex_func;
@@ -465,22 +469,26 @@ static int asn1_bio_get_ex(BIO *b, int cmd,
return ret;
}
-int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix, asn1_ps_func *prefix_free)
+int
+BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix, asn1_ps_func *prefix_free)
{
return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free);
}
-int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix, asn1_ps_func **pprefix_free)
+int
+BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix, asn1_ps_func **pprefix_free)
{
return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free);
}
-int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix, asn1_ps_func *suffix_free)
+int
+BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix, asn1_ps_func *suffix_free)
{
return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free);
}
-int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix, asn1_ps_func **psuffix_free)
+int
+BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix, asn1_ps_func **psuffix_free)
{
return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free);
}