2014-10-03 2 views
1

JPG 이미지를 BASE64로 변환하여 Cassandra 클러스터에 삽입해야합니다. C의 모든 내용은이 How do I base64 encode (decode) in C?입니다. 그러나 이미지에 fRead 결과를 넣으려고하면 세그먼트 화 오류가 발생합니다. 인쇄 caracters 문제)C에서 이미지를 base64로 변환하는 방법?

/* ----------------------------------------------------------------------------------Includes DO NOT MODIFY---------------------------------------------------------------------------------------------- */ 

#include <stdlib.h> 
#include <stdio.h> 
#include <unistd.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <string.h> 
#include "cassandra.h" 
#include "../headers/other.h" 

size_t fileLen; 
/* ---------------------------------------------------------------------------------------CONFIG HERE !---------------------------------------------------------------------------------------------------*/ 

/*                    ---------Connection----------                       */ 

const char *server_adress = "127.0.0.1"; /* Insert your server adress here */ 

const char *keyframe = "hallo"; /* Insert name of the table you want to insert your data into (Must be created) */ 

const char *table = "blobi"; /* Insert name of the table you want to insert your data into (Must be created) */ 

const char *column_name1 = "file_name"; /* Insert name of the column for the key input */ 
const char *column_name2 = "content"; /* Insert name of the column for the blob input */ 

/*                    ------------Data-------------                       */ 

const char *file_path = "/home/nicolas/Pictures/Minions.jpg"; /* Insert the name of the binary to read and input into your table (changes coming soon !) */ 

/* -------------------------------------------------------------------------------Do not modify beyond this point ----------------------------------------------------------------------------------------*/ 

void ReadFile(const char *name) 
{ 
    FILE *file; 
    unsigned char *buffer; 
    char *lobi; 

    //Open file                                                 
    file = fopen(name , "rb"); 
    if (!file) 
    { 
     fprintf(stderr, "Unable to open file %s", name); 
     return; 
    } 

    //Get file length                                               
    fseek(file, 0, SEEK_END); 
    fileLen=ftell(file); 
    fseek(file, 0, SEEK_SET); 

    //Allocate memory                                               
    buffer=(char *)malloc(fileLen+1); 
    if (!buffer) 
    { 
     fprintf(stderr, "Memory error!"); 
     fclose(file); 
     return; 
    } 

    //Read file contents into buffer                                           
    fread(buffer, fileLen, 1, file); 
    size_t *output_length; 
    lobi = base64_encode(buffer, fileLen, output_length); 
    printf("%s\n", lobi); 
    fclose(file); 
    insert_blob(buffer); 
    free(buffer); 
} 

을 보인다 그리고 여기

#include <stdint.h> 
#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 
#include <unistd.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() { 

    decoding_table = malloc(256); 

    for (int i = 0; i < 64; i++) 
    decoding_table[(unsigned char) encoding_table[i]] = i; 
} 

char *base64_encode(const unsigned char *data, 
        size_t input_length, 
        size_t *output_length) { 

    *output_length = 4 * ((input_length + 2)/3); 

    char *encoded_data = malloc(*output_length); 
    if (encoded_data == NULL) return NULL; 

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

    uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0; 
    uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0; 
    uint32_t octet_c = i < input_length ? (unsigned char)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] = '='; 

    return encoded_data; 
} 

는 내가이 작업을 수행하는 데 사용할 수있는 어떤 조언이나 lib 디렉토리를 가지고 base64.c입니까? 여기

GDB에 대한 역 추적 : 사전에

#0 0x00000000004013e4 in base64_encode (data=0x604960 "\377\330\377", <incomplete sequence \340>, input_length=55605, output_length=0x401e20 <__libc_csu_init>) at sources/base64.c:30 

#1 0x0000000000401a45 in ReadFile (name=0x401ed0 "/home/nicolas/Pictures/Minions.jpg") at sources/blob_test.c:81 

#2 0x0000000000401b2e in main() at sources/blob_test.c:110 

감사합니다!

+3

"오류를 세그멘테이션 것 같다"해야한다 없습니다 매우 정확한. 우리에게 당신의 코드를 보여주십시오. – unwind

+0

제공 한 링크에는 12 가지의 다른 잠재적 인 솔루션이 있습니다. 우리는 당신이 시도한 것과 추측 한 것을 추측하고 있습니까? – WhozCraig

+0

@WhozCraig 위로 대답 : –

답변

3

이것은 잘못된 것입니다 :

size_t *output_length; // declared an indeterminate pointer 
lobi = base64_encode(buffer, fileLen, output_length); // sends bogus address 

그것은

size_t output_length = 0; // note *NOT* a pointer 
lobi = base64_encode(buffer, fileLen, &output_length); // note address-of operator 
+0

MAGIC !! 그것은 작동합니다! 고마워! –

+0

@ girard_s 문제 없습니다. 로그 및 백 트레이스를 게시 해 주셔서 감사합니다. 이제 둘 다 가져 와서 그 정보가 내가 위에서 한 대답을 결론 짓는 데 어떻게 사용되었는지 볼 수 있는지보십시오. 일단 그것을 파악하고 나면 유사한 문제가 다음 번 나타날 때 기억하십시오. – WhozCraig

관련 문제