Solve challenge 5
This commit is contained in:
parent
27539840e3
commit
55fba9cfd2
3 changed files with 44 additions and 0 deletions
2
ex05/input.txt
Normal file
2
ex05/input.txt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
Burning 'em, if you ain't quick and nimble
|
||||||
|
I go crazy when I hear a cymbal
|
41
ex05/main.c
Normal file
41
ex05/main.c
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
#include "utils.h"
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
if (argc < 3) {
|
||||||
|
printf("Usage: %s <key> <filename>\n", argv[0]);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *key = argv[1];
|
||||||
|
const char *filename = argv[2];
|
||||||
|
|
||||||
|
FILE *fp = fopen(filename, "r");
|
||||||
|
if (fp == NULL) {
|
||||||
|
printf("Error opening file %s\n", filename);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int key_length = strlen(key);
|
||||||
|
|
||||||
|
char buf[1024];
|
||||||
|
size_t nread = 0;
|
||||||
|
size_t counter = 0;
|
||||||
|
while ((nread = fread(buf, 1, sizeof buf, fp)) > 0) {
|
||||||
|
for (size_t i = 0; i < nread; ++i) {
|
||||||
|
printf("%02x", buf[i] ^ key[counter % key_length]);
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
putchar('\n');
|
||||||
|
|
||||||
|
if (fclose(fp)) {
|
||||||
|
printf("Error closing file %s\n", filename);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
|
@ -7,3 +7,4 @@ executable('ex01', ['ex01/main.c', 'utils.c'], dependencies: m_dep)
|
||||||
executable('ex02', ['ex02/main.c', 'utils.c'], dependencies: m_dep)
|
executable('ex02', ['ex02/main.c', 'utils.c'], dependencies: m_dep)
|
||||||
executable('ex03', ['ex03/main.c', 'utils.c'], dependencies: m_dep)
|
executable('ex03', ['ex03/main.c', 'utils.c'], dependencies: m_dep)
|
||||||
executable('ex04', ['ex04/main.c', 'utils.c'], dependencies: m_dep)
|
executable('ex04', ['ex04/main.c', 'utils.c'], dependencies: m_dep)
|
||||||
|
executable('ex05', ['ex05/main.c', 'utils.c'], dependencies: m_dep)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue