Back to index

lightning-sunbird  0.9+nobinonly
mangle.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2003
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 /*
00038  * Test program to mangle 1 bit in a binary
00039  *
00040  * $Id: mangle.c,v 1.6.28.1 2007/01/26 16:21:33 nelson%bolyard.com Exp $
00041  */
00042 
00043 #include "nspr.h"
00044 #include "plstr.h"
00045 #include "plgetopt.h"
00046 #include "prio.h"
00047 
00048 static PRFileDesc *pr_stderr;
00049 static void
00050 usage (char *program_name)
00051 {
00052 
00053     PR_fprintf (pr_stderr, "Usage:");
00054     PR_fprintf (pr_stderr, "%s -i shared_library_name -o byte_offset -b bit\n", program_name);
00055 }
00056 
00057 
00058 int
00059 main (int argc, char **argv)
00060 {
00061     /* buffers and locals */
00062     PLOptState       *optstate;
00063     char      *programName;
00064     char      cbuf;
00065 
00066     /* parameter set variables */
00067     const char  *libFile = NULL;   
00068     int bitOffset = -1;
00069 
00070     /* return values */
00071     int               retval = 2;  /* 0 - test succeeded.
00072                             * 1 - illegal args 
00073                             * 2 - function failed */
00074     PRFileDesc *fd = NULL;
00075     int bytesRead;
00076     int bytesWritten;
00077     PROffset32   offset = -1;
00078     PROffset32   pos;
00079 
00080     programName = PL_strrchr(argv[0], '/');
00081     programName = programName ? (programName + 1) : argv[0];
00082 
00083     pr_stderr = PR_STDERR;
00084 
00085     optstate = PL_CreateOptState (argc, argv, "i:o:b:");
00086     if (optstate == NULL) {
00087        return 1;
00088     }
00089 
00090     while (PL_GetNextOpt (optstate) == PL_OPT_OK) {
00091        switch (optstate->option) {
00092           case 'i':
00093             libFile = optstate->value;
00094             break;
00095 
00096           case 'o':
00097             offset = atoi(optstate->value);
00098             break;
00099 
00100           case 'b':
00101             bitOffset = atoi(optstate->value);
00102             break;
00103        }
00104     }
00105 
00106     if (libFile == NULL) {
00107        usage(programName);
00108        return 1;
00109     }
00110     if ((bitOffset >= 8) || (bitOffset < 0)) {
00111        usage(programName);
00112        return 1;
00113     }
00114 
00115     /* open the target signature file */
00116     fd = PR_OpenFile(libFile,PR_RDWR,0666);
00117     if (fd == NULL ) {
00118        /* lperror(libFile); */
00119        PR_fprintf(pr_stderr,"Couldn't Open %s\n",libFile);
00120        goto loser;
00121     }
00122 
00123     if (offset < 0) { /* convert to positive offset */
00124        pos = PR_Seek(fd, offset, PR_SEEK_END);
00125        if (pos == -1) {
00126            PR_fprintf(pr_stderr,"Seek for read on %s (to %d) failed\n", 
00127                      libFile, offset);
00128            goto loser;
00129        }
00130        offset = pos;
00131     }
00132 
00133     /* read the byte */
00134     pos = PR_Seek(fd, offset, PR_SEEK_SET);
00135     if (pos != offset) {
00136        PR_fprintf(pr_stderr,"Seek for read on %s (to %d) failed\n", 
00137                   libFile, offset);
00138        goto loser;
00139     }
00140     bytesRead = PR_Read(fd, &cbuf, 1);
00141     if (bytesRead != 1) {
00142        PR_fprintf(pr_stderr,"Read on %s (to %d) failed\n", libFile, offset);
00143        goto loser;
00144     }
00145 
00146     PR_fprintf(pr_stderr,"Changing byte 0x%08x (%d): from %02x (%d) to ", 
00147               offset, offset, (unsigned char)cbuf, (unsigned char)cbuf);
00148     /* change it */
00149     cbuf ^= 1 << bitOffset;
00150     PR_fprintf(pr_stderr,"%02x (%d)\n", 
00151                (unsigned char)cbuf, (unsigned char)cbuf);
00152 
00153     /* write it back out */
00154     pos = PR_Seek(fd, offset, PR_SEEK_SET);
00155     if (pos != offset) {
00156        PR_fprintf(pr_stderr,"Seek for write on %s (to %d) failed\n", 
00157                   libFile, offset);
00158        goto loser;
00159     }
00160     bytesWritten = PR_Write(fd, &cbuf, 1);
00161     if (bytesWritten != 1) {
00162        PR_fprintf(pr_stderr,"Write on %s (to %d) failed\n", libFile, offset);
00163        goto loser;
00164     }
00165 
00166     retval = 0;
00167 
00168 loser:
00169     if (fd)
00170        PR_Close(fd);
00171     PR_Cleanup ();
00172     return retval;
00173 }
00174 
00175 /*#DEFINES += -DSHLIB_SUFFIX=\"$(DLL_SUFFIX)\" -DSHLIB_PREFIX=\"$(DLL_PREFIX)\" */