コード例 #1
0
 def _encodeParalogClusterId(self, prefix, nr):
     letters = []
     while nr/26 > 0:
         letters.append(chr(97+nr%26))
         nr = nr/26 - 1
     letters.append(chr(97+nr%26))
     return prefix+''.join(letters[::-1]) # letters were in reverse order
コード例 #2
0
 def _encodeParalogClusterId(self, prefix, nr):
     letters = []
     while nr / 26 > 0:
         letters.append(chr(97 + nr % 26))
         nr = nr / 26 - 1
     letters.append(chr(97 + nr % 26))
     return prefix + ''.join(letters[::-1])  # letters were in reverse order
コード例 #3
0
ファイル: test_logger.py プロジェクト: pyarnold/Testify
 def _colorize(self, message, color = CYAN):
     if not color or not self.use_color:
         return message
     else:
         start_color = chr(0o033) + '[1;%sm' % color
         end_color = chr(0o033) + '[m'
         return start_color + message + end_color
コード例 #4
0
 def _encodeParalogClusterId(self, prefix, nr):
     """encode the paralogGroups at the same level, e.g. 1a, 1b, 1c
     for 3 paralogGroups next to each other. the nr argument
     identifies the individual indices of those 3 paralogGroups."""
     letters = []
     while nr // 26 > 0:
         letters.append(chr(97 + (nr % 26)))
         nr = nr // 26 - 1
     letters.append(chr(97 + (nr % 26)))
     return prefix + ''.join(letters[::-1])  # letters were in reverse order
コード例 #5
0
 def _encodeParalogClusterId(self, prefix, nr):
     """encode the paralogGroups at the same level, e.g. 1a, 1b, 1c
     for 3 paralogGroups next to each other. the nr argument
     identifies the individual indices of those 3 paralogGroups."""
     letters = []
     while nr//26 > 0:
         letters.append(chr(97 + (nr % 26)))
         nr = nr//26 - 1
     letters.append(chr(97 + (nr % 26)))
     return prefix+''.join(letters[::-1])  # letters were in reverse order
コード例 #6
0
ファイル: html.py プロジェクト: yukiohmiya/mezzanine
 def decode(m):
     html = m.group(0)
     if html[:2] == "&#":
         try:
             if html[:3] == "&#x":
                 return chr(int(html[3:-1], 16))
             else:
                 return chr(int(html[2:-1]))
         except ValueError:
             pass
     else:
         try:
             html = chr(name2codepoint[html[1:-1]])
         except KeyError:
             pass
     return html
コード例 #7
0
ファイル: slpacket.py プロジェクト: shineusn/obspy
 def getType(self):
     #print "DEBUG: self.slhead:", repr(self.slhead)
     if self.slhead[0: len(SLPacket.INFOSIGNATURE)].lower() == \
             SLPacket.INFOSIGNATURE.lower():
         if (chr(self.slhead[self.SLHEADSIZE - 1]) != '*'):
             return self.TYPE_SLINFT
         else:
             return self.TYPE_SLINF
     msrecord_py = self.getMSRecord()
     #print "DEBUG: msrecord_py:", msrecord_py
     #print "DEBUG: msrecord_py.reclen:", msrecord_py.reclen
     #print "DEBUG: msrecord_py.sequence_number:",
     #print msrecord_py.sequence_number
     #print "DEBUG: msrecord_py.samplecnt:", msrecord_py.samplecnt
     #print "DEBUG: msrecord_py.encoding:", msrecord_py.encoding
     #print "DEBUG: msrecord_py.byteorder:", msrecord_py.byteorder
     #print "DEBUG: msrecord_py.numsamples:", msrecord_py.numsamples
     #print "DEBUG: msrecord_py.sampletype:", msrecord_py.sampletype
     #print "DEBUG: msrecord_py.blkts:", msrecord_py.blkts
     blockette = msrecord_py.blkts.contents
     while blockette:
         #print "DEBUG: ===================="
         #print "DEBUG: blkt_type:", blockette.blkt_type
         #print "DEBUG: next_blkt:", blockette.next_blkt
         #print "DEBUG: blktdata:", blockette.blktdata
         #print "DEBUG: blktdatalen:", blockette.blktdatalen
         #print "DEBUG: next:", blockette.next
         try:
             blockette = blockette.next.contents
         except:
             blockette = None
     return msrecord_py.blkts.contents.blkt_type
コード例 #8
0
    def __init__(self, key, msg = None, digestmod = None):
        """Create a new HMAC object.

        key:       key for the keyed hash object.
        msg:       Initial input for the hash, if provided.
        digestmod: A module supporting PEP 247. Defaults to the md5 module.
        """
        if digestmod is None:
            import md5
            digestmod = md5

        if key == None: #TREVNEW - for faster copying
            return      #TREVNEW

        self.digestmod = digestmod
        self.outer = digestmod.new()
        self.inner = digestmod.new()
        self.digest_size = digestmod.digest_size

        blocksize = 64
        ipad = "\x36" * blocksize
        opad = "\x5C" * blocksize

        if len(key) > blocksize:
            key = digestmod.new(key).digest()

        key = key + chr(0) * (blocksize - len(key))
        self.outer.update(_strxor(key, opad))
        self.inner.update(_strxor(key, ipad))
        if msg is not None:
            self.update(msg)
コード例 #9
0
    def _populate_class_variables():
        lookup = {}
        reverse_lookup = {}
        characters_for_re = []

        # &apos is an XHTML entity and an HTML 5, but not an HTML 4
        # entity. We don't want to use it, but we want to recognize it on the way in.
        #
        # TODO: Ideally we would be able to recognize all HTML 5 named
        # entities, but that's a little tricky.
        extra = [(39, 'apos')]
        for codepoint, name in list(codepoint2name.items()) + extra:
            character = chr(codepoint)
            if codepoint not in (34, 39):
                # There's no point in turning the quotation mark into
                # " or the single quote into ', unless it
                # happens within an attribute value, which is handled
                # elsewhere.
                characters_for_re.append(character)
                lookup[character] = name
            # But we do want to recognize those entities on the way in and
            # convert them to Unicode characters.
            reverse_lookup[name] = character
        re_definition = "[%s]" % "".join(characters_for_re)
        return lookup, reverse_lookup, re.compile(re_definition)
コード例 #10
0
ファイル: tahoe_status.py プロジェクト: tahoe-lafs/tahoe-lafs
def pretty_progress(percent, size=10, output_ascii=False):
    """
    Displays a unicode or ascii based progress bar of a certain
    length. Should we just depend on a library instead?

    (Originally from txtorcon)
    """

    curr = int(percent / 100.0 * size)
    part = (percent / (100.0 / size)) - curr

    if output_ascii:
        part = int(part * 4)
        part = '.oO%'[part]
        block_chr = '#'

    else:
        block_chr = u'\u2588'
        # there are 8 unicode characters for vertical-bars/horiz-bars
        part = int(part * 8)

        # unicode 0x2581 -> 2589 are vertical bar chunks, like rainbarf uses
        # and following are narrow -> wider bars
        part = chr(0x258f - part) # for smooth bar
        # part = chr(0x2581 + part) # for neater-looking thing

    # hack for 100+ full so we don't print extra really-narrow/high bar
    if percent >= 100.0:
        part = ''
    curr = int(curr)
    return '%s%s%s' % ((block_chr * curr), part, (' ' * (size - curr - 1)))
