|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 /* |
|
6 * Test program to mangle 1 bit in a binary |
|
7 */ |
|
8 |
|
9 #include "nspr.h" |
|
10 #include "plstr.h" |
|
11 #include "plgetopt.h" |
|
12 #include "prio.h" |
|
13 |
|
14 static PRFileDesc *pr_stderr; |
|
15 static void |
|
16 usage (char *program_name) |
|
17 { |
|
18 |
|
19 PR_fprintf (pr_stderr, "Usage:"); |
|
20 PR_fprintf (pr_stderr, "%s -i shared_library_name -o byte_offset -b bit\n", program_name); |
|
21 } |
|
22 |
|
23 |
|
24 int |
|
25 main (int argc, char **argv) |
|
26 { |
|
27 /* buffers and locals */ |
|
28 PLOptState *optstate; |
|
29 char *programName; |
|
30 char cbuf; |
|
31 |
|
32 /* parameter set variables */ |
|
33 const char *libFile = NULL; |
|
34 int bitOffset = -1; |
|
35 |
|
36 /* return values */ |
|
37 int retval = 2; /* 0 - test succeeded. |
|
38 * 1 - illegal args |
|
39 * 2 - function failed */ |
|
40 PRFileDesc *fd = NULL; |
|
41 int bytesRead; |
|
42 int bytesWritten; |
|
43 PROffset32 offset = -1; |
|
44 PROffset32 pos; |
|
45 |
|
46 programName = PL_strrchr(argv[0], '/'); |
|
47 programName = programName ? (programName + 1) : argv[0]; |
|
48 |
|
49 pr_stderr = PR_STDERR; |
|
50 |
|
51 optstate = PL_CreateOptState (argc, argv, "i:o:b:"); |
|
52 if (optstate == NULL) { |
|
53 return 1; |
|
54 } |
|
55 |
|
56 while (PL_GetNextOpt (optstate) == PL_OPT_OK) { |
|
57 switch (optstate->option) { |
|
58 case 'i': |
|
59 libFile = optstate->value; |
|
60 break; |
|
61 |
|
62 case 'o': |
|
63 offset = atoi(optstate->value); |
|
64 break; |
|
65 |
|
66 case 'b': |
|
67 bitOffset = atoi(optstate->value); |
|
68 break; |
|
69 } |
|
70 } |
|
71 |
|
72 if (libFile == NULL) { |
|
73 usage(programName); |
|
74 return 1; |
|
75 } |
|
76 if ((bitOffset >= 8) || (bitOffset < 0)) { |
|
77 usage(programName); |
|
78 return 1; |
|
79 } |
|
80 |
|
81 /* open the target signature file */ |
|
82 fd = PR_OpenFile(libFile,PR_RDWR,0666); |
|
83 if (fd == NULL ) { |
|
84 /* lperror(libFile); */ |
|
85 PR_fprintf(pr_stderr,"Couldn't Open %s\n",libFile); |
|
86 goto loser; |
|
87 } |
|
88 |
|
89 if (offset < 0) { /* convert to positive offset */ |
|
90 pos = PR_Seek(fd, offset, PR_SEEK_END); |
|
91 if (pos == -1) { |
|
92 PR_fprintf(pr_stderr,"Seek for read on %s (to %d) failed\n", |
|
93 libFile, offset); |
|
94 goto loser; |
|
95 } |
|
96 offset = pos; |
|
97 } |
|
98 |
|
99 /* read the byte */ |
|
100 pos = PR_Seek(fd, offset, PR_SEEK_SET); |
|
101 if (pos != offset) { |
|
102 PR_fprintf(pr_stderr,"Seek for read on %s (to %d) failed\n", |
|
103 libFile, offset); |
|
104 goto loser; |
|
105 } |
|
106 bytesRead = PR_Read(fd, &cbuf, 1); |
|
107 if (bytesRead != 1) { |
|
108 PR_fprintf(pr_stderr,"Read on %s (to %d) failed\n", libFile, offset); |
|
109 goto loser; |
|
110 } |
|
111 |
|
112 PR_fprintf(pr_stderr,"Changing byte 0x%08x (%d): from %02x (%d) to ", |
|
113 offset, offset, (unsigned char)cbuf, (unsigned char)cbuf); |
|
114 /* change it */ |
|
115 cbuf ^= 1 << bitOffset; |
|
116 PR_fprintf(pr_stderr,"%02x (%d)\n", |
|
117 (unsigned char)cbuf, (unsigned char)cbuf); |
|
118 |
|
119 /* write it back out */ |
|
120 pos = PR_Seek(fd, offset, PR_SEEK_SET); |
|
121 if (pos != offset) { |
|
122 PR_fprintf(pr_stderr,"Seek for write on %s (to %d) failed\n", |
|
123 libFile, offset); |
|
124 goto loser; |
|
125 } |
|
126 bytesWritten = PR_Write(fd, &cbuf, 1); |
|
127 if (bytesWritten != 1) { |
|
128 PR_fprintf(pr_stderr,"Write on %s (to %d) failed\n", libFile, offset); |
|
129 goto loser; |
|
130 } |
|
131 |
|
132 retval = 0; |
|
133 |
|
134 loser: |
|
135 if (fd) |
|
136 PR_Close(fd); |
|
137 PR_Cleanup (); |
|
138 return retval; |
|
139 } |
|
140 |
|
141 /*#DEFINES += -DSHLIB_SUFFIX=\"$(DLL_SUFFIX)\" -DSHLIB_PREFIX=\"$(DLL_PREFIX)\" */ |