Botan  1.11.15
src/lib/utils/dyn_load/dyn_load.h
Go to the documentation of this file.
00001 /*
00002 * Dynamically Loaded Object
00003 * (C) 2010 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #ifndef BOTAN_DYNAMIC_LOADER_H__
00009 #define BOTAN_DYNAMIC_LOADER_H__
00010 
00011 #include <string>
00012 
00013 namespace Botan {
00014 
00015 /**
00016 * Represents a DLL or shared object
00017 */
00018 class Dynamically_Loaded_Library
00019    {
00020    public:
00021       /**
00022       * Load a DLL (or fail with an exception)
00023       * @param lib_name name or path to a library
00024       *
00025       * If you don't use a full path, the search order will be defined
00026       * by whatever the system linker does by default. Always using fully
00027       * qualified pathnames can help prevent code injection attacks (eg
00028       * via manipulation of LD_LIBRARY_PATH on Linux)
00029       */
00030       Dynamically_Loaded_Library(const std::string& lib_name);
00031 
00032       /**
00033       * Unload the DLL
00034       * @warning Any pointers returned by resolve()/resolve_symbol()
00035       * should not be used after this destructor runs.
00036       */
00037       ~Dynamically_Loaded_Library();
00038 
00039       /**
00040       * Load a symbol (or fail with an exception)
00041       * @param symbol names the symbol to load
00042       * @return address of the loaded symbol
00043       */
00044       void* resolve_symbol(const std::string& symbol);
00045 
00046       /**
00047       * Convenience function for casting symbol to the right type
00048       * @param symbol names the symbol to load
00049       * @return address of the loaded symbol
00050       */
00051       template<typename T>
00052       T resolve(const std::string& symbol)
00053          {
00054          return reinterpret_cast<T>(resolve_symbol(symbol));
00055          }
00056 
00057    private:
00058       Dynamically_Loaded_Library(const Dynamically_Loaded_Library&);
00059       Dynamically_Loaded_Library& operator=(const Dynamically_Loaded_Library&);
00060 
00061       std::string lib_name;
00062       void* lib;
00063    };
00064 
00065 }
00066 
00067 #endif