0

I am trying to get Base 64 encode value for a string value "simple" by using the implementation mentioned here

#include<openssl/sha.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdint.h>
#include<stdlib.h>


static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
                                'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
                                'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
                                'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
                                'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
                                'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
                                'w', 'x', 'y', 'z', '0', '1', '2', '3',
                                '4', '5', '6', '7', '8', '9', '+', '/'};

static int mod_table[] = {0,2,1};

char *base64_encode(const unsigned char *data,
                    size_t input_length,
                    size_t *output_length)
{
        printf("-- Begins -- ");
        *output_length = (size_t) (4.0 * ceil((double) input_length / 3.0));
        char *encoded_data = malloc(*output_length);

        for (int i = 0, j = 0; i < input_length;) {

        uint32_t octet_a = i < input_length ? data[i++] : 0;
        uint32_t octet_b = i < input_length ? data[i++] : 0;
        uint32_t octet_c = i < input_length ? data[i++] : 0;

        uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;

        encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
        encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
        encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
        encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
        }

    for (int i = 0; i < mod_table[input_length % 3]; i++)
        encoded_data[*output_length - 1 - i] = '=';
        printf(" -- Ends -- ");

    return encoded_data;


   }

int main(int argc, char **argv)
{
        unsigned char ibuf[] = "simple";
        size_t *outLen;
        size_t *inLen = (size_t) strlen(ibuf);
        char *encodeVal = base64_encode(ibuf, inLen, outLen);
        return(0);
}

When I tried debugging using gdb, all I got was:

Program received signal SIGSEGV, Segmentation fault.
0x000000000040073f in base64_encode (data=0x7fff65298a90 "simple", input_length=6, output_length=0x400a50) at openSha.c:25
25              *output_length = (size_t) (4.0 * ceil((double) input_length / 3.0));

What is the error here? I haven't coded for a long time in C, so I am sorry if I am missing something obvious here.

Community
  • 1
  • 1
name_masked
  • 9,544
  • 41
  • 118
  • 172
  • 1
    Look at this: `size_t *outLen;` and `base64_encode(ibuf, inLen, outLen);` : you try to dereference pointer that points to nothing! The same with `inlen` – Eddy_Em Jan 15 '13 at 20:52

4 Answers4

1
 size_t *inLen = (size_t) strlen(ibuf);

This looks wrong.

inLen is a pointer to size_t but you are assigning it a size_t value.

Also this call:

base64_encode(ibuf, inLen, outLen);

base64_encode expects a size_t as its second argument but you are passing a size_t *.

And finally outLen is not initialized but you are passing its value to base64_encode.

ouah
  • 142,963
  • 15
  • 272
  • 331
1
size_t *outLen;

You pass an uninitialised pointer to encodeVal, and that writes to where it points to:

*output_length = (size_t) (4.0 * ceil((double) input_length / 3.0));

You should pass the address of a size_t variable, or at least an initialised pointer pointing to allocated memory.

Also,

size_t *inLen = (size_t) strlen(ibuf);

is very probably wrong, that should be size_t inLen = strlen(ibuf);.

Probably, instead of

char *encoded_data = malloc(*output_length);

you should malloc one more, and 0-terminate the encoded string (but that depends on the use).

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
1

This pointer is uninitialized:

size_t *outLen;

And in line 25 you want to dereference it. Change your code to:

size_t outLen;
base64_encode(ibuf, inLen, &outLen);
Adam Sznajder
  • 9,108
  • 4
  • 39
  • 60
1

I've just work with this code.

That's work:

#include <stdint.h>
#include <stdlib.h>

static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
                                'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
                                'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
                                'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
                                'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
                                'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
                                'w', 'x', 'y', 'z', '0', '1', '2', '3',
                                '4', '5', '6', '7', '8', '9', '+', '/'};
static char *decoding_table = NULL;
static int mod_table[] = {0, 2, 1};

void build_decoding_table();

char *base64_encode(const unsigned char *data,
                    size_t input_length,
                    size_t *output_length) {
    int i, j;
    *output_length = 4 *((input_length + 2)/ 3);

    char *encoded_data = calloc(*output_length, 1);
    if (!encoded_data) return NULL;

    for (i = 0, j = 0; i < input_length;) {

        uint32_t octet_a = i < input_length ? data[i++] : 0;
        uint32_t octet_b = i < input_length ? data[i++] : 0;
        uint32_t octet_c = i < input_length ? data[i++] : 0;

        uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;

        encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
        encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
        encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
        encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
    }

    for (i = 0; i < mod_table[input_length % 3]; i++)
        encoded_data[*output_length - 1 - i] = '=';

    return encoded_data;
}


unsigned char *base64_decode(const char *data,
                    size_t input_length,
                    size_t *output_length) {
    int i, j;
    if (decoding_table == NULL) build_decoding_table();

    if (input_length % 4 != 0) return NULL;

    *output_length = input_length / 4 * 3;
    if (data[input_length - 1] == '=') (*output_length)--;
    if (data[input_length - 2] == '=') (*output_length)--;

    unsigned char *decoded_data = calloc(*output_length, 1);
    if (!decoded_data) return NULL;

    for (i = 0, j = 0; i < input_length;) {

        uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
        uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
        uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
        uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];

        uint32_t triple = (sextet_a << 3 * 6)
                        + (sextet_b << 2 * 6)
                        + (sextet_c << 1 * 6)
                        + (sextet_d << 0 * 6);

        if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
        if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
        if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
    }

    return decoded_data;
}


void build_decoding_table() {
    int i;
    decoding_table = malloc(256);

    for (i = 0; i < 0x40; i++)
        decoding_table[encoding_table[i]] = i;
}


void base64_cleanup() {
    free(decoding_table);
}




#include <string.h>
#include <stdio.h>

int main(int argc, char **argv){
    if(argc != 2) return -1;
    size_t len = strlen(argv[1]), olen;
    printf("len: %zd\n", 4 *((len + 2)/ 3));
    printf("argument %s coded: %s\n", argv[1], base64_encode(argv[1], len, &olen));
    base64_cleanup();
    return 0;
}
Eddy_Em
  • 864
  • 6
  • 14