Exemplo n.º 1
0
 def add(self, uid, **properties):
     tuples = self.tuples.write_batch(transaction=True)
     index = self.index.write_batch(transaction=True)
     for key, value in properties.items():
         tuples.put(pack(uid, key), pack(value))
         index.put(pack(key, value, uid), '')
     tuples.write()
     index.write()
Exemplo n.º 2
0
 def delete(self, uid):
     tuples = self.tuples.write_batch(transaction=True)
     index = self.index.write_batch(transaction=True)
     for key, value in self.tuples.iterator(start=pack(uid)):
         other, name = unpack(key)
         if uid == other:
             tuples.delete(key)
             value = unpack(value)[0]
             index.delete(pack(name, value, uid))
         else:
             break
     tuples.write()
     index.write()
Exemplo n.º 3
0
    def query(self, key, value=''):
        with self.index() as cursor:
            match = (key, value) if value else (key,)
            cursor.set_key(key, pack(value), 0)
            code = cursor.search_near()
            if code == WT_NOT_FOUND:
                return
            if code == -1:
                if cursor.next() == WT_NOT_FOUND:
                    return

            while True:
                key, value, uid = cursor.get_key()
                value = unpack(value)[0]
                other = (key, value)
                ok = reduce(
                    lambda previous, x: (cmp(*x) == 0) and previous,
                    zip(match, other),
                    True
                )
                if ok:
                    yield [key, value, uid]
                    if cursor.next() == WT_NOT_FOUND:
                        break
                else:
                    break
Exemplo n.º 4
0
 def __get():
     for key, value in self.tuples.iterator(start=pack(uid)):
         other, key = unpack(key)
         if other == uid:
             value = unpack(value)[0]
             yield key, value
         else:
             break
Exemplo n.º 5
0
 def ref(self, uid, key):
     match = [uid, key]
     for key, value in self.tuples.iterator(start=pack(uid, key)):
         other = unpack(key)
         if other == match:
             value = unpack(value)[0]
             return value
         else:
             return None
Exemplo n.º 6
0
 def set_value(self, *args):
 	'''set_value(self) -> None
 	
 	@copydoc WT_CURSOR::set_value'''
 	if len(args) == 1 and type(args[0]) == tuple:
 		args = args[0]
 	# Keep the Python string pinned
 	self._value = pack(self.value_format, *args)
 	self._set_value(self._value)
Exemplo n.º 7
0
 def set_value(self, *args):
 	'''set_value(self) -> None
 	
 	@copydoc WT_ASYNC_OP::set_value'''
 	if len(args) == 1 and type(args[0]) == tuple:
 		args = args[0]
 	# Keep the Python string pinned
 	self._value = pack(self.value_format, *args)
 	self._set_value(self._value)
Exemplo n.º 8
0
def check_common(fmt, verbose, *v):
    v = list(v)
    packed = pack(fmt, *v)
    unpacked = unpack(fmt, packed)
    if unpacked == v:
        result = 'ok'
    else:
        result = '** FAIL!'
    print '* %s as %s: %s' % (repr(v), fmt, result)
    if verbose or unpacked != v:
        print '** packed: ', ''.join('%02x' % ord(c) for c in packed)
        print '** unpacked: ', unpacked
Exemplo n.º 9
0
 def set_key(self, *args):
 	'''set_key(self) -> None
 	
 	@copydoc WT_ASYNC_OP::set_key'''
 	if len(args) == 1 and type(args[0]) == tuple:
 		args = args[0]
 	if self.is_column:
 		self._set_recno(long(args[0]))
 	else:
 		# Keep the Python string pinned
 		self._key = pack(self.key_format, *args)
 		self._set_key(self._key)
Exemplo n.º 10
0
def check_common(fmt, verbose, *v):
    v = list(v)
    packed = pack(fmt, *v)
    unpacked = unpack(fmt, packed)
    if unpacked == v:
        result = 'ok'
    else:
        result = '** FAIL!'
    print '* %s as %s: %s' % (repr(v), fmt, result)
    if verbose or unpacked != v:
        print '** packed: ', ''.join('%02x' % ord(c) for c in packed)
        print '** unpacked: ', unpacked
