CppMonster
CppMonster

Reputation: 1276

Writing to allocated char buffer

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;
}

Upvotes: 0

Views: 107

Answers (4)

lurker
lurker

Reputation: 58234

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.

Upvotes: 1

Olaf Dietsche
Olaf Dietsche

Reputation: 74018

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;

Upvotes: 1

LearningC
LearningC

Reputation: 3162

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';

Upvotes: 0

R Sahu
R Sahu

Reputation: 206567

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.

Upvotes: 3

Related Questions