How to make Shared Library in linux OS with C++

How to make Shared Library in linux OS with C++

????? ???? ?? ?????? ?? ?? ??? ????? ?? ?? ?? ???????? ????? ????? ????? ?? ???? ????? Factory Method ???? ???? ?? ???? ???? ??????.

????!

???? ????? ????? ?? ?? ?????? ?? ??????? ?? ??? ??????? ?? ????? ?? ?? ?? ???? ?? ?? ???? ???? ????? ??? ??? ?? ?? Factory Method ???????? ??? ?????? ????.

???? ?????? ???? ???????? ?? ?? ??? ??? ?? Main ?????? ???????? ????? ????...!

#ifndef __PLUGIN_H__
#define __PLUGIN_H__
 ?
#include <sstream>
#include <string.h>
  
typedef char* Bytes;
typedef unsigned int size_t;
typedef const char* cBytes;
  
#define DELOBJ(obj)         {if(obj!=NULL){delete obj;obj=NULL;}}
  
enum encryptAloglType {
    BASE64, HASH , MD4 ,MD5 ,OPENSSL
};
  
enum ioType {?
    CPP_STREAM , C_LOWLEVEL
};
 ?
class Encryptor_Interface
{
public:
    virtual ~Encryptor_Interface() {}
  
    virtual void getEncBuffer(Bytes & buffer, cBytes fileName) const = 0;
    virtual void getDecBuffer(std::istringstream** sstream, cBytes fileName) const = 0;
    virtual void setIoType(ioType =C_LOWLEVEL) = 0;
    virtual void setAlgolType(encryptAloglType) = 0;
    virtual void dispose(int) =0;
    virtual size_t getReadedSize() =0;
  
};
  
typedef Plugin_Interface* PluginEnctyptorPtr;
  
#endif // __PLUGIN_H__

?? ??? ????? ??? ?? ????? ???? ???????? ??? ?????? ???? ???? ????? ??? ???? ???? ???????? ??? ?? ?? ?????? ??????? ????.??? ????? ?????? ??? ?? bash script ??????? ????? ?? ?????? ?? ??? ??? ??????? ?? ????? ????...

gcc -ggdb3 -shared -fPIC libdep.c -o libEncryptor.so

????? ?? ??? ????? ???? ??? ?? ?? ???? ??? ?? ?? ?? ???? template ????? ?? ?? ????? ?? ????? ????? ??? ??????? ??????. ?? ??? ???? ???? ??? ?? ??? ???? ???? ???????? ?? ?? ???? ????? ?? ??? ?? ????? ?? ?????? ??? ?? ???????? ?? ??????????? ???? ????? ?? ????? ?????????? ???? ?? ?? ?????? ??? ???? ????? ??????.

?? ??? ???? ?? ??????? ?? ????? ???????? ??? ?? ?????? ???? ???? ??? ???????? ?? ?? ?? ????? ??????? ?????? ??? ???? ??? ?? ?? ???? Wrapper ??????? ?? ???? ???? Solver ?? ?? ??? ???? ???? ? ?? ??? solve ??? ???? ???? ????? ???? ???????? ??? ?? ???????.

????? ?? ??? ?? ????? ??? ???? ???? ??? ?????? ?? ?? ??? ?? ???????? ????? ??????? ?????? ?? ????? ?? ?????? ?? ??? ???? ????? ??????? ?????? ?? ?? ??????? ??? ?? ?? ??? ???? ????? ? ?? ???? ??? ??? ????? ?? ???? ???? ??? ?? ??? Single Responsibility ?? ?? ?? ???? ?????? ???? ????? ??? ????? ?? ?? ???? ??? ??????? ?????? ????? ?????? ????? ???? ??????? ??.

/*
 * Library_Loader.h
 *
 *  Created on: Jan 1, 2019
 *      Author: f.shiri
 */?
  
#ifndef PLUGINLOADER_H_
#define PLU?GINLOADER_H_
  
#include "Encryptor_interface.h"
#include <stdexcept>
#include <memory>
#include <dlfcn.h>
  
#define LibEncryptorName "./libEncryptor.so"
#define LibFactoryMethod "create_plugin"
#define LibDestyoryMethod "destroy_plugin"
 ?
#define LogicFileName    "logic.ini"
#define __CleanupLibClose __attribute__((cleanup(closedl)))
  
#ifdef SCL_EQUIPPED
#include "SCLVoter_Inter?face.h"
#include "initsclvoter.h"
extern ISCLVoterManager* pSCLVoterManager;
#endif
  
typedef Encryptor_Interface* encAbstractPtr;
//typedef ISCLVoterManager*    pISCLVoter;
/* load shared library or static library and dynamic
 * declare library virtual function.
 * template arg1 : interface class library pointer.
 * */
template<class InterfacePtr>
class PluginLoader {
private:
  
