libenigma
Enigma machine utility library
Loading...
Searching...
No Matches
enigma.h File Reference
#include "reflector.h"
#include "rotor.h"

Go to the source code of this file.

Data Structures

struct  Enigma
 Represents the state and configuration of an Enigma machine. More...
 

Functions

char enigma_encode (Enigma *, int)
 Encode a character using the Enigma machine.
 
int enigma_encode_string (Enigma *, const char *, char *, int)
 Encode a string using the Enigma machine.
 
int enigma_init_rotors (Enigma *, const EnigmaRotor *, int)
 Initialize the rotors of the Enigma machine.
 
int enigma_init_default_config (Enigma *)
 Load the default configuration for the Enigma machine.
 
int enigma_init_random_config (Enigma *)
 Load a random configuration for the Enigma machine.
 
Enigmaenigma_new (void)
 Create a new Enigma machine.
 
const char * enigma_version (void)
 Get the version of the Enigma library.
 
const char * enigma_get_plugboard (const Enigma *)
 Get the plugboard configuration.
 
const EnigmaReflectorenigma_get_reflector (const Enigma *)
 Get the reflector configuration.
 
const EnigmaRotorenigma_get_rotor (const Enigma *, int)
 Get the rotor configuration.
 
int enigma_get_rotor_count (const Enigma *)
 Get the number of rotors.
 
int enigma_get_rotor_flag (const Enigma *)
 Get the rotor flag.
 
int enigma_get_rotor_index (const Enigma *, int)
 Get the index of a rotor.
 
int enigma_set_plugboard (Enigma *, const char *)
 Set the plugboard configuration.
 
int enigma_set_reflector (Enigma *, int)
 
int enigma_set_rotor (Enigma *, int, int)
 Set the rotor.
 
int enigma_set_rotor_count (Enigma *, int)
 Set the number of rotors.
 
int enigma_set_rotor_flag (Enigma *, int)
 Set the rotor flag status.
 
int enigma_set_rotor_index (Enigma *, int, int)
 Set the index of a rotor.
 

Detailed Description

This file defines the core structures and functions for the Enigma machine, including the main Enigma structure, encoding functions, and initialization routines for default and random configurations.

Function Documentation

◆ enigma_encode()

char enigma_encode ( Enigma enigma,
int  c 
)

Encode a character using the Enigma machine.

This function takes an input character, processes it through the Enigma machine's rotors and reflector, and returns the encoded character. It handles both uppercase and lowercase characters.

Assumes the input character is an uppercase ASCII letter.

Parameters
enigmaPointer to the Enigma machine structure.
cThe character to encode.
Returns
The encoded character.

◆ enigma_encode_string()

int enigma_encode_string ( Enigma enigma,
const char *  input,
char *  output,
int  length 
)

Encode a string using the Enigma machine.

This function encodes a string of characters using the Enigma machine. It processes each character through the machine and stores the result in the output string. Assumes the input string is uppercase and null-terminated.

Parameters
enigmaPointer to the Enigma machine structure.
inputThe input string to encode.
outputThe output string to store the encoded result.
lengthThe length of the input string (the output buffer should be at least the same length).
Returns
ENIGMA_SUCCESS on success, ENIGMA_FAILURE on failure.

◆ enigma_get_plugboard()

const char * enigma_get_plugboard ( const Enigma enigma)

Get the plugboard configuration.

This function returns the current plugboard configuration as a string.

Parameters
enigmaPointer to the Enigma machine.
Returns
Pointer to the plugboard configuration string.

◆ enigma_get_reflector()

const EnigmaReflector * enigma_get_reflector ( const Enigma enigma)

Get the reflector configuration.

This function returns the current reflector.

Parameters
enigmaPointer to the Enigma machine.
Returns
Pointer to the reflector configuration.

◆ enigma_get_rotor()

const EnigmaRotor * enigma_get_rotor ( const Enigma enigma,
int  rotorIndex 
)

Get the rotor configuration.

This function returns the rotor at the specified index.

Parameters
enigmaPointer to the Enigma machine.
rotorIndexIndex of the rotor to retrieve.
Returns
Pointer to the rotor configuration.

◆ enigma_get_rotor_count()

int enigma_get_rotor_count ( const Enigma enigma)

Get the number of rotors.

This function returns the number of rotors in the Enigma machine.

Parameters
enigmaPointer to the Enigma machine.
Returns
Number of rotors, or ENIGMA_FAILURE if the Enigma machine is invalid.

◆ enigma_get_rotor_flag()

int enigma_get_rotor_flag ( const Enigma enigma)

Get the rotor flag.

This function returns the rotor flag of the Enigma machine.

Parameters
enigmaPointer to the Enigma machine.
Returns
Rotor flag value, or ENIGMA_FAILURE if the Enigma machine is invalid.

