Reading a .bmp image with fread() - c

I am trying to read a .bmp image and write the data into a text file. Code is running fine but the problem is, it cannot read whole image once so I have to call fread() function many times. While doing this my code is repeatedly storing the first read data into the text file. What changes do I have to do in order to read the whole image properly? Below is my code snippet.
int size = width * height;
unsigned char* data = new unsigned char[size];
filename = "image.bmp";
fname = "image_data.txt";
FILE* f = fopen(filename, "rb");
FILE *fp = fopen(fname, "w");
while(totalBytes < size)
{
readsize = fread(data, sizeof(unsigned char), size, f);
totalBytes += readsize;
for(i = 0; i < readsize; i++)
{
fprintf(fp, "%d", data[i]);
if((i % width) == 0 && i != 0)
fprintf(fp, "\n");
}
fseek(f, readsize, SEEK_SET);
readsize = 0;
}

Your fseek call is wrong. After the fread call the file position will be behind the read data, so you can just read on without seeking.
What happened before was that you read X bytes, did an unnecessary but harmless fseek to file position X, then read Y bytes, but did a harmful fseek back to file position X, so you kept reading the same data again.
while(totalBytes < size)
{
readsize=fread(data, sizeof(unsigned char), size, f);
totalBytes+=readsize;
for(i = 0; i < readsize; i++)
{
fprintf(fp,"%d",data[i]);
if((i % width)== 0 && i!=0)
fprintf(fp,"\n");
}
}

Related

CS50 recover doesn't output any images

I'm working on the recover program of the CS50 course. Here are the instructions:
Implement your program in a file called recover.c in a directory called recover.
Your program should accept exactly one command-line argument, the name of a forensic image from which to recover JPEGs.
If your program is not executed with exactly one command-line argument, it should remind the user of correct usage, and main should
return 1.
If the forensic image cannot be opened for reading, your program should inform the user as much, and main should return 1.
Your program, if it uses malloc, must not leak any memory.
I think my code is supposed to work, but it doesn't. In fact, it doesn't output any images at all! Here's the code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE * pFile = NULL;
unsigned char *buffer = malloc(512);
char* filename = NULL;
int filenumber = 0;
//If user didn't print 2 items
if(argc != 2)
{
printf("Usage: ./recover image\n");
return 1;
}
//Open the file
pFile = fopen(argv[1], "r");
if (!pFile)
{
fprintf(stderr, "File cannot be opened\n");
return 2;
}
int j=0;
// checking the card by 512b chunks
//loop (i=0, i++);
while (pFile)
{
int i =0;
i++;
//k=fread (buffer, 512, i, *file);
int k = fread(buffer, 512, i, pFile);
// if 512 byte block is jpeg, make new jpeg file
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
// if it's not the first file, we should close the last one
if (filename != NULL)
{
fclose(pFile);
}
//sprintf
sprintf(filename, "%03i.jpg", 2);
//FILE = fopen (W)
pFile = fopen(filename, "w");
// fwrite (buffer, 512, j, *file1)
fwrite (buffer, 512, j, pFile);
//j=j+1
j = j + 1;
}
// if k<512 - end of the loop
if (k < 512)
{
return 0;
}
}
free(buffer);
}
I don't understand it, but I see no new files or JPEGs pop-up in my files. When I try to double-click on the file, which is called card.raw, it doesn't let me open it.
You have a load of problems. Running your code in a debugger should reveal most of them within a second.
Let's take a look:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE * pFile = NULL;
unsigned char *buffer = malloc(512);
char* filename = NULL; <<==== You never allocate any memory for this. Use an array.
int filenumber = 0;
//If user didn't print 2 items
if(argc != 2)
{
printf("Usage: ./recover image\n");
return 1;
}
//Open the file
pFile = fopen(argv[1], "r");
if (!pFile)
{
fprintf(stderr, "File cannot be opened\n");
return 2;
}
int j=0;
// checking the card by 512b chunks
//loop (i=0, i++); <<== No information provided by this comment.
while (pFile) <<== pFile is your input file. This should never change. ???
{
int i =0;
i++;
//k=fread (buffer, 512, i, *file); <<== Useless comment. Nearly same as code below but causes compiler error
int k = fread(buffer, 512, i, pFile); <<== i is always 1 and must be 1. Don't use variable.
<<== BTW: You should check k **before** using the buffer.
// if 512 byte block is jpeg, make new jpeg file
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
// if it's not the first file, we should close the last one
if (filename != NULL)
{
fclose(pFile); <<== Yikes!!! This is your input file.
}
//sprintf <<== Yes, that's obvious. Useless comment.
sprintf(filename, "%03i.jpg", 2); <<== Yikes!! You never allocate memory. NULL pointer!!
<<== Why do you always print 2? you have a counter.
//FILE = fopen (W) <<== Again no useful information in comment
pFile = fopen(filename, "w"); <<== Feed NULL into fopen and kill pFile.
// fwrite (buffer, 512, j, *file1) <<== you know what I mean...
fwrite (buffer, 512, j, pFile); <<== You only have 1 buffer, why write j blocks?
//j=j+1 <<== obvious
j = j + 1;
}
// if k<512 - end of the loop
if (k < 512) <<== fread returns number of elements, i.e. 1, not number of bytes.
{
<< you return without
- closing files
- freeing buffer
return 0;
}
<<== Now you go back to top of the loop and want to read next block from your raw file but pFile was killed in the loop.
}
free(buffer);
}

