Chris@0: """ Chris@0: vis.py Chris@0: ====== Chris@0: Chris@0: Ctypes based module to access libbsd's strvis & strunvis functions. Chris@0: Chris@0: The `vis` function is the equivalent of strvis. Chris@0: The `unvis` function is the equivalent of strunvis. Chris@0: All functions accept unicode string as input and return a unicode string. Chris@0: Chris@0: Constants: Chris@0: ---------- Chris@0: Chris@0: * to select alternate encoding format Chris@0: `VIS_OCTAL`: use octal \ddd format Chris@0: `VIS_CSTYLE`: use \[nrft0..] where appropiate Chris@0: Chris@0: * to alter set of characters encoded Chris@0: (default is to encode all non-graphic except space, tab, and newline). Chris@0: `VIS_SP`: also encode space Chris@0: `VIS_TAB`: also encode tab Chris@0: `VIS_NL`: also encode newline Chris@0: `VIS_WHITE`: same as (VIS_SP | VIS_TAB | VIS_NL) Chris@0: `VIS_SAFE`: only encode "unsafe" characters Chris@0: Chris@0: * other Chris@0: `VIS_NOSLASH`: inhibit printing '\' Chris@0: `VIS_HTTP1808`: http-style escape % hex hex Chris@0: `VIS_HTTPSTYLE`: http-style escape % hex hex Chris@0: `VIS_MIMESTYLE`: mime-style escape = HEX HEX Chris@0: `VIS_HTTP1866`: http-style &#num; or &string; Chris@0: `VIS_NOESCAPE`: don't decode `\' Chris@0: `VIS_GLOB`: encode glob(3) magic characters Chris@0: Chris@0: :Authors: Chris@0: - ju1ius (http://github.com/ju1ius) Chris@0: :Version: 1 Chris@0: :Date: 2014-01-05 Chris@0: """ Chris@0: from ctypes import CDLL, c_char_p, c_int Chris@0: from ctypes.util import find_library Chris@0: Chris@0: Chris@0: __all__ = [ Chris@0: 'vis', 'unvis', Chris@0: 'VIS_OCTAL', 'VIS_CSTYLE', Chris@0: 'VIS_SP', 'VIS_TAB', 'VIS_NL', 'VIS_WHITE', 'VIS_SAFE', Chris@0: 'VIS_NOSLASH', 'VIS_HTTP1808', 'VIS_HTTPSTYLE', 'VIS_MIMESTYLE', Chris@0: 'VIS_HTTP1866', 'VIS_NOESCAPE', 'VIS_GLOB' Chris@0: ] Chris@0: Chris@0: Chris@0: ############################################################# Chris@0: # Constants from bsd/vis.h Chris@0: ############################################################# Chris@0: Chris@0: #to select alternate encoding format Chris@0: VIS_OCTAL = 0x0001 Chris@0: VIS_CSTYLE = 0x0002 Chris@0: # to alter set of characters encoded Chris@0: # (default is to encode all non-graphic except space, tab, and newline). Chris@0: VIS_SP = 0x0004 Chris@0: VIS_TAB = 0x0008 Chris@0: VIS_NL = 0x0010 Chris@0: VIS_WHITE = VIS_SP | VIS_TAB | VIS_NL Chris@0: VIS_SAFE = 0x0020 Chris@0: # other Chris@0: VIS_NOSLASH = 0x0040 Chris@0: VIS_HTTP1808 = 0x0080 Chris@0: VIS_HTTPSTYLE = 0x0080 Chris@0: VIS_MIMESTYLE = 0x0100 Chris@0: VIS_HTTP1866 = 0x0200 Chris@0: VIS_NOESCAPE = 0x0400 Chris@0: VIS_GLOB = 0x1000 Chris@0: Chris@0: ############################################################# Chris@0: # Import libbsd/vis functions Chris@0: ############################################################# Chris@0: Chris@0: _libbsd = CDLL(find_library('bsd')) Chris@0: Chris@0: _strvis = _libbsd.strvis Chris@0: _strvis.argtypes = [c_char_p, c_char_p, c_int] Chris@0: _strvis.restype = c_int Chris@0: Chris@0: _strunvis = _libbsd.strunvis Chris@0: _strvis.argtypes = [c_char_p, c_char_p] Chris@0: _strvis.restype = c_int Chris@0: Chris@0: Chris@0: def vis(src, flags=VIS_WHITE): Chris@0: """ Chris@0: Encodes the string `src` into libbsd's vis encoding. Chris@0: `flags` must be one of the VIS_* constants Chris@0: Chris@0: C definition: Chris@0: int strvis(char *dst, char *src, int flags); Chris@0: """ Chris@0: src = bytes(src, 'utf-8') Chris@0: dst_p = c_char_p(bytes(len(src) * 4)) Chris@0: src_p = c_char_p(src) Chris@0: flags = c_int(flags) Chris@0: Chris@0: bytes_written = _strvis(dst_p, src_p, flags) Chris@0: if -1 == bytes_written: Chris@0: raise RuntimeError('vis failed to encode string "{}"'.format(src)) Chris@0: Chris@0: return dst_p.value.decode('utf-8') Chris@0: Chris@0: Chris@0: def unvis(src): Chris@0: """ Chris@0: Decodes a string encoded by vis. Chris@0: Chris@0: C definition: Chris@0: int strunvis(char *dst, char *src); Chris@0: """ Chris@0: src = bytes(src, 'utf-8') Chris@0: dst_p = c_char_p(bytes(len(src))) Chris@0: src_p = c_char_p(src) Chris@0: Chris@0: bytes_written = _strunvis(dst_p, src_p) Chris@0: if -1 == bytes_written: Chris@0: raise RuntimeError('unvis failed to decode string "{}"'.format(src)) Chris@0: Chris@0: return dst_p.value.decode('utf-8')