1 #include <stdlib.h> //EXIT_FAILURE
10 #define handle_error(msg) \
11 do { perror(msg); exit(EXIT_FAILURE); } while (0)
17 [HTTP/1.1 404 File Not Found
18 Content-Type: text/octet
19 User-ReturnCode: -29104
24 Offset 0 1 2 3 4 5 6 7 8 9 A B C D E F
26 00100000 48 54 54 50 2F 31 2E 31 20 34 30 34 20 46 69 6C HTTP/1.1 404 Fil
27 00100010 65 20 4E 6F 74 20 46 6F 75 6E 64 0D 0A 43 6F 6E e Not Found Con
28 00100020 74 65 6E 74 2D 54 79 70 65 3A 20 74 65 78 74 2F tent-Type: text/
29 00100030 6F 63 74 65 74 0D 0A 55 73 65 72 2D 52 65 74 75 octet User-Retu
30 00100040 72 6E 43 6F 64 65 3A 20 2D 32 39 31 30 34 0D 0A rnCode: -29104
31 00100050 43 6F 6E 74 65 6E 74 2D 4C 65 6E 67 74 68 3A 20 Content-Length:
32 00100060 31 36 0D 0A 0D 0A AB CD 98 76 FF FF 8E 50 16 «Í˜vÿÿŽP
34 char theStr
[]="HTTP/1.1 404 File Not Found\r\nContent-Type: text/octet\r\nUser-ReturnCode: -29104\r\nContent-Length: 16\r\n\r\n\xAB\xCD\x98\x76\xFF\xFF\x8E\x50";
35 int theStrLen
= sizeof(theStr
) - 1;
37 int main (int argc
, char *argv
[]) {
38 printf("Searching for:\n[%s] %d %zd\n",theStr
,theStrLen
,strlen(theStr
));
40 fprintf(stderr
,"Usage: %s <main file> <2nd file> <out file>\n",argv
[0]);
47 in1
= open(argv
[1], O_RDONLY
);
49 handle_error("open1");
50 in2
= open(argv
[2], O_RDONLY
);
52 handle_error("open2");
54 if (fstat(in1
, &sb1
) == -1) /* To obtain file size */
55 handle_error("fstat1");
56 if (fstat(in2
, &sb2
) == -1) /* To obtain file size */
57 handle_error("fstat2");
58 if (sb1
.st_size
!= sb2
.st_size
) {
59 fprintf(stderr
,"Input files with different size: %zd,%zd .\n",sb1
.st_size
,sb2
.st_size
);
62 if ( !(sb1
.st_size
&& sb2
.st_size
) ) {
63 fprintf(stderr
,"Input files are empty.\n");
67 p1
= mmap(NULL
, sb1
.st_size
, PROT_READ
, MAP_PRIVATE
, in1
, 0);
69 handle_error("mmap1");
70 p2
= mmap(NULL
, sb2
.st_size
, PROT_READ
, MAP_PRIVATE
, in2
, 0);
72 handle_error("mmap2");
74 outf
= fopen(argv
[3],"w+x");
76 handle_error("open3");
80 while ( thisp
- p1
< sb1
.st_size
) {
82 if ( memcmp(thisp
,theStr
,theStrLen
) == 0 ) {
83 printf("1 %zd\t(%zx)\t",thisp
-p1
,thisp
-p1
);
84 char *p2thisp
= thisp
- p1
+ p2
;
85 if ( memcmp(p2thisp
,theStr
,theStrLen
) == 0 ) {
88 if ( fwrite(p2thisp
,theStrLen
,1,outf
) < 1 ) {
89 handle_error("write");
94 char *extsp
= thisp
+ EXTLEN
;
95 while ( (*thisp
!= *p2thisp
) || (thisp
< extsp
) ) {
97 if (*thisp
!= *p2thisp
)
98 printf("%zd:%02hhX-%02hhX ", thisp
-lastp
, *thisp
, *p2thisp
);
103 } else { fputc(*thisp
,outf
);++thisp
; }
104 } else { fputc(*thisp
,outf
);++thisp
; }