SHA - Output of program is different from sha512sum command

In summary, I program for first time with openssl/sha.h and everything goes right in compilation. here is my code:
#include <stdlib.h>
#include <stdio.h>
#include <openssl/sha.h>
int main()
{
int j;
FILE *hash_file = fopen("hash.txt", "wb");
for(j = 0; j < 256; j++)
{
unsigned char md[SHA512_DIGEST_LENGTH];
char* fileName = malloc(sizeof(int));
sprintf(fileName, "%X%s", j, ".txt");
int i;
FILE *file = fopen(fileName, "rb");
SHA512_CTX mdcontext;
int bytes;
unsigned char data[2048];
if(file == NULL)
{
printf("%s can not be opened\n", fileName);
return;
}
SHA512_Init(&mdcontext);
while((bytes = fread(data, 1, 2048, file) != 0))
SHA512_Update(&mdcontext, data, bytes);
SHA512_Final(md, &mdcontext);
for(i = 0; i < SHA512_DIGEST_LENGTH; i++)
{
printf("%x", md[i]);
fprintf(hash_file, "%x", md[i]);
}
// fprintf(hash_file, "\n");
printf("\n");
free(fileName);
fclose(file);
}
fclose(hash_file);
return 0;
}
and I have this output :
711c22448e721e5491d8245b49425aa861f1fc4a15287f735e23799b65cffec5b5abdfddd91cd643aeb3b530d48f5e258e7e230a94ed525c1387bb4e1b
But when I hash same file with sha512sum command in Linux i got this output:
6e3ea4bec3cd738f06f011c2f4ee4f6cd6d12205cafe41c083d52f94d9de4ab8b9e702664a367b633be14024a96e88a140a2e7fee4dc2c6e2f0bd436e281e35b make.sh
what is the problem?
Oh boi!
A tiny little parantheses can make you pull your hair out.
The problem is in this statement while((bytes = fread(data, 1, 2048, file) != 0)). You see, in this statement the != condition will be evaluated first. So, when fread reads (and returns), let's say n number of characters, it checks if n!=0. If it evaluates to true then it sets bytes to 1 (true is casted to 1).
And now, the function SHA512_Update(&mdcontext, data, bytes); becomes SHA512_Update(&mdcontext, data, 1); while it should have been SHA512_Update(&mdcontext, data, n); (where n is the number of characters successfully read).
Solution
Change while((bytes = fread(data, 1, 2048, file) != 0)) to while((bytes = fread(data, 1, 2048, file)) != 0).
Update [Saving the hash in a char array]:
The output of SHA512 is of 128 characters. So, we need a char array of size twice that of SHA512_DIGEST_LENGTH (64). Then, we can just store it in the char array using sprintf.
char hash[SHA512_DIGEST_LENGTH*2];
for(i = 0; i < SHA512_DIGEST_LENGTH; i++)
sprintf(&hash[i*2], "%02x", md[i]);
Why i*2? Because output has a width of 2 bytes. So, result will be stored at hash[i] and hash[i+1].
Now, to print it:
for(i = 0; i < SHA512_DIGEST_LENGTH*2; i++)
printf("%c", hash[i]);
PS:
Read about precedence.
And don't forget to follow the advices in comments to your post, especially the one about using %02x by Steve.

