Created
May 3, 2017 06:07
-
-
Save chanjungkim/f8fc5fffbda3e0dc1c1c15f0398b222b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
recover.c | |
/** | |
* Copies a BMP piece by piece, just because. | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include "bmp.h" | |
int writting = 0; | |
int main(int argc, char *argv[]) | |
{ | |
// ensure proper usage | |
if (argc != 2) | |
{ | |
fprintf(stderr, "Usage: ./recover infile\n"); | |
return 1; | |
} | |
// remember filenames | |
char *infile = argv[1]; | |
// open input file | |
FILE *inptr = fopen(infile, "r"); | |
if (inptr == NULL) | |
{ | |
fprintf(stderr, "Could not open %s.\n", infile); | |
return 2; | |
} | |
FILE *outptr = fopen("temp","w"); | |
//int outfile = 0; | |
//record block beginning location | |
long lineStart = 1024;//ftell(inptr); | |
// do | |
// read and check block for beginning of jpeg | |
// while | |
// not at end of file. | |
// read infile's first | |
fseek(inptr, lineStart , SEEK_SET); | |
for(int i = 0; i<10; i++){ | |
// malloc block | |
int *r = malloc(512 * sizeof(int)); | |
if (r == NULL) | |
{ | |
printf("ERROR: Out of memory\n"); | |
return 1; | |
} | |
lineStart = ftell(inptr); | |
JPEGBLOCK signature; | |
fread(&signature, sizeof(JPEGBLOCK), 1, inptr); | |
fseek(inptr, lineStart , SEEK_SET); | |
printf("%x%x%x\n",signature.jpegs1,signature.jpegs2,signature.jpegs3); | |
// read in a block | |
fread(&r,(512 * sizeof(int)), 1, inptr); | |
/* | |
// check if block begins with jpeg signature | |
if ( | |
(signature.jpegs1 == 0xff && signature.jpegs2 == 0xd8 && signature.jpegs3 == 0xff ) && | |
( | |
signature.jpegs4 == 0xe0 || signature.jpegs4 == 0xe1 || | |
signature.jpegs4 == 0xe2 || signature.jpegs4 == 0xe3 || | |
signature.jpegs4 == 0xe4 || signature.jpegs4 == 0xe5 || | |
signature.jpegs4 == 0xe6 || signature.jpegs4 == 0xe7 || | |
signature.jpegs4 == 0xe8 || signature.jpegs4 == 0xe9 || | |
signature.jpegs4 == 0xea || signature.jpegs4 == 0xeb || | |
signature.jpegs4 == 0xec || signature.jpegs4 == 0xed || | |
signature.jpegs4 == 0xee || signature.jpegs4 == 0xef | |
) | |
) | |
{ | |
// if so, | |
// close any open file, | |
fclose(outptr); | |
outfile++; | |
// change sentinel to indicate currentlyWriting | |
writting = 1; | |
// open new output file | |
char str[0x2]; | |
sprintf(str,"%d",outfile); | |
char writeName[0x100]; | |
snprintf(writeName, sizeof(writeName), "%s.jpg",str); | |
outptr = fopen(writeName, "w"); | |
if (outptr == NULL) | |
{ | |
fclose(inptr); | |
fprintf(stderr, "Could not create %s.\n", str); | |
return 3; | |
} | |
// write mallock to file | |
fwrite(&r, 512, 1, outptr); | |
} else if (writting == 1){ | |
// write mallock to file | |
fwrite(&r, 512, 1, outptr); | |
} | |
*/ | |
// free memory; | |
//free(r); | |
} | |
// close infile | |
fclose(inptr); | |
// close outfile | |
fclose(outptr); | |
// success | |
return 0; | |
} | |
bmp.h: | |
/** | |
* BMP-related data types based on Microsoft's own. | |
*/ | |
#include <stdint.h> | |
/** | |
* Common Data Types | |
* | |
* The data types in this section are essentially aliases for C/C++ | |
* primitive data types. | |
* | |
* Adapted from https://msdn.microsoft.com/en-us/library/cc230309.aspx. | |
* See http://en.wikipedia.org/wiki/Stdint.h for more on stdint.h. | |
*/ | |
typedef uint8_t BYTE; | |
typedef uint32_t DWORD; | |
typedef int32_t LONG; | |
typedef uint16_t WORD; | |
/** | |
* BITMAPFILEHEADER | |
* | |
* The BITMAPFILEHEADER structure contains information about the type, size, | |
* and layout of a file that contains a DIB [device-independent bitmap]. | |
* | |
* Adapted from https://msdn.microsoft.com/en-us/library/dd183374(v=vs.85).aspx. | |
*/ | |
typedef struct | |
{ | |
WORD bfType; | |
DWORD bfSize; | |
WORD bfReserved1; | |
WORD bfReserved2; | |
DWORD bfOffBits; | |
} __attribute__((__packed__)) | |
BITMAPFILEHEADER; | |
/** | |
* JPEGBLOCK | |
* | |
* Contains JPEGS | |
*/ | |
typedef struct | |
{ | |
BYTE jpegs1; | |
BYTE jpegs2; | |
BYTE jpegs3; | |
BYTE jpegs4; | |
} __attribute__((__packed__)) | |
JPEGBLOCK; | |
/** | |
* BITMAPINFOHEADER | |
* | |
* The BITMAPINFOHEADER structure contains information about the | |
* dimensions and color format of a DIB [device-independent bitmap]. | |
* | |
* Adapted from https://msdn.microsoft.com/en-us/library/dd183376(v=vs.85).aspx. | |
*/ | |
typedef struct | |
{ | |
DWORD biSize; | |
LONG biWidth; | |
LONG biHeight; | |
WORD biPlanes; | |
WORD biBitCount; | |
DWORD biCompression; | |
DWORD biSizeImage; | |
LONG biXPelsPerMeter; | |
LONG biYPelsPerMeter; | |
DWORD biClrUsed; | |
DWORD biClrImportant; | |
} __attribute__((__packed__)) | |
BITMAPINFOHEADER; | |
/** | |
* RGBTRIPLE | |
* | |
* This structure describes a color consisting of relative intensities of | |
* red, green, and blue. | |
* | |
* Adapted from https://msdn.microsoft.com/en-us/library/dd162939(v=vs.85).aspx. | |
*/ | |
typedef struct | |
{ | |
BYTE rgbtBlue; | |
BYTE rgbtGreen; | |
BYTE rgbtRed; | |
} __attribute__((__packed__)) | |
RGBTRIPLE; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment