/*
* armor.c - Routines to (de)armor OpenPGP packet streams.
*
- * Jonathan McDowell <noodles@earth.li>
+ * Copyright 2002-2004, 2011 Jonathan McDowell <noodles@earth.li>
*
- * Copyright 2002 Project Purple
+ * 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.
*
- * $Id: armor.c,v 1.5 2003/06/04 20:57:06 noodles Exp $
+ * 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.
+ *
+ * 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 <assert.h>
#include <stdlib.h>
#include "armor.h"
#include "keystructs.h"
+#include "log.h"
#include "onak-conf.h"
#include "parsekey.h"
+#include "version.h"
#define ARMOR_WIDTH 64
} else if (c == 63) {
c = '/';
} else {
- assert(c < 64);
+ log_assert(c < 64);
}
return c;
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;
};
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);
unsigned char t;
int i;
- assert(ctx != NULL);
+ log_assert(ctx != NULL);
state = (struct armor_context *) ctx;
switch (state->curoctet++) {
}
-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;
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;
};
static void dearmor_finish(struct dearmor_context *state)
{
- // Check the checksum,
+ /*
+ * Check the checksum
+ */
state->crc24 &= 0xffffffL;
-// state->putchar_func(state->ctx, '\n');
-// state->putchar_func(state->ctx, '=');
-// state->putchar_func(state->ctx, encode64(state->crc24 >> 18));
-// state->putchar_func(state->ctx, encode64((state->crc24 >> 12) & 0x3F));
-// state->putchar_func(state->ctx, encode64((state->crc24 >> 6) & 0x3F));
-// state->putchar_func(state->ctx, encode64(state->crc24 & 0x3F));
-
+ /*
+ state->putchar_func(state->ctx, '\n');
+ state->putchar_func(state->ctx, '=');
+ state->putchar_func(state->ctx, encode64(state->crc24 >> 18));
+ state->putchar_func(state->ctx, encode64((state->crc24 >> 12) & 0x3F));
+ state->putchar_func(state->ctx, encode64((state->crc24 >> 6) & 0x3F));
+ state->putchar_func(state->ctx, encode64(state->crc24 & 0x3F));
+ */
}
unsigned char tmpc;
int i;
- assert(ctx != NULL);
+ log_assert(ctx != NULL);
state = (struct dearmor_context *) ctx;
*c = 0;
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;
* 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)
{
*/
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;
* 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)
{
dearmor_init(&dearmor_ctx);
dearmor_ctx.getchar_func = getchar_func;
dearmor_ctx.ctx = ctx;
- read_openpgp_stream(dearmor_getchar_c, &dearmor_ctx, packets);
+ read_openpgp_stream(dearmor_getchar_c, &dearmor_ctx,
+ packets, 0);
dearmor_finish(&dearmor_ctx);
- // TODO: Look for armor footer
+ /*
+ * TODO: Look for armor footer
+ */
}
return 0;