OSDN Git Service

2018/11/02(Fri) 21:34
[drdeamon64/drdeamon64.git] / libgoblin / drd64_libgoblin_binfo.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_BINFO
38 #include"drd64_libgoblin.h"
39
40
41 /*----------------------------------------------------------------------
42 ----------------------------------------------------------------------*/
43 LIBGOBLIN_BINFO_EXTERN
44 LibGoblin_BinaryInfo *
45         BinaryInfo_GetBinInfo(
46                 int     i_bid )
47 {
48         if(( gi_max_binary_info <= i_bid ) || ( 0 > i_bid ))
49                 { return NULL; }
50         
51         return BINFO(i_bid);
52 }
53
54
55 /*----------------------------------------------------------------------
56 ----------------------------------------------------------------------*/
57 LIBGOBLIN_BINFO_EXTERN
58 const char *
59         BinaryInfo_GetRPath(
60                 LibGoblin_BinaryInfo    *p_binfo,
61                 char    *pstr_rpath )
62 {
63         const char                              *pcstr_result;
64         LibGoblin_BinaryFile    *p_binfile;
65
66         assert( NULL != p_binfo );
67         p_binfile       = BinaryFile_GetBinaryFile( p_binfo->i_binfile );
68         assert( NULL != p_binfile );
69
70         if( NULL != pstr_rpath )        {
71                 strncpy( pstr_rpath, p_binfile->str_rpath, DRD64_MAX_PATH );
72                 pcstr_result    = (const char *)pstr_rpath;
73         }
74         else    {
75                 pcstr_result    = (const char *)p_binfile->str_rpath;
76         }
77
78         return pcstr_result;
79 }
80
81
82 /***********************************************************************
83 ***********************************************************************/
84 LIBGOBLIN_API_BINFO
85 const char *
86         LibGoblin_GetRPath_fromBinInfo(
87                 int     i_bid,
88                 char    *pstr_rpath )
89 {
90         const char                              *pcstr_result;
91         LibGoblin_BinaryInfo    *p_binfo;
92
93         p_binfo = BinaryInfo_GetBinInfo( i_bid );
94         if( NULL == p_binfo )   {
95                 return NULL;
96         }
97
98         pcstr_result    = BinaryInfo_GetRPath( p_binfo, pstr_rpath );
99
100         return pcstr_result;
101 }
102
103
104 /*----------------------------------------------------------------------
105 ----------------------------------------------------------------------*/
106 void
107         BinaryInfo_ClearBinInfo(
108                         LibGoblin_BinaryInfo    *p_binfo )
109 {
110         assert( NULL != p_binfo );
111
112         memset( p_binfo, 0x00, sizeof( LibGoblin_BinaryInfo ) );
113
114 /*
115         if( NULL != p_binfo->p_binfile )        {
116                 p_binfo->p_binfile->i_fd        = -0x01;
117         }
118 */
119
120         p_binfo->i_id                   = -0x01;
121         p_binfo->i_binfo_next   = -0x01;
122         p_binfo->i_binfile              = -0x01;
123
124         return;
125 }
126
127
128 /*----------------------------------------------------------------------
129 ----------------------------------------------------------------------*/
130 LIBGOBLIN_BINFO_EXTERN
131 int
132         BinInfo_AppendChain(
133                         LibGoblin_BinaryInfo    *p_binfo_now )
134 {
135         LibGoblin_BinaryInfo    *p_binfo_top;
136         LibGoblin_BinaryInfo    *p_binfo_end;
137
138         assert( NULL != p_binfo_now );
139
140         // Search Parent BinaryInfo ---
141         p_binfo_top     = p_binfo_now;
142         while( -1 < p_binfo_top->i_parent_bid )
143                 { p_binfo_top   = BINFO( p_binfo_top->i_parent_bid ); }
144
145         if( p_binfo_now == p_binfo_top )
146                 { return 0x00; }
147
148         // Search & Get ChainEnd BinaryInfo ---
149         p_binfo_end     = p_binfo_top;
150         while( ( -1 != p_binfo_end->i_binfo_next )
151                                 && ( p_binfo_end !=  p_binfo_now ) )    {
152                 p_binfo_end     = BINFO( p_binfo_end->i_binfo_next);
153         }
154
155         assert( NULL != p_binfo_end );
156
157         p_binfo_end->i_binfo_next       = BINFOID(p_binfo_now);
158
159         return 0x00;
160 }
161
162
163 /*----------------------------------------------------------------------
164 ----------------------------------------------------------------------*/
165 LIBGOBLIN_BINFO_EXTERN
166 int
167         BinInfo_DeleteChain(
168                         LibGoblin_BinaryInfo    *p_binfo_del )
169 {
170         LibGoblin_BinaryInfo    *p_binfo_top;
171         LibGoblin_BinaryInfo    *p_binfo_now;
172         LibGoblin_BinaryInfo    *p_binfo_before;
173         LibGoblin_BinaryInfo    *p_binfo_next;
174
175         assert( NULL != p_binfo_del );
176
177         // Search Parent BinaryInfo ---
178         p_binfo_top     = p_binfo_del;
179         while( -1 < p_binfo_top->i_parent_bid )
180                 { p_binfo_top   = BINFO( p_binfo_top->i_parent_bid ); }
181
182         // Search & Get ChainEnd & ChainEnd-1 BinaryInfo ---
183         p_binfo_before  = NULL;
184         p_binfo_now             = p_binfo_top;
185         while( p_binfo_now !=  p_binfo_del )    {
186                 assert( NULL != p_binfo_now );
187                 p_binfo_before  = p_binfo_now;
188                 p_binfo_now             = BINFO(p_binfo_now->i_binfo_next);
189         }
190
191         if( NULL != p_binfo_before )    {
192                 p_binfo_next    = BINFO(p_binfo_now->i_binfo_next);
193                 p_binfo_before->i_binfo_next    = BINFOID(p_binfo_next);
194         }
195         p_binfo_del->i_binfo_next               = -1;
196
197         return 0x00;
198 }
199
200
201 /*----------------------------------------------------------------------
202 ----------------------------------------------------------------------*/
203 LIBGOBLIN_BINFO_EXTERN
204 int
205         BinaryInfo_SetRPath(
206                         LibGoblin_BinaryInfo    *p_binfo,
207                         const char      *pstr_rpath )
208 {
209         int                     i_len;
210         char            str_src[MAXPATHLEN];
211         char            str_dest[MAXPATHLEN];
212         char            *pstr_result;
213         char            *pstr_rbasepath;
214         LibGoblin_BinaryFile    *p_binfile;
215
216         if( LIBGOBLIN_BINFO_PHASE_SETPROG > p_binfo->b_phase )  {
217                 return -0x01;
218         }
219
220         p_binfile       = BinaryFile_GetBinaryFile( p_binfo->i_binfile );
221         assert( NULL != p_binfile );
222
223         memset( str_src, 0x00, MAXPATHLEN );
224
225         // p_binfo->str_path is current path 
226         if( 0x00 != p_binfile->str_remotepath[0] )
227                 { pstr_rbasepath        = p_binfile->str_remotepath; }
228         else
229                 { pstr_rbasepath        = p_binfile->str_localpath; }
230
231
232         i_len   = strnlen( pstr_rbasepath, DRD64_MAX_PATH );
233         strncpy( str_src, pstr_rbasepath, i_len + 1 ); 
234         str_src[ i_len ]        = '/';
235         strncat( str_src, pstr_rpath, (MAXPATHLEN - i_len - 1) ); 
236         
237         pstr_result     = realpath( str_src, str_dest );
238         if( NULL == pstr_result )       {
239                 return -0x02;
240         }
241
242         i_len   = strnlen( str_dest, MAXPATHLEN );
243         if( DRD64_MAX_PATH <= i_len )   {
244                 return -0x03;
245         }
246         strncpy( p_binfile->str_rpath, str_dest, DRD64_MAX_PATH );
247
248         return 0x00;
249 }
250
251
252 /*----------------------------------------------------------------------
253   pstr_progpath: Absolute & Relative Path in this-machine program.
254                  Absolute Path ONLY in remote-machine program.
255   * pstr_progpath != pstr_realpath is remote-machine program.
256 ----------------------------------------------------------------------*/
257 LIBGOBLIN_BINFO_EXTERN
258 int
259         BinaryInfo_SetProgramPath(
260                         LibGoblin_BinaryInfo    *p_binfo,
261                         const char      *pstr_remotepath,
262                         const char      *pstr_localpath )
263 {
264         int                     i_len;
265         char            str_local[MAXPATHLEN];
266         char            str_remote[MAXPATHLEN];
267         char            *pstr_result;
268         char            *pstr_remote;
269         
270         LibGoblin_BinaryFile    *p_bfile;
271
272         assert( NULL != p_binfo );
273
274         // Check BinaryInfo & BinaryFile struct status ---
275         if( LIBGOBLIN_BINFO_PHASE_SETPROG <= p_binfo->b_phase ) {
276                 return -0x01;
277         }
278
279         // localpath convert to RelativePath to AbsolutePath ---
280         pstr_result     = realpath( pstr_localpath, str_local );
281         if( NULL == pstr_result )       {
282                 return -0x04;
283         }
284         i_len   = strnlen( str_local, DRD64_MAX_PATH );
285         if( '/' == str_local[ i_len ] ) { str_local[ i_len ] = '\0'; }
286
287         // remotepath convert to RelativePath to AbsolutePath ---
288         pstr_remote     = NULL;
289         if( NULL != pstr_remotepath )   {
290                 if( '\0' != *pstr_remotepath )  {
291                         pstr_result     = realpath( pstr_remotepath, str_remote );
292                         if( NULL == pstr_result )       {
293                                 return -0x05;
294                         }
295                         i_len   = strnlen( str_remote, DRD64_MAX_PATH );
296                         if( '/' == str_remote[ i_len ] )        { str_remote[ i_len ] = '\0'; }
297
298                         pstr_remote     = str_remote;
299                 }
300         }
301
302         // Alloc BinaryFile struct & Set File Path ---
303         p_bfile = BinaryFile_DispenseBinaryFile( p_binfo, str_local, pstr_remote);
304         if( NULL == p_bfile )   {
305                 return -0x06;
306         }
307
308         p_binfo->i_binfile      = p_bfile->i_id;
309         p_binfo->b_phase        = LIBGOBLIN_BINFO_PHASE_SETPROG;
310
311         return 0x00;
312 }
313
314
315 /*----------------------------------------------------------------------
316 ----------------------------------------------------------------------*/
317 LIBGOBLIN_BINFO_EXTERN
318 LibGoblin_BinaryInfo *
319         BinaryInfo_AllocBinInfo(
320                         LibGoblin_ProgramInfo   *p_pginfo,
321                         const char *pstr_filename,
322                         int     i_parent_bid )
323 {
324         int             i_cnt;
325         int             i_id_now;
326         LibGoblin_BinaryInfo *p_binfo_base;
327         LibGoblin_BinaryInfo *p_binfo_now;
328         
329         p_binfo_now     = NULL;
330         i_id_now        = -1;
331         
332         if( gi_now_binary_info < gi_max_binary_info )   {
333                 for( i_cnt = 0; i_cnt < gi_max_binary_info; i_cnt++ )   {
334
335                         if( -1 == BINFO(i_cnt)->i_id )  {
336                                 p_binfo_now     = BINFO(i_cnt);
337                                 i_id_now        = i_cnt;
338                                 gi_now_binary_info++;
339                                 break;
340                         }
341                 }
342         }
343         else if( gi_now_binary_info == gi_alloc_binary_info )   {
344
345                 p_binfo_base    = realloc( gp_binary_info,
346                                                                         sizeof( LibGoblin_BinaryInfo ) *
347                                                                         (gi_alloc_binary_info + LIBGOBLIN_BINFO_UNITS) );
348                 if( NULL == p_binfo_base )      {
349                         return NULL;
350                 }
351
352                 for( i_cnt = gi_alloc_binary_info;
353                                         i_cnt < (gi_alloc_binary_info + LIBGOBLIN_BINFO_UNITS); i_cnt++ )       {
354                         BinaryInfo_ClearBinInfo( p_binfo_base + i_cnt );
355                 }
356                 
357                 gp_binary_info                  = p_binfo_base;
358                 gi_alloc_binary_info    += LIBGOBLIN_BINFO_UNITS; 
359
360                 p_binfo_now     = BINFO(gi_now_binary_info);
361                 i_id_now        = gi_now_binary_info++;
362
363                 if( i_id_now == gi_max_binary_info )    { gi_max_binary_info++; }
364         }
365         else    {
366                 p_binfo_now     = BINFO(gi_now_binary_info);
367                 i_id_now        = gi_now_binary_info++;
368
369                 if( i_id_now == gi_max_binary_info )    { gi_max_binary_info++; }
370         }
371
372         assert( NULL != p_binfo_now );
373
374         BinaryInfo_ClearBinInfo( p_binfo_now );
375         p_binfo_now->i_id               = i_id_now;
376         //p_binfo_now->b_phase  = LIBGOBLIN_BINFO_PHASE_ALLOCED;
377         p_binfo_now->pv_pginfo  = (void *)p_pginfo;
378         p_binfo_now->i_parent_bid       = i_parent_bid;
379         strncpy( p_binfo_now->str_filename, pstr_filename, DRD64_MAX_PATH );
380         p_binfo_now->b_phase    = LIBGOBLIN_BINFO_PHASE_SETNAME;
381
382         BinInfo_AppendChain( p_binfo_now );
383
384         return p_binfo_now;
385 }
386
387
388 /*----------------------------------------------------------------------
389 ----------------------------------------------------------------------*/
390 LIBGOBLIN_BINFO_EXTERN
391 int
392         BinaryInfo_FreeBinInfo(
393                 LibGoblin_BinaryInfo *p_binfo )
394 {
395         int             i_binfoid;      
396         int             i_result;
397         LibGoblin_BinaryFile    *p_binfile;
398
399         if( NULL == p_binfo )   { return 0x01; }
400
401         if( -0x01 != p_binfo->i_binfile )       {
402                 p_binfile       = BinaryFile_GetBinaryFile( p_binfo->i_binfile );
403                 assert( NULL != p_binfile );
404
405                 if( NULL == p_binfile->p_elf )  {
406                         free( p_binfile->p_elf );
407                         p_binfile->p_elf        = NULL;
408                 }
409
410                 BinaryFile_FreeBinaryFile( p_binfile );
411                 p_binfo->i_binfile      = -0x01;
412         }
413
414         BinInfo_DeleteChain( p_binfo );
415
416         i_binfoid       = p_binfo->i_id;
417         BinaryInfo_ClearBinInfo( p_binfo );
418
419         if( gi_max_binary_info == (i_binfoid + 1) )     { gi_max_binary_info--; }
420         gi_now_binary_info--;
421         if( 0 == gi_now_binary_info )   { gi_max_binary_info    = 0; }
422         
423         return 0x00;
424 }
425
426
427 /*----------------------------------------------------------------------
428 ----------------------------------------------------------------------*/
429 LIBGOBLIN_BINFO_EXTERN
430 int
431         BinaryInfo_Init(
432                 void)
433 {
434         int             i_cnt;
435         LibGoblin_BinaryInfo    *p_binfo;
436
437         if( 0 != gi_max_binary_info )   {
438                 return -0x01;
439         }
440
441         gp_binary_info  = malloc( sizeof( LibGoblin_BinaryInfo )
442                                                                                 * LIBGOBLIN_BINFO_UNITS );
443         if( NULL == gp_binary_info )    {
444                 return -0x02;
445         }
446
447         for( i_cnt = 0; i_cnt < LIBGOBLIN_BINFO_UNITS; i_cnt++ )        {
448                 p_binfo = BINFO(i_cnt);
449                 BinaryInfo_ClearBinInfo( p_binfo );
450         }
451
452         gi_alloc_binary_info    = LIBGOBLIN_BINFO_UNITS;
453         gi_max_binary_info              = 0;
454         gi_now_binary_info              = 0;
455
456         return 0x00;
457 }
458
459
460 /*----------------------------------------------------------------------
461 ----------------------------------------------------------------------*/
462 LIBGOBLIN_BINFO_EXTERN
463 int
464         BinaryInfo_Term(
465                 void )
466 {
467         LibGoblin_BinaryInfo    *p_binfo;
468
469         if( 0 < gi_now_binary_info )    {
470                 for( ; gi_now_binary_info >= 0; gi_now_binary_info-- )  {
471                         p_binfo = BinaryInfo_GetBinInfo( gi_now_binary_info );
472                         BinaryInfo_FreeBinInfo( p_binfo );
473                 }
474         }
475         
476         free( gp_binary_info );
477         
478         gp_binary_info          = NULL;
479         gi_alloc_binary_info    = 0;
480         gi_max_binary_info              = 0;
481         gi_now_binary_info              = 0;
482
483         return 0x00;
484 }
485
486
487 /* EOF of drd64_.c ----------------------------------- */