This commit is contained in:
relyea%netscape.com 2003-02-07 23:02:43 +00:00
Родитель 61ab134d46
Коммит bcafc7572b
1 изменённых файлов: 19 добавлений и 40 удалений

Просмотреть файл

@ -34,7 +34,7 @@
/*
* Test program to mangle 1 bit in a binary
*
* $Id: mangle.c,v 1.2 2003-02-07 21:12:26 relyea%netscape.com Exp $
* $Id: mangle.c,v 1.3 2003-02-07 23:02:43 relyea%netscape.com Exp $
*/
#include "nspr.h"
@ -42,44 +42,15 @@
#include "plgetopt.h"
#include "prio.h"
static PRFileDesc *pr_stderr;
static void
usage (char *program_name)
{
PRFileDesc *pr_stderr;
pr_stderr = PR_STDERR;
PR_fprintf (pr_stderr, "Usage:");
PR_fprintf (pr_stderr, "%s -i shared_library_name -o byte_offset -b bit\n", program_name);
}
#ifdef notdef
static char *
mkoutput(const char *input)
{
int in_len = PORT_Strlen(input);
char *output = PORT_Alloc(in_len+sizeof(SGN_SUFFIX));
int index = in_len + 1 - sizeof("."SHLIB_SUFFIX);
if ((index > 0) &&
(PORT_Strncmp(&input[index],
"."SHLIB_SUFFIX,sizeof("."SHLIB_SUFFIX)) == 0)) {
in_len = index;
}
PORT_Memcpy(output,input,in_len);
PORT_Memcpy(&output[in_len],SGN_SUFFIX,sizeof(SGN_SUFFIX));
return output;
}
static void
lperror(const char *string)
{
int errNum = PORT_GetError();
const char *error = SECU_Strerror(errNum);
fprintf(stderr,"%s: %s\n",string, error);
}
#endif
int
main (int argc, char **argv)
@ -95,7 +66,9 @@ main (int argc, char **argv)
int bitOffset = -1;
/* return values */
int retval = -1; /* 0 - test succeeded. -1 - test failed */
int retval = 2; /* 0 - test succeeded.
* 1 - illegal args
* 2 - function failed */
PRFileDesc *fd;
int bytesRead;
int bytesWritten;
@ -104,10 +77,11 @@ main (int argc, char **argv)
programName = PL_strrchr(argv[0], '/');
programName = programName ? (programName + 1) : argv[0];
pr_stderr = PR_STDERR;
optstate = PL_CreateOptState (argc, argv, "i:o:b:");
if (optstate == NULL) {
SECU_PrintError (programName, "PL_CreateOptState failed");
return -1;
return 1;
}
while (PL_GetNextOpt (optstate) == PL_OPT_OK) {
@ -128,48 +102,53 @@ main (int argc, char **argv)
if (libFile == NULL) {
usage(programName);
return -1;
return 1;
}
if ((bitOffset >= 8) || (bitOffset < 0)) {
usage(programName);
return -1;
return 1;
}
if (offset < 0) {
usage(programName);
return -1;
return 1;
}
/* open the target signature file */
fd = PR_OpenFile(libFile,PR_RDWR,0666);
if (fd == NULL ) {
/* lperror(libFile); */
PR_fprintf(pr_stderr,"Open %s failed\n",libFile);
goto loser;
}
/* read the byte */
pos = PR_Seek(fd,offset, PR_SEEK_SET);
pos = PR_Seek(fd, offset, PR_SEEK_SET);
if (pos != offset) {
PR_fprintf(pr_stderr,"Seek for read on %s (to %d) failed\n", libFile, offset);
goto loser;
}
bytesRead = PR_Read(fd, &cbuf, 1);
if (bytesRead != 1) {
PR_fprintf(pr_stderr,"Read on %s (to %d) failed\n", libFile, offset);
goto loser;
}
printf("Changing byte 0x%08x (%d): from %02x (%d) to ",
PR_fprintf(pr_stderr,"Changing byte 0x%08x (%d): from %02x (%d) to ",
offset, offset, cbuf, cbuf);
/* change it */
cbuf ^= 1 << bitOffset;
printf("%02x (%d)\n", cbuf, cbuf);
PR_fprintf(pr_stderr,"%02x (%d)\n", cbuf, cbuf);
/* write it back out */
pos = PR_Seek(fd, offset, PR_SEEK_SET);
if (pos != offset) {
PR_fprintf(pr_stderr,"Seek for write on %s (to %d) failed\n", libFile, offset);
goto loser;
}
bytesWritten = PR_Write(fd, &cbuf, 1);
if (bytesWritten != 1) {
PR_fprintf(pr_stderr,"Write on %s (to %d) failed\n", libFile, offset);
goto loser;
}