Exemplo n.º 11
0
 def set_key(self, *args):
 	'''set_key(self) -> None
 	
 	@copydoc WT_CURSOR::set_key'''
 	if len(args) == 1 and type(args[0]) == tuple:
 		args = args[0]
 	if self.is_column:
 		self._set_recno(long(args[0]))
 	else:
 		# Keep the Python string pinned
 		self._key = pack(self.key_format, *args)
 		self._set_key(self._key)
Exemplo n.º 12
0
    def query(self, key, value=''):
        match = (key, value) if value else (key,)

        iterator = self.index.iterator(start=pack(key, value))
        for key, value in iterator:
            other = unpack(key)
            ok = reduce(
                lambda previous, x: (cmp(*x) == 0) and previous,
                zip(match, other),
                True
            )
            if ok:
                yield other
            else:
                break
Exemplo n.º 13
0
def sockaddr(host, port, network='ipv4'):
    """sockaddr(host, port, network = 'ipv4') -> (data, length, family)

    Creates a sockaddr_in or sockaddr_in6 memory buffer for use in shellcode.

    Arguments:
      host (str): Either an IP address or a hostname to be looked up.
      port (int): TCP/UDP port.
      network (str): Either 'ipv4' or 'ipv6'.

    Returns:
      A tuple containing the sockaddr buffer, length, and the address family.
"""
    address_family = {'ipv4': socket.AF_INET, 'ipv6': socket.AF_INET6}[network]

    for family, _, _, _, ip in socket.getaddrinfo(host, None, address_family):
        ip = ip[0]
        if family == address_family:
            break
    else:
        log.error("Could not find %s address for %r" % (network, host))

    info = socket.getaddrinfo(host, None, address_family)
    host = socket.inet_pton(address_family, ip)
    sockaddr = p16(address_family)
    sockaddr += pack(
        port, word_size=16,
        endianness='big')  #Port should be big endian = network byte order
    length = 0

    if network == 'ipv4':
        sockaddr += host
        length = 16  # Save ten bytes by skipping two 'push 0'
    else:
        sockaddr += p32(0xffffffff)  # Save three bytes 'push -1' vs 'push 0'
        sockaddr += host
        length = len(sockaddr) + 4  # Save five bytes 'push 0'
    return (sockaddr, length, address_family)
Exemplo n.º 14
0
def sockaddr(host, port, network = 'ipv4'):
    """sockaddr(host, port, network = 'ipv4') -> (data, length, family)

    Creates a sockaddr_in or sockaddr_in6 memory buffer for use in shellcode.

    Arguments:
      host (str): Either an IP address or a hostname to be looked up.
      port (int): TCP/UDP port.
      network (str): Either 'ipv4' or 'ipv6'.

    Returns:
      A tuple containing the sockaddr buffer, length, and the address family.
"""
    address_family = {'ipv4':socket.AF_INET,'ipv6':socket.AF_INET6}[network]

    for family, _, _, _, ip in socket.getaddrinfo(host, None, address_family):
        ip = ip[0]
        if family == address_family:
            break
    else:
        log.error("Could not find %s address for %r" % (network, host))

    info = socket.getaddrinfo(host, None, address_family)
    host = socket.inet_pton(address_family, ip)
    sockaddr  = p16(address_family)
    sockaddr += pack(port, word_size = 16, endianness = 'big') #Port should be big endian = network byte order
    length    = 0

    if network == 'ipv4':
        sockaddr += host
        length    = 16 # Save ten bytes by skipping two 'push 0'
    else:
        sockaddr += p32(0xffffffff) # Save three bytes 'push -1' vs 'push 0'
        sockaddr += host
        length    = len(sockaddr) + 4 # Save five bytes 'push 0'
    return (sockaddr, length, address_family)
Exemplo n.º 15
0
def check(fmt, *v):
	print fmt, repr(v), ''.join('%02x' % ord(c) for c in pack(fmt, *v))
Exemplo n.º 16
0
def check(fmt, *v):
    print fmt, repr(v), "".join("%02x" % ord(c) for c in pack(fmt, *v))
Exemplo n.º 17
0
def check(fmt, *v):
    print fmt, repr(v), ''.join('%02x' % ord(c) for c in pack(fmt, *v))
Exemplo n.º 18
0
 def add(self, uid, **properties):
     with self.tuples() as cursor:
         for key, value in properties.items():
             cursor.set_key(uid, key)
             cursor.set_value(pack(value))
             cursor.insert()