    /*clean up macro function calling by compiler, delete the pointer
     when the pointer's life cycle is end */
    static inline int closedl(void* handle) {
        int tmp = -1;
        if (handle)
            tmp = dlclose(handle);
        handle = NULL;
        return tmp;
    }
   struct {
        /* union data type contained
         * dynamic link library objects.
         * */
        union {
            void* dlsym;
            InterfacePtr (*factoryMethod)();
        } factoryObject;
  
        union {
            void* dlsym;
            bool (*destoryMethod)(InterfacePtr);
        } destroyObject;
  
        /* shared library handler */
   } libraryObject;
  
    void* m_handle;
  
public:
    explicit PluginLoader() {
    }
    ;
    ~PluginLoader() {
    }
    ;
  
    // Dynamic load, encryption function from shared library.
    /*
     * arg1 : shared library name.
     * arg2 : reference factory method name in shared library.
     * arg3 : reference declarative interface class pointer.
     * arg4 : own request for the symbol in library or main executable.
    // arg5 : reference destroy method name in shared library.
     * return type : reference interface pointer.
     */
    template<class CInterfacePtr>
    inline void operator()(cBytes libName, cBytes factoryFuncName,
             CInterfacePtr** pClassInterface,cBytes destroyFuncName =NULL) {
  
        dlerror();
        /*
         * The RTLD_DEEPBIND attribute specifies that when resolving a symbol in a shared library,
         * the symbols in the shared library are put ahead of the global symbol scope.
         * example-> When you load the ???.so, it has it's own request for the symbol.
         * Because you use RTLD_DEEPBIND this definition is looked up in the dependencies of this library first,
         * before looking in the main executable.
         */
        __CleanupLibClose void* m_handle = dlopen(libName, RTLD_DEEPBIND | RTLD_NOW | RTLD_LOCAL);
  
  
        if (!m_handle) {
            throw std::runtime_error(dlerror());
        }
  
        if(factoryFuncName!=NULL){
            if (!(libraryObject.factoryObject.dlsym = dlsym(m_handle, factoryFuncName))) {
                throw std::runtime_error(dlerror());
            }
        }
  
        if(destroyFuncName!=NULL){
            if (!(libraryObject.destroyObject.dlsym = dlsym(m_handle, destroyFuncName))) {
                throw std::runtime_error(dlerror());
            }
        }
  
        *pClassInterface =(*libraryObject.factoryObject.factoryMethod)();
  
    }
  
    template<class CInterfacePtr>
    bool destroy(CInterfacePtr** pClassInterface) {
        return libraryObject.destroyObject.destoryMethod(*pClassI  nterface);
    }
  
private:
    PluginLoader(const PluginLoader&);
    PluginLoader& operator=(const PluginLoader&);
  
};
  
/* this class is invoke strategy shared library wrapper
 * template arg1: reference interface class
 */
template<class InterfacePtr>
class Solver {
public:
    Solver(){};
    Solver(cBytes cLibName, cBytes cFactoryFuncName
            ,cBytes cDestroyFuncName=NULL) :m_interfacePtr(NULL){
        m_pluginLoader(cLibName, cFactoryFuncName,
                &m_interfacePtr ,cDestroyFuncName);
    }
    virtual ~Solver(){
        m_pluginLoader.destroy(&m_interfacePtr);
        DELOBJ(m_interfacePtr);
    }
    // important: this pure virtual method must be implemented in derived class.
    virtual void solve() =0;
  
protected:
    template<class CInterfacePtr>
    inline void getInterface(CInterfacePtr& inputInterFace){
        if(m_interfacePtr!=NULL){
            inputInterFace = m_interfacePtr;
        }
    }
private:
    //shared library interface class member.
    InterfacePtr m_interfacePtr;
    //instance of plugin loader class.
    PluginLoader<InterfacePtr> m_pluginLoader;
};
  
  
  
/*proxy class shared library. this class is immutable type
 * class type: pointer type of shared library interface class.
 * template arg1 : encrypt interface class pointer
 * template arg2 : string stream reference pointer
 * */
template<class InterfacePtr ,typename InputType>
class EncryptLibraryLoader : public Solver<InterfacePtr>{
  
protected:
  
public:
    // arg1 : shared library name.
    // arg2 : reference factory method name in shared library.
    // arg3 : reference destroy method name in shared library.
    // arg4 : output string stream
    EncryptLibraryLoader(InputType outStream ,cBytes cLibName, cBytes cFactoryFuncName
            ,cBytes cDestroyFuncName=NULL) : Solver<InterfacePtr>(cLibName,cFactoryFuncName,cDe  stroyFuncName)
             , m_outStream(outStream) {
        /* important: must be call getInterface in super class and get
         *            interface class reference and copy in member pointer in this class.
         */
        getInterface(m_interfacePtr);
    }
    virtual ~EncryptLibraryLoader() {
        //clean up member pointer
        DELOBJ(m_interfacePtr);
        DELOBJ(m_outStream);
    }
    /* get the byte stream from encrypt logic.ini file.
     * arg1 : reference bytes stream.
     * return type : reference stream pointer
     *
     * important: must be call getInterface in super class and get
     *            interface class reference and copy in member pointer in this class.
     * */
    inline void solve(){
        if(m_outStream!=NULL){
            try {
                if (m_interfacePtr!=NULL) {
                    m_interfacePtr->setIoType(C_LOWLEVEL);
                    m_interfacePtr->getDecBuffer(m_outStream,const_cast<char*> (LogicFileName));
                }
                //important : if interfacePtr is loaded.
                //            this method dispossess member static pointer in memory section of library.
                m_interfacePtr->dispose(0);
            } catch (std::runtime_error& e) {
                printf("%s \n", e.what());
            }
        }
    }
  
private:
    //EncryptLibraryLoader();
    EncryptLibraryLoader(const EncryptLibraryLoader&);
    EncryptLibraryLoader& operator=(const EncryptLibraryLoader&);
  
