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 2004/04/25 15:02:54 gerv%gerv.net 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 offset = -1;
00069     int bitOffset = -1;
00070 
00071     /* return values */
00072     int               retval = 2;  /* 0 - test succeeded.
00073                             * 1 - illegal args 
00074                             * 2 - function failed */
00075     PRFileDesc *fd;
00076     int bytesRead;
00077     int bytesWritten;
00078     int 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     if (offset < 0) {
00116        usage(programName);
00117        return 1;
00118     }
00119 
00120     /* open the target signature file */
00121     fd = PR_OpenFile(libFile,PR_RDWR,0666);
00122     if (fd == NULL ) {
00123        /* lperror(libFile); */
00124        PR_fprintf(pr_stderr,"Couldn't Open %s\n",libFile);
00125        goto loser;
00126     }
00127 
00128     /* read the byte */
00129     pos = PR_Seek(fd, offset, PR_SEEK_SET);
00130     if (pos != offset) {
00131        PR_fprintf(pr_stderr,"Seek for read on %s (to %d) failed\n", libFile, offset);
00132        goto loser;
00133     }
00134     bytesRead = PR_Read(fd, &cbuf, 1);
00135     if (bytesRead != 1) {
00136        PR_fprintf(pr_stderr,"Read on %s (to %d) failed\n", libFile, offset);
00137        goto loser;
00138     }
00139 
00140     PR_fprintf(pr_stderr,"Changing byte 0x%08x (%d): from %02x (%d) to ", 
00141               offset, offset, (unsigned char)cbuf, (unsigned char)cbuf);
00142     /* change it */
00143     cbuf ^= 1 << bitOffset;
00144     PR_fprintf(pr_stderr,"%02x (%d)\n", (
00145                             unsigned char)cbuf, (unsigned char)cbuf);
00146 
00147     /* write it back out */
00148     pos = PR_Seek(fd, offset, PR_SEEK_SET);
00149     if (pos != offset) {
00150        PR_fprintf(pr_stderr,"Seek for write on %s (to %d) failed\n", libFile, offset);
00151        goto loser;
00152     }
00153     bytesWritten = PR_Write(fd, &cbuf, 1);
00154     if (bytesWritten != 1) {
00155        PR_fprintf(pr_stderr,"Write on %s (to %d) failed\n", libFile, offset);
00156        goto loser;
00157     }
00158 
00159     PR_Close(fd);
00160     retval = 0;
00161 
00162 
00163 loser:
00164 
00165     PR_Cleanup ();
00166     return retval;
00167 }
00168 
00169 /*#DEFINES += -DSHLIB_SUFFIX=\"$(DLL_SUFFIX)\" -DSHLIB_PREFIX=\"$(DLL_PREFIX)\" */