OSDN Git Service

2018/11/02(Fri) 21:34
[drdeamon64/drdeamon64.git] / libgoblin / drd64_libgoblin_readbin.c
1 /*DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64
2
3                          D r . D e a m o n  6 4
4                         for INTEL64(R), AMD64(R)
5         
6    Copyright(C) 2007-2009 Koine Yuusuke(koinec). All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
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.
16
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.
28
29 DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64*/
30
31 /* File Info -----------------------------------------------------------
32 File: drd64_.c
33 Function: 
34 Comment: 
35 ----------------------------------------------------------------------*/
36
37 #define DRD64_SRC_LIBGOBLIN_READBIN
38 #include"drd64_libgoblin.h"
39
40
41 /*----------------------------------------------------------------------
42 ----------------------------------------------------------------------*/
43 LIBGOBLIN_READBIN_EXTERN
44 int
45         ReadBinary_File(
46                         LibGoblin_BinaryInfo    *p_binfo )
47 {
48         int             i_result        = 0x00;
49         int             i_err;
50         int             i_fd    = -0x01;
51         const char      *pstr_binfile;
52         char    str_binfilepath[DRD64_MAX_PATH];
53         Byte    *pb_head;
54         LibFileType_FileType    t_ftype;
55         LibGoblin_BinaryFile    *p_bfile;
56
57         assert( NULL != p_binfo );
58         p_bfile = BinaryFile_GetBinaryFile( p_binfo->i_binfile );
59         assert( NULL != p_bfile );
60
61         if ( LIBGOBLIN_BINFO_PHASE_SETPROG != p_binfo->b_phase )        {
62                 return -0x01;
63         }
64                 
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 )      {
70                 return -0x02;
71         }
72
73         /* Open Test LM */
74         i_fd    = open( str_binfilepath, O_RDONLY );
75         if( -1 >= i_fd )        {
76                 i_result        = -0x10;
77                 goto    goto_ReadBinary_File_post;
78         }
79
80         /* Get FileSize & FileType by LibFileType */
81         i_err   = LibFileType_CheckFileType( &t_ftype, str_binfilepath, i_fd );
82         if( 0x00 != i_err )     {
83                 close( i_fd );
84                 i_result        = -0x11;
85                 goto    goto_ReadBinary_File_post;
86         }
87
88         /* XXX : check i_filetype value */
89
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 )             {
93                 close( i_fd );
94                 i_result        = -0x12;
95                 goto    goto_ReadBinary_File_post;
96         }
97
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;
104
105 goto_ReadBinary_File_post:
106
107         return i_result;
108 }
109
110
111 /*----------------------------------------------------------------------
112 ----------------------------------------------------------------------*/
113 LIBGOBLIN_READBIN_EXTERN
114 int
115         ReadBinary_Memory(
116                         LibGoblin_BinaryInfo    *p_binfo,
117                         Byte    *pb_data,
118                         DWord   dw_size )
119 {
120         int             i_result;
121         LibGoblin_BinaryFile    *p_bfile;
122         LibFileType_FileType    t_ftype;
123
124         assert( NULL != p_binfo );
125
126         /* Get FileSize & FileType by LibFileType */
127         i_result        = LibFileType_CheckMemoryType( &t_ftype, pb_data, dw_size );
128         if( 0x00 != i_result )  {
129                 return -0x01;
130         }
131
132         p_bfile = BinaryFile_GetBinaryFile( p_binfo->i_binfile );
133         assert( NULL != p_bfile );
134
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;
140
141         return i_result;
142 }
143
144
145 /*----------------------------------------------------------------------
146 ----------------------------------------------------------------------*/
147 LIBGOBLIN_READBIN_EXTERN
148 int
149         ReadBinary_FreeBinary(
150                         LibGoblin_BinaryInfo    *p_binfo )
151 {
152         int             i_cnt;
153         LibGoblin_BinaryFile    *p_bfile;
154
155         assert( NULL != p_binfo );
156
157         if( LIBGOBLIN_BINFO_PHASE_BINREAD > p_binfo->b_phase )  {
158                 return -0x01;
159         }
160
161         for( i_cnt = 0; i_cnt < LIBGOBLIN_BINFO_MAXFILES; i_cnt++ )     {
162                 p_bfile = BinaryFile_GetBinaryFile( p_binfo->i_binfile );
163
164                 if( -0x01 == p_bfile->i_fd )    { continue; }
165
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 );
169                 }
170                 else if( LIBGOBLIN_BINFO_FILE_INTALLOC & p_bfile->dw_flag )     {
171                         free( p_bfile->pb_binary );
172                         close( p_bfile->i_fd );
173                 }
174
175                 p_bfile->pb_binary      = NULL;
176                 p_bfile->dw_size        = 0;
177                 p_bfile->i_fd           = -0x01;
178         }
179
180         p_binfo->b_phase        = LIBGOBLIN_BINFO_PHASE_ALLOCED;
181         
182         return 0x00;
183 }
184
185
186 /* EOF of drd64_.c ----------------------------------- */