-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStep1Coder.c
More file actions
273 lines (227 loc) · 9.75 KB
/
Copy pathStep1Coder.c
File metadata and controls
273 lines (227 loc) · 9.75 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
************************************************************
* COMPILERS COURSE - Algonquin College
* Code version: Fall, 2025
* Author: Alex Vu and Yassine Amraoui
* Professors: Paulo Sousa
************************************************************
"\t=-------------------------------------------------------------------=\n",
"\t| COMPILERS - ALGONQUIN COLLEGE (S25) |\n",
"\t=-------------------------------------------------------------------=\n",
"\t @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ \n",
"\t @@ @@ \n",
"\t @@ %&@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@ @@ \n",
"\t @@ @%% (@@@@@@@@@ @ @ @@ \n",
"\t @@ @& @ @ @ @ @ @@ \n",
"\t @@ @ @ % / / @@@@@@ @ @@ \n",
"\t @@ & @ @ @@ @ @@ \n",
"\t @@ @/ @*@ @ @ @ @ @@ \n",
"\t @@ @@@@ @@ @ @ @ @@ \n",
"\t @@ /@@ @@@ @ @ @@ \n",
"\t @@ @ / / @@ @ @ @@ \n",
"\t @@ @ @@ /@/ @@@ @ @ @@ \n",
"\t @@ @@@@@@@@@@@@@@@ @ @@ \n",
"\t @@ @@ \n",
"\t @@ S T R U C T U R A L @@ \n",
"\t @@ By Alex Vu and Yassine Amraoui @@ \n",
"\t @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ \n",
"\t \n",
"\t[COMPILER SCRIPT ..................................................]\n",*/
/*
***********************************************************
* File name: Reader.c
* Compiler: MS Visual Studio 2022
* Course: CST 8152 � Compilers, Lab Section: [011, 012, 013]
* Assignment: A12.
* Date: Sep 01 2025
* Professor: Paulo Sousa
* Purpose: This file is the main code for Buffer/Reader (A12)
************************************************************
*/
/*
*.............................................................................
* MAIN ADVICE:
* - Please check the "TODO" labels to develop your activity.
* - Review the functions to use "Defensive Programming".
*.............................................................................
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifndef COMPILERS_H_
#include "Compilers.h"
#endif
#ifndef CODER_H_
#include "Step1Coder.h"
#endif
void vigenereFile(const struct_strg inputFileName, const struct_strg outputFileName, const struct_strg key, struct_int encode) {
FILE* inputFile = fopen(inputFileName, "r");
if (!inputFile) {
fprintf(stderr, "ERROR: Cannot open input file: %s\n", inputFileName);
return;
}
FILE* outputFile = fopen(outputFileName, "w");
if (!outputFile) {
fprintf(stderr, "ERROR: Cannot open output file: %s\n", outputFileName);
fclose(inputFile);
return;
}
if (!key || strlen(key) == 0) {
fprintf(stderr, "ERROR: Key cannot be empty.\n");
fclose(inputFile);
fclose(outputFile);
return;
}
struct_int ch;
size_t keyLen = strlen(key);
size_t keyIndex = 0;
struct_char prevChar = '\0';
struct_char nextChar = '\0';
struct_int peekCh;
while ((ch = fgetc(inputFile)) != EOF) {
struct_char currentChar = (struct_char)ch;
struct_char processedChar;
// Peek ahead for next character
peekCh = fgetc(inputFile);
nextChar = (peekCh != EOF) ? (struct_char)peekCh : '\0';
if (peekCh != EOF) {
ungetc(peekCh, inputFile);
}
// Only process visible characters (ASCII 32 to 126)
if (currentChar >= 32 && currentChar <= 126) {
// Check if it's a letter
if (isalpha(currentChar)) {
// Don't encode single uppercase letters (variables) if they're isolated
// A letter is a variable if: uppercase AND (prev is not letter) AND (next is not letter)
struct_int isSingleUppercase = isupper(currentChar) &&
!isalpha(prevChar) &&
!isalpha(nextChar);
if (isSingleUppercase) {
// Keep variable as-is for consistency
processedChar = currentChar;
} else {
struct_char keyCh = key[keyIndex % keyLen];
struct_char base = isupper(currentChar) ? 'A' : 'a';
struct_char keyBase = isupper(keyCh) ? 'A' : 'a';
struct_int c = currentChar - base;
struct_int k = keyCh - keyBase;
struct_int result = encode ? (c + k) % 26 : (c - k + 26) % 26;
processedChar = (struct_char)(result + base);
keyIndex++;
}
} else {
// Non-letter visible characters are not encoded
processedChar = currentChar;
}
} else {
// Non-visible characters are not encoded
processedChar = currentChar;
}
fputc(processedChar, outputFile);
prevChar = currentChar;
}
fclose(inputFile);
fclose(outputFile);
}
struct_strg vigenereMem(const struct_strg inputFileName, const struct_strg key, struct_int encode) {
// Defensive programming: check inputs
if (!inputFileName || !key || strlen(key) == 0) {
fprintf(stderr, "ERROR: Invalid input file name or key.\n");
return NULL;
}
// Open file for reading in BINARY mode to preserve exact byte count
FILE* inputFile = fopen(inputFileName, "rb");
if (!inputFile) {
fprintf(stderr, "ERROR: Cannot open input file: %s\n", inputFileName);
return NULL;
}
// Determine size of the file
fseek(inputFile, 0, SEEK_END);
struct_long fileSize = ftell(inputFile);
rewind(inputFile);
if (fileSize <= 0) {
fprintf(stderr, "ERROR: Input file is empty or unreadable.\n");
fclose(inputFile);
return NULL;
}
// Allocate memory to read input file content
struct_strg inputBuffer = (struct_strg)malloc((fileSize + 1) * sizeof(struct_char));
if (!inputBuffer) {
fprintf(stderr, "ERROR: Memory allocation failed.\n");
fclose(inputFile);
return NULL;
}
fread(inputBuffer, sizeof(struct_char), fileSize, inputFile);
inputBuffer[fileSize] = '\0'; // Null-terminate
fclose(inputFile);
// Prepare output buffer
struct_strg output = (struct_strg)calloc(fileSize + 1, sizeof(struct_char));
if (!output) {
fprintf(stderr, "ERROR: Memory allocation failed for output.\n");
free(inputBuffer);
return NULL;
}
size_t keyLen = strlen(key);
size_t keyIndex = 0;
for (size_t i = 0; i < fileSize; ++i) {
struct_char ch = inputBuffer[i];
struct_char processedChar;
// Get prev and next characters for context
struct_char prevChar = (i > 0) ? inputBuffer[i - 1] : '\0';
struct_char nextChar = (i < fileSize - 1) ? inputBuffer[i + 1] : '\0';
if (ch >= 32 && ch <= 126) {
if (isalpha(ch)) {
// Don't encode single uppercase letters (variables) if they're isolated
struct_int isSingleUppercase = isupper(ch) &&
!isalpha(prevChar) &&
!isalpha(nextChar);
if (isSingleUppercase) {
// Keep variable as-is for consistency
processedChar = ch;
} else {
struct_char keyCh = key[keyIndex % keyLen];
struct_char base = isupper(ch) ? 'A' : 'a';
struct_char keyBase = isupper(keyCh) ? 'A' : 'a';
struct_int c = ch - base;
struct_int k = keyCh - keyBase;
struct_int result = encode ? (c + k) % 26 : (c - k + 26) % 26;
processedChar = (struct_char)(result + base);
keyIndex++;
}
}
else {
// Non-alpha visible character – copy as-is
processedChar = ch;
}
}
else {
// Non-visible character – copy as-is
processedChar = ch;
}
output[i] = processedChar;
}
output[fileSize] = '\0'; // Explicitly null-terminate output
free(inputBuffer);
return output;
}
// Function to encode (cypher)
void cypher(const struct_strg inputFileName, const struct_strg outputFileName, const struct_strg key) {
vigenereFile(inputFileName, outputFileName, key, CYPHER);
}
// Function to decode (decypher)
void decypher(const struct_strg inputFileName, const struct_strg outputFileName, const struct_strg key) {
vigenereFile(inputFileName, outputFileName, key, DECYPHER);
}
struct_int getSizeOfFile(const struct_strg filename) {
struct_int size = 0;
FILE* file = fopen(filename, "rb");
if (!file) {
fprintf(stderr, "ERROR: Cannot open file for size check: %s\n", filename);
return -1; // or any error code you prefer
}
fseek(file, 0, SEEK_END);
size = (struct_int)ftell(file);
fclose(file);
return size;
}