Skip to content

Commit

Permalink
Merged release/v1.0.0 into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Agbar committed Oct 9, 2016
2 parents 3cd5af5 + 0542f45 commit b2283eb
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 474 deletions.
144 changes: 0 additions & 144 deletions cipher.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include "ciphertext.h"
#include "global.h"
#include "charmap.h"
Expand Down Expand Up @@ -596,147 +593,6 @@ double dgetic_ALL(const Key *key, int len)

}


/* enciphers or deciphers text read from stdin to file */
void en_deciph_stdin_ALL(FILE *file, const Key *key)
{
char *buf, *cp;
long i, buf_fill, mem, pagesize;
int c;

int ukwnum = key->ukwnum;
int g_slot = key->slot.g;
int l_slot = key->slot.l;
int m_slot = key->slot.m;
int r_slot = key->slot.r;
int g_ring = key->ring.g;
int l_ring = key->ring.l;
int m_ring = key->ring.m;
int r_ring = key->ring.r;
int g_mesg = key->mesg.g;
int l_mesg = key->mesg.l;
int m_mesg = key->mesg.m;
int r_mesg = key->mesg.r;

int g_offset, l_offset, m_offset, r_offset;
int m_turn, r_turn;
int m_turn2 = -1, r_turn2 = -1;
int p2 = 0, p3 = 0;


#ifndef WINDOWS
mem = pagesize = sysconf(_SC_PAGESIZE);
#endif
#ifdef WINDOWS
mem = pagesize = 4096;
#endif

if ((buf = malloc(mem)) == NULL) {
fputs("enigma: error: out of memory.\n", stderr);
exit(EXIT_FAILURE);
}
i = 0;
buf_fill = 2;
while ((c = getchar()) != EOF) {
if (++buf_fill > pagesize) {
buf_fill = 1;
mem += pagesize;
if ((buf = realloc(buf, mem)) == NULL) {
fputs("enigma: error: out of memory.\n", stderr);
exit(EXIT_FAILURE);
}
}
buf[i++] = c;
}
/* in case people use CTRL-D twice */
if (file == stdout)
fputc('\n', file);
if (i > 0)
if (buf[i-1] != '\n')
buf[i++] = '\n';

buf[i] = '\0';


/* calculate effective offset from ring and message settings */
r_offset = (26 + r_mesg-r_ring) % 26;
m_offset = (26 + m_mesg-m_ring) % 26;
l_offset = (26 + l_mesg-l_ring) % 26;
g_offset = (26 + g_mesg-g_ring) % 26;

/* calculate turnover points from ring settings */
r_turn = (26 + wal_turn[r_slot]-r_ring) % 26;
m_turn = (26 + wal_turn[m_slot]-m_ring) % 26;

/* second turnover points for wheels 6,7,8 */
if (r_slot > 5)
r_turn2 = (26 + 25-r_ring) % 26;
if (m_slot > 5)
m_turn2 = (26 + 25-m_ring) % 26;


cp = buf;
while ((c = *cp++) != '\0') {

if (isspace((unsigned char)c)) {
fputc((unsigned char)c, file);
continue;
}
if (code[(unsigned char)c] == 26) {
fputc('\n', file);
fprintf(stderr, "\nenigma: error: use only letters [A-Za-z] or white-space.\n\n");
exit(EXIT_FAILURE);
}

/* determine if pawls are engaged */
if (r_offset == r_turn || r_offset == r_turn2)
p2 = 1;
/* in reality pawl 3 steps both m_wheel and l_wheel */
if (m_offset == m_turn || m_offset == m_turn2) {
p3 = 1;
p2 = 1;
}

r_offset++;
r_offset %= 26;
if (p2) {
m_offset++;
m_offset %= 26;
p2 = 0;
}
if (p3) {
l_offset++;
l_offset %= 26;
p3 = 0;
}

/* thru steckerbrett to scramblers */
c = code[c];
c = key->stbrett.letters[c];

/* thru scramblers and back */
c = wal[r_slot][c+r_offset+26];
c = wal[m_slot][c-r_offset+m_offset+26];
c = wal[l_slot][c-m_offset+l_offset+26];
c = wal[g_slot][c-l_offset+g_offset+26];
c = ukw[ukwnum][c-g_offset+26];
c = rev_wal[g_slot][c+g_offset+26];
c = rev_wal[l_slot][c+l_offset-g_offset+26];
c = rev_wal[m_slot][c+m_offset-l_offset+26];
c = rev_wal[r_slot][c+r_offset-m_offset+26];
c = etw[c-r_offset+26];

/* thru steckerbrett to lamp */
fputc(alpha[key->stbrett.letters[c]], file);

}

if (file == stdout)
fputc('\n', file);
free(buf);

}

