1 /*DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64
4 for INTEL64(R), AMD64(R)
6 Copyright(C) 2007-2009 Koine Yuusuke(koinec). All rights reserved.
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
11 1. Redistributions of source code must retain the above copyright notice,
12 this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY Koine Yuusuke(koinec) ``AS IS'' AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL Koine Yuusuke(koinec) OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 OF THE POSSIBILITY OF SUCH DAMAGE.
29 DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64*/
31 /* File Info -----------------------------------------------------------
35 ----------------------------------------------------------------------*/
37 #define DRD64_SRC_LIBGOBLIN_READBIN
38 #include"drd64_libgoblin.h"
41 /*----------------------------------------------------------------------
42 ----------------------------------------------------------------------*/
43 LIBGOBLIN_READBIN_EXTERN
46 LibGoblin_BinaryInfo *p_binfo )
51 const char *pstr_binfile;
52 char str_binfilepath[DRD64_MAX_PATH];
54 LibFileType_FileType t_ftype;
55 LibGoblin_BinaryFile *p_bfile;
57 assert( NULL != p_binfo );
58 p_bfile = BinaryFile_GetBinaryFile( p_binfo->i_binfile );
59 assert( NULL != p_bfile );
61 if ( LIBGOBLIN_BINFO_PHASE_SETPROG != p_binfo->b_phase ) {
65 strncpy( str_binfilepath, p_bfile->str_localpath, DRD64_MAX_PATH);
66 strcat( str_binfilepath, "/" );
67 strncat( str_binfilepath, p_bfile->str_filename, DRD64_MAX_PATH);
68 pstr_binfile = p_bfile->str_localpath;
69 if( '\0' == pstr_binfile ) {
74 i_fd = open( str_binfilepath, O_RDONLY );
77 goto goto_ReadBinary_File_post;
80 /* Get FileSize & FileType by LibFileType */
81 i_err = LibFileType_CheckFileType( &t_ftype, str_binfilepath, i_fd );
85 goto goto_ReadBinary_File_post;
88 /* XXX : check i_filetype value */
90 /* Memory Mapping from Binary-file */
91 pb_head = mmap( NULL, t_ftype.dw_filesize, PROT_READ, MAP_SHARED, i_fd, 0 );
92 if( MAP_FAILED == pb_head ) {
95 goto goto_ReadBinary_File_post;
98 memcpy( (void *)&(p_bfile->t_ftype), (void *)&t_ftype, sizeof( LibFileType_FileType ));
99 p_bfile->pb_binary = pb_head;
100 p_bfile->dw_size = t_ftype.dw_filesize;
101 p_bfile->i_fd = i_fd;
102 p_bfile->dw_flag = LIBGOBLIN_BINFO_FILE_MMAP;
103 p_binfo->b_phase = LIBGOBLIN_BINFO_PHASE_BINREAD;
105 goto_ReadBinary_File_post:
111 /*----------------------------------------------------------------------
112 ----------------------------------------------------------------------*/
113 LIBGOBLIN_READBIN_EXTERN
116 LibGoblin_BinaryInfo *p_binfo,
121 LibGoblin_BinaryFile *p_bfile;
122 LibFileType_FileType t_ftype;
124 assert( NULL != p_binfo );
126 /* Get FileSize & FileType by LibFileType */
127 i_result = LibFileType_CheckMemoryType( &t_ftype, pb_data, dw_size );
128 if( 0x00 != i_result ) {
132 p_bfile = BinaryFile_GetBinaryFile( p_binfo->i_binfile );
133 assert( NULL != p_bfile );
135 memcpy( (void *)&(p_bfile->t_ftype), (void *)&t_ftype, sizeof( LibFileType_FileType ));
136 p_bfile->pb_binary = pb_data;
137 p_bfile->dw_size = dw_size;
138 p_bfile->dw_flag = LIBGOBLIN_BINFO_FILE_EXTALLOC;
139 p_binfo->b_phase = LIBGOBLIN_BINFO_PHASE_BINREAD;
145 /*----------------------------------------------------------------------
146 ----------------------------------------------------------------------*/
147 LIBGOBLIN_READBIN_EXTERN
149 ReadBinary_FreeBinary(
150 LibGoblin_BinaryInfo *p_binfo )
153 LibGoblin_BinaryFile *p_bfile;
155 assert( NULL != p_binfo );
157 if( LIBGOBLIN_BINFO_PHASE_BINREAD > p_binfo->b_phase ) {
161 for( i_cnt = 0; i_cnt < LIBGOBLIN_BINFO_MAXFILES; i_cnt++ ) {
162 p_bfile = BinaryFile_GetBinaryFile( p_binfo->i_binfile );
164 if( -0x01 == p_bfile->i_fd ) { continue; }
166 if( LIBGOBLIN_BINFO_FILE_MMAP & p_bfile->dw_flag ) {
167 munmap( p_bfile->pb_binary, p_bfile->dw_size );
168 close( p_bfile->i_fd );
170 else if( LIBGOBLIN_BINFO_FILE_INTALLOC & p_bfile->dw_flag ) {
171 free( p_bfile->pb_binary );
172 close( p_bfile->i_fd );
175 p_bfile->pb_binary = NULL;
176 p_bfile->dw_size = 0;
177 p_bfile->i_fd = -0x01;
180 p_binfo->b_phase = LIBGOBLIN_BINFO_PHASE_ALLOCED;
186 /* EOF of drd64_.c ----------------------------------- */