0

I have following code:

framingStatus compressToFrame(char* inputBuffer, size_t inputBufferLength, char* frame, size_t* frameLength)
{
    dword crc32 = GetMaskedCrc(inputBuffer,inputBufferLength);
    size_t length = snappy_max_compressed_length(inputBufferLength);
    char* compressed = (char*)malloc(length);
    snappy_status status = snappy_compress(inputBuffer,inputBufferLength,compressed,&length);
    if( status!=SNAPPY_OK )
        return FS_ERROR;
    frame[0] = 0x00; // Typ ramki skompresowany
    frame[1] = length&0xff;
    frame[2] = (length&0xff00)>>8;
    frame[3] = (length&0xff00)>>16;
    frame[4] = crc32&0xff;
    frame[5] = (crc32&0xff00)>>8;
    frame[6] = (crc32&0xff0000)>>16;
    frame[7] = (crc32&0xff000000)>>24;
    frame[8] = '\0'; // Pomoc dla strcat

    strcat(frame,compressed);
    *frameLength = length+8;
    free(compressed);

    return FS_OK;
}

Before calling this function I allocate memory for buffer named frame. All is ok, but assign instructions frame[x] = ... don't seem to write anything to buffer called frame and strcat concatenate compressed data to empty buffer without header I need.

Why assign instructions frame[x] = ... etc. don't give any result?

[EDIT:] Can you suggest what function I have to use if I want to concatenate frame header with compressed data?

[EDIT2:] Code presented below works just fine.

framingStatus compressToFrame(char* inputBuffer, size_t inputBufferLength, char* frame, size_t* frameLength)
{
    dword crc32 = GetMaskedCrc(inputBuffer,inputBufferLength);
    size_t length = snappy_max_compressed_length(inputBufferLength);
    char* compressed = (char*)malloc(length);
    snappy_status status = snappy_compress(inputBuffer,inputBufferLength,compressed,&length);
    if( status!=SNAPPY_OK )
        return FS_ERROR;
    frame[0] = 0x00; // Typ ramki skompresowany
    frame[1] = length;
    frame[2] = length >> 8;
    frame[3] = length >> 16;
    frame[4] = crc32;
    frame[5] = crc32 >>8;
    frame[6] = crc32 >>16;
    frame[7] = crc32 >>24;

    memcpy(&frame[8],compressed,length);
    *frameLength = length+8;
    free(compressed);

  return FS_OK;
}
CppMonster
  • 1,216
  • 4
  • 18
  • 35

4 Answers4

3

You have

frame[0] = 0x00;

which is the same as

frame[0] = '\0';

No matter what you add after the first character, frame becomes a 0 length string.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

strcat is for strings, not general binary bytes. Because frame first byte is zero, strcat will copy compressed starting at frame[0] and will stop copying when it sees a zero in compressed.

Try memcpy instead.

memcpy(&frame[8], compressed, length);

Also, since the length of frame is passed as an argument, you might want to be checking the total length you are copying to frame to make sure there's no illegal overwrite in that case.

lurker
  • 56,987
  • 9
  • 69
  • 103
  • After this instruction I have empty frame, – CppMonster Apr 09 '14 at 15:20
  • @CppMonster Did you remove your `strcat`? And where and how are you checking the contents of `frame`? – lurker Apr 09 '14 at 15:22
  • Yes, I'm checking it with VS2008 debugger and cout instruction and it gives the same results frame is empty. – CppMonster Apr 09 '14 at 15:23
  • @CppMonster Did you check the contents of `compressed` right before the `memcpy`? Also check the value of `length`. `memcpy` should work as you need (http://www.tutorialspoint.com/c_standard_library/c_function_memcpy.htm) and `strcat` definitely is the wrong function to use. – lurker Apr 09 '14 at 15:23
  • When I saw your answer I was sure that it's solution, but something it's wrong or maybe it's something wrong with rest of code. Yes, buffer named compressed contains correct data, length variable is not zero too. – CppMonster Apr 09 '14 at 15:26
  • 1
    @CppMonster When you test with `cout`, the same principle applies. `cout` stops at a NUL byte, it encounters, which is the very first character in your frame. – Olaf Dietsche Apr 09 '14 at 15:34
  • Debugger don't shows data in frame, but when I wrote: for(int i=0;i – CppMonster Apr 09 '14 at 15:38
1

As others already pointed out, you use binary data and not text strings. Therefore, strcat function is inappropriate here, use memcpy instead.

Furthermore, you should use unsigned char instead of plain char. Additionally, you don't need to mask the values before shifting

frame[2] = (length&0xff00)>>8;

could be just

frame[2] = length >> 8;

And in this case, it is even buggy

frame[3] = (length&0xff00)>>16;

Same here

frame[5] = crc32 >> 8;
frame[6] = crc32 >> 16;
frame[7] = crc32 >> 24;
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
0
frame[0] = 0x00;

will make the first character as end of string character therefore your string frame is empty.

frame[0] = 0x00;

is same as writing,

frame[0] = '\0';
LearningC
  • 3,182
  • 1
  • 12
  • 19