コード例 #11
0
ファイル: slpacket.py プロジェクト: gthompson/obspy
 def getType(self):
     #print "DEBUG: self.slhead:", repr(self.slhead)
     if self.slhead[0: len(SLPacket.INFOSIGNATURE)].lower() == \
             SLPacket.INFOSIGNATURE.lower():
         if (chr(self.slhead[self.SLHEADSIZE - 1]) != '*'):
             return self.TYPE_SLINFT
         else:
             return self.TYPE_SLINF
     msrecord_py = self.getMSRecord()
     #print "DEBUG: msrecord_py:", msrecord_py
     #print "DEBUG: msrecord_py.reclen:", msrecord_py.reclen
     #print "DEBUG: msrecord_py.sequence_number:",
     #print msrecord_py.sequence_number
     #print "DEBUG: msrecord_py.samplecnt:", msrecord_py.samplecnt
     #print "DEBUG: msrecord_py.encoding:", msrecord_py.encoding
     #print "DEBUG: msrecord_py.byteorder:", msrecord_py.byteorder
     #print "DEBUG: msrecord_py.numsamples:", msrecord_py.numsamples
     #print "DEBUG: msrecord_py.sampletype:", msrecord_py.sampletype
     #print "DEBUG: msrecord_py.blkts:", msrecord_py.blkts
     blockette = msrecord_py.blkts.contents
     while blockette:
         #print "DEBUG: ===================="
         #print "DEBUG: blkt_type:", blockette.blkt_type
         #print "DEBUG: next_blkt:", blockette.next_blkt
         #print "DEBUG: blktdata:", blockette.blktdata
         #print "DEBUG: blktdatalen:", blockette.blktdatalen
         #print "DEBUG: next:", blockette.next
         try:
             blockette = blockette.next.contents
         except:
             blockette = None
     return msrecord_py.blkts.contents.blkt_type
コード例 #12
0
ファイル: html.py プロジェクト: batpad/mezzanine
 def decode(m):
     html = m.group(0)
     if html[:2] == "&#":
         try:
             if html[:3] == "&#x":
                 return chr(int(html[3:-1], 16))
             else:
                 return chr(int(html[2:-1]))
         except ValueError:
             pass
     else:
         try:
             html = chr(name2codepoint[html[1:-1]])
         except KeyError:
             pass
     return html
コード例 #13
0
def _max_append(L, s, maxlen, extra=''):
    if not isinstance(s, str):
        s = chr(s)
    if not L:
        L.append(s.lstrip())
    elif len(L[-1]) + len(s) <= maxlen:
        L[-1] += extra + s
    else:
        L.append(s.lstrip())
コード例 #14
0
ファイル: quoprimime.py プロジェクト: 3liz/Quantum-GIS
def _max_append(L, s, maxlen, extra=''):
    if not isinstance(s, str):
        s = chr(s)
    if not L:
        L.append(s.lstrip())
    elif len(L[-1]) + len(s) <= maxlen:
        L[-1] += extra + s
    else:
        L.append(s.lstrip())
コード例 #15
0
def truelen_cut(string, length):
    current_length = 0
    current_pos = 0
    for c in string:
        current_length += 2 if c > chr(127) else 1
        if current_length > length:
            return string[:current_pos]
        current_pos += 1
    return string
