Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / cmd / shlibsign / mangle / mangle.c
blob23ea36fc94bd26e030b4ebe5ae0cfa494ef02e2a
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
12 * License.
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.
21 * Contributor(s):
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 $
43 #include "nspr.h"
44 #include "plstr.h"
45 #include "plgetopt.h"
46 #include "prio.h"
48 static PRFileDesc *pr_stderr;
49 static void
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);
58 int
59 main (int argc, char **argv)
61 /* buffers and locals */
62 PLOptState *optstate;
63 char *programName;
64 char cbuf;
66 /* parameter set variables */
67 const char *libFile = NULL;
68 int bitOffset = -1;
70 /* return values */
71 int retval = 2; /* 0 - test succeeded.
72 * 1 - illegal args
73 * 2 - function failed */
74 PRFileDesc *fd = NULL;
75 int bytesRead;
76 int bytesWritten;
77 PROffset32 offset = -1;
78 PROffset32 pos;
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) {
87 return 1;
90 while (PL_GetNextOpt (optstate) == PL_OPT_OK) {
91 switch (optstate->option) {
92 case 'i':
93 libFile = optstate->value;
94 break;
96 case 'o':
97 offset = atoi(optstate->value);
98 break;
100 case 'b':
101 bitOffset = atoi(optstate->value);
102 break;
106 if (libFile == NULL) {
107 usage(programName);
108 return 1;
110 if ((bitOffset >= 8) || (bitOffset < 0)) {
111 usage(programName);
112 return 1;
115 /* open the target signature file */
116 fd = PR_OpenFile(libFile,PR_RDWR,0666);
117 if (fd == NULL ) {
118 /* lperror(libFile); */
119 PR_fprintf(pr_stderr,"Couldn't Open %s\n",libFile);
120 goto loser;
123 if (offset < 0) { /* convert to positive offset */
124 pos = PR_Seek(fd, offset, PR_SEEK_END);
125 if (pos == -1) {
126 PR_fprintf(pr_stderr,"Seek for read on %s (to %d) failed\n",
127 libFile, offset);
128 goto loser;
130 offset = pos;
133 /* read the byte */
134 pos = PR_Seek(fd, offset, PR_SEEK_SET);
135 if (pos != offset) {
136 PR_fprintf(pr_stderr,"Seek for read on %s (to %d) failed\n",
137 libFile, offset);
138 goto loser;
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);
143 goto loser;
146 PR_fprintf(pr_stderr,"Changing byte 0x%08x (%d): from %02x (%d) to ",
147 offset, offset, (unsigned char)cbuf, (unsigned char)cbuf);
148 /* change it */
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);
155 if (pos != offset) {
156 PR_fprintf(pr_stderr,"Seek for write on %s (to %d) failed\n",
157 libFile, offset);
158 goto loser;
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);
163 goto loser;
166 retval = 0;
168 loser:
169 if (fd)
170 PR_Close(fd);
171 PR_Cleanup ();
172 return retval;
175 /*#DEFINES += -DSHLIB_SUFFIX=\"$(DLL_SUFFIX)\" -DSHLIB_PREFIX=\"$(DLL_PREFIX)\" */