extern void Step1( int8_t* ringOffset );

extern void CalculatePermutationMap3Rotors( PermutationMap_t* const restrict map, struct RingsState rings, const Key* const restrict key );
Expand Down
12 changes: 0 additions & 12 deletions cipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ PURE_FUNCTION
int scrambler_state(const Key *key, int len);
PURE_FUNCTION
double dgetic_ALL(const Key *key, int len);
void en_deciph_stdin_ALL(FILE *file, const Key *key);

typedef void (*enigma_prepare_decoder_lookup_function_pt) (const Key *key, int len);

Expand Down Expand Up @@ -53,7 +52,6 @@ extern text_t etw[52];
// path_lookup[Offset][(Index)*(LAST_DIMENSION)+(Cx)];
// is synonyme to
// path_lookup[Offset+Index][(Cx)];
#ifdef INLINE_IS_FAST
inline
text_t decode(size_t offset,size_t index, const PermutationMap_t* const stbrett)
{
Expand All @@ -63,16 +61,6 @@ text_t decode(size_t offset,size_t index, const PermutationMap_t* const stbrett)
c = path_lookup[offset][index*LAST_DIMENSION+c];
return stbrett->letters[c];
}
#else

#error DECODE macro needs refactoring
#define DECODE(Cx,Offset,Index) \
Cx = (&ciphertext.plain[(Offset)])[(Index)]; \
Cx = stbrett[(Cx)]; \
Cx = path_lookup[Offset][(Index)*(LAST_DIMENSION)+(Cx)];\
Cx = stbrett[(Cx)];

#endif

inline
void Step1( int8_t* ringOffset )
Expand Down
18 changes: 4 additions & 14 deletions code_blocks/enigma-opt.cbp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@
</MakeCommands>
</Target>
</Build>
<VirtualTargets>
<Add alias="Release All" targets="Release Windows x64;Release Windows x86;Release Linux x64;" />
</VirtualTargets>
<Compiler>
<Add option="-Wextra" />
<Add option="-Wall" />
Expand Down Expand Up @@ -354,6 +357,7 @@
<Option target="Release Windows x86" />
<Option target="Release Linux x64" />
</Unit>
<Unit filename="../config/releaseVersion.cpp" />
<Unit filename="../config/releaseVersion.h" />
<Unit filename="../config/testing.h">
<Option target="Release Windows x64" />
Expand Down Expand Up @@ -475,20 +479,6 @@
<Option target="Release Windows x86" />
<Option target="Release Linux x64" />
</Unit>
<Unit filename="../ic.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="../ic.h">
<Option target="Release Windows x64" />
<Option target="ReleaseConstSeed" />
<Option target="Profiling" />
<Option target="Coverage" />
<Option target="Debug" />
<Option target="Profile-Generate" />
<Option target="Profile-Use" />
<Option target="Release Windows x86" />
<Option target="Release Linux x64" />
</Unit>
<Unit filename="../input.c">
<Option compilerVar="CC" />
</Unit>
Expand Down
3 changes: 3 additions & 0 deletions config/releaseVersion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "releaseVersion.h"

char const* releaseVersion = "v1.0.0";
3 changes: 2 additions & 1 deletion config/releaseVersion.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once

char releaseVersion[] = "v1.0.0-beta.3";
extern char const* releaseVersion;

57 changes: 11 additions & 46 deletions display.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,38 @@
#include <stdlib.h>

#include "display.h"
#include "config/releaseVersion.h"

void usage(void)
{
fprintf(stderr, "enigma: usage: enigma \
[ -hvicxaR ] \
[ -M model ] \
[ -u umkehrwalze ] \
[ -w wheel-order ] \
[ -r ring-settings ] \
[ -s stecker-pairs ] \
[ -m message-key ] \
[ -hvR ] \
[ -o output-file ] \
[ -n passes ] \
[ -z maximum-score ] \
[ -k single-key ] \
[ -f lower-bound ] \
[ -t upper-bound ] \
[ trigram-dictionary bigram-dictionary ciphertext ] \
\n\nUse enigma -h for detailed help\n\n");

exit(EXIT_FAILURE);
}


