Move useful functions to separate file

This commit is contained in:
Dimitri Lozeve 2020-06-02 21:10:18 +02:00
parent 2d2cb64b8d
commit 90ec7de0ac
5 changed files with 52 additions and 37 deletions

2
.gitignore vendored
View file

@ -1,2 +1,4 @@
*\~ *\~
build/ build/
compile_commands.json
.clangd/

View file

@ -1,39 +1,6 @@
#include <ctype.h> #include "utils.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
size_t hex_to_binary(unsigned char *out, const char hex[static 1]) {
for (size_t i = 0; i < strlen(hex); ++i) {
char c = tolower(hex[i]);
unsigned char value;
if (isdigit(c)) {
value = c - '0';
} else {
value = 10 + c - 'a';
}
if (i % 2 == 0) {
value <<= 4;
}
out[i / 2] += value;
}
return strlen(hex) / 2 + strlen(hex) % 2;
}
unsigned char *binary_to_base64(unsigned char *out,
const unsigned char in[static 1], size_t len) {
static const unsigned char base64_table[65] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
for (size_t i = 0; i < len - 2; i += 3) {
size_t j = i / 3 * 4;
out[j] = base64_table[in[i] >> 2];
out[j+1] = base64_table[((in[i] & 0x03) << 4) | (in[i+1] >> 4)];
out[j+2] = base64_table[((in[i+1] & 0x0f) << 2) | (in[i+2] >> 6)];
out[j+3] = base64_table[in[i+2] & 0x3f];
}
return out;
}
int main(int argc, char *argv[argc + 1]) { int main(int argc, char *argv[argc + 1]) {
if (argc < 2) { if (argc < 2) {
@ -42,9 +9,9 @@ int main(int argc, char *argv[argc + 1]) {
} }
unsigned char buf[512] = {0}; unsigned char buf[512] = {0};
size_t len = hex_to_binary(buf, argv[1]); size_t len = hex_to_bytes(buf, argv[1]);
unsigned char out[512] = {'\0'}; unsigned char out[512] = {'\0'};
binary_to_base64(out, buf, len); bytes_to_base64(out, buf, len);
printf("%s\n", out); printf("%s\n", out);
return EXIT_SUCCESS; return EXIT_SUCCESS;

View file

@ -1,2 +1,3 @@
project('cryptopals', 'c') project('cryptopals', 'c')
executable('ex01', 'ex01/main.c')
executable('ex01', ['ex01/main.c', 'utils.c'])

35
utils.c Normal file
View file

@ -0,0 +1,35 @@
#include "utils.h"
#include <ctype.h>
#include <string.h>
size_t hex_to_bytes(unsigned char *out, const char hex[static 1]) {
for (size_t i = 0; i < strlen(hex); ++i) {
char c = tolower(hex[i]);
unsigned char value;
if (isdigit(c)) {
value = c - '0';
} else {
value = 10 + c - 'a';
}
if (i % 2 == 0) {
value <<= 4;
}
out[i / 2] += value;
}
return strlen(hex) / 2 + strlen(hex) % 2;
}
unsigned char *bytes_to_base64(unsigned char *out,
const unsigned char in[static 1], size_t len) {
static const unsigned char base64_table[65] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
for (size_t i = 0; i < len - 2; i += 3) {
size_t j = i / 3 * 4;
out[j] = base64_table[in[i] >> 2];
out[j + 1] = base64_table[((in[i] & 0x03) << 4) | (in[i + 1] >> 4)];
out[j + 2] = base64_table[((in[i + 1] & 0x0f) << 2) | (in[i + 2] >> 6)];
out[j + 3] = base64_table[in[i + 2] & 0x3f];
}
return out;
}

10
utils.h Normal file
View file

@ -0,0 +1,10 @@
#ifndef UTILS_H
#define UTILS_H
#include <stdlib.h>
size_t hex_to_bytes(unsigned char *out, const char hex[static 1]);
unsigned char *bytes_to_base64(unsigned char *out,
const unsigned char in[static 1], size_t len);
#endif /* UTILS_H */