Ejemplo n.º 1
0
class LibWwqParseCFFI(LibWwqLyParseBase):
    def __init__(self):
        super(LibWwqParseCFFI, self).__init__()
        from cffi import FFI
        self.ffi = FFI()
        self.lib = self.ffi.dlopen(self.lib_path)
        self.ffi.cdef("""
    char * get_uuid();
    char * get_name();
    int parse(char * c,int length,char **result,int *result_length);
    int free_str(char * c);
        """)
        self.lib.__class__.__repr__ = lambda s: "<%s object at 0x%016X>" % (s.__class__.__name__, id(s))
        logging.debug("successful load lib %s" % self.lib)
        weakref.finalize(self,
                         lambda: logging.debug("%s released" % self.lib) if self.ffi.dlclose(self.lib) or 1 else None)

    def get_uuid(self) -> bytes:
        return self.ffi.string(self.lib.get_uuid())

    def get_name(self) -> bytes:
        return self.ffi.string(self.lib.get_name())

    def lib_parse(self, byte_str: bytes) -> bytes:
        length = self.ffi.cast("int", len(byte_str))
        result_length = self.ffi.new("int *")
        result_p = self.ffi.new("char **")
        # p = self.ffi.new("char []", byte_str)
        p = self.ffi.from_buffer(byte_str)
        self.lib.parse(p, length, result_p, result_length)
        result = self.ffi.unpack(result_p[0], result_length[0])
        self.lib.free_str(result_p[0])
        return result
Ejemplo n.º 2
0
def kdf_calc(pw_string, salt_byte, iterations):
    ffi = FFI()
    ffi.cdef(DEF_gcry_kdf_derive)
    libgcrypt = ffi.dlopen("libgcrypt.so.20")
    if isinstance(pw_string, str):
        pw_byte = pw_string.encode("UTF-8")
    else:
        pw_byte = pw_string
    pw = ffi.new("char []", pw_byte)
    salt = ffi.new("char []", salt_byte)
    kb = ffi.new("char []", 32)
    r = libgcrypt.gcry_kdf_derive(
        pw,
        len(pw_string),
        GCRY_KDF_ITERSALTED_S2K,
        GCRY_MD_SHA256,
        salt,
        8,
        iterations,
        32,
        kb,
    )
    if r != 0:
        raise ValueError("libgcrypt error", r)
    return ffi.unpack(kb, 32)
Ejemplo n.º 3
0
    def fit(self, n: int) -> [float]:
        """
        Fit regressor.
        :param n: Number of features.
        """
        ffi = FFI()
        ffi.cdef("""
            void fit(double * buffer, uint64_t n, uint64_t method, uint64_t iterations, double alpha, 
                     uint64_t penalty, double tolerance, uint64_t shuffle, uint64_t verbose, uint64_t stumble, double eta);
        """)
        C = ffi.dlopen('target/release/libregression.so')
        buffer = ffi.new(f'double [{n}]')
        C.fit(buffer, n, self.method, self.iterations, self.alpha,
              self.penalty, self.tolerance, self.shuffle, self.verbose,
              self.stumble, self.eta)
        weights = ffi.unpack(buffer, n)

        return weights
Ejemplo n.º 4
0
def kdf_calc(pw_string, salt_byte, iterations):
    ffi = FFI()
    ffi.cdef(DEF_gcry_kdf_derive)
    # libgcrypt.so.20 for windows https://github.com/ShiftMediaProject/libgcrypt/releases
    if os.name == 'nt':
        libgcrypt = ffi.dlopen("gcrypt.dll")
    else:
        libgcrypt = ffi.dlopen("libgcrypt.so.20")
    if isinstance(pw_string, str):
        pw_byte = pw_string.encode('UTF-8')
    else:
        pw_byte = pw_string
    pw = ffi.new("char []", pw_byte)
    salt = ffi.new("char []", salt_byte)
    kb = ffi.new("char []", 32)
    r = libgcrypt.gcry_kdf_derive(pw, len(pw_string), GCRY_KDF_ITERSALTED_S2K,
                                  GCRY_MD_SHA256, salt, 8, iterations, 32, kb)
    if r != 0:
        raise ValueError("libgcrypt error", r)
    return ffi.unpack(kb, 32)
