Browse Source

Merge branch 'develop'

tags/v0.2.2^0
Damien Goutte-Gattat 7 years ago
parent
commit
811608c043
11 changed files with 438 additions and 57 deletions
  1. +4
    -0
      NEWS
  2. +9
    -0
      adfgvx.1.in
  3. +1
    -1
      configure.ac
  4. +26
    -17
      po/adfgvx.pot
  5. +33
    -22
      po/fr.po
  6. +2
    -1
      src/Makefile.am
  7. +102
    -9
      src/adfgvx.c
  8. +16
    -7
      src/cipher.c
  9. +6
    -0
      src/cipher.h
  10. +189
    -0
      src/encoding.c
  11. +50
    -0
      src/encoding.h

+ 4
- 0
NEWS View File

@@ -1,3 +1,7 @@
Changes in adfgvx 0.2.2

* Add option --binary.

Changes in adfgvx 0.2.1

* Add internationalization support.


+ 9
- 0
adfgvx.1.in View File

@@ -16,6 +16,7 @@ adfgvx \- ADFGVX cipher
.RB [ \-p | --pad64 ]
.RB [ \-s | --space ]
.RB [ \-g | --genkey ]
.RB [ \-b | --binary ]
.I file
.YS

@@ -23,6 +24,8 @@ adfgvx \- ADFGVX cipher
.PP
.B adfgvx
encrypts or decrypts a given file using the ADFGVX cipher.
If \fIfile\fR is \*(Aq-\*(Aq or is not specified, read from
standard input.

.SH OPTIONS
.TP
@@ -54,6 +57,12 @@ When encrypting, insert spaces between groups of five letters.
.TP
.BR -g ", " --genkey
Generate a new random key.
.TP
.BR -b ", " --binary
When encrypting, first encode the input in Base32 (if using an
output alphabet of 6 characters) or Base64 (if using an output
alphabet of 8 characters); when decrypting, decode the retrieved
cleartext from base32 or Base64.

.SH REPORTING BUGS
.PP


+ 1
- 1
configure.ac View File

@@ -1,6 +1,6 @@
dnl Configure template for the adfgvx package

AC_INIT([adfgvx], [0.2.1], [dgouttegattat@incenp.org])
AC_INIT([adfgvx], [0.2.2], [dgouttegattat@incenp.org])
AC_CONFIG_SRCDIR([configure.ac])
AC_CONFIG_AUX_DIR([config])
AC_CONFIG_HEADERS([config.h])


+ 26
- 17
po/adfgvx.pot View File

@@ -6,9 +6,9 @@
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: adfgvx 0.2.1\n"
"Project-Id-Version: adfgvx 0.2.2\n"
"Report-Msgid-Bugs-To: dgouttegattat@incenp.org\n"
"POT-Creation-Date: 2013-03-05 22:04+0100\n"
"POT-Creation-Date: 2013-03-09 23:37+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@@ -17,37 +17,46 @@ msgstr ""
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"

#: src/adfgvx.c:51
#: src/adfgvx.c:56
#, c-format
msgid ""
"Usage: %s [options] [FILE]\n"
"\n"
msgstr ""

#: src/adfgvx.c:56
#: src/adfgvx.c:61
msgid ""
"Options:\n"
" -h, --help Display this help message.\n"
" -v, --version Display the version message.\n"
msgstr ""

#: src/adfgvx.c:61
#: src/adfgvx.c:66
msgid ""
" -d, --decrypt Decrypt the given file (default action).\n"
" -d, --decrypt Decrypt the fiven file (default action).\n"
" -e, --encrypt Encrypt the given file.\n"
" -g, --genkey Generate a random key.\n"
msgstr ""

#: src/adfgvx.c:72
msgid ""
" -k, --key FILE Use the key in the specified file.\n"
" -b, --binary Add a Base32/64 layer around the cipher.\n"
msgstr ""

#: src/adfgvx.c:77
msgid ""
" -o, --output FILE Send output to FILE instead of stdout.\n"
" -p, --pad64 Pad output for Base64 compliance.\n"
" -s, --space Insert spaces between pentagrams.\n"
" -g, --genkey Generate a random key.\n"
msgstr ""

#: src/adfgvx.c:71
#: src/adfgvx.c:83
#, c-format
msgid "Report bugs to <%s>.\n"
msgstr ""

#: src/adfgvx.c:81
#: src/adfgvx.c:93
#, c-format
msgid ""
"adfgvx %s\n"
@@ -57,39 +66,39 @@ msgid ""
"See the COPYING file or <http://www.gnu.org/licences/gpl.html>.\n"
msgstr ""

#: src/adfgvx.c:109
#: src/adfgvx.c:121
#, c-format
msgid "cannot load key file %s"
msgstr ""

#: src/adfgvx.c:113
#: src/adfgvx.c:125
#, c-format
msgid "no valid key block in %s"
msgstr ""

#: src/adfgvx.c:185
#: src/adfgvx.c:197
#, c-format
msgid "Output alphabet (6 or 8 characters in A-Z): "
msgstr ""

#: src/adfgvx.c:193
#: src/adfgvx.c:205
msgid "Alphabet should be 6 or 8 characters long"
msgstr ""

#: src/adfgvx.c:198
#: src/adfgvx.c:210
msgid "Alphabet should contain only characters in A-Z"
msgstr ""

#: src/adfgvx.c:290
#: src/adfgvx.c:309
msgid "no valid message block"
msgstr ""

#: src/adfgvx.c:375 src/adfgvx.c:403
#: src/adfgvx.c:453 src/adfgvx.c:485
#, c-format
msgid "cannot open file %s"
msgstr ""

#: src/adfgvx.c:395
#: src/adfgvx.c:477
msgid "no keyfile specified (-k)\n"
msgstr ""



+ 33
- 22
po/fr.po View File

@@ -5,10 +5,10 @@
#
msgid ""
msgstr ""
"Project-Id-Version: adfgvx 0.2.1\n"
"Project-Id-Version: adfgvx 0.2.2\n"
"Report-Msgid-Bugs-To: dgouttegattat@incenp.org\n"
"POT-Creation-Date: 2013-03-05 22:04+0100\n"
"PO-Revision-Date: 2013-05-05 21:04+0100\n"
"POT-Creation-Date: 2013-03-09 23:37+0100\n"
"PO-Revision-Date: 2013-05-09 23:38+0100\n"
"Last-Translator: Damien Goutte-Gattat <dgouttegattat@incenp.org>\n"
"Language-Team: fr\n"
"Language: \n"
@@ -16,7 +16,7 @@ msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: src/adfgvx.c:51
#: src/adfgvx.c:56
#, c-format
msgid ""
"Usage: %s [options] [FILE]\n"
@@ -25,7 +25,7 @@ msgstr ""
"Utilisation : %s [options] [FICHIER]\n"
"\n"

#: src/adfgvx.c:56
#: src/adfgvx.c:61
msgid ""
"Options:\n"
" -h, --help Display this help message.\n"
@@ -35,32 +35,43 @@ msgstr ""
" -h, --help Affiche ce message d’aide.\n"
" -v, --version Affiche les informations de version.\n"

#: src/adfgvx.c:61
#: src/adfgvx.c:66
msgid ""
" -d, --decrypt Decrypt the given file (default action).\n"
" -d, --decrypt Decrypt the fiven file (default action).\n"
" -e, --encrypt Encrypt the given file.\n"
" -k, --key FILE Use the key in the specified file.\n"
" -o, --output FILE Send output to FILE instead of stdout.\n"
" -p, --pad64 Pad output for Base64 compliance.\n"
" -s, --space Insert spaces between pentagrams.\n"
" -g, --genkey Generate a random key.\n"
msgstr ""
" -d, --decrypt Déchiffre le fichier (action par défaut).\n"
" -e, --encrypt Chiffre le fichier.\n"
" -g, --genkey Génère une nouvelle clé aléatoire.\n"

#: src/adfgvx.c:72
msgid ""
" -k, --key FILE Use the key in the specified file.\n"
" -b, --binary Add a Base32/64 layer around the cipher.\n"
msgstr ""
" -k, --key FICHIER Utilise la clé contenue dans le fichier\n"
" spécifié.\n"
" -b, --binary Ajoute une couche d’encodage en Base32\n"
" ou Base64 avant le chiffrement.\n"

#: src/adfgvx.c:77
msgid ""
" -o, --output FILE Send output to FILE instead of stdout.\n"
" -p, --pad64 Pad output for Base64 compliance.\n"
" -s, --space Insert spaces between pentagrams.\n"
msgstr ""
" -o, --output FICHIER Envoie la sortie dans le fichier spécifié.\n"
" -p, --pad64 Produit une sortie compatible avec l’encodage\n"
" Base64.\n"
" -s, --space Sépare les groupes de 5 lettres par des espaces.\n"
" -g, --genkey Génère une nouvelle clé aléatoire.\n"

#: src/adfgvx.c:71
#: src/adfgvx.c:83
#, c-format
msgid "Report bugs to <%s>.\n"
msgstr "Signalez les bugs à <%s>.\n"

#: src/adfgvx.c:81
#: src/adfgvx.c:93
#, c-format
msgid ""
"adfgvx %s\n"
@@ -76,41 +87,41 @@ msgstr ""
"Publique Générale GNU. Consultez le fichier COPYING ou la page\n"
"<http://www.gnu.org/licenses/gpl.html>.\n"

#: src/adfgvx.c:109
#: src/adfgvx.c:121
#, c-format
msgid "cannot load key file %s"
msgstr "ne peut charger la clé contenue dans le fichier %s"

#: src/adfgvx.c:113
#: src/adfgvx.c:125
#, c-format
msgid "no valid key block in %s"
msgstr "aucune clé trouvée dans %s"

#: src/adfgvx.c:185
#: src/adfgvx.c:197
#, c-format
msgid "Output alphabet (6 or 8 characters in A-Z): "
msgstr "Alphabet de sortie (6 ou 8 caractères entre A et Z) : "

#: src/adfgvx.c:193
#: src/adfgvx.c:205
msgid "Alphabet should be 6 or 8 characters long"
msgstr "L’alphabet de sortie doit comprendre 6 ou 8 caractères"

#: src/adfgvx.c:198
#: src/adfgvx.c:210
msgid "Alphabet should contain only characters in A-Z"
msgstr ""
"L’alphabet de sortie ne doit contenir que des caractères\n"
"compris entre A et Z"

#: src/adfgvx.c:290
#: src/adfgvx.c:309
msgid "no valid message block"
msgstr "aucun message chiffré trouvé"

#: src/adfgvx.c:375 src/adfgvx.c:403
#: src/adfgvx.c:453 src/adfgvx.c:485
#, c-format
msgid "cannot open file %s"
msgstr "ne peut ouvrir le fichier %s"

#: src/adfgvx.c:395
#: src/adfgvx.c:477
msgid "no keyfile specified (-k)\n"
msgstr "aucune clé spécifiée (option -k manquante ?)\n"



+ 2
- 1
src/Makefile.am View File

@@ -3,7 +3,8 @@ bin_PROGRAMS = adfgvx
adfgvx_SOURCES = adfgvx.c \
util.c util.h \
reader.c reader.h \
cipher.c cipher.h
cipher.c cipher.h \
encoding.c encoding.h

localedir = $(datadir)/locale
adfgvx_CPPFLAGS = -DLOCALEDIR=\"$(localedir)\" -I$(top_srcdir)/lib


+ 102
- 9
src/adfgvx.c View File

@@ -29,6 +29,7 @@
#include <cipher.h>
#include <reader.h>
#include <util.h>
#include <encoding.h>

/*
@@ -37,9 +38,13 @@

#define PAD_BASE64 0x01
#define SPACE_CIPHERTEXT 0x02
#define WRAP_BINARY 0x04

static unsigned int flags = 0;

void (* wrap_block)(cipher_t *, const char *, size_t);
size_t (*unwrap_block)(const char *, size_t, FILE *);

/*
* Help and informations about the program
@@ -59,13 +64,20 @@ Usage: %s [options] [FILE]\n\
"));

puts(_("\
-d, --decrypt Decrypt the given file (default action).\n\
-d, --decrypt Decrypt the fiven file (default action).\n\
-e, --encrypt Encrypt the given file.\n\
-g, --genkey Generate a random key.\n\
"));

puts(_("\
-k, --key FILE Use the key in the specified file.\n\
-b, --binary Add a Base32/64 layer around the cipher.\n\
"));

puts(_("\
-o, --output FILE Send output to FILE instead of stdout.\n\
-p, --pad64 Pad output for Base64 compliance.\n\
-s, --space Insert spaces between pentagrams.\n\
-g, --genkey Generate a random key.\n\
"));

printf(_("\
@@ -222,6 +234,13 @@ generate_key(void)
#define END_MESSAGE "-----END ADFGVX MESSAGE-----"
#define LINE_WIDTH 60

/*
* Size of the input buffer used in encrypt mode; this value
* must be a multiple of 3 and 5, so that a given block can be
* encoded to Base64 or Base32 independently of the next block.
*/
#define ENCRYPT_BUFFER_SIZE 960

struct outfile
{
FILE *file;
@@ -252,15 +271,15 @@ static void
on_decrypted_block(char *buffer, size_t len, void *data)
{
struct outfile *out = (struct outfile *)data;
fwrite(buffer, 1, len, out->file);
out->pos += len;
out->pos += unwrap_block(buffer, len, out->file);
}

static void
encrypt_file(cipher_key_t *key, FILE *in, FILE *out)
{
cipher_t *cipher;
char buffer[512];
char buffer[ENCRYPT_BUFFER_SIZE];
size_t n;
struct outfile of = { out, 0 };

@@ -268,7 +287,7 @@ encrypt_file(cipher_key_t *key, FILE *in, FILE *out)

cipher = cipher_create(key, on_encrypted_block, (void *)&of);
while ( (n = fread(buffer, 1, sizeof(buffer), in)) > 0 )
cipher_encrypt(cipher, buffer, n);
wrap_block(cipher, buffer, n);
cipher_destroy(cipher);

fprintf(out, "\n" END_MESSAGE "\n");
@@ -304,7 +323,62 @@ decrypt_file(cipher_key_t *key, FILE *in, FILE *out)
i -= 1;
}
}
fputc('\n', out);

if ( (flags & WRAP_BINARY) == 0 )
fputc('\n', out);
}

/*
* Base64/Base32 wrapping
*/

#define wrap_text cipher_encrypt

static void
wrap_base32(cipher_t *cipher, const char *data, size_t len)
{
char buffer[BASE32_ENCODED_SIZE_OF(ENCRYPT_BUFFER_SIZE)];
size_t n;

n = base32_encode(data, len, buffer, sizeof(buffer));
cipher_encrypt(cipher, buffer, n);
}

static void
wrap_base64(cipher_t *cipher, const char *data, size_t len)
{
char buffer[BASE64_ENCODED_SIZE_OF(ENCRYPT_BUFFER_SIZE)];
size_t n;

n = base64_encode(data, len, buffer, sizeof(buffer));
cipher_encrypt(cipher, buffer, n);
}

static size_t
unwrap_text(const char *data, size_t len, FILE *out)
{
return fwrite(data, 1, len, out);
}

static size_t
unwrap_base32(const char *data, size_t len, FILE *out)
{
char buffer[BASE32_DECODED_SIZE_OF(CIPHER_CLEARTEXT_BLOCK_SIZE)];
size_t n;

n = base32_decode(data, len, buffer, sizeof(buffer));
return fwrite(buffer, 1, n, out);
}

static size_t
unwrap_base64(const char *data, size_t len, FILE *out)
{
char buffer[BASE64_DECODED_SIZE_OF(CIPHER_CLEARTEXT_BLOCK_SIZE)];
size_t n;

n = base64_decode(data, len, buffer, sizeof(buffer));
return fwrite(buffer, 1, n, out);
}

@@ -329,6 +403,7 @@ main(int argc, char *argv[])
{ "pad64", 0, NULL, 'p' },
{ "space", 0, NULL, 's' },
{ "genkey", 0, NULL, 'g' },
{ "binary", 0, NULL, 'b' },
{ NULL, 0, NULL, 0 }
};

