Browse Source

working on writing my own, instead of modifying the base one so I understand it better

master
Grant Montgomery 2 years ago
parent
commit
4089a4dbf4
  1. 1
      columnar.c
  2. 155
      columnar1.c
  3. 42
      test.c

1
columnar.c

@ -21,7 +21,6 @@
#define MODE ENCODE
#endif
int print_buffer(char *buf, unsigned int bytes) {
/* takes in a pointer to a buffer and prints out as many
* bytes as specified */

155
columnar1.c

@ -0,0 +1,155 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define TRANS 1
#define NOTRANS 0
#ifndef DEBUG
#define DEBUG 0
#endif
#define ENCODE 0
#define DECODE 1
#ifndef MODE
#define MODE ENCODE
#endif
int transpose_buffer(char *startBuf, char *endBuf, int dim) {
int j = 0;
int mod = dim*dim -1;
for(int i = 0; i < dim * dim; i++) {
if(j > mod) {
j -= mod;
}
//printf("%s", startBuf[j]);
endBuf[i] = startBuf[j];
j+=3;
}
return 0;
}
int pad_buffer(char startBuf, int dim) {
int length = sizeof(startBuf/sizeof(char));
printf("length : %d\n", length);
}
int main(int argc, char *argv[]) {
if (argc < 4) {
printf("Missing arguments!\n\n");
printf("Usage: vigenere keyfile infile outfile\n\n");
printf("Note: outfile will be overwritten.\n");
return 1;
}
int dim = atoi(argv[1]);
char *input = argv[2];
char *output = argv[3];
// printf("%d\n", dim);
char startBuf[100];
char endBuf[100];
/* open the input or quit on error. */
FILE *INPUT;
if ((INPUT = fopen(input, "r")) == NULL) {
printf("Problem opening input file '%s'; errno: %d\n", input, errno);
return 1;
}
// Open the output or quit on error.
FILE *OUTPUT;
if ((OUTPUT = fopen(output, "w+")) == NULL) {
printf("Problem truncating output file '%s'; errno: %d\n", output, errno);
return 1;
}
printf("Starting Buffer\n-------------------\n");
//fclose(OUTPUT);
char dataByte;
int i;
int k;
for(k = 0; startBuf[dim*dim - 1] != 'Y'; k++) {
// printf("%d\n", k);
for(i = 0; i < dim * dim; i++) {
dataByte = fgetc(INPUT);
if(dataByte != EOF) {
//printf("%d : ", i);
if(dataByte == '\n') {
printf("\\n");
}
else {
printf("%c", dataByte);
}
// printf("\n");
startBuf[i] = dataByte;
}
else {
if(MODE == ENCODE) {
if(startBuf[i - 1] != 'X' && startBuf[i - 1] != 'Y' ) {
startBuf[i] = 'X';
printf("X");
}
else {
startBuf[i] = 'Y';
printf("Y");
}
}
}
}
printf("\n");
for(int w = 0; w < dim*dim; w++) {
fprintf(OUTPUT, "%c", startBuf[w]);
}
// fprintf(OUTPUT, "%s", startBuf);
}
// i--;
// if(i < dim * dim) {
// startBuf[i] = 'X';
// i++;
// for(i = i; i < dim*dim; i++) {
// startBuf[i] = 'Y';
// }
// }
// for(int j = 0; j < 18; j++) {
// printf("%c", startBuf[j]);
// }
// //Prepare block for transposition
// char dataByte;
// int counter;
// for(counter = 0; counter < dim * dim; counter++) {
// if(dataByte = (int)fgetc(INPUT) != EOF) {
// startBuf[counter] = dataByte;
// }
// }
// startBuf[counter] = "X";
// counter++;
// for(counter = counter; counter < 100; counter++) {
// startBuf[counter] = "Y";
// }
// int j = 0;
// int length = (sizeof(startBuf)/sizeof(char));
// for(int i = 0; i < length; i++) {
// printf("%c", startBuf[i]);
// }
// printf("\n");
// printf("\nEncoded Buffer\n-------------------\n");
// transpose_buffer(startBuf, endBuf, 3);
// for(int i = 0; i < 9; i++) {
// fprintf(OUTPUT, "%d", (char)endBuf[i]);
// }
// printf("\n");
fclose(INPUT);
fclose(OUTPUT);
return 0;
}

42
test.c

@ -0,0 +1,42 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define TRANS 1
#define NOTRANS 0
#ifndef DEBUG
#define DEBUG 0
#endif
#define ENCODE 0
#define DECODE 1
#ifndef MODE
#define MODE ENCODE
#endif
int main() {
printf("Starting Buffer\n-------------------\n");
int startBuf[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
int endBuf[9];
//print buffer starting state
int j = 0;
for(int i = 0; i < 9; i++) {
printf("%d", startBuf[i]);
}
printf("\n");
printf("\nEncoded Buffer\n-------------------\n");
//print buffer ending state
transpose_buffer(startBuf, endBuf, 3);
for(int i = 0; i < 9; i++) {
printf("%d", endBuf[i]);
}
printf("\n");
return 0;
}
Loading…
Cancel
Save