X-Git-Url: https://git.sommitrealweird.co.uk/onak.git/blobdiff_plain/ae384229c538b20b429ea640ec30071ae944c0e7..f93d57195dfa072ca6fea0c76428fde5337bcabc:/armor.c diff --git a/armor.c b/armor.c index f2e97fc..397118c 100644 --- a/armor.c +++ b/armor.c @@ -4,17 +4,16 @@ * Jonathan McDowell * * Copyright 2002 Project Purple - * - * $Id: armor.c,v 1.7 2003/09/30 20:40:10 noodles Exp $ */ -#include #include #include "armor.h" #include "keystructs.h" +#include "log.h" #include "onak-conf.h" #include "parsekey.h" +#include "version.h" #define ARMOR_WIDTH 64 @@ -36,7 +35,7 @@ static unsigned char encode64(unsigned char c) { } else if (c == 63) { c = '/'; } else { - assert(c < 64); + log_assert(c < 64); } return c; @@ -78,7 +77,7 @@ struct armor_context { int curoctet; int count; long crc24; - int (*putchar_func)(void *ctx, size_t count, unsigned char *c); + int (*putchar_func)(void *ctx, size_t count, void *c); void *ctx; }; @@ -102,16 +101,28 @@ static void armor_finish(struct armor_context *state) state->putchar_func(state->ctx, 1, &c); state->putchar_func(state->ctx, 1, (unsigned char *) "="); state->putchar_func(state->ctx, 1, (unsigned char *) "="); + state->count += 3; + if ((state->count % ARMOR_WIDTH) == 0) { + state->putchar_func(state->ctx, 1, + (unsigned char *) "\n"); + } break; case 2: c = encode64((state->lastoctet & 0xF) << 2); state->putchar_func(state->ctx, 1, &c); state->putchar_func(state->ctx, 1, (unsigned char *) "="); + state->count += 2; + if ((state->count % ARMOR_WIDTH) == 0) { + state->putchar_func(state->ctx, 1, + (unsigned char *) "\n"); + } break; } state->crc24 &= 0xffffffL; - state->putchar_func(state->ctx, 1, (unsigned char *) "\n"); + if ((state->count % ARMOR_WIDTH) != 0) { + state->putchar_func(state->ctx, 1, (unsigned char *) "\n"); + } state->putchar_func(state->ctx, 1, (unsigned char *) "="); c = encode64(state->crc24 >> 18); state->putchar_func(state->ctx, 1, &c); @@ -132,7 +143,7 @@ static int armor_putchar_int(void *ctx, unsigned char c) unsigned char t; int i; - assert(ctx != NULL); + log_assert(ctx != NULL); state = (struct armor_context *) ctx; switch (state->curoctet++) { @@ -173,12 +184,14 @@ static int armor_putchar_int(void *ctx, unsigned char c) } -static int armor_putchar(void *ctx, size_t count, unsigned char *c) +static int armor_putchar(void *ctx, size_t count, void *c) { int i; + log_assert(c != NULL); + for (i = 0; i < count; i++) { - armor_putchar_int(ctx, c[i]); + armor_putchar_int(ctx, ((char *) c)[i]); } return 0; @@ -197,7 +210,7 @@ struct dearmor_context { int curoctet; int count; long crc24; - int (*getchar_func)(void *ctx, size_t count, unsigned char *c); + int (*getchar_func)(void *ctx, size_t count, void *c); void *ctx; }; @@ -233,7 +246,7 @@ static int dearmor_getchar(void *ctx, unsigned char *c) unsigned char tmpc; int i; - assert(ctx != NULL); + log_assert(ctx != NULL); state = (struct dearmor_context *) ctx; *c = 0; @@ -278,12 +291,12 @@ static int dearmor_getchar(void *ctx, unsigned char *c) return (tmpc == 64); } -static int dearmor_getchar_c(void *ctx, size_t count, unsigned char *c) +static int dearmor_getchar_c(void *ctx, size_t count, void *c) { int i, rc = 0; for (i = 0; i < count && rc == 0; i++) { - rc = dearmor_getchar(ctx, &c[i]); + rc = dearmor_getchar(ctx, &((unsigned char *) c)[i]); } return rc; @@ -299,7 +312,7 @@ static int dearmor_getchar_c(void *ctx, size_t count, unsigned char *c) * using putchar_func. */ int armor_openpgp_stream(int (*putchar_func)(void *ctx, size_t count, - unsigned char *c), + void *c), void *ctx, struct openpgp_packet_list *packets) { @@ -310,8 +323,8 @@ int armor_openpgp_stream(int (*putchar_func)(void *ctx, size_t count, */ putchar_func(ctx, sizeof("-----BEGIN PGP PUBLIC KEY BLOCK-----\n") - 1, (unsigned char *) "-----BEGIN PGP PUBLIC KEY BLOCK-----\n"); - putchar_func(ctx, sizeof("Version: onak " VERSION "\n\n") - 1, - (unsigned char *) "Version: onak " VERSION "\n\n"); + putchar_func(ctx, sizeof("Version: onak " ONAK_VERSION "\n\n") - 1, + (unsigned char *) "Version: onak " ONAK_VERSION "\n\n"); armor_init(&armor_ctx); armor_ctx.putchar_func = putchar_func; @@ -339,7 +352,7 @@ int armor_openpgp_stream(int (*putchar_func)(void *ctx, size_t count, * packets. */ int dearmor_openpgp_stream(int (*getchar_func)(void *ctx, size_t count, - unsigned char *c), + void *c), void *ctx, struct openpgp_packet_list **packets) {