void help(void)
{
fprintf(stderr, "\nOptions:\n\n\
-M Machine Type. Valid settings are H, M3 or M4. Defaults to H (Heer).\n\
This must be the first argument on the command line!\n\n\
-u Umkehrwalze. Select A (Heer only), B or C. Defaults to B.\n\n\
-w Wheel Order. Valid settings are 123 thru G876.\n\
Left letter [B,G] stands for wheels Beta or Gamma (M4 only).\n\
Left digit stands for left wheel.\n\
Middle digit stands for middle wheel.\n\
Right digit stands for right wheel.\n\
Digits must not be repeated. Defaults to 123.\n\n\
-r Ring Settings. Valid settings are AAA thru ZZZZ. Defaults to AAA.\n\n\
-s Stecker Pairs. Valid settings are 1 to 13 pairs of letters\n\
Letters must not be repeated! Defaults to none\n\n\
-m Message Key. Valid settings are AAA thru ZZZZ. Defaults to AAA.\n\n\
-o Output file. Defaults to stdout.\n\n\
-i Break an enigma message using ic statistics. Requires command line \n\
arguments <trigram-dict>, <bigram-dict> and <ciphertext>.\n\n\
-c Break an enigma message using hill climbing. Requires command line\n\
arguments <trigram-dict>, <bigram-dict> and <ciphertext>.\n\
This can take a really long time!\n\n\
-x In combination with -c: Iterate thru all middle wheel ring settings, excluding A\n\n\
-a In combination with -c: Iterate thru all middle wheel ring settings\n\n\
-n In combination with -c: Number of passes. Defaults to 1\n\n\
-z In combination with -c: Terminate if score n is reached. Defaults to INT_MAX.\n\n\
-k In combination with -c: Perform hill climb on a single key.\n\n\
-R Standalone option: Resume a hill climb, reading the previous state from 00hc.resume.\n\n\
-f In combination with -i,-c: Search from\n\n\
-t In combination with -i,-c: Search up to\n\n\
-R Standalone option: Resume a hill climb, reading the previous state from\n\
00hc.resume.\n\n\
-h Display this help screen.\n\n\
-v Display version information.\n\n\n\
Examples:\n\n\
enigma -M M3 -u C -w 781 -r GHL -s QWERTYUIOPASDFGHJKLZ -m WSK\n\
enigma -M H -i -f \"B:456:AA:FFF\" -t \"C:123:AA:BCF\" trigram-dict bigram-dict ciphertext\n\
enigma -M M4 -ca -n 300 -f \"B:B547:CD:JKNH\" -t \"C:G128:JH:REWQ\" trigram-dict bigram-dict ciphertext\n\n");
-v Display version information.\n" );

exit(EXIT_SUCCESS);
}


void version(void)
{
fprintf(stdout, "enigma-suite version 0.76\n\
Copyright (C) 2005 Stefan Krah <stefan@bytereef.org>\n\n\
fprintf(stdout, "enigma-optima version %s\n\
Copyright (C) 2005 Stefan Krah <stefan@bytereef.org>\n\
(C) 2016 Agbar <https://github.com/Agbar>\n\n\
This program is free software; you can redistribute it and/or modify it\n\
under the terms of version 2 (only) of the GNU General Public License as\n\
published by the Free Software Foundation.\n\n\
Expand All @@ -81,7 +44,9 @@ Copyright (C) 2005 Stefan Krah <stefan@bytereef.org>\n\n\
You should have received a copy of the GNU General Public License\n\
along with this program; if not, write to the Free Software\n\
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.\n\n\
To report bugs or offer suggestions, please mail to <enigma-suite@bytereef.org>.\n\n");
To report bugs or offer suggestions, please use GitHub.\n\
https://github.com/Agbar/enigma-optima/issues\n\n"
, releaseVersion );

exit(EXIT_SUCCESS);
}
Expand Down
Loading

0 comments on commit b2283eb

Please sign in to comment.