1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2003
19 * the Initial Developer. All Rights Reserved.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
38 * Test program to mangle 1 bit in a binary
40 * $Id: mangle.c,v 1.7 2007/01/26 19:38:05 nelson%bolyard.com Exp $
48 static PRFileDesc
*pr_stderr
;
50 usage (char *program_name
)
53 PR_fprintf (pr_stderr
, "Usage:");
54 PR_fprintf (pr_stderr
, "%s -i shared_library_name -o byte_offset -b bit\n", program_name
);
59 main (int argc
, char **argv
)
61 /* buffers and locals */
66 /* parameter set variables */
67 const char *libFile
= NULL
;
71 int retval
= 2; /* 0 - test succeeded.
73 * 2 - function failed */
74 PRFileDesc
*fd
= NULL
;
77 PROffset32 offset
= -1;
80 programName
= PL_strrchr(argv
[0], '/');
81 programName
= programName
? (programName
+ 1) : argv
[0];
83 pr_stderr
= PR_STDERR
;
85 optstate
= PL_CreateOptState (argc
, argv
, "i:o:b:");
86 if (optstate
== NULL
) {
90 while (PL_GetNextOpt (optstate
) == PL_OPT_OK
) {
91 switch (optstate
->option
) {
93 libFile
= optstate
->value
;
97 offset
= atoi(optstate
->value
);
101 bitOffset
= atoi(optstate
->value
);
106 if (libFile
== NULL
) {
110 if ((bitOffset
>= 8) || (bitOffset
< 0)) {
115 /* open the target signature file */
116 fd
= PR_OpenFile(libFile
,PR_RDWR
,0666);
118 /* lperror(libFile); */
119 PR_fprintf(pr_stderr
,"Couldn't Open %s\n",libFile
);
123 if (offset
< 0) { /* convert to positive offset */
124 pos
= PR_Seek(fd
, offset
, PR_SEEK_END
);
126 PR_fprintf(pr_stderr
,"Seek for read on %s (to %d) failed\n",
134 pos
= PR_Seek(fd
, offset
, PR_SEEK_SET
);
136 PR_fprintf(pr_stderr
,"Seek for read on %s (to %d) failed\n",
140 bytesRead
= PR_Read(fd
, &cbuf
, 1);
141 if (bytesRead
!= 1) {
142 PR_fprintf(pr_stderr
,"Read on %s (to %d) failed\n", libFile
, offset
);
146 PR_fprintf(pr_stderr
,"Changing byte 0x%08x (%d): from %02x (%d) to ",
147 offset
, offset
, (unsigned char)cbuf
, (unsigned char)cbuf
);
149 cbuf
^= 1 << bitOffset
;
150 PR_fprintf(pr_stderr
,"%02x (%d)\n",
151 (unsigned char)cbuf
, (unsigned char)cbuf
);
153 /* write it back out */
154 pos
= PR_Seek(fd
, offset
, PR_SEEK_SET
);
156 PR_fprintf(pr_stderr
,"Seek for write on %s (to %d) failed\n",
160 bytesWritten
= PR_Write(fd
, &cbuf
, 1);
161 if (bytesWritten
!= 1) {
162 PR_fprintf(pr_stderr
,"Write on %s (to %d) failed\n", libFile
, offset
);
175 /*#DEFINES += -DSHLIB_SUFFIX=\"$(DLL_SUFFIX)\" -DSHLIB_PREFIX=\"$(DLL_PREFIX)\" */