Problems writing binary data to a void* buffer in C
NickName:Andrei Plesoianu Ask DateTime:2020-03-20T17:59:31

Problems writing binary data to a void* buffer in C

So the scenario is this. I have to implement minimal stdio library in C and the unit test that fails writes binary data to a file and then calls the fread function from my library to read that data. And the problem is that when using memcmp on the initial buffer and the data read through my API, they differ. Have to mention that on printable ASCII characters, it works just fine. The problem is with binary data.

FUNC_DECL_PREFIX int so_fgetc(SO_FILE *stream) {
ssize_t bytes_read = 0;
unsigned char c = 0;

if (stream == NULL)
    return SO_EOF;

if (stream->fd < 0) {
    stream->flag_error = 1;
    return SO_EOF;
}

if (stream->buffer_size <= 0) {
    bytes_read = read(stream->fd, stream->buffer, (ssize_t) BUFFER_SIZE-1);
    if (bytes_read == 0) {
        stream->flag_eof = 1;
        return SO_EOF;
    } else if (bytes_read < 0) {
        stream->flag_error = 1;
        return SO_EOF;
    }

    stream->buffer_size = bytes_read;
}

c = (unsigned char) stream->buffer[stream->current_index];

char *temp_buffer = NULL;
temp_buffer = calloc(BUFFER_SIZE, sizeof(char));
if (!temp_buffer) {
    stream->flag_error = 1;
    perror("Memory allocation");
    return SO_EOF;
}

if (stream->buffer != NULL) {
    strcpy(temp_buffer, stream->buffer);
    strcpy(stream->buffer, temp_buffer+1);      
}
free(temp_buffer);

if (stream->buffer == NULL) {
    stream->flag_error = 1;
    return SO_EOF;
}
stream->buffer_size -= 1;
stream->cursor_position += 1;
stream->last_operation = 0;

return (int) c;
}

That was the function that reads data in a buffer and provides data from the buffer one char at a time

FUNC_DECL_PREFIX
size_t so_fread(void *ptr, size_t size, size_t nmemb, SO_FILE *stream) {
    if (size <= 0 || nmemb <= 0 || !stream) return 0;

    ssize_t bytes_read = 0;
    char *p = NULL;
    p = ptr;
    int i = 0, c = 0;

    for(i=0; i<nmemb*size; i++) {
        c = so_fgetc(stream);
        if (so_ferror(stream)) {
            return 0;
        }

        if (c == SO_EOF) return bytes_read;
        bytes_read += 1;
        memcpy((void*) p+i, (char*) &c, 1);
    }

    return bytes_read;
}

And that is the fread i implemented. I suspect there is a problem casting the data and storing it to the buffer but i cannot figure it out.

Copyright Notice:Content Author:「Andrei Plesoianu」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/60771956/problems-writing-binary-data-to-a-void-buffer-in-c

More about “Problems writing binary data to a void* buffer in C” related questions

Problems writing binary data to a void* buffer in C

So the scenario is this. I have to implement minimal stdio library in C and the unit test that fails writes binary data to a file and then calls the fread function from my library to read that data...

Show Detail

Writing binary data to Buffer

Normally, I would expect that the following would be good enough to represent binary data in a Buffer: new Buffer('01001000','binary') but I am pretty certain Node.js/JS does not support this 'bi...

Show Detail

Writing a binary buffer to a file in python

I have some python code that: Takes a BLOB from a database which is compressed. Calls an uncompression routine in C that uncompresses the data. Writes the uncompressed data to a file. It uses cty...

Show Detail

writing binary file in C

I realize this question has been asked a ton, and I've searched through (most) of the articles, but I just can't seem to write a file in binary. I can open the file and write in human-readable form...

Show Detail

Storing binary data buffer in protocol buffer message

I have a binary data buffer which i want to store in a protocol buffer. In the documentation (https://developers.google.com/protocol-buffers/docs/proto#scalar) it says that the bytes type is equiv...

Show Detail

How to write data into a buffer and write the buffer into a binary file with a second thread?

I am getting data from a sensor(camera) and writing the data into a binary file. The problem is it takes lot of space on the disk. So, I used the compression from boost (zlib) and the space reduced...

Show Detail

Problem writing binary data with ofstream

Hey all, I'm writing an application which records microphone input to a WAV file. Previously, I had written this to fill a buffer of a specified size and that worked fine. Now, I'd like to be able...

Show Detail

Returning Buffer filled with Binary Data from recv

Assuming I have a function that fills a buffer with binary data easily, how can I make that function return said buffer for further use? The way I am currently doing it is by having it write the ...

Show Detail

C printing a void * buffer

kay so this void printPacketBuffer(void *buffer, unsigned int length) { unsigned int i=0; char *c; unsigned int limit = ( (length != 0)?length:PACKET_DATA ); for (i=0; i&lt;limit;...

Show Detail

Differences between writing/reading binary/text in c

I'm working on a client/server program where the client sends/receives files. The files may be text files or binary files. However, I am not sure what changes I need to make, if any, to accommodate...

Show Detail