コード例 #16
0
def hex_2_str(hex_str):
    unicode_str = ""
    try:
        for i in range(0, len(hex_str) // 4):
            unicode_str += unichr(int(hex_str[i * 4:i * 4 + 4], 16))
    except NameError:
        from future.builtins import chr
        for i in range(0, len(hex_str) // 4):
            unicode_str += chr(int(hex_str[i * 4:i * 4 + 4], 16))
    return unicode_str
コード例 #17
0
 def update(self):
     self.display = self.content
     curTime = time() // 1
     offset = max(int((curTime - self.START) % len(self.content)) - 1, 0)
     while offset > 0:
         if self.display[0] > chr(127):
             offset -= 1
             self.display = self.display[1:] + self.display[:1]
         else:
             offset -= 1
             self.display = self.display[2:] + self.display[:2]
コード例 #18
0
ファイル: scrollstring.py プロジェクト: darknessomi/musicbox
 def update(self):
     self.display = self.content
     curTime = time() // 1
     offset = max(int((curTime - self.START) % len(self.content)) - 1, 0)
     while offset > 0:
         if self.display[0] > chr(127):
             offset -= 1
             self.display = self.display[3:] + self.display[:3]
         else:
             offset -= 1
             self.display = self.display[1:] + self.display[:1]
コード例 #19
0
    def testErrorDetection(self):
        """Tests the end to end encrypted communicators."""
        # Install the client - now we can verify its signed messages
        self._MakeClientRecord()

        # Make something to send
        message_list = rdf_flows.MessageList()
        for i in range(0, 10):
            message_list.job.Append(session_id=str(i))

        result = rdf_flows.ClientCommunication()
        self.client_communicator.EncodeMessages(message_list, result)

        # TODO: We use `bytes` from the `future` package here to have
        # Python 3 iteration behaviour. This call should be a noop in Python 3 and
        # should be safe to remove on support for Python 2 is dropped.
        cipher_text = bytes(result.SerializeToBytes())

        # Depending on this modification several things may happen:
        # 1) The padding may not match which will cause a decryption exception.
        # 2) The protobuf may fail to decode causing a decoding exception.
        # 3) The modification may affect the signature resulting in UNAUTHENTICATED
        #    messages.
        # 4) The modification may have no effect on the data at all.
        for x in range(0, len(cipher_text), 50):
            # Futz with the cipher text (Make sure it's really changed)
            mod = chr((cipher_text[x] % 250) + 1).encode("latin-1")
            mod_cipher_text = cipher_text[:x] + mod + cipher_text[x + 1:]

            # TODO: Now we revert back to native `bytes` object because
            # proto deserialization assumes native indexing behaviour.
            if compatibility.PY2:
                mod_cipher_text = mod_cipher_text.__native__()

            try:
                decoded, client_id, _ = self.server_communicator.DecryptMessage(
                    mod_cipher_text)

                for i, message in enumerate(decoded):
                    # If the message is actually authenticated it must not be changed!
                    if message.auth_state == message.AuthorizationState.AUTHENTICATED:
                        self.assertEqual(message.source, client_id)

                        # These fields are set by the decoder and are not present in the
                        # original message - so we clear them before comparison.
                        message.auth_state = None
                        message.source = None
                        self.assertEqual(message, message_list.job[i])
                    else:
                        logging.debug("Message %s: Authstate: %s", i,
                                      message.auth_state)

            except communicator.DecodingError as e:
                logging.debug("Detected alteration at %s: %s", x, e)
コード例 #20
0
def remove_control_chars(string):
    """
        remove control characters
    :param string: string to modify
    :type string: unicode
    :return: modified string
    :rtype: unicode
    """
    control_chars = ''.join(map(chr, list(range(0, 32)) + list(range(127, 160))))
    control_char_re = re.compile(u'[%s]' % re.escape(control_chars))
    tem_string = control_char_re.sub('', string)
    control_char_re = re.compile(u'[%s]' % re.escape(chr(160)))
    return control_char_re.sub(' ', tem_string)
コード例 #21
0
ファイル: mathtls.py プロジェクト: jankoprowski/google-gdata
def PRF_SSL(secret, seed, length):
    secretStr = bytesToString(secret)
    seedStr = bytesToString(seed)
    bytes = createByteArrayZeros(length)
    index = 0
    for x in range(26):
        A = chr(ord('A') + x) * (x + 1)  # 'A', 'BB', 'CCC', etc..
        input = secretStr + sha.sha(A + secretStr + seedStr).digest()
        output = md5.md5(input).digest()
        for c in output:
            if index >= length:
                return bytes
            bytes[index] = ord(c)
            index += 1
    return bytes
コード例 #22
0
ファイル: mathtls.py プロジェクト: hfalcic/google-gdata
def PRF_SSL(secret, seed, length):
    secretStr = bytesToString(secret)
    seedStr = bytesToString(seed)
    bytes = createByteArrayZeros(length)
    index = 0
    for x in range(26):
        A = chr(ord('A')+x) * (x+1) # 'A', 'BB', 'CCC', etc..
        input = secretStr + sha.sha(A + secretStr + seedStr).digest()
        output = md5.md5(input).digest()
        for c in output:
            if index >= length:
                return bytes
            bytes[index] = ord(c)
            index += 1
    return bytes
コード例 #23
0
def truelen(string):
    """
    It appears one Asian character takes two spots, but __len__
    counts it as three, so this function counts the dispalyed
    length of the string.

    >>> truelen('abc')
    3
    >>> truelen('你好')
    4
    >>> truelen('1二3')
    4
    >>> truelen('')
    0
    """
    return len(string) + sum(1 for c in string if c > chr(127))
コード例 #24
0
ファイル: scrollstring.py プロジェクト: darknessomi/musicbox
def truelen(string):
    """
    It appears one Asian character takes two spots, but __len__
    counts it as three, so this function counts the dispalyed
    length of the string.

    >>> truelen('abc')
    3
    >>> truelen('你好')
    4
    >>> truelen('1二3')
    4
    >>> truelen('')
    0
    """
    return len(string) + sum(1 for c in string if c > chr(127))
コード例 #25
0
ファイル: frontend_lib_test.py プロジェクト: costaafm/grr
    def testErrorDetection(self):
        """Tests the end to end encrypted communicators."""
        # Install the client - now we can verify its signed messages
        self._MakeClientRecord()

        # Make something to send
        message_list = rdf_flows.MessageList()
        for i in range(0, 10):
            message_list.job.Append(session_id=str(i))

        result = rdf_flows.ClientCommunication()
        self.client_communicator.EncodeMessages(message_list, result)
        cipher_text = result.SerializeToString()

        # Depending on this modification several things may happen:
        # 1) The padding may not match which will cause a decryption exception.
        # 2) The protobuf may fail to decode causing a decoding exception.
        # 3) The modification may affect the signature resulting in UNAUTHENTICATED
        #    messages.
        # 4) The modification may have no effect on the data at all.
        for x in range(0, len(cipher_text), 50):
            # Futz with the cipher text (Make sure it's really changed)
            mod = chr((ord(cipher_text[x]) % 250) + 1).encode("latin-1")
            mod_cipher_text = cipher_text[:x] + mod + cipher_text[x + 1:]

            try:
                decoded, client_id, _ = self.server_communicator.DecryptMessage(
                    mod_cipher_text)

                for i, message in enumerate(decoded):
                    # If the message is actually authenticated it must not be changed!
                    if message.auth_state == message.AuthorizationState.AUTHENTICATED:
                        self.assertEqual(message.source, client_id)

                        # These fields are set by the decoder and are not present in the
                        # original message - so we clear them before comparison.
                        message.auth_state = None
                        message.source = None
                        self.assertEqual(message, message_list.job[i])
                    else:
                        logging.debug("Message %s: Authstate: %s", i,
                                      message.auth_state)

            except communicator.DecodingError as e:
                logging.debug("Detected alteration at %s: %s", x, e)
コード例 #26
0
ファイル: cookies.py プロジェクト: 3liz/Quantum-GIS
def _unquote(mystr):
    # If there aren't any doublequotes,
    # then there can't be any special characters.  See RFC 2109.
    if len(mystr) < 2:
        return mystr
    if mystr[0] != '"' or mystr[-1] != '"':
        return mystr

    # We have to assume that we must decode this string.
    # Down to work.

    # Remove the "s
    mystr = mystr[1:-1]

    # Check for special sequences.  Examples:
    #    \012 --> \n
    #    \"   --> "
    #
    i = 0
    n = len(mystr)
    res = []
    while 0 <= i < n:
        o_match = _OctalPatt.search(mystr, i)
        q_match = _QuotePatt.search(mystr, i)
        if not o_match and not q_match:              # Neither matched
            res.append(mystr[i:])
            break
        # else:
        j = k = -1
        if o_match:
            j = o_match.start(0)
        if q_match:
            k = q_match.start(0)
        if q_match and (not o_match or k < j):     # QuotePatt matched
            res.append(mystr[i:k])
            res.append(mystr[k+1])
            i = k + 2
        else:                                      # OctalPatt matched
            res.append(mystr[i:j])
            res.append(chr(int(mystr[j+1:j+4], 8)))
            i = j + 4
    return _nulljoin(res)
コード例 #27
0
def _unquote(mystr):
    # If there aren't any doublequotes,
    # then there can't be any special characters.  See RFC 2109.
    if len(mystr) < 2:
        return mystr
    if mystr[0] != '"' or mystr[-1] != '"':
        return mystr

    # We have to assume that we must decode this string.
    # Down to work.

    # Remove the "s
    mystr = mystr[1:-1]

    # Check for special sequences.  Examples:
    #    \012 --> \n
    #    \"   --> "
    #
    i = 0
    n = len(mystr)
    res = []
    while 0 <= i < n:
        o_match = _OctalPatt.search(mystr, i)
        q_match = _QuotePatt.search(mystr, i)
        if not o_match and not q_match:  # Neither matched
            res.append(mystr[i:])
            break
        # else:
        j = k = -1
        if o_match:
            j = o_match.start(0)
        if q_match:
            k = q_match.start(0)
        if q_match and (not o_match or k < j):  # QuotePatt matched
            res.append(mystr[i:k])
            res.append(mystr[k + 1])
            i = k + 2
        else:  # OctalPatt matched
            res.append(mystr[i:j])
            res.append(chr(int(mystr[j + 1:j + 4], 8)))
            i = j + 4
    return _nulljoin(res)
コード例 #28
0
ファイル: naming.py プロジェクト: edliaw/fungidb_tools
    def abbrev(self, genus, species, strain):
        """Return a new OrthoMCL abbreviation for the given organism."""
        if (genus, species, strain) in self.abbrevs:
            return self.abbrevs[(genus, species, strain)]
        else:
            abbrevs = set(self.abbrevs.values())

            long_name = genus[0] + species + abbrev_strain(strain)
            long_name = RE_NON_ALPHA.sub("", long_name).lower()

            length = 4
            prefix = long_name[:length - 1]
            mod = ord(long_name[length - 1])
            for i in range(26):
                name = prefix + chr(mod + i)
                if name not in abbrevs:
                    self.abbrevs[(genus, species, strain)] = name
                    return name
            raise Exception("Couldn't find a unique abbreviation for "
                            "{} {} {}".format(genus, species, strain))
コード例 #29
0
ファイル: Session.py プロジェクト: hfalcic/google-gdata
    def _createSharedKey(self, sharedKeyUsername, sharedKey):
        if len(sharedKeyUsername)>16:
            raise ValueError()
        if len(sharedKey)>47:
            raise ValueError()

        self.sharedKeyUsername = sharedKeyUsername

        self.sessionID = createByteArrayZeros(16)
        for x in range(len(sharedKeyUsername)):
            self.sessionID[x] = ord(sharedKeyUsername[x])

        premasterSecret = createByteArrayZeros(48)
        sharedKey = chr(len(sharedKey)) + sharedKey
        for x in range(48):
            premasterSecret[x] = ord(sharedKey[x % len(sharedKey)])

        self.masterSecret = PRF(premasterSecret, "shared secret",
                                createByteArraySequence([]), 48)
        self.sharedKey = True
        return self
コード例 #30
0
ファイル: Session.py プロジェクト: jankoprowski/google-gdata
    def _createSharedKey(self, sharedKeyUsername, sharedKey):
        if len(sharedKeyUsername) > 16:
            raise ValueError()
        if len(sharedKey) > 47:
            raise ValueError()

        self.sharedKeyUsername = sharedKeyUsername

        self.sessionID = createByteArrayZeros(16)
        for x in range(len(sharedKeyUsername)):
            self.sessionID[x] = ord(sharedKeyUsername[x])

        premasterSecret = createByteArrayZeros(48)
        sharedKey = chr(len(sharedKey)) + sharedKey
        for x in range(48):
            premasterSecret[x] = ord(sharedKey[x % len(sharedKey)])

        self.masterSecret = PRF(premasterSecret, "shared secret",
                                createByteArraySequence([]), 48)
        self.sharedKey = True
        return self
コード例 #31
0
ファイル: frontend_lib_test.py プロジェクト: costaafm/grr
        def Corruptor(url="", data=None, **kwargs):
            """Futz with some of the fields."""
            comm_cls = rdf_flows.ClientCommunication
            if data is not None:
                self.client_communication = comm_cls.FromSerializedString(data)
            else:
                self.client_communication = comm_cls(None)

            if self.corruptor_field and "server.pem" not in url:
                orig_str_repr = self.client_communication.SerializeToString()
                field_data = getattr(self.client_communication,
                                     self.corruptor_field)
                if hasattr(field_data, "SerializeToString"):
                    # This converts encryption keys to a string so we can corrupt them.
                    field_data = field_data.SerializeToString()

                # TODO: On Python 2.7.6 and lower `array.array` accepts
                # only byte strings as argument so the call below is necessary. Once
                # support for old Python versions is dropped, this call should be
                # removed.
                modified_data = array.array(str("c"), field_data)
                offset = len(field_data) // 2
                char = field_data[offset]
                modified_data[offset] = chr((ord(char) % 250) +
                                            1).encode("latin-1")
                setattr(self.client_communication, self.corruptor_field,
                        modified_data.tostring())

                # Make sure we actually changed the data.
                self.assertNotEqual(field_data, modified_data)

                mod_str_repr = self.client_communication.SerializeToString()
                self.assertLen(orig_str_repr, len(mod_str_repr))
                differences = [
                    True for x, y in zip(orig_str_repr, mod_str_repr) if x != y
                ]
                self.assertLen(differences, 1)

            data = self.client_communication.SerializeToString()
            return self.UrlMock(url=url, data=data, **kwargs)
コード例 #32
0
def body_check(octet):
    """Return True if the octet should be escaped with body quopri."""
    return chr(octet) != _QUOPRI_BODY_MAP[octet]
コード例 #33
0
def header_check(octet):
    """Return True if the octet should be escaped with header quopri."""
    return chr(octet) != _QUOPRI_HEADER_MAP[octet]
コード例 #34
0
ファイル: quoprimime.py プロジェクト: 3liz/Quantum-GIS
def body_check(octet):
    """Return True if the octet should be escaped with body quopri."""
    return chr(octet) != _QUOPRI_BODY_MAP[octet]
コード例 #35
0
def getBase64Nonce(numChars=22):  #defaults to an 132 bit nonce
    bytes = getRandomBytes(numChars)
    bytesStr = "".join([chr(b) for b in bytes])
    return stringToBase64(bytesStr)[:numChars]
コード例 #36
0
 def bytesToString(bytes):
     return "".join([chr(b) for b in bytes])
コード例 #37
0
    def check_verses(self, found_chapter, verse_blocks):

        last_verse = 0
        processed_verses = []

        # go to the first verse marker
        current_cv_index = 0
        while current_cv_index < len(verse_blocks) and verse_blocks[current_cv_index][:2] != '\\v':
            current_cv_index += 1

        # are all the verse markers missing?
        if current_cv_index >= len(verse_blocks):
            self.append_error('All verse markers are missing for ' + self.book_id + ' ' + str(found_chapter.number))
            return

        # verses should be sequential, starting at 1 and ending at found_chapter.expected_max_verse_number
        while current_cv_index < len(verse_blocks):

            # parse the verse number
            test_num = verse_blocks[current_cv_index][3:].strip()

            bridge_marker = None  # type: str

            # check for invalid dash characters in verse bridge
            # en dash = \u2013, 8211
            # em dash = \u2014, 8212
            if chr(8211) in test_num:
                bridge_marker = chr(8211)
                self.append_error('Invalid verse bridge (en dash used), ' + self.book_id + ' ' +
                                  str(found_chapter.number) + ':' + test_num)

            elif chr(8212) in test_num:
                bridge_marker = chr(8212)
                self.append_error('Invalid verse bridge (em dash used), ' + self.book_id + ' ' +
                                  str(found_chapter.number) + ':' + test_num)

            # is this a verse bridge?
            elif '-' in test_num:
                bridge_marker = '-'

            if bridge_marker:
                nums = test_num.split(bridge_marker)
                if len(nums) != 2 or not nums[0].strip().isdigit() or not nums[1].strip().isdigit():
                    self.append_error('Invalid verse bridge, ' + self.book_id + ' ' +
                                      str(found_chapter.number) + ':' + test_num)

                else:
                    for bridge_num in range(int(nums[0].strip()), int(nums[1].strip()) + 1):
                        last_verse = self.check_this_verse(found_chapter, bridge_num, last_verse, processed_verses)

            else:
                if not test_num.isdigit():

                    # the verse number isn't a number
                    self.append_error('Invalid verse number, ' + self.book_id + ' ' +
                                      str(found_chapter.number) + ':' + test_num)

                else:
                    verse_num = int(test_num)
                    last_verse = self.check_this_verse(found_chapter, verse_num, last_verse, processed_verses)

            current_cv_index += 2

        # are there verses missing from the end
        if last_verse < found_chapter.expected_max_verse_number:
            self.append_error('Verses ' + str(last_verse + 1) + ' through ' +
                              str(found_chapter.expected_max_verse_number) + ' for ' + self.book_id + ' ' +
                              str(found_chapter.number) + ' are missing.')
コード例 #38
0
ファイル: hmac.py プロジェクト: hfalcic/google-gdata
def _strxor(s1, s2):
    """Utility method. XOR the two strings s1 and s2 (must have same length).
    """
    return "".join(map(lambda x, y: chr(ord(x) ^ ord(y)), s1, s2))
コード例 #39
0
ファイル: _encoded_words.py プロジェクト: 3liz/Quantum-GIS
 def __missing__(self, key):
     if key in self.safe:
         self[key] = chr(key)
     else:
         self[key] = "={:02X}".format(key)
     return self[key]
コード例 #40
0
ファイル: jython_compat.py プロジェクト: hfalcic/google-gdata
 def bytesToString(bytes):
     return "".join([chr(b) for b in bytes])
コード例 #41
0
    def _sendMsg(self, msg, skipEmptyFrag=False):
        bytes = msg.write()
        contentType = msg.contentType

        #Whenever we're connected and asked to send a message,
        #we first send an empty Application Data message.  This prevents
        #an attacker from launching a chosen-plaintext attack based on
        #knowing the next IV.
        if not self.closed and not skipEmptyFrag and self.version == (3,1):
            if self._writeState.encContext:
                if self._writeState.encContext.isBlockCipher:
                    for result in self._sendMsg(ApplicationData(),
                                               skipEmptyFrag=True):
                        yield result

        #Update handshake hashes
        if contentType == ContentType.handshake:
            bytesStr = bytesToString(bytes)
            self._handshake_md5.update(bytesStr)
            self._handshake_sha.update(bytesStr)

        #Calculate MAC
        if self._writeState.macContext:
            seqnumStr = self._writeState.getSeqNumStr()
            bytesStr = bytesToString(bytes)
            mac = self._writeState.macContext.copy()
            mac.update(seqnumStr)
            mac.update(chr(contentType))
            if self.version == (3,0):
                mac.update( chr( int(len(bytes)/256) ) )
                mac.update( chr( int(len(bytes)%256) ) )
            elif self.version in ((3,1), (3,2)):
                mac.update(chr(self.version[0]))
                mac.update(chr(self.version[1]))
                mac.update( chr( int(len(bytes)/256) ) )
                mac.update( chr( int(len(bytes)%256) ) )
            else:
                raise AssertionError()
            mac.update(bytesStr)
            macString = mac.digest()
            macBytes = stringToBytes(macString)
            if self.fault == Fault.badMAC:
                macBytes[0] = (macBytes[0]+1) % 256

        #Encrypt for Block or Stream Cipher
        if self._writeState.encContext:
            #Add padding and encrypt (for Block Cipher):
            if self._writeState.encContext.isBlockCipher:

                #Add TLS 1.1 fixed block
                if self.version == (3,2):
                    bytes = self.fixedIVBlock + bytes

                #Add padding: bytes = bytes + (macBytes + paddingBytes)
                currentLength = len(bytes) + len(macBytes) + 1
                blockLength = self._writeState.encContext.block_size
                paddingLength = blockLength-(currentLength % blockLength)

                paddingBytes = createByteArraySequence([paddingLength] * \
                                                       (paddingLength+1))
                if self.fault == Fault.badPadding:
                    paddingBytes[0] = (paddingBytes[0]+1) % 256
                endBytes = concatArrays(macBytes, paddingBytes)
                bytes = concatArrays(bytes, endBytes)
                #Encrypt
                plaintext = stringToBytes(bytes)
                ciphertext = self._writeState.encContext.encrypt(plaintext)
                bytes = stringToBytes(ciphertext)

            #Encrypt (for Stream Cipher)
            else:
                bytes = concatArrays(bytes, macBytes)
                plaintext = bytesToString(bytes)
                ciphertext = self._writeState.encContext.encrypt(plaintext)
                bytes = stringToBytes(ciphertext)

        #Add record header and send
        r = RecordHeader3().create(self.version, contentType, len(bytes))
        s = bytesToString(concatArrays(r.write(), bytes))
        while 1:
            try:
                bytesSent = self.sock.send(s) #Might raise socket.error
            except socket.error as why:
                if why[0] == errno.EWOULDBLOCK:
                    yield 1
                    continue
                else:
                    raise
            if bytesSent == len(s):
                return
            s = s[bytesSent:]
            yield 1
コード例 #42
0
    def _decryptRecord(self, recordType, bytes):
        if self._readState.encContext:

            #Decrypt if it's a block cipher
            if self._readState.encContext.isBlockCipher:
                blockLength = self._readState.encContext.block_size
                if len(bytes) % blockLength != 0:
                    for result in self._sendError(\
                            AlertDescription.decryption_failed,
                            "Encrypted data not a multiple of blocksize"):
                        yield result
                ciphertext = bytesToString(bytes)
                plaintext = self._readState.encContext.decrypt(ciphertext)
                if self.version == (3,2): #For TLS 1.1, remove explicit IV
                    plaintext = plaintext[self._readState.encContext.block_size : ]
                bytes = stringToBytes(plaintext)

                #Check padding
                paddingGood = True
                paddingLength = bytes[-1]
                if (paddingLength+1) > len(bytes):
                    paddingGood=False
                    totalPaddingLength = 0
                else:
                    if self.version == (3,0):
                        totalPaddingLength = paddingLength+1
                    elif self.version in ((3,1), (3,2)):
                        totalPaddingLength = paddingLength+1
                        paddingBytes = bytes[-totalPaddingLength:-1]
                        for byte in paddingBytes:
                            if byte != paddingLength:
                                paddingGood = False
                                totalPaddingLength = 0
                    else:
                        raise AssertionError()

            #Decrypt if it's a stream cipher
            else:
                paddingGood = True
                ciphertext = bytesToString(bytes)
                plaintext = self._readState.encContext.decrypt(ciphertext)
                bytes = stringToBytes(plaintext)
                totalPaddingLength = 0

            #Check MAC
            macGood = True
            macLength = self._readState.macContext.digest_size
            endLength = macLength + totalPaddingLength
            if endLength > len(bytes):
                macGood = False
            else:
                #Read MAC
                startIndex = len(bytes) - endLength
                endIndex = startIndex + macLength
                checkBytes = bytes[startIndex : endIndex]

                #Calculate MAC
                seqnumStr = self._readState.getSeqNumStr()
                bytes = bytes[:-endLength]
                bytesStr = bytesToString(bytes)
                mac = self._readState.macContext.copy()
                mac.update(seqnumStr)
                mac.update(chr(recordType))
                if self.version == (3,0):
                    mac.update( chr( int(len(bytes)/256) ) )
                    mac.update( chr( int(len(bytes)%256) ) )
                elif self.version in ((3,1), (3,2)):
                    mac.update(chr(self.version[0]))
                    mac.update(chr(self.version[1]))
                    mac.update( chr( int(len(bytes)/256) ) )
                    mac.update( chr( int(len(bytes)%256) ) )
                else:
                    raise AssertionError()
                mac.update(bytesStr)
                macString = mac.digest()
                macBytes = stringToBytes(macString)

                #Compare MACs
                if macBytes != checkBytes:
                    macGood = False

            if not (paddingGood and macGood):
                for result in self._sendError(AlertDescription.bad_record_mac,
                                          "MAC failure (or padding failure)"):
                    yield result

        yield bytes
コード例 #43
0
ファイル: strings.py プロジェクト: franzinc/agraph-python
    """
    Return a unicode string escaped according to N-Triples
    canonical encoding rules.
    """
    if not isinstance(string, unicode):
        string = unicode(string, 'utf-8')

    for char, replacement in ESCAPES:
        string = string.replace(char, replacement)
    return string


ESCAPES = [
    # Replacements will be performed sequentially, so backslash 
    # must be the first character on the list
    (chr(0x5C), r'\\'),  
    (chr(0x0A), r'\n'),
    (chr(0x0D), r'\r'),
    (chr(0x22), r'\"'),
]

uri_escaped_chars = re.compile(r'[\x00-\x20<>"{}|^`\\]')


def uri_escape_match(match):
    """
    Converts a Match object representing a single character
    into an ntriple escape sequence.
    """
    code = ord(match.group())
    if code <= 0xffff:
コード例 #44
0
 def bchr(s):
     return chr(s)
コード例 #45
0
ファイル: cryptomath.py プロジェクト: hfalcic/google-gdata
def getBase64Nonce(numChars=22): #defaults to an 132 bit nonce
    bytes = getRandomBytes(numChars)
    bytesStr = "".join([chr(b) for b in bytes])
    return stringToBase64(bytesStr)[:numChars]
コード例 #46
0
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  # 0x60
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5,  # 0x70
    2, 5, 1, 4, 1, 1, 3, 3, 4, 3, 1, 1, 1, 5, 1, 5,  # 0x80
    5, 1, 1, 1, 1, 3, 1, 1, 4, 1, 1, 1, 1, 5, 1, 1,  # 0x90
    1, 0, 2, 2, 3, 2, 4, 2, 4, 2, 2, 0, 3, 1, 1, 4,  # 0xa0
    2, 2, 3, 3, 4, 3, 3, 2, 4, 4, 4, 0, 3, 3, 3, 0,  # 0xb0
    0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  # 0xc0
    1, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0,  # 0xd0
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  # 0xe0
    1, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0,  # 0xf0
)

# Pre-cache the Unicode data saying which of these first 256 characters are
# letters. We'll need it often.
SINGLE_BYTE_LETTERS = [
    unicodedata.category(chr(i)).startswith('L')
    for i in range(256)
]

# A table telling us how to interpret the first word of a letter's Unicode
# name. The number indicates how frequently we expect this script to be used
# on computers. Many scripts not included here are assumed to have a frequency
# of '0' -- if you're going to write in Linear B using Unicode, you're
# probably aware enough of encoding issues to get it right.
#
# The lowercase name is a general category -- for example, Han characters and
# Hiragana characters are very frequently adjacent in Japanese, so they all go
# into category 'cjk'. Letters of different categories are assumed not to
# appear next to each other often.
SCRIPT_TABLE = {
    'LATIN': (3, 'latin'),
コード例 #47
0
 def strip_control_characters(input_txt):
     """
     Strips control character from ``input_txt``
     :param input_txt: text to strip.
     :return: stripped text
     """
     if input_txt:
         # unicode invalid characters
         re_illegal = \
             '([\u0000-\u0008\u000b-\u000c\u000e-\u001f\ufffe-\uffff])' + \
             '|' + \
             '([%s-%s][^%s-%s])|([^%s-%s][%s-%s])|([%s-%s]$)|(^[%s-%s])' % \
             (chr(0xd800), chr(0xdbff), chr(0xdc00), chr(0xdfff),
              chr(0xd800), chr(0xdbff), chr(0xdc00), chr(0xdfff),
              chr(0xd800), chr(0xdbff), chr(0xdc00), chr(0xdfff))
         input_txt = re.sub(re_illegal, "", input_txt)
         # ascii control characters
         input_txt = re.sub(r"[\x01-\x1F\x7F]", "", input_txt)
     return input_txt
コード例 #48
0
ファイル: parse.py プロジェクト: BethMwangi/Flask-social
 def __missing__(self, b):
     # Handle a cache miss. Store quoted string in cache and return.
     res = chr(b) if b in self.safe else '%{0:02X}'.format(b)
     self[b] = res
     return res
コード例 #49
0
NL = '\n'
EMPTYSTRING = ''

# Build a mapping of octets to the expansion of that octet.  Since we're only
# going to have 256 of these things, this isn't terribly inefficient
# space-wise.  Remember that headers and bodies have different sets of safe
# characters.  Initialize both maps with the full expansion, and then override
# the safe bytes with the more compact form.
_QUOPRI_MAP = dict((c, '=%02X' % c) for c in range(256))
_QUOPRI_HEADER_MAP = _QUOPRI_MAP.copy()
_QUOPRI_BODY_MAP = _QUOPRI_MAP.copy()

# Safe header bytes which need no encoding.
for c in bytes(b'-!*+/' + ascii_letters.encode('ascii') +
               digits.encode('ascii')):
    _QUOPRI_HEADER_MAP[c] = chr(c)
# Headers have one other special encoding; spaces become underscores.
_QUOPRI_HEADER_MAP[ord(' ')] = '_'

# Safe body bytes which need no encoding.
for c in bytes(b' !"#$%&\'()*+,-./0123456789:;<>'
               b'?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`'
               b'abcdefghijklmnopqrstuvwxyz{|}~\t'):
    _QUOPRI_BODY_MAP[c] = chr(c)


# Helpers
def header_check(octet):
    """Return True if the octet should be escaped with header quopri."""
    return chr(octet) != _QUOPRI_HEADER_MAP[octet]
コード例 #50
0
ファイル: utils.py プロジェクト: baboq/api-gateway
 def _pad(self, s):
     return s + (self.bs - len(s) % self.bs) * utf8(chr(self.bs - len(s) % self.bs))
コード例 #51
0
ファイル: core.py プロジェクト: kaeufl/obspy
def readMSEED(mseed_object, starttime=None, endtime=None, headonly=False,
              sourcename=None, reclen=None, recinfo=True, details=False,
              header_byteorder=None, verbose=None, **kwargs):
    """
    Reads a Mini-SEED file and returns a Stream object.

    .. warning::
        This function should NOT be called directly, it registers via the
        ObsPy :func:`~obspy.core.stream.read` function, call this instead.

    :param mseed_object: Filename or open file like object that contains the
        binary Mini-SEED data. Any object that provides a read() method will be
        considered to be a file like object.
    :type starttime: UTCDateTime
    :param starttime: Only read data samples after or at the starttime.
    :type endtime: UTCDateTime
    :param endtime: Only read data samples before or at the starttime.
    :param headonly: Determines whether or not to unpack the data or just
        read the headers.
    :type sourcename: str
    :param sourcename: Sourcename has to have the structure
        'network.station.location.channel' and can contain globbing characters.
        Defaults to ``None``.
    :param reclen: If it is None, it will be automatically determined for every
        record. If it is known, just set it to the record length in bytes which
        will increase the reading speed slightly.
    :type recinfo: bool, optional
    :param recinfo: If ``True`` the byteorder, record length and the
        encoding of the file will be read and stored in every Trace's
        stats.mseed AttribDict. These stored attributes will also be used while
        writing a Mini-SEED file. Only the very first record of the file will
        be read and all following records are assumed to be the same. Defaults
        to ``True``.
    :type details: bool, optional
    :param details: If ``True`` read additional information: timing quality
        and availability of calibration information.
        Note, that the traces are then also split on these additional
        information. Thus the number of traces in a stream will change.
        Details are stored in the mseed stats AttribDict of each trace.
        -1 specifies for both cases, that these information is not available.
        ``timing_quality`` specifies the timing quality from 0 to 100 [%].
        ``calibration_type`` specifies the type of available calibration
        information: 1 == Step Calibration, 2 == Sine Calibration, 3 ==
        Pseudo-random Calibration, 4 == Generic Calibration and -2 ==
        Calibration Abort.
    :type header_byteorder: [``0`` or ``'<'`` | ``1`` or ``'>'`` | ``'='``],
        optional
    :param header_byteorder: Must be either ``0`` or ``'<'`` for LSBF or
        little-endian, ``1`` or ``'>'`` for MBF or big-endian. ``'='`` is the
        native byteorder. Used to enforce the header byteorder. Useful in some
        rare cases where the automatic byte order detection fails.

    .. rubric:: Example

    >>> from obspy import read
    >>> st = read("/path/to/two_channels.mseed")
    >>> print(st)  # doctest: +ELLIPSIS
    2 Trace(s) in Stream:
    BW.UH3..EHE | 2010-06-20T00:00:00.279999Z - ... | 200.0 Hz, 386 samples
    BW.UH3..EHZ | 2010-06-20T00:00:00.279999Z - ... | 200.0 Hz, 386 samples

    >>> from obspy import UTCDateTime
    >>> st = read("/path/to/test.mseed",
    ...           starttime=UTCDateTime("2003-05-29T02:16:00"),
    ...           selection="NL.*.*.?HZ")
    >>> print(st)  # doctest: +ELLIPSIS
    1 Trace(s) in Stream:
    NL.HGN.00.BHZ | 2003-05-29T02:15:59.993400Z - ... | 40.0 Hz, 5629 samples
    """
    # Parse the headonly and reclen flags.
    if headonly is True:
        unpack_data = 0
    else:
        unpack_data = 1
    if reclen is None:
        reclen = -1
    elif reclen not in VALID_RECORD_LENGTHS:
        msg = 'Invalid record length. Autodetection will be used.'
        warnings.warn(msg)
        reclen = -1

    # Determine the byteorder.
    if header_byteorder == "=":
        header_byteorder = NATIVE_BYTEORDER

    if header_byteorder is None:
        header_byteorder = -1
    elif header_byteorder in [0, "0", "<"]:
        header_byteorder = 0
    elif header_byteorder in [1, "1", ">"]:
        header_byteorder = 1

    # The quality flag is no more supported. Raise a warning.
    if 'quality' in kwargs:
        msg = 'The quality flag is no more supported in this version of ' + \
            'obspy.mseed. obspy.mseed.util has some functions with similar' + \
            ' behaviour.'
        warnings.warn(msg, category=DeprecationWarning)

    # Parse some information about the file.
    if recinfo:
        # Pass the byteorder if enforced.
        if header_byteorder == 0:
            bo = "<"
        elif header_byteorder > 0:
            bo = ">"
        else:
            bo = None

        info = util.getRecordInformation(mseed_object, endian=bo)
        info['encoding'] = ENCODINGS[info['encoding']][0]
        # Only keep information relevant for the whole file.
        info = {'encoding': info['encoding'],
                'filesize': info['filesize'],
                'record_length': info['record_length'],
                'byteorder': info['byteorder'],
                'number_of_records': info['number_of_records']}

    # If its a filename just read it.
    if isinstance(mseed_object, (str, native_str)):
        # Read to NumPy array which is used as a buffer.
        buffer = np.fromfile(mseed_object, dtype='b')
    elif hasattr(mseed_object, 'read'):
        buffer = np.fromstring(mseed_object.read(), dtype='b')

    # Get the record length
    try:
        record_length = pow(2, int(''.join([chr(_i) for _i in buffer[19:21]])))
    except ValueError:
        record_length = 4096

    # Search for data records and pass only the data part to the underlying C
    # routine.
    offset = 0
    # 0 to 9 are defined in a row in the ASCII charset.
    min_ascii = ord('0')
    # Small function to check whether an array of ASCII values contains only
    # digits.
    isdigit = lambda x: True if (x - min_ascii).max() <= 9 else False
    while True:
        # This should never happen
        if (isdigit(buffer[offset:offset + 6]) is False) or \
                (buffer[offset + 6] not in VALID_CONTROL_HEADERS):
            msg = 'Not a valid (Mini-)SEED file'
            raise Exception(msg)
        elif buffer[offset + 6] in SEED_CONTROL_HEADERS:
            offset += record_length
            continue
        break
    buffer = buffer[offset:]
    buflen = len(buffer)

    # If no selection is given pass None to the C function.
    if starttime is None and endtime is None and sourcename is None:
        selections = None
    else:
        select_time = SelectTime()
        selections = Selections()
        selections.timewindows.contents = select_time
        if starttime is not None:
            if not isinstance(starttime, UTCDateTime):
                msg = 'starttime needs to be a UTCDateTime object'
                raise ValueError(msg)
            selections.timewindows.contents.starttime = \
                util._convertDatetimeToMSTime(starttime)
        else:
            # HPTERROR results in no starttime.
            selections.timewindows.contents.starttime = HPTERROR
        if endtime is not None:
            if not isinstance(endtime, UTCDateTime):
                msg = 'endtime needs to be a UTCDateTime object'
                raise ValueError(msg)
            selections.timewindows.contents.endtime = \
                util._convertDatetimeToMSTime(endtime)
        else:
            # HPTERROR results in no starttime.
            selections.timewindows.contents.endtime = HPTERROR
        if sourcename is not None:
            if not isinstance(sourcename, (str, native_str)):
                msg = 'sourcename needs to be a string'
                raise ValueError(msg)
            # libmseed uses underscores as separators and allows filtering
            # after the dataquality which is disabled here to not confuse
            # users. (* == all data qualities)
            selections.srcname = (sourcename.replace('.', '_') + '_*').\
                encode('ascii', 'ignore')
        else:
            selections.srcname = b'*'
    all_data = []

    # Use a callback function to allocate the memory and keep track of the
    # data.
    def allocate_data(samplecount, sampletype):
        # Enhanced sanity checking for libmseed 2.10 can result in the
        # sampletype not being set. Just return an empty array in this case.
        if sampletype == b"\x00":
            data = np.empty(0)
        else:
            data = np.empty(samplecount, dtype=DATATYPES[sampletype])
        all_data.append(data)
        return data.ctypes.data
    # XXX: Do this properly!
    # Define Python callback function for use in C function. Return a long so
    # it hopefully works on 32 and 64 bit systems.
    allocData = C.CFUNCTYPE(C.c_long, C.c_int, C.c_char)(allocate_data)

    def log_error_or_warning(msg):
        if msg.startswith(b"ERROR: "):
            raise InternalMSEEDReadingError(msg[7:].strip())
        if msg.startswith(b"INFO: "):
            warnings.warn(msg[6:].strip(), InternalMSEEDReadingWarning)
    diag_print = C.CFUNCTYPE(C.c_void_p, C.c_char_p)(log_error_or_warning)

    def log_message(msg):
        print(msg[6:].strip())
    log_print = C.CFUNCTYPE(C.c_void_p, C.c_char_p)(log_message)

    try:
        verbose = int(verbose)
    except:
        verbose = 0

    lil = clibmseed.readMSEEDBuffer(
        buffer, buflen, selections, C.c_int8(unpack_data),
        reclen, C.c_int8(verbose), C.c_int8(details), header_byteorder,
        allocData, diag_print, log_print)

    # XXX: Check if the freeing works.
    del selections

    traces = []
    try:
        currentID = lil.contents
    # Return stream if not traces are found.
    except ValueError:
        clibmseed.lil_free(lil)
        del lil
        return Stream()

    while True:
        # Init header with the essential information.
        header = {'network': currentID.network.strip(),
                  'station': currentID.station.strip(),
                  'location': currentID.location.strip(),
                  'channel': currentID.channel.strip(),
                  'mseed': {'dataquality': currentID.dataquality}}
        # Loop over segments.
        try:
            currentSegment = currentID.firstSegment.contents
        except ValueError:
            break
        while True:
            header['sampling_rate'] = currentSegment.samprate
            header['starttime'] = \
                util._convertMSTimeToDatetime(currentSegment.starttime)
            # TODO: write support is missing
            if details:
                timing_quality = currentSegment.timing_quality
                if timing_quality == 0xFF:  # 0xFF is mask for not known timing
                    timing_quality = -1
                header['mseed']['timing_quality'] = timing_quality
                header['mseed']['calibration_type'] = \
                    currentSegment.calibration_type

            if headonly is False:
                # The data always will be in sequential order.
                data = all_data.pop(0)
                header['npts'] = len(data)
            else:
                data = np.array([])
                header['npts'] = currentSegment.samplecnt
            # Make sure to init the number of samples.
            # Py3k: convert to unicode
            header['mseed'] = dict((k, v.decode())
                                   if isinstance(v, bytes) else (k, v)
                                   for k, v in header['mseed'].items())
            header = dict((k, v.decode()) if isinstance(v, bytes) else (k, v)
                          for k, v in header.items())
            trace = Trace(header=header, data=data)
            # Append information if necessary.
            if recinfo:
                for key, value in info.items():
                    setattr(trace.stats.mseed, key, value)
            traces.append(trace)
            # A Null pointer access results in a ValueError
            try:
                currentSegment = currentSegment.next.contents
            except ValueError:
                break
        try:
            currentID = currentID.next.contents
        except ValueError:
            break

    clibmseed.lil_free(lil)  # NOQA
    del lil  # NOQA
    return Stream(traces=traces)
コード例 #52
0
    0,
    2,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,  # 0xf0
)

# Pre-cache the Unicode data saying which of these first 256 characters are
# letters. We'll need it often.
SINGLE_BYTE_LETTERS = [
    unicodedata.category(chr(i)).startswith('L') for i in range(256)
]

# A table telling us how to interpret the first word of a letter's Unicode
# name. The number indicates how frequently we expect this script to be used
# on computers. Many scripts not included here are assumed to have a frequency
# of '0' -- if you're going to write in Linear B using Unicode, you're
# probably aware enough of encoding issues to get it right.
#
# The lowercase name is a general category -- for example, Han characters and
# Hiragana characters are very frequently adjacent in Japanese, so they all go
# into category 'cjk'. Letters of different categories are assumed not to
# appear next to each other often.
SCRIPT_TABLE = {
    'LATIN': (3, 'latin'),
    'CJK': (2, 'cjk'),
コード例 #53
0
def unquote(s):
    """Turn a string in the form =AB to the ASCII character with value 0xab"""
    return chr(int(s[1:3], 16))
コード例 #54
0
ファイル: test_repairer.py プロジェクト: sajith/tahoe-lafs
 def _corruptor(s, debug=False):
     return s[:which] + chr(ord(s[which]) ^ 0x01) + s[which + 1:]
コード例 #55
0
ファイル: quoprimime.py プロジェクト: 3liz/Quantum-GIS
CRLF = '\r\n'
NL = '\n'
EMPTYSTRING = ''

# Build a mapping of octets to the expansion of that octet.  Since we're only
# going to have 256 of these things, this isn't terribly inefficient
# space-wise.  Remember that headers and bodies have different sets of safe
# characters.  Initialize both maps with the full expansion, and then override
# the safe bytes with the more compact form.
_QUOPRI_HEADER_MAP = dict((c, '=%02X' % c) for c in range(256))
_QUOPRI_BODY_MAP = _QUOPRI_HEADER_MAP.copy()

# Safe header bytes which need no encoding.
for c in bytes(b'-!*+/' + ascii_letters.encode('ascii') + digits.encode('ascii')):
    _QUOPRI_HEADER_MAP[c] = chr(c)
# Headers have one other special encoding; spaces become underscores.
_QUOPRI_HEADER_MAP[ord(' ')] = '_'

# Safe body bytes which need no encoding.
for c in bytes(b' !"#$%&\'()*+,-./0123456789:;<>'
               b'?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`'
               b'abcdefghijklmnopqrstuvwxyz{|}~\t'):
    _QUOPRI_BODY_MAP[c] = chr(c)



# Helpers
def header_check(octet):
    """Return True if the octet should be escaped with header quopri."""
    return chr(octet) != _QUOPRI_HEADER_MAP[octet]
コード例 #56
0
ファイル: parse.py プロジェクト: club9822/rest
 def __missing__(self, b):
     # Handle a cache miss. Store quoted string in cache and return.
     res = chr(b) if b in self.safe else '%{0:02X}'.format(b)
     self[b] = res
     return res
コード例 #57
0
ファイル: quoprimime.py プロジェクト: 3liz/Quantum-GIS
def unquote(s):
    """Turn a string in the form =AB to the ASCII character with value 0xab"""
    return chr(int(s[1:3], 16))
コード例 #58
0
ファイル: hashlib.py プロジェクト: GammaC0de/pyload
    algorithms = __always_supported

    __all__ = __always_supported + ('new', 'algorithms_guaranteed',
                                    'algorithms_available', 'algorithms',
                                    'pbkdf2_hmac')

    try:
        # OpenSSL's PKCS5_PBKDF2_HMAC requires OpenSSL 1.0+ with HMAC and SHA
        from _hashlib import pbkdf2_hmac

    except ImportError:
        import binascii
        import struct

        _trans_5C = b''.join(chr(i ^ 0x5C) for i in range(256))
        _trans_36 = b''.join(chr(i ^ 0x36) for i in range(256))

        def pbkdf2_hmac(hash_name, password, salt, iterations, dklen=None):
            """Password based key derivation function 2 (PKCS #5 v2.0)

            This Python implementations based on the hmac module about
            as fast as OpenSSL's PKCS5_PBKDF2_HMAC for short passwords
            and much faster for long passwords.

            """
            if not isinstance(hash_name, str):
                raise TypeError(hash_name)

            if not isinstance(password, (bytes, bytearray)):
                password = bytes(buffer(password))
コード例 #59
0
ファイル: quoprimime.py プロジェクト: 3liz/Quantum-GIS
def header_check(octet):
    """Return True if the octet should be escaped with header quopri."""
    return chr(octet) != _QUOPRI_HEADER_MAP[octet]