0

How can i send example.wav file bytes encoded base64 via UNIX socket?

char* readFileBytes(const char *name){
 FILE *fl = fopen(name, "rb");
 fseek(fl, 0, SEEK_END);
 long len = ftell(fl);
 char *ret = malloc(len);
 fseek(fl, 0, SEEK_SET);
 fread(ret, 1, len, fl);
 fclose(fl);
 return ret;
}

This is my read from file. Now i need to get string or something from this array, because i need to encode, and when i try to:

printf("%s\n", ret);
Output: RIFF�3

program encodes only that RIFF�3.

I can do cycle

for(int i=0; i<len;i++){printf("%u ",ren[0])}

Then i get something more intresting, but still, how can i encode whole binary array. Later i will send encoded string via UNIX socket, but that's not the case ATM.

EDITED:> I use http://fm4dd.com/programming/base64/base64_stringencode_c.htm algorithm for encoding. Is it wrong for binary?

  • You can't use string functions with binary data. Binary data might contain bytes that are zero anywhere inside of the data, and that is the same value used for the string terminator. It can also of course contain unprintable "characters" as you see in your output. – Some programmer dude Jan 16 '17 at 15:11
  • Then how should i send binary data via socket, or convert to base64? – Šarūnas Rakauskas Jan 16 '17 at 15:15
  • 1
    Possible duplicate of [How do I base64 encode (decode) in C?](http://stackoverflow.com/questions/342409/how-do-i-base64-encode-decode-in-c) – J. Piquard Jan 16 '17 at 15:15
  • 2
    Isn't the whole idea of `base64` to convert binary data to a printable string? – Eugene Sh. Jan 16 '17 at 15:15
  • Just send it as it is? Sockets have no awareness of the actual data you transmit. But remember to send the length first, so the receiving side knows the amount of data to expect. That of course means you need to "return" the length somehow from the function that reads the data. – Some programmer dude Jan 16 '17 at 15:20
  • 2
    Open the file as binary fopen(name, "rb") – berendeanicolae Jan 16 '17 at 15:26
  • You should modify b64_encode to encode the whole buffer. The check while(clrstr[j]) in the functions stops when finding a null character. – berendeanicolae Jan 16 '17 at 15:32
  • Why do you want to encode it? TCP connections are not limited to text based data. – Gerhardh Jan 16 '17 at 15:34
  • Where is the code that does the base64 encoding? There is nothing here showing what OP has attempted to do the encoding. Too many possibilities. – chux - Reinstate Monica Jan 16 '17 at 15:47

1 Answers1

0

The function you are using (b64_encode) can only encode strings. Make sure you modify the function so that it iterates over all the characters in the buffer.

Maybe something like this will work (I haven't tested the code):

/* encode - base64 encode a stream, adding padding if needed */
void b64_encode(char *clrstr, int length, char *b64dst) {
  unsigned char in[3];
  int i, len = 0;
  int j = 0;

  b64dst[0] = '\0';
  while(j<length) {
    len = 0;
    for(i=0; i<3; i++) {
     in[i] = (unsigned char) clrstr[j];
     if(j<length) {
        len++; j++;
      }
      else in[i] = 0;
    }
    if( len ) {
      encodeblock( in, b64dst, len );
    }
  }
}
berendeanicolae
  • 100
  • 1
  • 8