Skip to main content
Tweeted twitter.com/StackCodeReview/status/698367913603026944
deleted 18 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

What do you think of my Base64 encoding implementation?

Here are all files, myMy main question is: should I declare the index_table array inside the get_base64_digit function like static char index_table[] = "blabla"?

funcs.cfuncs.c

funcs.hfuncs.h

main.cmain.c

What do you think of my Base64 encoding implementation?

Here are all files, my main question is: should I declare the index_table array inside the get_base64_digit function like static char index_table[] = "blabla"?

funcs.c

funcs.h

main.c

Base64 encoding implementation

My main question is: should I declare the index_table array inside the get_base64_digit function like static char index_table[] = "blabla"?

funcs.c

funcs.h

main.c

added 19 characters in body
Source Link
SirPython
  • 13.3k
  • 3
  • 38
  • 93

Here are all files, my main question is: should I declare the index_table array inside the get_base64_digitget_base64_digit 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;
}

}

Here are all files, my main question is: should I declare the index_table array inside the get_base64_digit function (static char index_table[] = "blabla") though a general code review would be appreciated.

funcs.c

funcs.h

main.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;

}

Here are all files, my main question is: should I declare the index_table array inside the get_base64_digit function like static char index_table[] = "blabla"?

A general code review would also be appreciated.

funcs.c

funcs.h

main.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;
}
added 50 characters in body
Source Link

Here are all files, my main question is: should I declare the index_table array inside the get_base64_digit function (static char index_table[] = "blabla") though a general code review would be appreciated.

Here are all files, my main question is: should I declare the index_table array inside the get_base64_digit function (static char index_table[] = "blabla").

Here are all files, my main question is: should I declare the index_table array inside the get_base64_digit function (static char index_table[] = "blabla") though a general code review would be appreciated.

Source Link
Loading