X-Git-Url: https://git.sommitrealweird.co.uk/onak.git/blobdiff_plain/ae384229c538b20b429ea640ec30071ae944c0e7..HEAD:/armor.c diff --git a/armor.c b/armor.c index f2e97fc..9b61b1a 100644 --- a/armor.c +++ b/armor.c @@ -1,24 +1,43 @@ -/* - * armor.c - Routines to (de)armor OpenPGP packet streams. +/** + * @file armor.c + * @brief Routines to (de)armor OpenPGP packet streams. + * + * Copyright 2002-2004, 2011 Jonathan McDowell * - * Jonathan McDowell + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; version 2 of the License. * - * Copyright 2002 Project Purple + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. * - * $Id: armor.c,v 1.7 2003/09/30 20:40:10 noodles Exp $ + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include #include #include "armor.h" #include "keystructs.h" #include "onak-conf.h" #include "parsekey.h" +#include "version.h" +/** + * @brief Line length we'll use for armored output + */ #define ARMOR_WIDTH 64 +/** + * @brief CRC24 initialisation value + */ #define CRC24_INIT 0xb704ceL +/** + * @brief CRC24 polynomial value + */ #define CRC24_POLY 0x1864cfbL /** @@ -36,7 +55,7 @@ static unsigned char encode64(unsigned char c) { } else if (c == 63) { c = '/'; } else { - assert(c < 64); + c = '?'; } return c; @@ -66,19 +85,20 @@ static unsigned char decode64(unsigned char c) { } /** - * @lastoctet: The last octet we got. - * @curoctet: The current octet we're expecting (0, 1 or 2). - * @count: The number of octets we've seen. - * @crc24: A running CRC24 of the data we've seen. - * @putchar_func: The function to output a character. - * @ctx: Context for putchar_func. + * @brief Holds the context of an ongoing ASCII armor operation */ struct armor_context { + /** The last octet we got. */ unsigned char lastoctet; + /** The current octet we're expecting (0, 1 or 2). */ int curoctet; + /** The number of octets we've seen. */ int count; + /** A running CRC24 of the data we've seen. */ long crc24; - int (*putchar_func)(void *ctx, size_t count, unsigned char *c); + /** The function to output a character. */ + int (*putchar_func)(void *ctx, size_t count, void *c); + /** Context for putchar_func. */ void *ctx; }; @@ -102,16 +122,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 +164,6 @@ static int armor_putchar_int(void *ctx, unsigned char c) unsigned char t; int i; - assert(ctx != NULL); state = (struct armor_context *) ctx; switch (state->curoctet++) { @@ -173,31 +204,33 @@ 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; + for (i = 0; i < count; i++) { - armor_putchar_int(ctx, c[i]); + armor_putchar_int(ctx, ((char *) c)[i]); } return 0; } /** - * @lastoctet: The last octet we got. - * @curoctet: The current octet we're expecting (0, 1 or 2). - * @count: The number of octets we've seen. - * @crc24: A running CRC24 of the data we've seen. - * @putchar_func: The function to output a character. - * @ctx: Context for putchar_func. + * @brief Holds the context of an ongoing ASCII dearmor operation */ struct dearmor_context { + /** The last octet we got. */ unsigned char lastoctet; + /** The current octet we're expecting (0, 1 or 2). */ int curoctet; + /** The number of octets we've seen. */ int count; + /** A running CRC24 of the data we've seen. */ long crc24; - int (*getchar_func)(void *ctx, size_t count, unsigned char *c); + /** The function to get the next character. */ + int (*getchar_func)(void *ctx, size_t count, void *c); + /** Context for getchar_func. */ void *ctx; }; @@ -233,7 +266,6 @@ static int dearmor_getchar(void *ctx, unsigned char *c) unsigned char tmpc; int i; - assert(ctx != NULL); state = (struct dearmor_context *) ctx; *c = 0; @@ -278,28 +310,19 @@ 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; } -/** - * armor_openpgp_stream - Takes a list of OpenPGP packets and armors it. - * @putchar_func: The function to output the next armor character. - * @ctx: The context pointer for putchar_func. - * @packets: The list of packets to output. - * - * This function ASCII armors a list of OpenPGP packets and outputs it - * 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 +333,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; @@ -328,18 +351,8 @@ int armor_openpgp_stream(int (*putchar_func)(void *ctx, size_t count, return 0; } -/** - * dearmor_openpgp_stream - Reads & decodes an ACSII armored OpenPGP msg. - * @getchar_func: The function to get the next character from the stream. - * @ctx: The context pointer for getchar_func. - * @packets: The list of packets. - * - * This function uses getchar_func to read characters from an ASCII - * armored OpenPGP stream and outputs the data as a linked list of - * 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) {