Ejemplo n.º 5
0
class LibWwqParseCFFI(LibWwqLyParseBase):
    def __init__(self):
        super(LibWwqParseCFFI, self).__init__()
        from cffi import FFI
        self.ffi = FFI()
        self.lib = self.ffi.dlopen(self.lib_path)
        self.ffi.cdef("""
    char * get_uuid();
    char * get_name();
    int parse(char * c,int length,char **result,int *result_length);
    int free_str(char * c);
    void* atomic_int64_init();
    int atomic_int64_destroy(void* ptr);
    int64_t atomic_int64_get(void* ptr);
    int64_t atomic_int64_set(void* ptr, int64_t val);
    int64_t atomic_int64_add(void* ptr, int64_t val);
    int64_t atomic_int64_sub(void* ptr, int64_t val);
    int64_t atomic_int64_and(void* ptr, int64_t val);
    int64_t atomic_int64_or(void* ptr, int64_t val);
    int64_t atomic_int64_xor(void* ptr, int64_t val);
    
    typedef union epoll_data {
      void* ptr;
      int fd;
      uint32_t u32;
      uint64_t u64;
      uintptr_t sock; /* Windows specific */
      void* hnd;  /* Windows specific */
    } epoll_data_t;
    
    typedef struct {
      uint32_t events;   /* Epoll events and flags */
      epoll_data_t data; /* User data variable */
    } epoll_event ;
    
    void* epoll_create(int size);
    void* epoll_create1(int flags);
    int epoll_close(void* ephnd);
    int epoll_ctl(void* ephnd, int op, uintptr_t sock, epoll_event* event);
    int epoll_wait(void* ephnd, epoll_event* events, int maxevents, int timeout);
        """)
        self.lib.__class__.__repr__ = lambda s: "<%s object at 0x%016X>" % (s.__class__.__name__, id(s))
        logging.debug("successful load lib %s" % self.lib)
        weakref.finalize(self,
                         lambda: logging.debug("%s released" % self.lib) if self.ffi.dlclose(self.lib) or 1 else None)

    def get_uuid(self) -> bytes:
        return self.ffi.string(self.lib.get_uuid())

    def get_name(self) -> bytes:
        return self.ffi.string(self.lib.get_name())

    def lib_parse(self, byte_str: bytes) -> bytes:
        length = self.ffi.cast("int", len(byte_str))
        result_length = self.ffi.new("int *")
        result_p = self.ffi.new("char **")
        # p = self.ffi.new("char []", byte_str)
        p = self.ffi.from_buffer(byte_str)
        self.lib.parse(p, length, result_p, result_length)
        result = self.ffi.unpack(result_p[0], result_length[0])
        self.lib.free_str(result_p[0])
        return result
Ejemplo n.º 6
0
 def test_unpack(self):
     ffi = FFI()
     p = ffi.new("char[]", b"abc\x00def")
     assert ffi.unpack(p + 1, 7) == b"bc\x00def\x00"
     p = ffi.new("int[]", [-123456789])
     assert ffi.unpack(p, 1) == [-123456789]
Ejemplo n.º 7
0
 def test_unpack(self):
     ffi = FFI()
     p = ffi.new("char[]", b"abc\x00def")
     assert ffi.unpack(p+1, 7) == b"bc\x00def\x00"
     p = ffi.new("int[]", [-123456789])
     assert ffi.unpack(p, 1) == [-123456789]
Ejemplo n.º 8
0
class VchanBase:
    def __init__(self, lib):
        self.ffi = FFI()

        self.ffi.cdef("""
struct libvchan;
typedef struct libvchan libvchan_t;

libvchan_t *libvchan_server_init(int domain, int port, size_t read_min, size_t write_min);
libvchan_t *libvchan_client_init(int domain, int port);
int libvchan_write(libvchan_t *ctrl, const void *data, size_t size);
int libvchan_send(libvchan_t *ctrl, const void *data, size_t size);
int libvchan_read(libvchan_t *ctrl, void *data, size_t size);
int libvchan_recv(libvchan_t *ctrl, void *data, size_t size);
int libvchan_wait(libvchan_t *ctrl);
void libvchan_close(libvchan_t *ctrl);
int libvchan_fd_for_select(libvchan_t *ctrl);
int libvchan_is_open(libvchan_t *ctrl);

int libvchan_data_ready(libvchan_t *ctrl);
int libvchan_buffer_space(libvchan_t *ctrl);
""")

        self.lib = self.ffi.dlopen(
            os.path.join(os.path.dirname(__file__), '..', lib))
        self.ctrl = None

    def close(self):
        if self.ctrl is not None:
            self.lib.libvchan_close(self.ctrl)
        self.ctrl = None

    def write(self, data: bytes) -> int:
        result = self.lib.libvchan_write(self.ctrl, data, len(data))
        if result < 0:
            raise VchanException('libvchan_write')
        return result

    def send(self, data: bytes) -> int:
        result = self.lib.libvchan_send(self.ctrl, data, len(data))
        if result < 0:
            raise VchanException('libvchan_send')
        return result

    def read(self, size: int) -> bytes:
        buf = self.ffi.new('char[]', size)
        result = self.lib.libvchan_read(self.ctrl, buf, size)
        if result < 0:
            raise VchanException('libvchan_read')
        return self.ffi.unpack(buf, result)

    def recv(self, size: int) -> bytes:
        buf = self.ffi.new('char[]', size)
        result = self.lib.libvchan_recv(self.ctrl, buf, size)
        if result < 0:
            raise VchanException('libvchan_recv')
        return self.ffi.unpack(buf, result)

    def wait(self):
        result = self.lib.libvchan_wait(self.ctrl)
        if result < 0:
            raise VchanException('libvchan_wait')

    def state(self) -> int:
        return self.lib.libvchan_is_open(self.ctrl)

    def fd_for_select(self) -> int:
        return self.lib.libvchan_fd_for_select(self.ctrl)

    def wait_for(self, pred):
        while not pred():
            self.wait()

    def wait_for_state(self, state: int):
        self.wait_for(lambda: self.state() == state)

    def data_ready(self):
        result = self.lib.libvchan_data_ready(self.ctrl)
        if result < 0:
            raise VchanException('libvchan_data_ready')
        return result

    def buffer_space(self):
        result = self.lib.libvchan_buffer_space(self.ctrl)
        if result < 0:
            raise VchanException('libvchan_buffer_space')
        return result

    def __enter__(self):
        pass

    def __exit__(self, _type, value, traceback):
        self.close()