dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] CVS: pnet/image image.h,1.22,1.23 pecoff_loader.c


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnet/image image.h,1.22,1.23 pecoff_loader.c,1.12,1.13
Date: Thu, 27 Mar 2003 17:01:38 -0500

Update of /cvsroot/dotgnu-pnet/pnet/image
In directory subversions:/tmp/cvs-serv21813/image

Modified Files:
        image.h pecoff_loader.c 
Log Message:


Implement ILImageLoadFromMemory.


Index: image.h
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnet/image/image.h,v
retrieving revision 1.22
retrieving revision 1.23
diff -C2 -r1.22 -r1.23
*** image.h     23 Feb 2003 02:28:19 -0000      1.22
--- image.h     27 Mar 2003 22:01:35 -0000      1.23
***************
*** 154,157 ****
--- 154,158 ----
        int                             only32Bit : 1;  /* Non-zero if image is 
32-bit only */
        int                             mapped : 1;             /* Non-zero if 
mmap used to load image */
+       int                             inPlace : 1;    /* Non-zero if in-place 
execution */
        int                             strRefBig : 1;  /* Non-zero if STRREF's 
are 32-bit */
        int                             blobRefBig : 1; /* Non-zero if 
BLOBREF's are 32-bit */

Index: pecoff_loader.c
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnet/image/pecoff_loader.c,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -r1.12 -r1.13
*** pecoff_loader.c     6 Mar 2003 05:02:07 -0000       1.12
--- pecoff_loader.c     27 Mar 2003 22:01:35 -0000      1.13
***************
*** 26,29 ****
--- 26,67 ----
  
  /*
+  * Input context.
+  */
+ typedef struct _tagILInputContext ILInputContext;
+ struct _tagILInputContext
+ {
+       FILE               *stream;
+       const char         *buffer;
+       unsigned long   bufLen;
+       int (*readFunc)(ILInputContext *ctx, void *buf, unsigned len);
+ };
+ 
+ /*
+  * Stdio-based read operation.
+  */
+ static int StdioRead(ILInputContext *ctx, void *buf, unsigned len)
+ {
+       return (int)fread(buf, 1, len, ctx->stream);
+ }
+ 
+ /*
+  * Memory-based read operation.
+  */
+ static int MemoryRead(ILInputContext *ctx, void *buf, unsigned len)
+ {
+       if(len < ctx->bufLen)
+       {
+               len = (unsigned)(ctx->bufLen);
+       }
+       if(len > 0)
+       {
+               ILMemCpy(buf, ctx->buffer, len);
+               ctx->buffer += len;
+               ctx->bufLen -= len;
+       }
+       return (int)len;
+ }
+ 
+ /*
   * Seek to a particular offset within a stream by reading
   * and discarding data.  This is designed to work on streams
***************
*** 31,35 ****
   * the seek failed.
   */
! static int SeekWithinStream(FILE *file, char *buffer,
                                                        unsigned long 
currentOffset,
                                                        unsigned long 
destOffset)
--- 69,73 ----
   * the seek failed.
   */
! static int SeekWithinStream(ILInputContext *ctx, char *buffer,
                                                        unsigned long 
currentOffset,
                                                        unsigned long 
destOffset)
***************
*** 46,50 ****
                        size = (unsigned)(destOffset - currentOffset);
                }
!               if(fread(buffer, 1, size, file) != size)
                {
                        return 0;
--- 84,88 ----
                        size = (unsigned)(destOffset - currentOffset);
                }
!               if((*(ctx->readFunc))(ctx, buffer, size) != (int)size)
                {
                        return 0;
***************
*** 113,117 ****
                ILUnmapFileFromMemory(image->mapAddress, image->mapLength);
        }
!       else if(image->data)
        {
                ILFree(image->data);
--- 151,155 ----
                ILUnmapFileFromMemory(image->mapAddress, image->mapLength);
        }
!       else if(image->data && !(image->inPlace))
        {
                ILFree(image->data);
***************
*** 119,124 ****
  }
  
! int ILImageLoad(FILE *file, const char *filename,
!                               ILContext *context, ILImage **image, int flags)
  {
        char buffer[1024];
--- 157,162 ----
  }
  