◆ enigma_get_rotor_index()

int enigma_get_rotor_index ( const Enigma enigma,
int  rotor 
)

Get the index of a rotor.

This function returns the index of the specified rotor.

Parameters
enigmaPointer to the Enigma machine.
rotorPointer to the rotor configuration.
Returns
Index of the rotor, or ENIGMA_FAILURE if the Enigma machine is invalid.

◆ enigma_init_default_config()

int enigma_init_default_config ( Enigma enigma)

Load the default configuration for the Enigma machine.

Default config is as follows:

  • Reflector: UKW-B
  • Rotors: I, II, III
  • Rotor positions: A, A, A
  • Empty plugboard
Parameters
enigmaPointer to the Enigma to be initialized.
Returns
ENIGMA_SUCCESS on success, ENIGMA_FAILURE on failure.

◆ enigma_init_random_config()

int enigma_init_random_config ( Enigma enigma)

Load a random configuration for the Enigma machine.

This function initializes the Enigma machine with a random configuration.

Parameters
enigmaPointer to the Enigma to be initialized.
Returns
ENIGMA_SUCCESS on success, ENIGMA_FAILURE on failure.

◆ enigma_init_rotors()

int enigma_init_rotors ( Enigma enigma,
const EnigmaRotor rotors,
int  count 
)

Initialize the rotors of the Enigma machine.

This populates the rotors array in the specified Enigma by copying the provided array of rotors. It also sets the rotor flag and count.

Parameters
enigmaPointer to the Enigma.
rotorsArray of EnigmaRotors to copy to the Enigma.
countNumber of rotors to copy.
Returns
ENIGMA_SUCCESS on success, ENIGMA_FAILURE on failure.

◆ enigma_new()

Enigma * enigma_new ( void  )

Create a new Enigma machine.

This function allocates memory for a new Enigma machine and initializes it with default configuration.

Returns
Pointer to the new Enigma machine.

◆ enigma_set_plugboard()

int enigma_set_plugboard ( Enigma enigma,
const char *  s 
)

Set the plugboard configuration.

This function sets the plugboard configuration based on the provided string. The string should contain pairs of uppercase letters representing the connections between the letters. For example, "ABCDEF" would connect A to B, C to D, and E to F.

Parameters
enigmaPointer to the Enigma machine.
sPointer to the string containing the plugboard configuration.
Returns
ENIGMA_SUCCESS on success, ENIGMA_FAILURE on failure.

◆ enigma_set_reflector()

int enigma_set_reflector ( Enigma enigma,
int  reflector 
)

Set the reflector configuration.

This function sets the reflector configuration based on the provided enigma_reflectors index.

Parameters
enigmaPointer to the Enigma machine.
reflectorIndex of the reflector to be set.
Returns
ENIGMA_SUCCESS on success, ENIGMA_FAILURE on failure.

◆ enigma_set_rotor()

int enigma_set_rotor ( Enigma enigma,
int  rotor,
int  index 
)

Set the rotor.

This function sets the rotor configuration based on the provided enigma_rotors index.

Parameters
enigmaPointer to the Enigma machine.
rotorIndex of the rotor in enigma_rotors.
indexIndex to place the rotor in enigma
Returns
ENIGMA_SUCCESS on success, ENIGMA_FAILURE on failure.

◆ enigma_set_rotor_count()

int enigma_set_rotor_count ( Enigma enigma,
int  count 
)

Set the number of rotors.

This function sets the number of rotors in the Enigma machine.

Parameters
enigmaPointer to the Enigma machine.
countNumber of rotors.
Returns
ENIGMA_SUCCESS on success, ENIGMA_FAILURE on failure.

◆ enigma_set_rotor_flag()

int enigma_set_rotor_flag ( Enigma enigma,
int  flag 
)

Set the rotor flag status.

This function sets the rotor flag status in the Enigma machine.

Parameters
enigmaPointer to the Enigma machine.
flagValue to set the rotor flag to.
Returns
ENIGMA_SUCCESS on success, ENIGMA_FAILURE on failure.

◆ enigma_set_rotor_index()

int enigma_set_rotor_index ( Enigma enigma,
int  rotor,
int  index 
)

Set the index of a rotor.

This function sets the index of a rotor in the Enigma machine.

Parameters
enigmaPointer to the Enigma machine.
rotorIndex of the rotor in the Enigma machine.
indexIndex to set the rotor to in the Enigma machine.
Returns
ENIGMA_SUCCESS on success, ENIGMA_FAILURE on failure.

◆ enigma_version()

const char * enigma_version ( void  )

Get the version of the Enigma library.

This function returns the version of the Enigma library as a string.

Returns
Pointer to the version string.