Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<stdint.h>
- #include<string.h>
- typedef struct
- {
- uint8_t one;
- uint8_t two;
- uint8_t three;
- uint8_t four;
- }header;
- typedef struct
- {
- char image[512];
- }fat;
- int main(int argc, char *argv[])
- {
- // ensure proper usage
- if (argc != 2)
- {
- fprintf(stderr, "Usage: ./copy infile\n");
- return 1;
- }
- // remember filenames
- char *infile = argv[1];
- // open input file
- FILE *inp = fopen(infile, "r");
- if (inp == NULL)
- {
- fclose(inp);
- fprintf(stderr, "Doesnt exis %s.\n", infile);
- return 3;
- }
- header a = {0xff,0xd8,0xff,0xe0};
- uint8_t *b = malloc(sizeof(header));
- uint8_t byte=0;
- fat img;
- char title[100];
- int i=0,c = 1, jpg_found=0;
- while(c==1)
- {
- if(feof(inp))
- {
- break;
- }
- fread(&byte,1,1,inp);
- if(!memcmp(&byte,&a,1))
- {
- fseek(inp,-1,SEEK_CUR);
- fread(b,sizeof(header),1,inp);
- *(b+3*sizeof(uint8_t)) = *(b+3*sizeof(uint8_t)) & 0xe0;
- if(!memcmp(&a,b,sizeof(header)))
- {
- sprintf(title,"%03d.jpg",i++);
- FILE *out = fopen(title, "w");
- if (out == NULL)
- {
- fclose(out);
- fprintf(stderr, "Error");
- return 3;
- }
- fseek(inp,-4,SEEK_CUR);
- memset(&img,0,sizeof(fat));
- while(1)
- {
- if(feof(inp))
- {
- break;
- }
- c=fread(&img,sizeof(fat),1,inp);
- if(memcmp(&img,&a,sizeof(header)) || jpg_found==0)
- {
- c=fwrite(&img,sizeof(fat),1,out);
- jpg_found++;
- }
- else
- {
- fseek(inp, -512, SEEK_CUR);
- break;
- }
- }
- fclose(out);
- jpg_found = 0;
- }
- }
- }
- fclose(inp);
- free(b);
- }
Add Comment
Please, Sign In to add comment