diff --git a/src/m_paper_key/output.cpp b/src/m_paper_key/output.cpp index 0888402..c6265df 100644 --- a/src/m_paper_key/output.cpp +++ b/src/m_paper_key/output.cpp @@ -16,16 +16,14 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include -#include -#include -#include -#include -#ifdef _WIN32 -#include -#include -#endif #include "output.h" + +#include +#include +#include +#include +#include + #include "packets.h" extern unsigned int output_width; @@ -331,14 +329,4 @@ void output_end() { } } -void set_binary_mode(FILE *stream) { -#ifdef _WIN32 - if (_setmode(_fileno(stream), _O_BINARY) == -1) { - fprintf(stderr, "Unable to set stream mode to binary: %s\n", - strerror(errno)); - exit(1); - } -#else - (void)stream; -#endif -} \ No newline at end of file +void set_binary_mode(FILE *stream) { (void)stream; } \ No newline at end of file diff --git a/src/m_paper_key/packets.cpp b/src/m_paper_key/packets.cpp index 5fd1f04..e459b94 100644 --- a/src/m_paper_key/packets.cpp +++ b/src/m_paper_key/packets.cpp @@ -18,9 +18,9 @@ #include "packets.h" -#include -#include -#include +#include +#include +#include #include "GFSDKBasic.h" #include "output.h" diff --git a/src/m_paper_key/paperkey.cpp b/src/m_paper_key/paperkey.cpp index 41cc33b..e8d07b5 100644 --- a/src/m_paper_key/paperkey.cpp +++ b/src/m_paper_key/paperkey.cpp @@ -16,17 +16,6 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include -#include -#include -#include -#include -#include -#include - -#include "extract.h" -#include "output.h" - int verbose = 0, ignore_crc_error = 0; unsigned int output_width = 78; char *comment = nullptr; @@ -46,42 +35,3 @@ enum options { OPT_FILE_FORMAT, OPT_COMMENT }; - -static struct option long_options[] = { - {"help", no_argument, NULL, OPT_HELP}, - {"version", no_argument, NULL, OPT_VERSION}, - {"verbose", no_argument, NULL, OPT_VERBOSE}, - {"output", required_argument, NULL, OPT_OUTPUT}, - {"input-type", required_argument, NULL, OPT_INPUT_TYPE}, - {"output-type", required_argument, NULL, OPT_OUTPUT_TYPE}, - {"output-width", required_argument, NULL, OPT_OUTPUT_WIDTH}, - {"secret-key", required_argument, NULL, OPT_SECRET_KEY}, - {"pubring", required_argument, NULL, OPT_PUBRING}, - {"secrets", required_argument, NULL, OPT_SECRETS}, - {"ignore-crc-error", no_argument, NULL, OPT_IGNORE_CRC_ERROR}, - {"file-format", no_argument, NULL, OPT_FILE_FORMAT}, - {"comment", required_argument, NULL, OPT_COMMENT}, - {NULL, 0, NULL, 0}}; - -static void usage(void) { - printf("Usage: paperkey [OPTIONS]\n"); - printf(" --help (-h)\n"); - printf(" --version (-V)\n"); - printf(" --verbose (-v) be more verbose\n"); - printf(" --output (-o) write output to this file\n"); - printf(" --input-type auto, base16 or raw (binary)\n"); - printf(" --output-type base16 or raw (binary)\n"); - printf(" --output-width maximum width of base16 output\n"); - printf( - " --secret-key" - " extract secret data from this secret key\n"); - printf( - " --pubring" - " public keyring to find non-secret data\n"); - printf( - " --secrets file containing secret" - " data to join with the public key\n"); - printf(" --ignore-crc-error don't reject corrupted input\n"); - printf(" --file-format show the paperkey file format\n"); - printf(" --comment add a comment to the base16 output\n"); -} diff --git a/src/m_paper_key/parse.cpp b/src/m_paper_key/parse.cpp index 32cb704..f3c924e 100644 --- a/src/m_paper_key/parse.cpp +++ b/src/m_paper_key/parse.cpp @@ -19,7 +19,6 @@ #include "parse.h" #include -#include #include #include @@ -32,142 +31,6 @@ extern int verbose; extern int ignore_crc_error; -struct packet *parse2(QDataStream &stream, unsigned char want, - unsigned char stop) { - int byte; - struct packet *packet = NULL; - - while (!stream.atEnd()) { - stream >> byte; - - unsigned char type; - unsigned int length; - - if ((byte & 0x80) != 0) { - int tmp; - - type = byte & 0x3F; - - /* Old-style packet type */ - if (!(byte & 0x40)) type >>= 2; - - if (type == stop) { - stream << byte; - break; - } - - if (byte & 0x40) { - /* New-style packets */ - stream >> byte; - if (byte == EOF) goto fail; - - if (byte == 255) { - /* 4-byte length */ - stream >> tmp; - if (tmp == EOF) goto fail; - length = tmp << 24; - stream >> tmp; - if (tmp == EOF) goto fail; - length |= tmp << 16; - stream >> tmp; - if (tmp == EOF) goto fail; - length |= tmp << 8; - stream >> tmp; - if (tmp == EOF) goto fail; - length |= tmp; - } else if (byte >= 224) { - /* Partial body length, so fail (keys can't use - partial body) */ - fprintf(stderr, "Invalid partial packet encoding\n"); - goto fail; - } else if (byte >= 192) { - /* 2-byte length */ - stream >> tmp; - if (tmp == EOF) goto fail; - length = ((byte - 192) << 8) + tmp + 192; - } else - length = byte; - } else { - /* Old-style packets */ - switch (byte & 0x03) { - case 0: - /* 1-byte length */ - stream >> byte; - if (byte == EOF) goto fail; - length = byte; - break; - - case 1: - /* 2-byte length */ - stream >> byte; - if (byte == EOF) goto fail; - stream >> tmp; - if (tmp == EOF) goto fail; - length = byte << 8; - length |= tmp; - break; - - case 2: - /* 4-byte length */ - stream >> tmp; - if (tmp == EOF) goto fail; - length = tmp << 24; - stream >> tmp; - if (tmp == EOF) goto fail; - length |= tmp << 16; - stream >> tmp; - if (tmp == EOF) goto fail; - length |= tmp << 8; - stream >> tmp; - if (tmp == EOF) goto fail; - length |= tmp; - break; - - default: - fprintf(stderr, "Error: unable to parse old-style length\n"); - goto fail; - } - } - - if (verbose > 1) - fprintf(stderr, "Found packet of type %d, length %d\n", type, length); - } else { - fprintf(stderr, - "Error: unable to parse OpenPGP packets" - " (is this armored data?)\n"); - goto fail; - } - - if (want == 0 || type == want) { - packet = - static_cast(GFAllocateMemory(sizeof(struct packet))); - packet->type = type; - packet->buf = static_cast(GFAllocateMemory(length)); - packet->len = length; - packet->size = length; - if (stream.readRawData(reinterpret_cast(packet->buf), - packet->len) < packet->len) { - fprintf(stderr, "Short read on packet type %d\n", type); - goto fail; - } - break; - } else { - /* We don't want it, so skip the packet. We don't use fseek - here since the input might be on stdin and that isn't - seekable. */ - - size_t i; - - for (i = 0; i < length; i++) stream >> byte; - } - } - - return packet; - -fail: - return nullptr; -} - struct packet *parse(FILE *input, unsigned char want, unsigned char stop) { int byte; struct packet *packet = NULL; diff --git a/src/m_paper_key/parse.h b/src/m_paper_key/parse.h index 4719772..a57175a 100644 --- a/src/m_paper_key/parse.h +++ b/src/m_paper_key/parse.h @@ -18,16 +18,11 @@ #pragma once -#include - #include "output.h" auto parse(FILE *input, unsigned char want, unsigned char stop) -> struct packet *; -struct packet *parse2(QDataStream &stream, unsigned char want, - unsigned char stop); - auto calculate_fingerprint(struct packet *packet, size_t public_len, unsigned char fingerprint[20]) -> int;