1 #ifndef OFX_BINARY_H
2 #define OFX_BINARY_H
3 
4 /*
5 Software License :
6 
7 Copyright (c) 2007-2009, The Open Effects Association Ltd. All rights reserved.
8 
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
11 
12 * Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 * Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 * Neither the name The Open Effects Association Ltd, nor the names of its
18 contributors may be used to endorse or promote products derived from this
19 software without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
25 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 #include <string>
33 #include <iostream>
34 
35 #if defined(_WIN32) || defined(_WIN64)
36 #ifndef WINDOWS
37 #define WINDOWS
38 #endif
39 #elif defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFLy__)  || defined( __APPLE__) || defined(unix) || defined(__unix) || defined(_XOPEN_SOURCE) || defined(_POSIX_SOURCE)
40 #define UNIX
41 #else
42 #error cannot detect operating system
43 #endif
44 
45 #if defined(UNIX)
46 #include <dlfcn.h>
47 #elif defined (WINDOWS)
48 #include "windows.h"
49 #include <assert.h>
50 #endif
51 
52 #include <sys/stat.h>
53 
54 namespace OFX
55 {
56 
57   /// class representing a DLL/Shared Object/etc
58   class Binary {
59     /// destruction will close the library and invalidate
60     /// any function pointers returned by lookupSymbol()
61   protected :
62     std::string _binaryPath;
63     bool _invalid;
64 #if defined(UNIX)
65     void *_dlHandle;
66 #elif defined (WINDOWS)
67     HINSTANCE _dlHandle;
68 #endif
69     time_t _time;
70     off_t _size;
71     int _users;
72   public :
73 
74     /// create object representing the binary.  will stat() it,
75     /// and this fails, will set binary to be invalid.
76     Binary(const std::string &binaryPath);
77 
~Binary()78     ~Binary() { unload(); }
79 
80     // calls stat, returns true if successfull, false otherwise
81     static bool getFileModTimeAndSize(const std::string &binaryPath, time_t& modificationTime, off_t& fileSize);
82 
isLoaded()83     bool isLoaded() const { return _dlHandle != 0; }
84 
85     /// is this binary invalid? (did the a stat() or load() on the file fail,
86     /// or are we missing a some of the symbols?
isInvalid()87     bool isInvalid() const { return _invalid; }
88 
89     /// set invalid status (e.g. called by user if a mandatory symbol was missing)
setInvalid(bool invalid)90     void setInvalid(bool invalid) { _invalid = invalid; }
91 
92     /// Last modification time of the file.
getTime()93     time_t getTime() const { return _time; }
94 
95     /// Current size of the file.
getSize()96     off_t getSize() const { return _size; }
97 
98     /// Path to the file.
getBinaryPath()99     const std::string &getBinaryPath() const { return _binaryPath; }
100 
101     void ref();
102     void unref();
103 
104     /// open the binary.
105     void load();
106 
107     /// close the binary
108     void unload();
109 
110     /// look up a symbol in the binary file and return it as a pointer.
111     /// returns null pointer if not found, or if the library is not loaded.
112     void *findSymbol(const std::string &symbol);
113   };
114 }
115 
116 #endif
117