Created
March 24, 2013 17:18
-
-
Save anonymous/5232702 to your computer and use it in GitHub Desktop.
This file contains 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
#include <stdbool.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define BUFFER_SIZE 512 | |
void IncStr(char*); | |
bool AllZeros(uint8_t*, size_t); | |
int main(int argc, char* argv[]) | |
{ | |
// Open the file storing the JPEGs | |
FILE* fp = fopen("card.raw", "r"); | |
if (fp == NULL) | |
{ | |
return 1; | |
} | |
// n_str is the name of the extracted JPEGs, it starts at 000 and increments | |
char n_str[4]; | |
n_str[0] = n_str[1] = n_str[2] = '0'; | |
n_str[3] = '\0'; | |
// ending is ".jpg" | |
char ending[5]; | |
ending[0] = '.'; | |
ending[1] = 'j'; | |
ending[2] = 'p'; | |
ending[3] = 'g'; | |
ending[4] = '\0'; | |
// Open the first extracted JPEG's destination | |
FILE* current_fp = fopen(strcat(n_str, ending), "w"); | |
if (current_fp == NULL) | |
{ | |
return 2; | |
} | |
uint8_t read_count = 0; | |
while (1) | |
{ | |
// Creates a buffer | |
uint8_t buffer[BUFFER_SIZE]; | |
// Reads a memory block into the buffer from the file | |
int r = fread(buffer, sizeof(uint8_t), BUFFER_SIZE, fp); | |
if (r != BUFFER_SIZE * size(uint8_t) | |
{ | |
return 3; | |
} | |
read_count++; | |
// If the buffer begins a new JPEG | |
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1) && read_count > 1) | |
{ | |
// Closes the current file | |
fclose(current_fp); | |
// Increments the file name | |
IncStr(n_str); | |
// Opens the new file | |
current_fp = fopen(strcat(n_str, ending), "w"); | |
if (current_fp == NULL) | |
{ | |
return 4; | |
} | |
// Writes the buffer to the new file | |
fwrite(buffer, sizeof(uint8_t), BUFFER_SIZE, current_fp); | |
} else if (AllZeros(buffer, BUFFER_SIZE)) | |
{ | |
fclose(current_fp); | |
break; | |
} else | |
{ | |
fwrite(buffer, sizeof(uint8_t), BUFFER_SIZE, current_fp); | |
} | |
} | |
fclose(fp); | |
return 0; | |
} | |
void IncStr(char* s) | |
{ | |
if (sizeof(s) * sizeof(char) != 4) | |
{ | |
return; | |
} | |
int n = atoi(s); | |
n++; | |
if (n < 10) | |
{ | |
sprintf(s + 2, "%d", n); | |
s[0] = s[1] = '0'; | |
} else if (n < 100) | |
{ | |
sprintf(s + 1, "%d", n); | |
s[0] = '0'; | |
} else if (n < 1000) | |
{ | |
sprintf(s, "%d", n); | |
} | |
} | |
bool AllZeros(uint8_t* block, size_t size) | |
{ | |
for (int i = 0; i < size; i++) | |
{ | |
if (block[i] != 0) | |
{ | |
return 0; | |
} | |
} | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment