1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
42 /* this file contains platform-depended code (parse dll, */
43 /* getting file list e.t.c.) */
62 char* GetPathFromModuleName( char* modulename )
68 /* if module is link find real path */
71 strcpy( real, modulename );
72 while( (len = readlink( real, real, 1000 )) > 0 )
74 char* path = (char*)malloc( strlen( real ) + 10 );
77 char* path = (char*)malloc( strlen( modulename ) + 10 );
78 strcpy( path, modulename );
81 for( i = strlen( path ) - 1;
82 i > 0 && path[i] != '\\' && path[i] != '/';
95 for( i = 0; i < (int)strlen( path ); i++ )
104 /* path - path to module (in) */
105 /* headers - array of headers (out) */
106 /* libs - array of libs (out) */
107 int GetFileList( char* path, char*** headers, int* header_num,
108 char*** headpath, int* headpath_num,
109 char*** libs, int* lib_num )
123 strcpy( init, path );
125 /* adding last '/' if needed */
126 int len = strlen( init );
129 if( len && init[len - 1] != '\\' && init[len - 1] != '/' )
134 strcpy( root, init );
137 strcat( init, "plugins.cfg" );
139 strcat( init, "plugins.conf" );
147 /* open config file */
148 FILE* cfg = fopen( init, "rb" );
152 fseek( cfg, 0, SEEK_END );
153 int size = ftell( cfg );
154 fseek( cfg, 0, SEEK_SET );
156 char* buffer = (char*)malloc( size + 2 );
157 fread( buffer, 1, size, cfg );
161 char** lpath = (char**)malloc( 1 );
162 char** ipath = (char**)malloc( 1 );
163 char** incl = (char**)malloc( 1 );
164 char** lib = (char**)malloc( 1 );
169 for( i = 0; i < size; )
173 for( j = i; buffer[j] <= ' ' && j < size; j++ );
177 for( j = i; buffer[j] != 0xa && buffer[j] != 0xd && j < size; j++ );
180 if( buffer[i] == '#' )
188 sscanf( buffer + i, "%s %s", what, where );
189 if( !strcmp( what, "include" ) )
191 incl = (char**)realloc( incl, sizeof( *incl ) * (ni + 1) );
192 incl[ni] = (char*)malloc( strlen( where ) + 1 );
193 strcpy( incl[ni], where );
196 else if( !strcmp( what, "incpath" ) )
198 ipath = (char**)realloc( ipath, sizeof( *ipath ) * (nip + 1) );
199 ipath[nip] = (char*)malloc( strlen( root ) + strlen( where ) + 1 );
202 if( where[1] != ':' )
204 if( where[0] != '/' )
206 strcpy( ipath[nip], root );
207 strcat( ipath[nip], where );
210 else if( !strcmp( what, "libpath" ) )
212 lpath = (char**)realloc( lpath, sizeof( *lpath ) * (nlp + 1) );
213 lpath[nlp] = (char*)malloc( strlen( root ) + strlen( where ) + 1 );
216 if( where[1] != ':' )
218 if( where[0] != '/' )
220 strcpy( lpath[nlp], root );
221 strcat( lpath[nlp], where );
224 else if( !strcmp( what, "library" ) )
228 if( where[1] != ':' )
230 if( where[0] != '/' )
232 for( int i = 0; i < nlp; i++ )
235 strcpy( findlib, lpath[i] );
236 strcat( findlib, "/" );
237 strcat( findlib, where );
238 FILE* lb = fopen( findlib, "rb" );
241 lib = (char**)realloc( lib, sizeof( *lib ) * (nl + 1) );
242 lib[nl] = (char*)malloc( strlen( findlib ) + 1 );
243 strcpy( lib[nl], findlib );
252 FILE* lb = fopen( where, "rb" );
255 lib = (char**)realloc( lib, sizeof( *lib ) * (nl + 1) );
256 lib[nl] = (char*)malloc( strlen( where ) + 1 );
257 strcpy( lib[nl], where );
267 sprintf( tmp, "Library %s hasn't been found\n", where );
279 for( int i = 0; i < nip; i++ )
288 for( int i = 0; i < nl; i++ )
293 if( headpath && nip )
297 for( int i = 0; i < nip; i++ )
302 for( i = 0; i < nlp; i++ )
320 #define MakePtr(cast,ptr,addValue) (cast)((DWORD)(ptr)+(DWORD)(addValue))
322 static int GetExportList( char* filename, char*** names )
325 HANDLE file = CreateFile( filename,
327 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
333 if( file == INVALID_HANDLE_VALUE )
336 unsigned long filesize;
338 char* buffer = (char*)malloc( filesize = SetFilePointer( file, 0, 0, FILE_END ) );
339 SetFilePointer( file, 0, 0, FILE_BEGIN );
340 BOOL res = ReadFile( file, buffer, filesize, &read, 0 );
348 PIMAGE_EXPORT_DIRECTORY exportDir;
349 PIMAGE_SECTION_HEADER header;
350 PIMAGE_NT_HEADERS pNTHeader;
351 PIMAGE_DOS_HEADER pDosHdr;
355 unsigned int exportsStartRVA, exportsEndRVA;
362 pDosHdr = (PIMAGE_DOS_HEADER)pfile;
365 pNTHeader = MakePtr(PIMAGE_NT_HEADERS,
366 pfile,pDosHdr->e_lfanew);
369 pNTHeader->OptionalHeader.DataDirectory
370 [IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
373 exportsEndRVA = exportsStartRVA + pNTHeader->OptionalHeader.DataDirectory
374 [IMAGE_DIRECTORY_ENTRY_EXPORT].Size;
376 // Get the IMAGE_SECTION_HEADER that contains the exports. This is
377 // usually the .edata section, but doesn't have to be.
379 PIMAGE_SECTION_HEADER section = IMAGE_FIRST_SECTION(pNTHeader);
383 for ( i=0; i < pNTHeader->FileHeader.NumberOfSections; i++, section++ )
385 // Is the RVA within this section?
386 if ( (exportsStartRVA >= section->VirtualAddress) &&
387 (exportsStartRVA < (section->VirtualAddress + section->Misc.VirtualSize)))
393 printf( "No Exports Table Found\n" );
398 delta = (INT)(header->VirtualAddress - header->PointerToRawData);
399 exportDir = MakePtr(PIMAGE_EXPORT_DIRECTORY, base,
400 exportsStartRVA - delta);
402 name = (PSTR *)((DWORD)exportDir->AddressOfNames - delta + base);
404 exportDir = MakePtr(PIMAGE_EXPORT_DIRECTORY, base,
405 exportsStartRVA - delta);
407 *names = (char**)malloc( (exportDir->NumberOfNames + 1) * sizeof(char*) );
409 for ( unsigned int j = 0; j < exportDir->NumberOfNames; j++ )
411 (*names)[j] = (char*)malloc( strlen( name[j] - delta + base ) + 1 );
412 strcpy( (*names)[j], name[j] - delta + base );
415 num = exportDir->NumberOfNames;
420 char* command = (char*)malloc( strlen( filename ) + 100 );
421 strcat( strcat( strcpy( command, "nm " ), filename ), " | grep \" T \"" );
422 FILE* exp = popen( command, "r" );
426 char* file = (char*)malloc( 1 );
427 while( (r = fread( buffer, 1, 1000, exp ) ) > 0 )
429 file = (char*)realloc( file, size + r );
430 memcpy( file + size, buffer, r );
435 (*names) = (char**)malloc( 1 );
438 for( int i = 0; i < size; )
442 for( j = i; file[j] <= ' ' && j < size; j++ );
446 for( j = i; file[j] != 0xa && file[j] != 0xd && j < size; j++ );
453 sscanf( file + i, "%s %s %s", entry, type, name );
454 (*names) = (char**)realloc( *names, sizeof(**names) * (num + 1) );
455 (*names)[num] = (char*)malloc( strlen( name ) + 1 );
456 strcpy( (*names)[num], name );
457 // printf( "%s\n", (*names)[num] );
466 int AddLibrary( char* path )
476 if( (funcs = GetExportList( path, &exports )) == 0 )
482 if( (lib = LoadLibrary( path )) == 0 )
484 if( (lib = dlopen( path, RTLD_LAZY )) == 0 )
488 for( int i = 0; i < funcs; i++ )
491 void* func = GetProcAddress( lib, exports[i] );
493 void* func = dlsym( lib, exports[i] );
496 AddExternFunction( func, exports[i] );
507 //-----------------------------------------------------------------------------
509 extern "C" int _eicUprintf(int (*output)(...), char **arg, char *fmt, arg_list ap);
510 extern "C" int _eicUscanf (int (*input)(...) , int (*uget)(...) ,
511 char **arg, const char *fmt, arg_list ap);
512 extern "C" int charout_(int c, char *buf);
513 extern "C" int charin_(char *buf);
514 extern "C" int charback_(int ch, char **buf);
515 typedef int(*put_t)(...);
516 //typedef int(*put0_t)();
518 static HANDLE g_hWritePipe = 0;
519 static HANDLE g_hErrorPipe = 0;
520 static HANDLE g_hInputPipe = 0;
522 static int g_hWritePipe = 0;
523 static int g_hErrorPipe = 0;
524 static int g_hInputPipe = 0;
527 static VaL output_printf( )
532 arg_list ag = getargs();
534 char* g_pchBuf = (char*) malloc( 0xF000 );
535 memset( g_pchBuf, 0, 0xF000 );
536 char* fmt = (char*) nextarg(ag,ptr_t).p;
538 v.ival = val.ival = _eicUprintf((put_t)charout_, &g_pchBuf, fmt, ag);
541 //console outout//v.ival = val.ival = _eicUprintf((put_t)fputc,stdout,fmt,ag);
545 WriteFile(g_hWritePipe,
551 write( g_hWritePipe, g_pchBuf, val.ival );
554 printf( "%s", g_pchBuf );
560 static VaL input_scanf()
565 arg_list ag = getargs();
567 char* g_pchBuf = (char*)malloc( 0xF000 );
568 char* _pchBuf = g_pchBuf;
569 memset( g_pchBuf, 0, 0xF000 );
574 ReadFile(g_hInputPipe,
580 g_pchBuf[num] = '\n';
582 WriteFile(g_hWritePipe,
589 num = read( g_hInputPipe, g_pchBuf, 255 );
592 scanf( "%s%n", g_pchBuf, &num );
594 char* fmt = (char*) nextarg(ag,ptr_t).p;
595 v.ival = val.ival = _eicUscanf ((put_t)charin_ ,
596 (put_t)charback_, &g_pchBuf, fmt, ag);
597 //console input//v.ival = _eicUscanf((put_t)fgetc,(put_t)ungetc,stdin,fmt,ag);
603 void ErrorOutput(char *msg)
606 unsigned long size = strlen( msg );
612 WriteFile(g_hWritePipe,
620 WriteFile(g_hErrorPipe,
629 write( g_hWritePipe, msg, size );
632 write( g_hErrorPipe, msg, size );
640 void _printf(char *msg)
645 void InitIO( char* output, char* errors, char* input )
648 sscanf( input, "%d", &g_hInputPipe );
651 EiC_add_builtinfunc( "scanf", input_scanf );
654 sscanf( output, "%d", &g_hWritePipe );
657 EiC_add_builtinfunc( "printf", output_printf );
660 sscanf( errors, "%d", &g_hErrorPipe );
661 EiC_setMessageDisplay(ErrorOutput);
665 EiC_setMessageDisplay(_printf);