/* $Id$
- * Include file for file handling utilities of libcaptive
+ * Include file for internal (rtl/) file handling utilities of libcaptive
* Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
*
* This program is free software; you can redistribute it and/or modify
*/
-#ifndef _CAPTIVE_FILE_H
-#define _CAPTIVE_FILE_H 1
+#ifndef _CAPTIVE_RTL_FILE_H
+#define _CAPTIVE_RTL_FILE_H 1
#include <glib/gmacros.h>
G_BEGIN_DECLS
-gpointer captive_file_mmap(size_t *lenp,const gchar *path,int open_flags,int mmap_prot,int mmap_flags);
-void captive_file_munmap(gpointer base);
+gpointer captive_rtl_file_mmap(size_t *lenp,const gchar *path,int open_flags,int mmap_prot,int mmap_flags);
+void captive_rtl_file_munmap(gpointer base);
+gpointer captive_rtl_file_read(gint fd,gsize *bufsizep);
G_END_DECLS
-#endif /* _CAPTIVE_FILE_H */
+#endif /* _CAPTIVE_RTL_FILE_H */
#include "captive/ldr.h" /* self */
#include "reactos/internal/ldr.h" /* self */
#include "captive/unicode.h"
-#include "captive/file.h"
+#include "captive/rtl-file.h"
#include <glib/gtypes.h>
#include <glib/gmessages.h>
#include <glib/gmem.h>
*ModuleObjectp=NULL;
/* Open the Module */
- ModuleLoadBase=captive_file_mmap(
+ ModuleLoadBase=captive_rtl_file_mmap(
NULL, /* lenp */
Filename_utf8, /* path */
O_RDONLY, /* open_flags */
err=LdrProcessModule(ModuleLoadBase,Filename,&Module);
if (!NT_SUCCESS(err)) {
g_error("LdrLoadModule(): LdrProcessModule()=0x%08lX",err);
- goto err_captive_file_munmap;
+ goto err_captive_rtl_file_munmap;
}
}
else {
}
/* we were successful */
- captive_file_munmap(ModuleLoadBase);
+ captive_rtl_file_munmap(ModuleLoadBase);
*ModuleObjectp=Module;
/* Hook for KDB on loading a driver. */
return STATUS_SUCCESS;
/* Error recoveries */
-err_captive_file_munmap:
- captive_file_munmap(ModuleLoadBase);
+err_captive_rtl_file_munmap:
+ captive_rtl_file_munmap(ModuleLoadBase);
g_return_val_if_reached(err);
}
/* $Id$
- * File handling utilities of libcaptive
+ * Internal (rtl/) file handling utilities of libcaptive
* Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
*
* This program is free software; you can redistribute it and/or modify
#include "config.h"
-#include "captive/file.h" /* self */
+#include "captive/rtl-file.h" /* self */
#include <glib/gtypes.h>
#include <glib/gmessages.h>
#include <glib/ghash.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
+#include <glib/gstring.h>
+#include <errno.h>
+#include <glib/galloca.h>
+#include "reactos/internal/mm.h" /* for PAGE_SIZE */
+#include <glib/gmem.h>
/* map: address -> GSIZE_TO_POINTER(length) */
-static GHashTable *captive_file_mmap_hash;
+static GHashTable *captive_rtl_file_mmap_hash;
-/* initialize 'captive_file_mmap_hash' */
-static void captive_file_mmap_hash_init(void)
+/* initialize 'captive_rtl_file_mmap_hash' */
+static void captive_rtl_file_mmap_hash_init(void)
{
- if (captive_file_mmap_hash)
+ if (captive_rtl_file_mmap_hash)
return;
- captive_file_mmap_hash=g_hash_table_new(g_direct_hash,g_direct_equal);
+ captive_rtl_file_mmap_hash=g_hash_table_new(g_direct_hash,g_direct_equal);
}
/**
- * captive_file_mmap:
+ * captive_rtl_file_mmap:
* @lenp: returns the file length if successful. %NULL pointer permitted.
* @path: File pathname to open(2).
* @open_flags: open(2) parameter #flags such as %O_RDONLY.
*
* Returns: address base with mmap(2)ed file (and @lenp filled) or #NULL if error.
*/
-gpointer captive_file_mmap(size_t *lenp,const gchar *path,int open_flags,int mmap_prot,int mmap_flags)
+gpointer captive_rtl_file_mmap(size_t *lenp,const gchar *path,int open_flags,int mmap_prot,int mmap_flags)
{
gpointer r;
int fd;
size_t len;
off_t lenoff;
- captive_file_mmap_hash_init();
+ captive_rtl_file_mmap_hash_init();
/* FIXME: use g_filename_from_utf8() on 'path' */
fd=open(path,open_flags);
if (fd==-1) {
- g_error("captive_file_mmap(\"%s\"): open: %m",path);
+ g_error("captive_rtl_file_mmap(\"%s\"): open: %m",path);
goto err;
}
/* should make no difference with g_hash_table_replace()
* as we are using (g_direct_hash,g_direct_equal)
*/
- g_hash_table_insert(captive_file_mmap_hash,
+ g_hash_table_insert(captive_rtl_file_mmap_hash,
r,GSIZE_TO_POINTER(len)); /* key,value */
return r;
}
/**
- * captive_file_munmap:
- * @base: file base address returned by captive_file_mmap(). %NULL pointer forbidden.
+ * captive_rtl_file_munmap:
+ * @base: file base address returned by captive_rtl_file_mmap(). %NULL pointer forbidden.
*
* munmap(2) the whole file and destroy all its resources.
*/
-void captive_file_munmap(gpointer base)
+void captive_rtl_file_munmap(gpointer base)
{
gpointer len_ptr; /* GSIZE_TO_POINTER(off_t len) */
gboolean found;
g_return_if_fail(base!=NULL);
- captive_file_mmap_hash_init();
+ captive_rtl_file_mmap_hash_init();
/* report if 'base' not found */
- found=g_hash_table_lookup_extended(captive_file_mmap_hash,
+ found=g_hash_table_lookup_extended(captive_rtl_file_mmap_hash,
base, /* lookup_key */
NULL, /* orig_key */
&len_ptr); /* value */
g_return_if_fail(found==TRUE);
/* remove 'base' from our database */
- found=g_hash_table_remove(captive_file_mmap_hash,
+ found=g_hash_table_remove(captive_rtl_file_mmap_hash,
base); /* key */
g_return_if_fail(found==TRUE); /* just a warning would be also possible */
/* munmap() has no return value */
munmap(base,GPOINTER_TO_SIZE(len_ptr));
}
+
+
+/**
+ * captive_rtl_file_read:
+ * @fd: file-descriptor to read data from.
+ * @bufsizep: Returns the size of resulting data.
+ * %NULL pointer is permitted.
+ *
+ * Reads the whole file into memory.
+ *
+ * Returns: address base of the memory being filled with file contents.
+ * Free the area by g_free() if no longer used.
+ */
+gpointer captive_rtl_file_read(gint fd,gsize *bufsizep)
+{
+off_t lenoff,gotoff;
+GString *r_GString;
+void *buf;
+ssize_t readgot;
+
+ errno=0;
+ lenoff=lseek(fd,0,SEEK_END);
+ switch (lenoff) {
+ case (off_t)-1:
+ if (errno!=ESPIPE)
+ g_assert_not_reached();
+ lenoff=PAGE_SIZE;
+ break;
+ case 0:
+ return NULL;
+ default:
+ gotoff=lseek(fd,0,SEEK_SET);
+ g_assert(gotoff==0);
+ }
+ r_GString=g_string_sized_new(lenoff);
+ buf=g_malloc(lenoff); /* do not use g_alloca() - the buffer may be big! */
+ while ((readgot=read(fd,buf,lenoff))>0) {
+ g_assert(readgot<=lenoff);
+ r_GString=g_string_append_len(r_GString,buf,readgot);
+ }
+ g_assert(readgot==0); /* EOF */
+ g_free(buf);
+
+ if (bufsizep)
+ *bufsizep=r_GString->len;
+ return g_string_free(r_GString,
+ FALSE); /* free_segment */
+}