2009-05-20 Miguel de Icaza <miguel@novell.com>
authorMiguel de Icaza <miguel@gnome.org>
Wed, 20 May 2009 23:40:54 +0000 (23:40 -0000)
committerMiguel de Icaza <miguel@gnome.org>
Wed, 20 May 2009 23:40:54 +0000 (23:40 -0000)
* locking.c: Split locking code into a separate file.

svn path=/trunk/mono/; revision=134486

mono/io-layer/versioninfo.c

index 8c80a3c8bbd685651efd96a396bad3302cd4c16d..90dbb344097310501b67622b9c12a73545e5328f 100644 (file)
@@ -11,7 +11,6 @@
 #include <glib.h>
 #include <string.h>
 #include <pthread.h>
-#include <sys/mman.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <mono/io-layer/io-portability.h>
 #include <mono/io-layer/error.h>
 #include <mono/utils/strenc.h>
+#include <mono/utils/mono-mmap.h>
 
 #define LOGDEBUG(...)  
 //define LOGDEBUG(...) g_message(__VA_ARGS__)
 
 #define ALIGN32(ptr) ptr = (gpointer)((char *)ptr + 3); ptr = (gpointer)((char *)ptr - ((gsize)ptr & 3));
 
-static WapiImageSectionHeader *get_enclosing_section_header (guint32 rva, WapiImageNTHeaders32 *nt_headers)
+static WapiImageSectionHeader *
+get_enclosing_section_header (guint32 rva, WapiImageNTHeaders32 *nt_headers)
 {
        WapiImageSectionHeader *section = _WAPI_IMAGE_FIRST_SECTION32 (nt_headers);
        guint32 i;
@@ -53,8 +54,8 @@ static WapiImageSectionHeader *get_enclosing_section_header (guint32 rva, WapiIm
 /* This works for both 32bit and 64bit files, as the differences are
  * all after the section header block
  */
-static gpointer get_ptr_from_rva (guint32 rva, WapiImageNTHeaders32 *ntheaders,
-                                 gpointer file_map)
+static gpointer
+get_ptr_from_rva (guint32 rva, WapiImageNTHeaders32 *ntheaders, gpointer file_map)
 {
        WapiImageSectionHeader *section_header;
        guint32 delta;
@@ -70,12 +71,13 @@ static gpointer get_ptr_from_rva (guint32 rva, WapiImageNTHeaders32 *ntheaders,
        return((guint8 *)file_map + rva - delta);
 }
 
-static gpointer scan_resource_dir (WapiImageResourceDirectory *root,
-                                  WapiImageNTHeaders32 *nt_headers,
-                                  gpointer file_map,
-                                  WapiImageResourceDirectoryEntry *entry,
-                                  int level, guint32 res_id, guint32 lang_id,
-                                  guint32 *size)
+static gpointer
+scan_resource_dir (WapiImageResourceDirectory *root,
+                  WapiImageNTHeaders32 *nt_headers,
+                  gpointer file_map,
+                  WapiImageResourceDirectoryEntry *entry,
+                  int level, guint32 res_id, guint32 lang_id,
+                  guint32 *size)
 {
        WapiImageResourceDirectoryEntry swapped_entry;
        gboolean is_string, is_dir;
@@ -144,9 +146,10 @@ static gpointer scan_resource_dir (WapiImageResourceDirectory *root,
        }
 }
 
-static gpointer find_pe_file_resources32 (gpointer file_map, guint32 map_size,
-                                         guint32 res_id, guint32 lang_id,
-                                         guint32 *size)
+static gpointer
+find_pe_file_resources32 (gpointer file_map, guint32 map_size,
+                         guint32 res_id, guint32 lang_id,
+                         guint32 *size)
 {
        WapiImageDosHeader *dos_header;
        WapiImageNTHeaders32 *nt_headers;
@@ -217,9 +220,10 @@ static gpointer find_pe_file_resources32 (gpointer file_map, guint32 map_size,
        return(NULL);
 }
 
-static gpointer find_pe_file_resources64 (gpointer file_map, guint32 map_size,
-                                         guint32 res_id, guint32 lang_id,
-                                         guint32 *size)
+static gpointer
+find_pe_file_resources64 (gpointer file_map, guint32 map_size,
+                         guint32 res_id, guint32 lang_id,
+                         guint32 *size)
 {
        WapiImageDosHeader *dos_header;
        WapiImageNTHeaders64 *nt_headers;
@@ -291,9 +295,10 @@ static gpointer find_pe_file_resources64 (gpointer file_map, guint32 map_size,
        return(NULL);
 }
 
-static gpointer find_pe_file_resources (gpointer file_map, guint32 map_size,
-                                       guint32 res_id, guint32 lang_id,
-                                       guint32 *size)
+static gpointer
+find_pe_file_resources (gpointer file_map, guint32 map_size,
+                       guint32 res_id, guint32 lang_id,
+                       guint32 *size)
 {
        /* Figure this out when we support 64bit PE files */
        if (1) {
@@ -305,7 +310,8 @@ static gpointer find_pe_file_resources (gpointer file_map, guint32 map_size,
        }
 }
 
-static gpointer map_pe_file (gunichar2 *filename, guint32 *map_size)
+static gpointer
+map_pe_file (gunichar2 *filename, gint32 *map_size, void **handle)
 {
        gchar *filename_ext;
        int fd;
@@ -344,7 +350,7 @@ static gpointer map_pe_file (gunichar2 *filename, guint32 *map_size)
                return(NULL);
        }
        *map_size = statbuf.st_size;
-
+       
        /* Check basic file size */
        if (statbuf.st_size < sizeof(WapiImageDosHeader)) {
                LOGDEBUG ("%s: File %s is too small: %lld", __func__, filename_ext, statbuf.st_size);
@@ -355,8 +361,8 @@ static gpointer map_pe_file (gunichar2 *filename, guint32 *map_size)
                return(NULL);
        }
        
-       file_map = mmap (NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
-       if (file_map == MAP_FAILED) {
+       file_map = mono_file_map (statbuf.st_size, MONO_MMAP_READ | MONO_MMAP_PRIVATE, fd, 0, handle);
+       if (file_map == NULL) {
                LOGDEBUG ("%s: Error mmap()int file %s: %s", __func__, filename_ext, strerror (errno));
 
                SetLastError (_wapi_get_win32_file_error (errno));
@@ -372,12 +378,14 @@ static gpointer map_pe_file (gunichar2 *filename, guint32 *map_size)
        return(file_map);
 }
 
-static void unmap_pe_file (gpointer file_map, guint32 map_size)
+static void
+unmap_pe_file (gpointer file_map, void *handle)
 {
-       munmap (file_map, map_size);
+       mono_file_unmap (file_map, handle);
 }
 
-static guint32 unicode_chars (const gunichar2 *str)
+static guint32
+unicode_chars (const gunichar2 *str)
 {
        guint32 len = 0;
        
@@ -389,7 +397,8 @@ static guint32 unicode_chars (const gunichar2 *str)
        } while(1);
 }
 
-static gboolean unicode_compare (const gunichar2 *str1, const gunichar2 *str2)
+static gboolean
+unicode_compare (const gunichar2 *str1, const gunichar2 *str2)
 {
        while (*str1 && *str2) {
                if (*str1 != *str2) {
@@ -405,7 +414,8 @@ static gboolean unicode_compare (const gunichar2 *str1, const gunichar2 *str2)
 /* compare a little-endian null-terminated utf16 string and a normal string.
  * Can be used only for ascii or latin1 chars.
  */
-static gboolean unicode_string_equals (const gunichar2 *str1, const gchar *str2)
+static gboolean
+unicode_string_equals (const gunichar2 *str1, const gchar *str2)
 {
        while (*str1 && *str2) {
                if (GUINT16_TO_LE (*str1) != *str2) {
@@ -429,8 +439,8 @@ typedef struct
 /* Returns a pointer to the value data, because there's no way to know
  * how big that data is (value_len is set to zero for most blocks :-( )
  */
-static gconstpointer get_versioninfo_block (gconstpointer data,
-                                           version_data *block)
+static gconstpointer
+get_versioninfo_block (gconstpointer data, version_data *block)
 {
        block->data_len = GUINT16_FROM_LE (*((guint16 *)data));
        data = (char *)data + sizeof(guint16);
@@ -451,8 +461,8 @@ static gconstpointer get_versioninfo_block (gconstpointer data,
        return(data);
 }
 
-static gconstpointer get_fixedfileinfo_block (gconstpointer data,
-                                             version_data *block)
+static gconstpointer
+get_fixedfileinfo_block (gconstpointer data, version_data *block)
 {
        gconstpointer data_ptr;
        gint32 data_len; /* signed to guard against underflow */
@@ -483,8 +493,8 @@ static gconstpointer get_fixedfileinfo_block (gconstpointer data,
        return(data_ptr);
 }
 
-static gconstpointer get_varfileinfo_block (gconstpointer data_ptr,
-                                           version_data *block)
+static gconstpointer
+get_varfileinfo_block (gconstpointer data_ptr, version_data *block)
 {
        /* data is pointing at a Var block
         */
@@ -493,11 +503,12 @@ static gconstpointer get_varfileinfo_block (gconstpointer data_ptr,
        return(data_ptr);
 }
 
-static gconstpointer get_string_block (gconstpointer data_ptr,
-                                      const gunichar2 *string_key,
-                                      gpointer *string_value,
-                                      guint32 *string_value_len,
-                                      version_data *block)
+static gconstpointer
+get_string_block (gconstpointer data_ptr,
+                 const gunichar2 *string_key,
+                 gpointer *string_value,
+                 guint32 *string_value_len,
+                 version_data *block)
 {
        guint16 data_len = block->data_len;
        guint16 string_len = 28; /* Length of the StringTable block */
@@ -545,12 +556,13 @@ static gconstpointer get_string_block (gconstpointer data_ptr,
  *
  * If lang == NULL it means we're just stepping through this block
  */
-static gconstpointer get_stringtable_block (gconstpointer data_ptr,
-                                           gchar *lang,
-                                           const gunichar2 *string_key,
-                                           gpointer *string_value,
-                                           guint32 *string_value_len,
-                                           version_data *block)
+static gconstpointer
+get_stringtable_block (gconstpointer data_ptr,
+                      gchar *lang,
+                      const gunichar2 *string_key,
+                      gpointer *string_value,
+                      guint32 *string_value_len,
+                      version_data *block)
 {
        guint16 data_len = block->data_len;
        guint16 string_len = 36; /* length of the StringFileInfo block */
@@ -611,8 +623,8 @@ static gconstpointer get_stringtable_block (gconstpointer data_ptr,
 }
 
 #if G_BYTE_ORDER == G_BIG_ENDIAN
-static gconstpointer big_up_string_block (gconstpointer data_ptr,
-                                         version_data *block)
+static gconstpointer
+big_up_string_block (gconstpointer data_ptr, version_data *block)
 {
        guint16 data_len = block->data_len;
        guint16 string_len = 28; /* Length of the StringTable block */
@@ -678,8 +690,8 @@ static gconstpointer big_up_string_block (gconstpointer data_ptr,
  * because the data length does not include padding bytes, so it's not
  * possible to just return the start position + length
  */
-static gconstpointer big_up_stringtable_block (gconstpointer data_ptr,
-                                              version_data *block)
+static gconstpointer
+big_up_stringtable_block (gconstpointer data_ptr, version_data *block)
 {
        guint16 data_len = block->data_len;
        guint16 string_len = 36; /* length of the StringFileInfo block */
@@ -730,7 +742,8 @@ static gconstpointer big_up_stringtable_block (gconstpointer data_ptr,
 /* Follows the data structures and turns all UTF-16 strings from the
  * LE found in the resource section into UTF-16BE
  */
-static void big_up (gconstpointer datablock, guint32 size)
+static void
+big_up (gconstpointer datablock, guint32 size)
 {
        gconstpointer data_ptr;
        gint32 data_len; /* signed to guard against underflow */
@@ -800,8 +813,8 @@ static void big_up (gconstpointer datablock, guint32 size)
 }
 #endif
 
-gboolean VerQueryValue (gconstpointer datablock, const gunichar2 *subblock,
-                       gpointer *buffer, guint32 *len)
+gboolean
+VerQueryValue (gconstpointer datablock, const gunichar2 *subblock, gpointer *buffer, guint32 *len)
 {
        gchar *subblock_utf8, *lang_utf8 = NULL;
        gboolean ret = FALSE;
@@ -915,23 +928,24 @@ gboolean VerQueryValue (gconstpointer datablock, const gunichar2 *subblock,
        return(ret);
 }
 
-guint32 GetFileVersionInfoSize (gunichar2 *filename, guint32 *handle)
+guint32
+GetFileVersionInfoSize (gunichar2 *filename, guint32 *handle)
 {
        gpointer file_map;
        gpointer versioninfo;
-       guint32 map_size;
+       void *map_handle;
+       gint32 map_size;
        guint32 size;
-       
+
        /* This value is unused, but set to zero */
        *handle = 0;
        
-       file_map = map_pe_file (filename, &map_size);
+       file_map = map_pe_file (filename, &map_size, &map_handle);
        if (file_map == NULL) {
                return(0);
        }
        
-       versioninfo = find_pe_file_resources (file_map, map_size, RT_VERSION,
-                                             0, &size);
+       versioninfo = find_pe_file_resources (file_map, map_size, RT_VERSION, 0, &size);
        if (versioninfo == NULL) {
                /* Didn't find the resource, so set the return value
                 * to 0
@@ -939,21 +953,22 @@ guint32 GetFileVersionInfoSize (gunichar2 *filename, guint32 *handle)
                size = 0;
        }
 
-       unmap_pe_file (file_map, map_size);
+       unmap_pe_file (file_map, map_handle);
 
        return(size);
 }
 
-gboolean GetFileVersionInfo (gunichar2 *filename, guint32 handle G_GNUC_UNUSED,
-                            guint32 len, gpointer data)
+gboolean
+GetFileVersionInfo (gunichar2 *filename, guint32 handle G_GNUC_UNUSED, guint32 len, gpointer data)
 {
        gpointer file_map;
        gpointer versioninfo;
-       guint32 map_size;
+       void *map_handle;
+       gint32 map_size;
        guint32 size;
        gboolean ret = FALSE;
        
-       file_map = map_pe_file (filename, &map_size);
+       file_map = map_pe_file (filename, &map_size, &map_handle);
        if (file_map == NULL) {
                return(FALSE);
        }
@@ -974,13 +989,13 @@ gboolean GetFileVersionInfo (gunichar2 *filename, guint32 handle G_GNUC_UNUSED,
 #endif
        }
 
-       unmap_pe_file (file_map, map_size);
+       unmap_pe_file (file_map, &map_handle);
        
        return(ret);
 }
 
-static guint32 copy_lang (gunichar2 *lang_out, guint32 lang_len,
-                         const gchar *text)
+static guint32
+copy_lang (gunichar2 *lang_out, guint32 lang_len, const gchar *text)
 {
        gunichar2 *unitext;
        int chars = strlen (text);
@@ -1004,7 +1019,8 @@ static guint32 copy_lang (gunichar2 *lang_out, guint32 lang_len,
        return(ret);
 }
 
-guint32 VerLanguageName (guint32 lang, gunichar2 *lang_out, guint32 lang_len)
+guint32
+VerLanguageName (guint32 lang, gunichar2 *lang_out, guint32 lang_len)
 {
        int primary, secondary;