! static int ImageLoad(ILInputContext *ctx, const char *filename,
!                                        ILContext *context, ILImage **image, 
int flags)
  {
        char buffer[1024];
***************
*** 148,151 ****
--- 186,190 ----
        unsigned char *runtimeHdr;
        int isMapped;
+       int isInPlace;
        void *mapAddress;
        unsigned long mapLength;
***************
*** 155,159 ****
           stub (executables and DLL's), or the beginning of a
           PE/COFF header (object files) */
!       if(fread(buffer, 1, 2, file) != 2)
        {
                return IL_LOADERR_TRUNCATED;
--- 194,198 ----
           stub (executables and DLL's), or the beginning of a
           PE/COFF header (object files) */
!       if((*(ctx->readFunc))(ctx, buffer, 2) != 2)
        {
                return IL_LOADERR_TRUNCATED;
***************
*** 162,166 ****
        {
                /* Read the MS-DOS stub and find the start of the PE header */
!               if(fread(buffer + 2, 1, 62, file) != 62)
                {
                        return IL_LOADERR_TRUNCATED;
--- 201,205 ----
        {
                /* Read the MS-DOS stub and find the start of the PE header */
!               if((*(ctx->readFunc))(ctx, buffer + 2, 62) != 62)
                {
                        return IL_LOADERR_TRUNCATED;
***************
*** 172,181 ****
                        return IL_LOADERR_BACKWARDS;
                }
!               if(!SeekWithinStream(file, buffer, offset, base))
                {
                        return IL_LOADERR_TRUNCATED;
                }
                offset = base;
!               if(fread(buffer, 1, 4, file) != 4)
                {
                        return IL_LOADERR_TRUNCATED;
--- 211,220 ----
                        return IL_LOADERR_BACKWARDS;
                }
!               if(!SeekWithinStream(ctx, buffer, offset, base))
                {
                        return IL_LOADERR_TRUNCATED;
                }
                offset = base;
!               if((*(ctx->readFunc))(ctx, buffer, 4) != 4)
                {
                        return IL_LOADERR_TRUNCATED;
***************
*** 187,191 ****
                        return IL_LOADERR_NOT_PE;
                }
!               if(fread(buffer, 1, 20, file) != 20)
                {
                        return IL_LOADERR_TRUNCATED;
--- 226,230 ----
                        return IL_LOADERR_NOT_PE;
                }
!               if((*(ctx->readFunc))(ctx, buffer, 20) != 20)
                {
                        return IL_LOADERR_TRUNCATED;
***************
*** 196,200 ****
        {
                /* This is an i386 PE/COFF object file: read the rest of the 
header */
!               if(fread(buffer + 2, 1, 18, file) != 18)
                {
                        return IL_LOADERR_TRUNCATED;
--- 235,239 ----
        {
                /* This is an i386 PE/COFF object file: read the rest of the 
header */
!               if((*(ctx->readFunc))(ctx, buffer + 2, 18) != 18)
                {
                        return IL_LOADERR_TRUNCATED;
***************
*** 209,213 ****
                /* This looks like a Java ".class" or ".jar" file, which
                   we need to pass off to "_ILImageJavaLoad" to handle */
!               return _ILImageJavaLoad(file, filename, context, image, flags, 
buffer);
        }
  #endif
--- 248,260 ----
                /* This looks like a Java ".class" or ".jar" file, which
                   we need to pass off to "_ILImageJavaLoad" to handle */
!               if(ctx->stream)
!               {
!                       return _ILImageJavaLoad(ctx->stream, filename, context,
!                                                                       image, 
flags, buffer);
!               }
!               else
!               {
!                       return IL_LOADERR_NOT_PE;
!               }
        }
  #endif
***************
*** 215,219 ****
        {
                /* This may be an "ar" archive file: read the rest of the 
header */
!               if(fread(buffer, 1, 6, file) != 6)
                {
                        return IL_LOADERR_NOT_PE;
--- 262,266 ----
        {
                /* This may be an "ar" archive file: read the rest of the 
header */
!               if((*(ctx->readFunc))(ctx, buffer, 6) != 6)
                {
                        return IL_LOADERR_NOT_PE;
***************
*** 249,253 ****
        if(headerSize != 0)
        {
!               if(fread(buffer, 1, headerSize, file) != headerSize)
                {
                        return IL_LOADERR_TRUNCATED;
--- 296,300 ----
        if(headerSize != 0)
        {
!               if((*(ctx->readFunc))(ctx, buffer, headerSize) != headerSize)
                {
                        return IL_LOADERR_TRUNCATED;
***************
*** 291,295 ****
        while(numSections > 0)
        {
!               if(fread(buffer, 1, 40, file) != 40)
                {
                        _ILFreeSectionMap(map);
--- 338,342 ----
        while(numSections > 0)
        {
!               if((*(ctx->readFunc))(ctx, buffer, 40) != 40)
                {
                        _ILFreeSectionMap(map);
***************
*** 390,394 ****
  
        /* Seek to the beginning of the first section */
!       if(!SeekWithinStream(file, buffer, offset, minAddress))
        {
                _ILFreeSectionMap(map);
--- 437,441 ----
  
        /* Seek to the beginning of the first section */
!       if(!SeekWithinStream(ctx, buffer, offset, minAddress))
        {
                _ILFreeSectionMap(map);
***************
*** 401,408 ****
           In particular, the IL bytecode can be pretty much anywhere */
        if((flags & IL_LOADFLAG_NO_MAP) == 0 &&
!          ILMapFileToMemory(fileno(file), minAddress, maxAddress,
                                             &mapAddress, &mapLength, &data))
        {
                isMapped = 1;
        }
        else
--- 448,465 ----
           In particular, the IL bytecode can be pretty much anywhere */
        if((flags & IL_LOADFLAG_NO_MAP) == 0 &&
!          ILMapFileToMemory(fileno(ctx->stream), minAddress, maxAddress,
                                             &mapAddress, &mapLength, &data))
        {
                isMapped = 1;
+               isInPlace = 0;
+       }
+       else if(!(ctx->stream) && (flags & IL_LOADFLAG_IN_PLACE) != 0)
+       {
+               /* Execute directly from the supplied buffer */
+               data = (char *)(ctx->buffer);
+               isMapped = 0;
+               mapAddress = 0;
+               mapLength = 0;
+               isInPlace = 1;
        }
        else
***************
*** 414,418 ****
                        return IL_LOADERR_MEMORY;
                }
!               if(fread(data, 1, maxAddress - minAddress, file) !=
                                        (maxAddress - minAddress))
                {
--- 471,475 ----
                        return IL_LOADERR_MEMORY;
                }
!               if((*(ctx->readFunc))(ctx, data, maxAddress - minAddress) !=
                                        (maxAddress - minAddress))
                {
***************
*** 424,427 ****
--- 481,485 ----
                mapAddress = 0;
                mapLength = 0;
+               isInPlace = 0;
        }
  
***************
*** 511,515 ****
                        ILUnmapFileFromMemory(mapAddress, mapLength);
                }
!               else
                {
                        ILFree(data);
--- 569,573 ----
                        ILUnmapFileFromMemory(mapAddress, mapLength);
                }
!               else if(!isInPlace)
                {
                        ILFree(data);
***************
*** 528,532 ****
                                ILUnmapFileFromMemory(mapAddress, mapLength);
                        }
!                       else
                        {
                                ILFree(data);
--- 586,590 ----
                                ILUnmapFileFromMemory(mapAddress, mapLength);
                        }
!                       else if(!isInPlace)
                        {
                                ILFree(data);
***************
*** 542,545 ****
--- 600,604 ----
        (*image)->only32Bit = only32Bit;
        (*image)->mapped = isMapped;
+       (*image)->inPlace = isInPlace;
        (*image)->map = map;
        (*image)->data = data;
***************
*** 571,574 ****
--- 630,644 ----
  }
  
+ int ILImageLoad(FILE *file, const char *filename,
+                               ILContext *context, ILImage **image, int flags)
+ {
+       ILInputContext ctx;
+       ctx.stream = file;
+       ctx.buffer = 0;
+       ctx.bufLen = 0;
+       ctx.readFunc = StdioRead;
+       return ImageLoad(&ctx, filename, context, image, flags);
+ }
+ 
  int ILImageLoadFromFile(const char *filename, ILContext *context,
                                                ILImage **image, int flags, int 
printErrors)
***************
*** 618,621 ****
--- 688,704 ----
        /* Done */
        return loadError;
+ }
+ 
+ int ILImageLoadFromMemory(const void *buffer, unsigned long bufLen,
+                                                 ILContext *context, ILImage 
**image,
+                                                 int flags, const char 
*filename)
+ {
+       ILInputContext ctx;
+       ctx.stream = 0;
+       ctx.buffer = (const char *)buffer;
+       ctx.bufLen = bufLen;
+       ctx.readFunc = MemoryRead;
+       return ImageLoad(&ctx, filename, context,
+                                        image, flags | IL_LOADFLAG_NO_MAP);
  }
  





reply via email to

[Prev in Thread] Current Thread [Next in Thread]