trying to read a file

basically trying to make an anti virus but all I get when trying to read the infected file into a buffer is EOF... it's a jpg and I have no idea how to fix this
about the file functions I'm allowed to use:
fread/fwrite
fgets
fputs
fclose
fopen
fgetc
fputc
fscanf
fprintf
int fullScan(FILE* sign, FILE* infected);
char* getFile(FILE* file);
int main(int argc, char** argv)
{
FILE* sign = fopen("KittenVirusSign", "rb");
FILE* infected = fopen("kitten_frog.jpg", "rb");
int j = 0;
if (infected == NULL)
{
printf("couldn't open the file (suspicious file)");
return -1;
}
if (sign == NULL)
{
printf("couldn't open the file (virus signature)");
return -1;
}
j = fullScan(sign, infected);
return 0;
}
int fullScan(FILE* sign, FILE* infected)
{
char* sign_c = NULL;
char* infec_c = NULL;
int infect_res = -1;
int sign_len = 0;
int infec_len = 0;
int i = 0;
int j = 0;
sign_c = getFile(sign);
infec_c = getFile(infected);
while (1)
{
if (*(infec_c + i) == *(sign_c + j))
{
infect_res = 1;
if (*(sign_c + j) == EOF)
{
break;
}
else if (*(infec_c + i) == EOF)
{
infect_res = -1;
break;
}
i++;
j++;
continue;
}
else if (*(infec_c + i) != *(sign_c + j))
{
if (*(infec_c + i) == EOF || *(sign_c + j) == EOF)
{
break;
}
i++;
j = 0;
infect_res = -1;
}
}
fclose(infected);
free(sign_c);
free(infec_c);
return infect_res;
}
char* getFile(FILE* file)
{
char* buffer;
long filelen;
int i;
fseek(file, 0, SEEK_END);
filelen = ftell(file);
fseek(file, 0, SEEK_SET);
buffer = (char *)malloc((filelen + 1)*sizeof(char));
for (i = 0; i < filelen; i++)
{
fread(buffer + i, sizeof(char), 1, file);
}
return buffer;
}
EOF is a special integer value returned by some input functions to indicate that the end of the file has been reached, but it is not part of the file data. Your fread() will therefore never store an EOF character into the input buffer you provided. However, if your C implementation features signed default chars, as many do, then there is a char value that is numerically equal to EOF (usually -1).
If either file happens to contain that byte, then your code will misinterpret it as designating the end of that file. If it happens to be the first byte in either file then the program will misinterpret the file as being empty.
Since you are analyzing binary files,
I recommend using buffers of unsigned char rather than default char.
All possible byte values can appear in the file data, so you cannot identify the end of the data by the value of any byte within.
Probably, getFile() should return a struct that contains both a pointer to the buffer and its size.
As other answer suggested, you should also send the file length and iterate over that, rather than waiting for a EOF.
Also, in your getFile() function, when you determine the length of the file you don't have to read byte by byte, you can just send the filelen to fread() like so
fread(buffer, sizeof(char), filelen, file);
fread now reads filelen elements of data each the size of a char (you can write 1 instead) from the stream file to buffer.

fread fwrite fseek in C