    //shared library interface class member.
    InterfacePtr m_interfacePtr;
    InputType m_outStream;
};
  
#endif /* PLUGINLOADER_H_ */

? ?? ??? ????? ?? ??????? ?? ????? ??? ??? ?? ???????? ??? ??? ??????? ??????.

std::istringstream* file=NULL ;
{
    Solver<encAbstractPtr> *loader = new    EncryptLibraryLoader<encAbstractPtr,std::istringst  ream**> (
            &file ,LibEncryptorName, LibFactoryMethod , LibDestyoryMethod);
    // initial encryption library.
    loader->solve();
    loader=NULL;
    
}

???? ?????

Rachelle Favaloro

Motivated Professional with 27+ Years of Combined Expertise

4 年

I want the real one. Love books. Pretty. :)

回复
Pouria Paimard

Testmanager @ Magna International | Agile Project Management

4 年

???? ??? ????? ?? ????? ???? numpy ?? ?????? ??? ??????? ???? ?? ?????? ?? ??????? ???? ???? ???? ???

??? ????? ????? ???? ??? ???? ???? ? ????? ???????? ??? ? ??????? ????? ??????

要查看或添加评论,请登录

Farhad Shiri的更多文章

  • ????? ?? ?? ?? ????? ???? ? ?????? ?? ?? ??????????

    ????? ?? ?? ?? ????? ???? ? ?????? ?? ?? ??????????

    ?? ?? ???????? ??????? ?????? ?????? ?? ?? ???? ???? ?????? ???? ?? ???. ?? ????? ???? ?? ? ?? ?????? ?? ?? ??? ?????…

  • ?????? ?????????? ????????

    ?????? ?????????? ????????

    ???? ?????? ????? ???? ???? ????? ???? ?? ???? ?? ???? ?????? ???? ??? ???. ???? ?????? ????? ???????? ???? ????? ????…

  • Introduction to TPL Dataflow

    Introduction to TPL Dataflow

    Introductory Examples One of the most common ways into TPL Dataflow is the ActionBlock class. This class can be thought…

  • POSIX IPC-Message Queue in Linux

    POSIX IPC-Message Queue in Linux

    ??? ?? ????????? ?? ?? ???? ?????? ????? ?? ????? ????? ???????? ?? ????? ???? ???????? ??? ??????? ?? ???? ????? ????…

    9 条评论
  • Optimizing and Safety Programming in C ++

    Optimizing and Safety Programming in C ++

    ?? ???? ?? ????? ????? ??? ?????? ???? ??? ???? ???? ?? ????? ?????????? ??? ????? ?????? ? ???? ??? ?????? ????? ?…

    5 条评论
  • Device console management in Linux(TTY)

    Device console management in Linux(TTY)

    ??????? ?? ?????? ?????? ??? ?? ???? ?????? ?? ?? ?????? ?? ?????? ??????? ??? ?? ?? ?????? ????? ???? ?? ???? ?????…

    2 条评论
  • Use reflection to dynamic Content Values When using SQLite

    Use reflection to dynamic Content Values When using SQLite

    ?? ????? ?? ????? ?? ??? ?????? ????? ??? ? ?? Map Content Values ???? ??? ???? ????????? Insert / Update ???? ????? ??…

  • How To use Fork Join Task Algorithm in Java

    How To use Fork Join Task Algorithm in Java

    ?? ??????????? ???????? ??? ?????? ????? ??? ??? ? ????????? ??????? ???????? ????? ????? ?? ????? ?? ???? ??? ????????…

  • Inline Assembly code VS Compiler Assembly code

    Inline Assembly code VS Compiler Assembly code

    ?? ???? ???? ?? ????? ?????? ?????? ????? ????? ????? ?? ??????? ?? ???? ????? ?????? ?? ???? ?? ???? ???? ?? ???? ???…

    3 条评论
  • Programmer Tools For Windows

    Programmer Tools For Windows

    ?????? ?? ?????? ????? ???? ?? ????? ?? ?? ???? ?????? ????? ?????? ?? ????????? ???? ???? ?? ?? ??? ??? ????? ???…

    1 条评论

社区洞察

其他会员也浏览了