Here are all files, my main question is: should I declare the index_table array inside the get_base64_digitget_base64_digi
t function like (static char index_table[] = "blabla") though astatic char index_table[] = "blabla"
?
A general code review would also be appreciated.
funcs.cfuncs.c
funcs.hfuncs.h
main.cmain.c
#include "funcs.h"
int main(int argc, char* argv[]){
char* text = argc == 2 ? argv[1] : '\0';
unsigned final, temp;
int text_len = strlen(text);
int remainder = text_len % 3;
//Iterate over the input string, three characters at a time.
for (int x = final = temp = 0; x < (remainder == 0 ? text_len : text_len - remainder); x += 3) {
final = (text[x] << 16) | (text[x+1] << 8) | text[x+2];
printf("%c%c%c%c", get_base64_digit(final, 0), get_base64_digit(final, 1), get_base64_digit(final, 2), get_base64_digit(final, 3));
}
//Handle the last bytes in case text_len wasn't a multiple of 3
if (remainder == 1) {
final = text[text_len-1] << 16;
printf("%c%c==\n", get_base64_digit(final, 0), get_base64_digit(final, 1));
} else if (remainder == 2){
final = (text[text_len-2] << 16) | (text[text_len-1] << 8);
printf("%c%c%c=\n", get_base64_digit(final, 0), get_base64_digit(final, 1), get_base64_digit(final, 2));
} else
return 0;
return 0;
}
}