Hello what i am trying to do is to reverse a binary file. The type of the file is wav so for example if the channel number is 2 and bits per sample are 16 each time i will copy 32/8 = 4 bytes. The first think to do is copy the header as it is(that part is ok) and then reverse he data. I've created a code to copy the header and then part of the data from the end this 10 times(for testing) but instead of copying 40 bytes it stops at 20 for some reason(even if it would do it 20 times it would still copy just the 20 bytes). This is the code which does that. I can't spot the mistake if you can see it tell me :) Maybe the mistake is somewhere else so i wrote the full function
void reverse(char **array)
{
int i=0;
word numberChannels;
word bitsPerSample;
FILE *pFile;
FILE *pOutFile;
byte head[44];
byte *rev;
int count;
if(checkFileName(array[2]) == 0 || checkFileName(array[3]) == 0)
{
printf("wrong file name\n");
exit(1);
}
pFile = fopen (array[2] ,"r");
fseek(pFile, 22, SEEK_SET);//position of channel
fread(&numberChannels, sizeof(word), 1, pFile);
fseek(pFile, 34, SEEK_SET);//position of bitsPerSample
fread(&bitsPerSample, sizeof(word), 1, pFile);
count = numberChannels * bitsPerSample;
rewind(pFile);
fread(head, sizeof(head), 1, pFile);
pOutFile = fopen (array[3] ,"w");
fwrite(head, sizeof(head), 1, pOutFile);
count = count/8;//in my example count = 32 so count =4
rev = (byte*)malloc(sizeof(byte) * count);//byte = unsigned char
fseek(pFile, -count, SEEK_END);
for(i=0; i<10 ; i++)
{
fread(rev, count, 1, pFile);
fwrite(rev, count, 1, pOutFile);
fseek(pFile, -count, SEEK_CUR);
}
fclose(pFile);
fclose(pOutFile);
}
sizeof(rev) will evaluate to the size of a pointer. You probably just want to use count instead.
Also, does the line count = count + count do what you want it to? (i.e. it doubles count every iteration)
I would change your fseek to move relatively from the current position (and use count instead of sizeof(rev)):
for(i=0; i<10; i++)
{
fread(rev, count, 1, pFile);
fwrite(rev, count, 1, pOutFile);
fseek(pFile, -count, SEEK_CUR);
}
You need to initialize count to 4 and add 4 to it progressively. Also, sizeof(rev) is only the size of a pointer (4/8 bytes). You need to use sizeof(byte) * count instead. You can also use count in the for directly:
pFile = fopen(array[2] ,"r");
pOutFile = fopen(array[3] ,"w");
rev = (byte*)malloc(sizeof(byte) * count); //byte = unsigned char
for(count = 4; count < 44; count += 4)
{
fseek(pFile, -count, SEEK_END);
fread(rev, sizeof(byte), count, pFile);
fwrite(rev, sizeof(byte), count, pOutFile);
}
fclose(pFile);
fclose(pOutFile);

split file in c error get buffer in readfile

