Introduce TinyHttp server
[lcapit-junk-code.git] / jpeg-recovery.c
blobd131df2875f667fa8d704f07e177edc37e116597
1 /*
2 * A really stupid jpeg file recover for Linux.
4 * You do not want to use this, you want to use photorec instead:
6 * http://www.cgsecurity.org/wiki/PhotoRec
8 * This program is licensed under the GPLv2.
10 * Luiz Fernando N. Capitulino
11 * <lcapitulino@gmail.com>
13 #include <stdio.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
20 // Start of Image, according to the spec
21 #define IMG_START 0xd8ff
23 // End of Image, according to hexdump :P
24 // Some images do not have the EOI specified by the standard (0xffd9),
25 // the value bellow worked for a lot of images.
26 #define IMG_END 0xff3f
28 static int open_file(void)
30 char filename[12];
31 static int counter = 0;
33 snprintf(filename, sizeof(filename), "img%d.jpeg", counter++);
34 return open(filename, O_RDWR | O_CREAT, 0444);
37 int main(int argc, char *argv[])
39 ssize_t bytes;
40 unsigned short c;
41 int found, amount, fd, fd2;
43 if (argc != 2) {
44 fprintf(stderr, "you have to specify the device name\n");
45 exit(1);
48 fd = open(argv[1], O_RDONLY);
49 if (fd < 0) {
50 perror("open()");
51 exit(1);
54 found = amount = 0;
56 while (read(fd, &c, 2) > 0) {
57 if (c == IMG_START) {
58 found = 1;
59 fd2 = open_file();
60 if (fd2 < 0) {
61 perror("open_file()");
62 exit(1);
64 fprintf(stderr, "Found %d\r", amount++);
67 if (found) {
68 bytes = write(fd2, &c, 2);
69 if (bytes < 0) {
70 perror("write()");
71 exit(1);
74 if (c == IMG_END) {
75 close(fd2);
76 found = 0;
81 fprintf(stderr, "\n");
82 return 0;