-
Notifications
You must be signed in to change notification settings - Fork 0
/
bvernam.h
176 lines (148 loc) · 3.55 KB
/
bvernam.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright (c) 2021.
// BVernam: a simple program for one-time-pad encryption.
#include <stdio.h>
#ifndef UNTITLED_BVERNAN_H
#define UNTITLED_BVERNAN_H
typedef unsigned char byte;
#define EMPTY_KEY_FILE 0;
/**
* Data structure used to store the bvernan key.
*/
typedef struct str_key {
/**
* Size of the buffer used to store data read from key file.
*/
long buffer_size;
/**
* Indicates the number of relevant elements in the buffer.
*/
long buffer_limit;
/**
* Current byte in the buffer.
*/
long buffer_counter;
/**
* Number of bytes encoded in the current round.
*/
long byte_counter;
/**
* Length of key file. When this value is not yet computed is -1.
*/
long file_length;
/**
* Key file.
*/
FILE* key_file;
/**
* Buffer used to store data read from file.
*/
byte *buffer;
/**
* Current encoding round.
*/
long round_counter;
} t_key;
/**
* Get the next byte from the key.
*
* @param key key structure
* @return next byte in the key.
*/
byte next(t_key* key);
/**
* Fill the buffer with the bytes read from the key file.
*
* @param key key structure
*/
void fill_buffer(t_key *key);
/**
* Encode a single byte e store the result in the given location.
*
* @param key key structure
* @param src source byte
* @return encoded byte.
*/
byte encode_byte(t_key* key, byte src);
/**
* Encode the first len bytes of buffer with the given key and store the result in output.
*
* @param key
* @param buffer
* @param output
* @param len
*/
void encode_buffer(t_key* key, byte* buffer, byte* output, long len);
/**
* Encode the input_file with the given key and store the result in output_file. Buffers of size
* buffer_size are used for i/o operations.
*
* @param key key structure.
* @param input_file input file.
* @param output_file output file.
* @param buffer_size buffer size.
*
*/
void encode_file(t_key* key, FILE* input_file, FILE* output_file, int buffer_size);
/**
* Encode the given input file by using the given key file and storing the result in the output_file.
*
* @param key_file_name key file name.
* @param input_file_name input file name.
* @param output_file_name output file name.
*/
void encode(char* key_file_name, char* input_file_name, char* output_file_name);
/**
* Open the key file and allocate the data structure.
*
* @param key_file_name key file name.
* @return key data structure.
*/
t_key* open_key_file(char* key_file_name, long buffer_size);
/**
* Allocate the key data structure with the given key file.
*
* @param keyFile pointer to the key file.
* @param buffer_size size of buffer.
* @return key data structure.
*/
t_key* get_key(FILE* keyFile, long buffer_size);
/**
* Close and deallocate the key data structure.
*
* @param key key data structure.
*/
void close_key(t_key* key);
/**
* Open input file.
*
* @param name input file name.
* @return pointer to the file to encode.
*/
FILE* open_input_file(char *name);
/**
* Open output file.
*
* @param name output file name.
*
* @return pointer to the output file.
*/
FILE* open_output_file(char *name);
/**
* Close a file.
*
* @param file a file to close.
*/
void close_file(FILE* file);
/**
* This function is used to rewind the key when a round is completed.
*
* @param key key data structure.
*/
void rewind_key(t_key *key);
/**
* This function is invoked when the end of key file is reached.
*
* @param key key data structure.
*/
void end_of_file(t_key *key);
#endif //UNTITLED_BVERNAN_H