I program a program to split file in C in Ubuntu.
I have error when get buffer in readfile.
here is my code.
int split(char *filename, unsigned long part) {
FILE *fp;
char *buffer;
size_t result; // bytes read
off_t fileSize;
fp = fopen(filename, "rb");
if (fp == NULL) {
fprintf(stderr, "Cannot Open %s", filename);
exit(2);
}
// Get Size
fileSize = get_file_size(filename);
// Buffer
buffer = (char*) malloc(sizeof(char) * (fileSize + 1));
if (buffer == NULL) {
fputs("Memory error", stderr);
fclose(fp);
return 1;
}
// Copy file into buffer
//char buffers[11];
result = fread(buffer, 1, fileSize, fp);
buffer[fileSize] = '\0';
if (result != fileSize) {
fputs("Reading error", stderr);
return 1;
}
// Split file
off_t partSize = fileSize / part;
// Last Part
off_t lastPartSize = fileSize - partSize * part;
unsigned long i;
unsigned long j;
// create part 1 to n-1
for (j = 0; j < part; j++) {
char partName[255];
char *content;
char partNumber[3];
// Content of file part
// for (i = j; i < partSize * (j + 1); i++) {
//
// }
content = (char*) malloc(sizeof(char) * partSize);
content = copychar(buffer, j + i, partSize + i);
i += partSize;
//copy name
strcpy(partName, filename);
// part Number
sprintf(partNumber, "%d", j);
// file name with .part1 2 3 4 ....
strcat(partName, ".part");
strcat(partName, partNumber);
// Write to file
writeFile(partName, content);
free(content);
}
// last part
char *content;
content = (char*) malloc(sizeof(char) * (fileSize - partSize * (part - 1)));
content = copychar(buffer, (part - 1) * partSize + 1, fileSize);
char lastPartNumber[3];
char lastPartName[255];
sprintf(lastPartNumber, "%d", part);
strcpy(lastPartName, filename);
strcat(lastPartName, ".part");
strcat(lastPartName, lastPartNumber);
writeFile(lastPartName, content);
free(content);
free(buffer);
fclose(fp);
return 0;
}
here is function copychar from start to end
char *copychar(char* buffer, unsigned long start, unsigned long end) {
if (start >= end)
return NULL;
char *result;
result = (char*) malloc(sizeof(char) * (end - start) + 1);
unsigned long i;
for (i = start; i <= end; i++)
result[i] = buffer[i];
result[end] = '\0';
return result;
}
here is function to get filesize
off_t get_file_size(char *filename) {
struct stat st;
if (stat(filename, &st) == 0)
return st.st_size;
fprintf(stderr, "Cannot determine size of %s: %s\n", filename);
return -1;
}
here is function to write file
int writeFile(char* filename, char*buffer) {
if (buffer == NULL || filename == NULL)
return 1;
FILE *file;
file = fopen(filename, "wb");
fwrite(buffer, sizeof(char), sizeof(buffer) + 1, file);
fclose(file);
return 0;
}
When I test I use file test 29MB and it dumped.
I debug It return fileSize true but when readfile in buffer get from file it only return 135 characters and when use copychar it error.
Breakpoint 1, 0x0000000000400a0b in copychar (buffer=0x7ffff5e3a010 "!<arch>\ndebian-binary 1342169369 0 0 100644 4 `\n2.0\ncontrol.tar.gz 1342169369 0 0 100644 4557 `\n\037\213\b", start=4154703576, end=4164450461) at final.c:43
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400a0b in copychar (buffer=0x7ffff5e3a010 "!<arch>\ndebian-binary 1342169369 0 0 100644 4 `\n2.0\ncontrol.tar.gz 1342169369 0 0 100644 4557 `\n\037\213\b", start=4154703576, end=4164450461) at final.c:43
Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
I don't know how to devide buffer into part to write into part when split.
Thank for advance!
It's highly impractical to copy files in 1 big block as you may have noticed. And it's not needed.
At the simplest level you could copy the file byte by byte, like this
while( ( ch = fgetc(source) ) != EOF ) {
fputc(ch, target);
}
Which will work, but it will be quite slow. Better to copy in blocks, like this:
unsigned char buf[4096];
size_t size;
while( (size = fread(buf, 1, sizeof(buf), fpRead) ) > 0) {
fwrite(buf, 1, size, fpWrite);
}
Notice that the resulting code is way simpler and contains no dynamic memory allocation.
You still need to add the splitting logic of course, but that can be done by tracking the number of bytes written and opening a new write-file before actually writing it.
EDIT: how to handle the multipart facet - schematically, you still need to implement extra checks for some special cases and test results of the different system calls of course
unsigned char buf[4096];
size_t size;
size_t partsize = 100000; // asssuming you want to write 100k parts.
size_t stilltobewritten = partsize; // bytes remaining to be written in current part
size_t chunksize = sizeof(buf); // first time around we read full buffersize
while( (size = fread(buf, 1, chunksize, fpRead) ) > 0) {
fwrite(buf, 1, size, fpWrite);
stilltobewritten -= size; // subtract bytes written from saldo
if (stilltobewritten == 0) {
// part is complete, close this part and open next
fclose(fpWrite);
fpWrite = fopen(nextpart,"wb");
// and reinit variables
stilltobewritten = partsize;
chunksize = sizeof(buf);
} else {
// prep next round on present file - just the special case of the last block
// to handle
chunksize = (stilltobewritten > sizeof(buf)) ? sizeof(buf) : stilltobewritten;
}
}
and EDIT 2: the file part name can be made a LOT simpler as well:
sprintf(partName, "%s.part%d",file, j);
concerning the original code, there's some confusion about start and end in the copychar. First, you probably meant sizeof(char) * (end - start + 1) rather than sizeof(char) * (end - start) + 1 in the malloc, second, you're copying end-start+1 symbols from the original buffer (for (i = start; i <= end; i++)) and then overwrite the last one with '\0', which probably isn't the intended behavior.

Resources