@@ -340,10 +415,13 @@ main(int argc, char *argv[])

setprogname(argv[0]);
key = NULL;
action = decrypt_file;
srand(time(NULL));

while ( (c = getopt_long(argc, argv, "hvdek:o:psg",
action = decrypt_file;
wrap_block = wrap_text;
unwrap_block = unwrap_text;

while ( (c = getopt_long(argc, argv, "hvdek:o:psgb",
options, NULL)) != -1 ) {
switch ( c ) {
case 'h':
@@ -387,6 +465,10 @@ main(int argc, char *argv[])
generate_key();
exit(EXIT_SUCCESS);
break;

case 'b':
flags |= WRAP_BINARY;
break;
}
}

@@ -403,6 +485,17 @@ main(int argc, char *argv[])
err(EXIT_FAILURE, _("cannot open file %s"), argv[optind]);
}

if ( flags & WRAP_BINARY ) {
if ( cipher_output_alphabet_size(key) == 6 ) {
wrap_block = wrap_base32;
unwrap_block = unwrap_base32;
}
else {
wrap_block = wrap_base64;
unwrap_block = unwrap_base64;
}
}

action(key, stdin, stdout);

cipher_destroy_key(key);


+ 16
- 7
src/cipher.c View File

@@ -24,8 +24,6 @@
#include <string.h>
#include <util.h>

#define BLOCK_SIZE 1024

struct cipher_key_t {
const char *alph;
const char *sub_key;
@@ -94,7 +92,7 @@ cipher_create(cipher_key_t *key,

cipher = xmalloc(sizeof *cipher);
if ( cipher != NULL ) {
cipher->buffer = xmalloc(BLOCK_SIZE);
cipher->buffer = xmalloc(CIPHER_BLOCK_SIZE);
if ( cipher->buffer != NULL ) {
cipher->key = key;
cipher->pos = 0;
@@ -124,6 +122,17 @@ cipher_destroy(cipher_t *cipher)
}

/*
* Accessors
*/

size_t
cipher_output_alphabet_size(cipher_key_t *key)
{
return key->alph_len;
}

/*
* Transposition helper functions
*/
@@ -238,9 +247,9 @@ cipher_encrypt(cipher_t *cipher, const char *text, size_t len)

cipher->mode = 1;

while ( cipher->pos < BLOCK_SIZE && n < len ) {
while ( cipher->pos < CIPHER_BLOCK_SIZE && n < len ) {
substitute(cipher, text[n++]);
if ( cipher->pos == BLOCK_SIZE )
if ( cipher->pos == CIPHER_BLOCK_SIZE )
do_encrypt(cipher);
}
}
@@ -318,7 +327,7 @@ cipher_decrypt(cipher_t *cipher, const char *text, size_t len)

cipher->mode = 0;

while ( cipher->pos < BLOCK_SIZE && n < len ) {
while ( cipher->pos < CIPHER_BLOCK_SIZE && n < len ) {
int i;

for ( i = 0; i < cipher->key->alph_len; i++ )
@@ -326,7 +335,7 @@ cipher_decrypt(cipher_t *cipher, const char *text, size_t len)
cipher->buffer[cipher->pos++] = text[n];
n += 1;

if ( cipher->pos == BLOCK_SIZE )
if ( cipher->pos == CIPHER_BLOCK_SIZE )
do_decrypt(cipher);
}
}

+ 6
- 0
src/cipher.h View File

@@ -21,6 +21,9 @@

#include <stdlib.h>

#define CIPHER_BLOCK_SIZE 768
#define CIPHER_CLEARTEXT_BLOCK_SIZE CIPHER_BLOCK_SIZE / 2

#ifdef __cplusplus
extern "C" {
#endif
@@ -35,6 +38,9 @@ cipher_create_key(const char *, const char *, const char *);
void
cipher_destroy_key(cipher_key_t *);

size_t
cipher_output_alphabet_size(cipher_key_t *);

cipher_t *
cipher_create(cipher_key_t *, cipher_callback_t, void *);



+ 189
- 0
src/encoding.c View File

@@ -0,0 +1,189 @@
/*
* adfgvx - ADFGVX ciphering tool
* Copyright (C) 2013 Damien Goutte-Gattat
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <encoding.h>
#include <stdint.h>
#include <stdio.h>

static char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";

size_t
base64_encode(const char *src, size_t slen, char *dst, size_t dlen)
{
int i, j;

for ( i = j = 0; i < slen && j + 3 < dlen; i += 3 ) {
int32_t val;

val = (unsigned char)src[i] << 16;
if ( i + 1 < slen )
val |= (unsigned char)src[i+1] << 8;
if ( i + 2 < slen )
val |= (unsigned char)src[i+2];

dst[j++] = b64[(val & 0xFC0000) >> 18];
dst[j++] = b64[(val & 0x3F000) >> 12];
dst[j++] = i + 1 < slen ? b64[(val & 0xFC0) >> 6] : '=';
dst[j++] = i + 2 < slen ? b64[ val & 0x3F ] : '=';
}

return j;
}

size_t
base64_decode(const char *src, size_t slen, char *dst, size_t dlen)
{
unsigned char a, b;
int i, j, k;

for ( i = j = k = 0; i < slen && j < dlen; i++ ) {
if ( src[i] >= 'A' && src[i] <= 'Z' )
a = src[i] - 'A';
else if ( src[i] >= 'a' && src[i] <= 'z' )
a = src[i] - 'a' + 26;
else if ( src[i] >= '0' && src[i] <= '9' )
a = src[i] - '0' + 52;
else if ( src[i] == '+' )
a = 62;
else if ( src[i] == '/' )
a = 63;
else if ( src[i] == '=' ) {
k = 0;
continue;
}
else
continue;

switch ( k++ ) {
case 0:
b = a;
break;

case 1:
dst[j++] = (b << 2) | (a >> 4);
b = a;
break;

case 2:
dst[j++] = ((b & 0xF) << 4) | ((a & 0x3C) >> 2);
b = a;
break;

case 3:
dst[j++] = ((b & 0x3) << 6) | (a & 0x3F);
k = 0;
break;
}
}

return j;
}

static char b32[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";

size_t
base32_encode(const char *src, size_t slen, char *dst, size_t dlen)
{
int i, j, k;

for ( i = j = 0; i < slen && j + 7 < dlen; i += 5 ) {
int64_t val;

for ( val = k = 0; k < 5; k++ )
if ( i + k < slen )
val |= (int64_t)((unsigned char)src[i+k]) << ((4 - k) * 8);

dst[j++] = b32[(val & 0xF800000000) >> 35];
dst[j++] = b32[(val & 0x7C0000000) >> 30];
dst[j++] = i + 1 < slen ? b32[(val & 0x3E000000) >> 25] : '=';
dst[j++] = i + 1 < slen ? b32[(val & 0x1F00000) >> 20] : '=';
dst[j++] = i + 2 < slen ? b32[(val & 0xF8000) >> 15] : '=';
dst[j++] = i + 3 < slen ? b32[(val & 0x7C00) >> 10] : '=';
dst[j++] = i + 3 < slen ? b32[(val & 0x3E0) >> 5] : '=';
dst[j++] = i + 4 < slen ? b32[ val & 0x1F ] : '=';
}

return j;
}

size_t
base32_decode(const char *src, size_t slen, char *dst, size_t dlen)
{
unsigned char a, b, c;
int i, j, k;

for ( i = j = k = b = c = 0; i < slen && j < dlen; i++ ) {
if ( src[i] >= 'A' && src[i] <= 'Z' )
a = src[i] - 'A';
else if ( src[i] >= '2' && src[i] <= '7' )
a = src[i] - '2' + 26;
else if ( src[i] == '=' ) {
k = 0;
continue;
}
else
continue;

switch ( k++ ) {
case 0:
b = a;
break;

case 1:
dst[j++] = (b << 3) | (a >> 2);
c = a;
break;

case 2:
b = a;
break;

case 3:
dst[j++] = (c << 6) | (b << 1) | (a >> 4);
b = a;
break;

case 4:
dst[j++] = (b << 4) | (a >> 1);
c = a;
break;

case 5:
b = a;
break;

case 6:
dst[j++] = (c << 7) | (b << 2) | (a >> 3);
b = a;
break;

case 7:
dst[j++] = (b << 5) | a;
k = 0;
break;
}
}

return j;
}

+ 50
- 0
src/encoding.h View File

@@ -0,0 +1,50 @@
/*
* adfgvx - ADFGVX ciphering tool
* Copyright (C) 2013 Damien Goutte-Gattat
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*/

#ifndef ICP20130307_ENCODING_H
#define ICP20130307_ENCODING_H

#include <stdlib.h>

#define BASE64_ENCODED_SIZE_OF(a) ((a) * 8 / 6)
#define BASE64_DECODED_SIZE_OF(a) ((a) * 6 / 8)

#define BASE32_ENCODED_SIZE_OF(a) ((a) * 8 / 5)
#define BASE32_DECODED_SIZE_OF(a) ((a) * 5 / 8)

#ifdef __cpluscplus
extern "C" {
#endif

size_t
base64_encode(const char *, size_t, char *, size_t);

size_t
base64_decode(const char *, size_t, char *, size_t);

size_t
base32_encode(const char *, size_t, char *, size_t);

size_t
base32_decode(const char *, size_t, char *, size_t);

#ifdef __cplusplus
}
#endif

#endif /* !ICP20130307_ENCODING_H */

Loading…
Cancel
Save