Example #1
0
def process_buffers_for_display(s, limit=40):
  """Process a buffer for human-readable display.

  This function performs the following operation on each of the buffers in `s`.
    1. Truncate input buffer if the length of the buffer is greater than
       `limit`, to prevent large strings from overloading the frontend.
    2. Apply `binascii.b2a_qp` on the truncated buffer to make the buffer
       printable and convertible to JSON.
    3. If truncation happened (in step 1), append a string at the end
       describing the original length and the truncation.

  Args:
    s: The buffer to be processed, either a single buffer or a nested array of
      them.
    limit: Length limit for each buffer, beyond which truncation will occur.

  Return:
    A single processed buffer or a nested array of processed buffers.
  """
  if isinstance(s, (list, tuple)):
    return [process_buffers_for_display(elem, limit=limit) for elem in s]
  else:
    length = len(s)
    if length > limit:
      return (binascii.b2a_qp(s[:limit]) +
              b' (length-%d truncated at %d bytes)' % (length, limit))
    else:
      return binascii.b2a_qp(s)
Example #2
0
    def test_qp(self):
        binascii.a2b_qp(data=b"", header=False)  # Keyword arguments allowed

        # A test for SF bug 534347 (segfaults without the proper fix)
        try:
            binascii.a2b_qp(b"", **{1: 1})
        except TypeError:
            pass
        else:
            self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError")

        self.assertEqual(binascii.a2b_qp(b"= "), b"= ")
        self.assertEqual(binascii.a2b_qp(b"=="), b"=")
        self.assertEqual(binascii.a2b_qp(b"=AX"), b"=AX")
        self.assertRaises(TypeError, binascii.b2a_qp, foo="bar")
        self.assertEqual(binascii.a2b_qp(b"=00\r\n=00"), b"\x00\r\n\x00")
        self.assertEqual(binascii.b2a_qp(b"\xff\r\n\xff\n\xff"),
                         b"=FF\r\n=FF\r\n=FF")
        self.assertEqual(binascii.b2a_qp(b"0" * 75 + b"\xff\r\n\xff\r\n\xff"),
                         b"0" * 75 + b"=\r\n=FF\r\n=FF\r\n=FF")

        self.assertEqual(binascii.b2a_qp(b'\0\n'), b'=00\n')
        self.assertEqual(binascii.b2a_qp(b'\0\n', quotetabs=True), b'=00\n')
        self.assertEqual(binascii.b2a_qp(b'foo\tbar\t\n'), b'foo\tbar=09\n')
        self.assertEqual(binascii.b2a_qp(b'foo\tbar\t\n', quotetabs=True),
                         b'foo=09bar=09\n')

        self.assertEqual(binascii.b2a_qp(b'.'), b'=2E')
        self.assertEqual(binascii.b2a_qp(b'.\n'), b'=2E\n')
        self.assertEqual(binascii.b2a_qp(b'a.\n'), b'a.\n')
Example #3
0
    def test_qp(self):
        if test_support.due_to_ironpython_bug(
                "http://tkbgitvstfat01:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=317834"
        ):
            return
        # A test for SF bug 534347 (segfaults without the proper fix)
        try:
            binascii.a2b_qp("", **{1: 1})
        except TypeError:
            pass
        else:
            self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError")
        self.assertEqual(binascii.a2b_qp("= "), "= ")
        self.assertEqual(binascii.a2b_qp("=="), "=")
        self.assertEqual(binascii.a2b_qp("=AX"), "=AX")
        self.assertRaises(TypeError, binascii.b2a_qp, foo="bar")
        self.assertEqual(binascii.a2b_qp("=00\r\n=00"), "\x00\r\n\x00")
        self.assertEqual(binascii.b2a_qp("\xff\r\n\xff\n\xff"),
                         "=FF\r\n=FF\r\n=FF")
        self.assertEqual(binascii.b2a_qp("0" * 75 + "\xff\r\n\xff\r\n\xff"),
                         "0" * 75 + "=\r\n=FF\r\n=FF\r\n=FF")

        self.assertEqual(binascii.b2a_qp('\0\n'), '=00\n')
        self.assertEqual(binascii.b2a_qp('\0\n', quotetabs=True), '=00\n')
        self.assertEqual(binascii.b2a_qp('foo\tbar\t\n'), 'foo\tbar=09\n')
        self.assertEqual(binascii.b2a_qp('foo\tbar\t\n', quotetabs=True),
                         'foo=09bar=09\n')

        self.assertEqual(binascii.b2a_qp('.'), '=2E')
        self.assertEqual(binascii.b2a_qp('.\n'), '=2E\n')
        self.assertEqual(binascii.b2a_qp('a.\n'), 'a.\n')
Example #4
0
    def test_qp(self):
        # A test for SF bug 534347 (segfaults without the proper fix)
        try:
            binascii.a2b_qp("", **{1:1})
        except TypeError:
            pass
        else:
            self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError")
        self.assertEqual(binascii.a2b_qp("= "), "= ")
        self.assertEqual(binascii.a2b_qp("=="), "=")
        self.assertEqual(binascii.a2b_qp("=AX"), "=AX")
        self.assertRaises(TypeError, binascii.b2a_qp, foo="bar")
        self.assertEqual(binascii.a2b_qp("=00\r\n=00"), "\x00\r\n\x00")
        self.assertEqual(
            binascii.b2a_qp("\xff\r\n\xff\n\xff"),
            "=FF\r\n=FF\r\n=FF"
        )
        self.assertEqual(
            binascii.b2a_qp("0"*75+"\xff\r\n\xff\r\n\xff"),
            "0"*75+"=\r\n=FF\r\n=FF\r\n=FF"
        )

        self.assertEqual(binascii.b2a_qp('\0\n'), '=00\n')
        self.assertEqual(binascii.b2a_qp('\0\n', quotetabs=True), '=00\n')
        self.assertEqual(binascii.b2a_qp('foo\tbar\t\n'), 'foo\tbar=09\n')
        self.assertEqual(binascii.b2a_qp('foo\tbar\t\n', quotetabs=True), 'foo=09bar=09\n')

        self.assertEqual(binascii.b2a_qp('.'), '=2E')
        self.assertEqual(binascii.b2a_qp('.\n'), '=2E\n')
        self.assertEqual(binascii.b2a_qp('a.\n'), 'a.\n')
Example #5
0
    def test_qp(self):
        # A test for SF bug 534347 (segfaults without the proper fix)
        try:
            binascii.a2b_qp(b"", **{1:1})
        except TypeError:
            pass
        else:
            self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError")
        self.assertEqual(binascii.a2b_qp(b"= "), b"= ")
        self.assertEqual(binascii.a2b_qp(b"=="), b"=")
        self.assertEqual(binascii.a2b_qp(b"=AX"), b"=AX")
        self.assertRaises(TypeError, binascii.b2a_qp, foo="bar")
        self.assertEqual(binascii.a2b_qp(b"=00\r\n=00"), b"\x00\r\n\x00")
        self.assertEqual(
            binascii.b2a_qp(b"\xff\r\n\xff\n\xff"),
            b"=FF\r\n=FF\r\n=FF")
        self.assertEqual(
            binascii.b2a_qp(b"0"*75+b"\xff\r\n\xff\r\n\xff"),
            b"0"*75+b"=\r\n=FF\r\n=FF\r\n=FF")

        self.assertEqual(binascii.b2a_qp(b'\0\n'), b'=00\n')
        self.assertEqual(binascii.b2a_qp(b'\0\n', quotetabs=True), b'=00\n')
        self.assertEqual(binascii.b2a_qp(b'foo\tbar\t\n'), b'foo\tbar=09\n')
        self.assertEqual(binascii.b2a_qp(b'foo\tbar\t\n', quotetabs=True),
                         b'foo=09bar=09\n')

        self.assertEqual(binascii.b2a_qp(b'.'), b'=2E')
        self.assertEqual(binascii.b2a_qp(b'.\n'), b'=2E\n')
        self.assertEqual(binascii.b2a_qp(b'a.\n'), b'a.\n')
Example #6
0
    def test_qp(self):
        if test_support.due_to_ironpython_bug(
            "http://tkbgitvstfat01:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=317834"
        ):
            return
        # A test for SF bug 534347 (segfaults without the proper fix)
        try:
            binascii.a2b_qp("", **{1: 1})
        except TypeError:
            pass
        else:
            self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError")
        self.assertEqual(binascii.a2b_qp("= "), "= ")
        self.assertEqual(binascii.a2b_qp("=="), "=")
        self.assertEqual(binascii.a2b_qp("=AX"), "=AX")
        self.assertRaises(TypeError, binascii.b2a_qp, foo="bar")
        self.assertEqual(binascii.a2b_qp("=00\r\n=00"), "\x00\r\n\x00")
        self.assertEqual(binascii.b2a_qp("\xff\r\n\xff\n\xff"), "=FF\r\n=FF\r\n=FF")
        self.assertEqual(binascii.b2a_qp("0" * 75 + "\xff\r\n\xff\r\n\xff"), "0" * 75 + "=\r\n=FF\r\n=FF\r\n=FF")

        self.assertEqual(binascii.b2a_qp("\0\n"), "=00\n")
        self.assertEqual(binascii.b2a_qp("\0\n", quotetabs=True), "=00\n")
        self.assertEqual(binascii.b2a_qp("foo\tbar\t\n"), "foo\tbar=09\n")
        self.assertEqual(binascii.b2a_qp("foo\tbar\t\n", quotetabs=True), "foo=09bar=09\n")

        self.assertEqual(binascii.b2a_qp("."), "=2E")
        self.assertEqual(binascii.b2a_qp(".\n"), "=2E\n")
        self.assertEqual(binascii.b2a_qp("a.\n"), "a.\n")
Example #7
0
 def EvtVarTypeBinary(self, value, index):  #stream of binary data
     string = ""
     for c in value:
         string += hex(c)[2:]
     for c in value:
         try:
             if int(binascii.b2a_qp(str(c))) >= 32:
                 string += chr(int(binascii.b2a_qp(str(c)))).encode('ascii')
             else:
                 string += "."
         except:
             string += "."
     return string
Example #8
0
    def _parse_method(self):
        (result, line) = self._read_line()

        if result:
            sp = line.split(b' ')

            self._method['method'] = b2a_qp(sp[0]).decode('utf-8')
            self._method['uri']    = b2a_qp(sp[1]).decode('utf-8')
            self._method['ver']    = b2a_qp(sp[2]).decode('utf-8')

            self._state = self.__HEADER
            return True
        else:
            return False
Example #9
0
    def _parse_response(self):
        (result, line) = self._read_line()

        if result:
            sp = line.split(b' ')

            self._response['ver']  = b2a_qp(sp[0]).decode('utf-8')
            self._response['code'] = b2a_qp(sp[1]).decode('utf-8')
            self._response['msg']  = b2a_qp((b' '.join(sp[2:]))).decode('utf-8')

            self._state = self.__HEADER
            return True
        else:
            return False
Example #10
0
 def test_qp(self):
     # A test for SF bug 534347 (segfaults without the proper fix)
     try:
         binascii.a2b_qp("", **{1: 1})
     except TypeError:
         pass
     else:
         self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError")
     self.assertEqual(binascii.a2b_qp("= "), "= ")
     self.assertEqual(binascii.a2b_qp("=="), "=")
     self.assertEqual(binascii.a2b_qp("=AX"), "=AX")
     self.assertRaises(TypeError, binascii.b2a_qp, foo="bar")
     self.assertEqual(binascii.a2b_qp("=00\r\n=00"), "\x00\r\n\x00")
     self.assertEqual(binascii.b2a_qp("\xff\r\n\xff\n\xff"), "=FF\r\n=FF\r\n=FF")
     self.assertEqual(binascii.b2a_qp("0" * 75 + "\xff\r\n\xff\r\n\xff"), "0" * 75 + "=\r\n=FF\r\n=FF\r\n=FF")
Example #11
0
def test_not_implemented():
    test_cases = [
                    lambda: binascii.a2b_qp(None),
                    lambda: binascii.a2b_qp(None, None),
                    lambda: binascii.b2a_qp(None),
                    lambda: binascii.b2a_qp(None, None),
                    lambda: binascii.b2a_qp(None, None, None, None),
                    lambda: binascii.a2b_hqx(None),
                    lambda: binascii.rledecode_hqx(None),
                    lambda: binascii.rlecode_hqx(None),
                    lambda: binascii.b2a_hqx(None),
                    lambda: binascii.crc_hqx(None, None),
                    ]
    for temp_func in test_cases:
        AssertError(NotImplementedError, temp_func)
def test_not_implemented():
    test_cases = [
        lambda: binascii.a2b_qp(None),
        lambda: binascii.a2b_qp(None, None),
        lambda: binascii.b2a_qp(None),
        lambda: binascii.b2a_qp(None, None),
        lambda: binascii.b2a_qp(None, None, None, None),
        lambda: binascii.a2b_hqx(None),
        lambda: binascii.rledecode_hqx(None),
        lambda: binascii.rlecode_hqx(None),
        lambda: binascii.b2a_hqx(None),
        lambda: binascii.crc_hqx(None, None),
    ]
    for temp_func in test_cases:
        AssertError(NotImplementedError, temp_func)
Example #13
0
    def _parse_header(self):
        (result, line) = self._read_line()

        if result:
            if line == b'':
                if 'content-length' in self._header:
                    self._remain = int(self._header['content-length'])

                    if self._remain > 0:
                        self._state = self.__BODY
                    elif ('transfer-encoding' in self._header and
                          self._header['transfer-encoding'].lower() == 'chunked'):
                        self._state = self.__CHUNK_LEN
                    elif self._is_client:
                        self._push_data()
                        self._state = self.__METHOD
                    else:
                        self._push_data()
                        self._state = self.__RESP
                elif ('transfer-encoding' in self._header and
                      self._header['transfer-encoding'].lower() == 'chunked'):

                    self._state = self.__CHUNK_LEN
                elif self._is_client:
                    if self._method['ver'] == 'HTTP/1.0':
                        self._remain = -1
                        self._state = self.__BODY
                    else:
                        self._push_data()
                        self._state = self.__METHOD
                else:
                    if self._response['ver'] == 'HTTP/1.0':
                        self._remain = -1
                        self._state = self.__BODY
                    else:
                        self._push_data()
                        self._state = self.__RESP
            else:
                sp = line.split(b': ')

                val = b2a_qp((b': '.join(sp[1:]))).decode('utf-8')
                val = val.strip()

                self._header[b2a_qp(sp[0]).decode('utf-8').lower()] = val

            return True
        else:
            return False
Example #14
0
 def identify_otp_sn(self):
         # OTP added in v4:
         if self.bl_rev > 3:
                 i = 1
                 for byte in range(0,32*6,4):
                         x = self.__getOTP(byte)
                         self.otp = self.otp + x
                         if i == 4:
                                 print(binascii.hexlify(x).decode('Latin-1').upper() + '\n', end='')
                                 i = 1
                         else:
                                 i += 1
                                 print(binascii.hexlify(x).decode('Latin-1').upper() + ' ', end='')
                 # see src/modules/systemlib/otp.h in px4 code:
                 self.otp_id = self.otp[0:4]
                 self.otp_idtype = self.otp[4:5]
                 self.otp_vid = self.otp[8:4:-1]
                 self.otp_pid = self.otp[12:8:-1]
                 self.otp_coa = self.otp[32:160]
                 # show user:
                 print("type: " + self.otp_id.decode('Latin-1'))
                 print("idtype: " + binascii.b2a_qp(self.otp_idtype).decode('Latin-1'))
                 print("vid: " + binascii.hexlify(self.otp_vid).decode('Latin-1').upper())
                 print("pid: "+ binascii.hexlify(self.otp_pid).decode('Latin-1').upper())
                 print("coa:")
                 print(textwrap.fill(binascii.b2a_base64(self.otp_coa).decode('Latin-1'), width=32))
                 print("sn: ", end='')
                 for byte in range(0,12,4):
                         x = self.__getSN(byte)
                         x = x[::-1]  # reverse the bytes
                         self.sn = self.sn + x
                         print(binascii.hexlify(x).decode('Latin-1').upper(), end='') # show user
                 print('')
Example #15
0
    def predict(self, sample):

        try:
            print "Starting Prediction"

            if self.disabled is True:
                # print "classifier is disabled"
                return None
            sample = re.sub(re1, 'XXX.XXX.XXX.XXX', sample.rstrip())
            sample = re.sub(re2, 'XXXX', sample.rstrip())
            if isinstance(sample, bytearray):
                # print "TRUE"
                data = binascii.b2a_qp(sample)
            # print "Classifier data", data

            x = np.array([sample.decode('unicode_escape').encode('utf-8')])
            t = time.time()
            all_labels = self.mlb.inverse_transform(self.classifier.predict(x))
            print "Prediction Time:", time.time() - t
            l = []
            for item, labels in zip(x, all_labels):
                #print labels, sample
                l.append(labels)
            return l
        except:
            traceback.print_exc()
Example #16
0
    async def write(self, chunk):
        if self.compress is not None:
            if chunk:
                chunk = self.compress.compress(chunk)
                if not chunk:
                    return

        if self.encoding == 'base64':
            self.encoding_buffer.extend(chunk)

            if self.encoding_buffer:
                buffer = self.encoding_buffer
                div, mod = divmod(len(buffer), 3)

                encoding_chunk = buffer[:div * 3]
                self.encoding_buffer = buffer[div * 3:]

                if encoding_chunk:
                    encoding_chunk = base64.b64encode(encoding_chunk)
                    await self.writer.write(encoding_chunk)

        elif self.encoding == 'quoted-printable':
            await self.writer.write(binascii.b2a_qp(chunk))

        else:
            await self.writer.write(chunk)
Example #17
0
    def __init__(self, file_name):
        if len(file_name) > 0:
            with open(file_name, "rb") as file:
                data = file.read()

                if b2a_qp(data[0:8]) != FLASH_HEADER:
                    raise AssertionError("File header is invalid")

                self.major_version = data[8]
                self.minor_version = data[9]
                self.flags = data[10]
                name_length = data[0x10]
                self.name = struct.pack('B' * name_length,
                                        *data[0x11:0x11 + name_length]).rstrip(
                                            bytes([0]))
                self.device_type = data[0x30]
                self.data_type = data[0x31]
                data_length = int.from_bytes(data[0x4A:0x4E],
                                             byteorder='little')
                self.data = data[0x4E:0x4E + data_length]
                #checksum = int.from_bytes(data[0x4E+data_length:0x50+data_length], byteorder='little')
                #for i in range(len(self.data)):
                #    checksum = (checksum - self.data[i]) & 0xFFFF
                #if checksum != 0:
                #    raise AssertionError("Checksum is invalid")
        return
Example #18
0
def set_bytes_content(msg,
                      data,
                      maintype,
                      subtype,
                      cte='base64',
                      disposition=None,
                      filename=None,
                      cid=None,
                      params=None,
                      headers=None):
    _prepare_set(msg, maintype, subtype, headers)
    if cte == 'base64':
        data = _encode_base64(data,
                              max_line_length=(msg.policy.max_line_length))
    elif cte == 'quoted-printable':
        data = binascii.b2a_qp(data,
                               istext=False,
                               header=False,
                               quotetabs=True)
        data = data.decode('ascii')
    elif cte == '7bit':
        data.encode('ascii')
    elif cte in ('8bit', 'binary'):
        data = data.decode('ascii', 'surrogateescape')
    msg.set_payload(data)
    msg['Content-Transfer-Encoding'] = cte
    _finalize_set(msg, disposition, filename, cid, params)
Example #19
0
def register(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            private = RSA.generate(1024, Random.new().read)
            user = User.objects.create_user(
                username=form.cleaned_data['username'],
                password=form.cleaned_data['password1'],
                email=form.cleaned_data['email'],
            )
            print(private)
            print(private.exportKey())
            UserProfile.objects.create(
                user=user,
                privateKey=private.exportKey(),
                publicKey=private.publickey().exportKey())
            #print(private.exportKey())
            priv = private.exportKey('PEM')
            priv_KEY = binascii.b2a_qp(priv).decode('latin_1')
            #print(priv_KEY)
            file = open("privateKeyDowload.txt", "w")
            file.write(str(priv))
            privateKeyDownload(request, priv)

            return render_to_response('success.html', {'private': priv_KEY})
    else:
        form = RegistrationForm()
    variables = RequestContext(request, {'form': form})

    return render_to_response(
        'registration/register.html',
        variables,
    )
Example #20
0
def safe_simpleterm_from_value(value):
    """create SimpleTerm from an untrusted value.

    - token need cleaned up: Vocabulary term tokens *must* be 7 bit values
    - anything for display has to be cleaned up, titles *must* be unicode
    """
    return SimpleTerm(value, b2a_qp(safe_encode(value)), safe_unicode(value))
Example #21
0
def gender_detector(name, geography="in"):
    try:
        s2 = name.split(' ')
        s2[0] = s2[0].encode('utf-8')

        s3 = binascii.b2a_qp(s2[0])
        s2[0] = s3.decode('utf-8')
        #print(s2[0])

        if len(s2) > 1:
            req = "http://api.namsor.com/onomastics/api/json/gender/" + s2[
                0] + "/" + s2[1] + "/" + geography
        else:
            req = "http://api.namsor.com/onomastics/api/json/gender/" + s2[
                0] + "/sharma/" + geography

        ans = urllib.request.urlopen(req).read().decode("utf-8")

        # print(ans)
        d = json.loads(ans)
        gen = d['gender']
        scale = d['scale']
        return gen
    except:
        pass

    return "Error"
Example #22
0
        def mmls_popup(query,result):
            result.decoration = "naked"
            my_offset = 0
            last_offset = 0
            done = False
            try:
                del query[offset]
            except: pass

            io = self.create(None, query.get('case'), query)
            #This loops through the disk looking for a partition table.
            #If it finds a block with the appropriate magic numbers, it
            #tries to create a mmls object. If it fails, it moves to the
            #next block.
            while(1):
                io.seek(last_offset)
                while(1):
                    my_offset = io.tell()#Starting position of this next read; if it works, this will be the partition table offset
                    foobarbaz = re.sub('=','',binascii.b2a_qp(io.partial_read(512)))
                    last_offset=io.tell()#This is the offset we will resume our search at if this table doesn't work.
                    if len(foobarbaz) == 0:#If we've reached the end of the RAID, we're done.
                        done = True
                    if re.search('0UAA$',foobarbaz) != None:#Check for magic numbers of DOS partition table
                        break
                io.seek(0)
                try:
                    print "trying"+str(my_offset) 
                    parts = sk.mmls(io,my_offset)
                except IOError, e:
                    if done:#If we've reached the end without finding a partition table, we give up.
                        result.heading("No Partitions found")
                        result.text("TESTFUNC %d Sleuthkit returned: %s" % (my_offset,e))
                        return
                else:#If we created mmls object without error, we're done searching, so break
                    break
Example #23
0
    def _ConvertCollectionInformationToDict(self, collection_information):
        """Converts a collection information dictionary into a JSON dictionary.

    Args:
      collection_information: a collection information dictionary.

    Returns:
      A dictionary of the JSON serialized objects.
    """
        json_dict = {}
        for attribute_name, attribute_value in iter(
                collection_information.items()):
            if attribute_value is None:
                continue

            if attribute_name == u'configured_zone':
                attribute_value = {
                    u'__type__': u'timezone',
                    u'zone': u'{0!s}'.format(attribute_value)
                }

            elif isinstance(attribute_value, py2to3.BYTES_TYPE):
                attribute_value = {
                    u'__type__': u'bytes',
                    u'stream':
                    u'{0:s}'.format(binascii.b2a_qp(attribute_value))
                }

            json_dict[attribute_name] = attribute_value

        return json_dict
Example #24
0
    def encodeOptimally(part, exact=True):
        """Encode a message part as needed.

        If the part is more than 10% high-bit characters, it will be encoded
        using base64 encoding.  If the contents are 7-bit and exact is False,
        the part will not be encoded.  Otherwise, the message will be encoded
        as quoted-printable.

        If quoted-printable encoding is used, exact will cause all line-ending
        characters to be quoted.

        :param part: The message part to encode.
        :param exact: If True, the encoding will ensure newlines are not
            mangled.  If False, 7-bit attachments will not be encoded.
        """
        # If encoding has already been done by virtue of a charset being
        # previously specified, then do nothing.
        if 'Content-Transfer-Encoding' in part:
            return
        orig_payload = part.get_payload()
        if not exact and is_ascii_only(orig_payload):
            return
        # Payloads which are completely ascii need no encoding.
        quopri_bytes = b2a_qp(orig_payload, istext=not exact)
        # If 10% of characters need to be encoded, len is 1.2 times
        # the original len.  If more than 10% need encoding, the result
        # is unlikely to be readable.
        if len(quopri_bytes) < len(orig_payload) * 1.2:
            part.set_payload(quopri_bytes)
            part['Content-Transfer-Encoding'] = 'quoted-printable'
        else:
            encode_base64(part)
Example #25
0
  def _ConvertCollectionInformationToDict(self, collection_information):
    """Converts a collection information dictionary into a JSON dictionary.

    Args:
      collection_information: a collection information dictionary.

    Returns:
      dict[str, object]: JSON serialized objects.
    """
    json_dict = {}
    for attribute_name, attribute_value in iter(collection_information.items()):
      if attribute_value is None:
        continue

      if attribute_name == u'configured_zone':
        attribute_value = {
            u'__type__': u'timezone',
            u'zone': u'{0!s}'.format(attribute_value)
        }

      elif isinstance(attribute_value, py2to3.BYTES_TYPE):
        attribute_value = {
            u'__type__': u'bytes',
            u'stream': u'{0:s}'.format(binascii.b2a_qp(attribute_value))
        }

      json_dict[attribute_name] = attribute_value

    return json_dict
Example #26
0
 def otp_read(self):
     """Read and display otp data"""
     # OTP added in v4:
     if self.bl_rev > 3:
         for byte in range(0,32*6,4):
             x = self.__getOTP(byte)
             self.otp  = self.otp + x
             print(binascii.hexlify(x).decode('Latin-1') + ' ', end='')
         # see src/modules/systemlib/otp.h in px4 code:
         self.otp_id = self.otp[0:4]
         self.otp_idtype = self.otp[4:5]
         self.otp_vid = self.otp[8:4:-1]
         self.otp_pid = self.otp[12:8:-1]
         self.otp_coa = self.otp[32:160]
         # show user:
         try:
                 print("\ntype: " + self.otp_id.decode('Latin-1'))
                 print("idtype: " + binascii.b2a_qp(self.otp_idtype).decode('Latin-1'))
                 print("vid: " + binascii.hexlify(self.otp_vid).decode('Latin-1'))
                 print("pid: "+ binascii.hexlify(self.otp_pid).decode('Latin-1'))
                 print("coa: "+ binascii.b2a_base64(self.otp_coa).decode('Latin-1'))
                 print("sn: ", end='')
                 for byte in range(0,12,4):
                         x = self.__getSN(byte)
                         x = x[::-1]  # reverse the bytes
                         self.sn  = self.sn + x
                         print(binascii.hexlify(x).decode('Latin-1'), end='') # show user
                 print('')
                 print("chip: %08x" % self.__getCHIP())
         except Exception:
                 # ignore bad character encodings
                 pass
     else:
         print("Bootloader too old to provide OTP data")
Example #27
0
	def read( self, address, delay=0 ):
		# Prepare read request command
		dataOut = bytearray( [ address ] )

		if self.debug_mode:
			print ( "READ REQ:", dataOut )

		# Request read
		self.device_write( 0x33, dataOut )

		# Delay if necessary
		time.sleep( delay )

		# Flush read buffer
		self.usb_read()

		# Read data sent from GPIB device
		byteData = self.device_read( 0x33 )

		# Strip final linefeed
		byteData = byteData[:-1]

		# Convert to an ascii byte array
		#byteData = bytearray( byteData, 'ascii' )
		byteData = binascii.b2a_qp( byteData )

		if self.debug_mode:
			print ( "READ REPLY:", byteData )

		return byteData
Example #28
0
	def process(self,received):
		try:
			dec = []
			cur = []
			for i in received:
				if i!=['']:
					cur += [binascii.b2a_qp(i,False,False,False)]
			
			for i in cur:
				if cmp(i[0],'='):
					dec+=[ord(i)]
				else:
					convert = 0
					for j in range(1,3):			
						if (ord(i[j])>64):
							convert += (ord(i[j])-55)
						else:
							convert += (ord(i[j])-48)
						if j==1:
							convert=convert*16
							
					dec+=[convert]

			if self.escape:
				decNoEscape = []
				for index,byte in enumerate(dec):
					if byte==0x7D:
						decNoEscape += [ dec[index+1]  ^ 0x20]
						del dec[index+1]
					else:
						decNoEscape += [ byte ]

				dec = decNoEscape	
			
			incoming=[]
			for i,j in enumerate(dec):
				if j==126: #if its beginning of msg
					if i+1<len(dec):
						length = dec[i+1]*256 + dec[i+2] #parse msg length
						if i+length+3<len(dec):
							potentialMsg = dec[i+3:i+length+4]
							if checksum(potentialMsg):
								incoming+=[Message.parse(potentialMsg)]

			while incoming:
				current = incoming.pop(0)
				if current.frame==XbeeFrame['AT']:
					response = AT.parse(current.data)
					self.updateAT(response)
				elif current.frame==XbeeFrame['Transmit']:
					self.bucket.dump(current.data)
				elif current.frame==XbeeFrame['Transmit ACK']:
					pass
					#print str(self.address()) + ": transmit ACK"
				else:
					print "Unhandled Xbee Frame:"
					print current
		except:
			import traceback
			print traceback.format_exc()
Example #29
0
def set_bytes_content(msg,
                      data,
                      maintype,
                      subtype,
                      cte='base64',
                      disposition=None,
                      filename=None,
                      cid=None,
                      params=None,
                      headers=None):
    _prepare_set(msg, maintype, subtype, headers)
    if cte == 'base64':
        data = _encode_base64(data, max_line_length=msg.policy.max_line_length)
    elif cte == 'quoted-printable':
        # XXX: quoprimime.body_encode won't encode newline characters in data,
        # so we can't use it.  This means max_line_length is ignored.  Another
        # bug to fix later.  (Note: encoders.quopri is broken on line ends.)
        data = binascii.b2a_qp(data,
                               istext=False,
                               header=False,
                               quotetabs=True)
        data = data.decode('ascii')
    elif cte == '7bit':
        data = data.decode('ascii')
    elif cte in ('8bit', 'binary'):
        data = data.decode('ascii', 'surrogateescape')
    msg.set_payload(data)
    msg['Content-Transfer-Encoding'] = cte
    _finalize_set(msg, disposition, filename, cid, params)
Example #30
0
    async def write(self, chunk):
        compressor = self.compressor
        if (compressor is not None):
            if chunk:
                chunk = compressor.compress(chunk)
                if not chunk:
                    return

        transfer_encoding = self.transfer_encoding
        if transfer_encoding == 'base64':
            encoding_buffer = self.encoding_buffer
            encoding_buffer.extend(chunk)

            if encoding_buffer:
                barrier = (len(encoding_buffer) // 3) * 3
                if barrier:
                    encoding_chunk = encoding_buffer[:barrier]
                    del encoding_buffer[:barrier]
                    encoding_chunk = base64.b64encode(encoding_chunk)
                    await self.writer.write(encoding_chunk)

        elif transfer_encoding == 'quoted-printable':
            await self.writer.write(binascii.b2a_qp(chunk))

        else:
            await self.writer.write(chunk)
Example #31
0
 def handle(self):
     try:
         while True:
             packetlen = int(
                 struct.unpack('!I',
                               self.request.recv(MILTER_LEN_BYTES))[0])
             inbuf = []
             read = 0
             while read < packetlen:
                 partial_data = self.request.recv(packetlen - read)
                 inbuf.append(partial_data)
                 read += len(partial_data)
             data = b"".join(inbuf)
             logger.debug('  <<< %s', binascii.b2a_qp(data))
             try:
                 response = self.__milter_dispatcher.Dispatch(data)
                 if type(response) == list:
                     for r in response:
                         self.__send_response(r)
                 elif response:
                     self.__send_response(response)
             except ppymilterbase.PpyMilterCloseConnection as e:
                 logger.info('Closing connection ("%s")', str(e))
                 break
     except Exception:
         # use similar error production as asyncore as they already make
         # good 1 line errors - similar to handle_error in asyncore.py
         # proper cleanup happens regardless even if we catch this exception
         (nil, t, v, tbinfo) = asyncore.compact_traceback()
         logger.error('uncaptured python exception, closing channel %s '
                      '(%s:%s %s)' % (repr(self), t, v, tbinfo))
 def test_b2a_qp_a2b_qp_round_trip(self, payload, quotetabs, istext,
                                   header):
     x = binascii.b2a_qp(payload,
                         quotetabs=quotetabs,
                         istext=istext,
                         header=header)
     self.assertEqual(payload, binascii.a2b_qp(x, header=header))
Example #33
0
	def getSourceInput(self, label):
		ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
		ser.flushInput()
		ser.write('\x05\x80' + self.outputNumber + '\x81')
		ser.read(2)
		out = ser.read()
		ser.close()
		foo = binascii.b2a_qp(out)
		source = foo[2]
		if source == '1':
			self.button8 = xbmcgui.ControlButton(63, 350, 150, 100, "", focusTexture='/home/vader/.xbmc/scripts/sourceMenu/6Icon/widiButton1.jpg', noFocusTexture='/home/vader/.xbmc/scripts/sourceMenu/6Icon/widiButton1.jpg')
			self.addControl(self.button8)    
			label.setLabel('WiDi 1')
		elif source == '2':
			self.button8 = xbmcgui.ControlButton(63, 350, 150, 100, "", focusTexture='/home/vader/.xbmc/scripts/sourceMenu/6Icon/appleTV1.jpg', noFocusTexture='/home/vader/.xbmc/scripts/sourceMenu/6Icon/appleTV1.jpg')
			self.addControl(self.button8)   
			label.setLabel('Apple TV 1')
		elif source == '3':
			self.button8 = xbmcgui.ControlButton(63, 350, 150, 100, "", focusTexture='/home/vader/.xbmc/scripts/sourceMenu/6Icon/widiButton2.jpg', noFocusTexture='/home/vader/.xbmc/scripts/sourceMenu/6Icon/widiButton2.jpg')
			self.addControl(self.button8)    
			label.setLabel('WiDi 2')
		elif source == '4':
			self.button8 = xbmcgui.ControlButton(63, 350, 150, 100, "", focusTexture='/home/vader/.xbmc/scripts/sourceMenu/6Icon/appleTV2.jpg', noFocusTexture='/home/vader/.xbmc/scripts/sourceMenu/6Icon/appleTV2.jpg')
			self.addControl(self.button8)
			label.setLabel('Apple TV 2')
		elif source == '5':
			self.button8 = xbmcgui.ControlButton(63, 350, 150, 100, "", focusTexture='/home/vader/.xbmc/scripts/sourceMenu/6Icon/vaderButton2.jpg', noFocusTexture='/home/vader/.xbmc/scripts/sourceMenu/Icon/vaderButton2.jpg')
			self.addControl(self.button8)
			label.setLabel('VADER')
		else:
			label.setLabel('UNKNOWN')
Example #34
0
    def __call__(self, context, query=None):
        site = api.portal.get()
        self.catalog = getToolByName(site, 'portal_catalog', None)
        if self.catalog is None:
            return SimpleVocabulary([])
        if self.indexName not in self.catalog._catalog.indexes.keys():
            return SimpleVocabulary([])
        index = self.catalog._catalog.getIndex(self.indexName)

        def safe_encode(term):
            if isinstance(term, unicode):
                # no need to use portal encoding for transitional encoding from
                # unicode to ascii. utf-8 should be fine.
                term = term.encode('utf-8')
            return term

        # Vocabulary term tokens *must* be 7 bit values, titles *must* be
        # unicode
        items = [
            SimpleTerm(safe_unicode(i), b2a_qp(
                safe_encode(i)), safe_unicode(i))
            for i in index._index
            if query is None or safe_encode(query) in safe_encode(i)
        ]
        return SimpleVocabulary(items)
Example #35
0
    def encodeOptimally(part, exact=True):
        """Encode a message part as needed.

        If the part is more than 10% high-bit characters, it will be encoded
        using base64 encoding.  If the contents are 7-bit and exact is False,
        the part will not be encoded.  Otherwise, the message will be encoded
        as quoted-printable.

        If quoted-printable encoding is used, exact will cause all line-ending
        characters to be quoted.

        :param part: The message part to encode.
        :param exact: If True, the encoding will ensure newlines are not
            mangled.  If False, 7-bit attachments will not be encoded.
        """
        # If encoding has already been done by virtue of a charset being
        # previously specified, then do nothing.
        if 'Content-Transfer-Encoding' in part:
            return
        orig_payload = part.get_payload()
        if not exact and is_ascii_only(orig_payload):
            return
        # Payloads which are completely ascii need no encoding.
        quopri_bytes = b2a_qp(orig_payload, istext=not exact)
        # If 10% of characters need to be encoded, len is 1.2 times
        # the original len.  If more than 10% need encoding, the result
        # is unlikely to be readable.
        if len(quopri_bytes) < len(orig_payload) * 1.2:
            part.set_payload(quopri_bytes)
            part['Content-Transfer-Encoding'] = 'quoted-printable'
        else:
            encode_base64(part)
Example #36
0
 def handle(self):
   try:
     while True:
       packetlen = int(struct.unpack('!I',
                                     self.request.recv(MILTER_LEN_BYTES))[0])
       inbuf = []
       read = 0
       while read < packetlen:
         partial_data = self.request.recv(packetlen - read)
         inbuf.append(partial_data)
         read += len(partial_data)
       data = "".join(inbuf)
       logger.debug('  <<< %s', binascii.b2a_qp(data))
       try:
         response = self.__milter_dispatcher.Dispatch(data)
         if type(response) == list:
           for r in response:
             self.__send_response(r)
         elif response:
           self.__send_response(response)
       except ppymilterbase.PpyMilterCloseConnection, e:
         logger.info('Closing connection ("%s")', str(e))
         break
   except Exception:
     # use similar error production as asyncore as they already make
     # good 1 line errors - similar to handle_error in asyncore.py
     # proper cleanup happens regardless even if we catch this exception
     (nil, t, v, tbinfo) = asyncore.compact_traceback()
     logger.error('uncaptured python exception, closing channel %s '
                   '(%s:%s %s)' % (repr(self), t, v, tbinfo))
Example #37
0
File: oxm.py Project: hkwi/ofpstr
def ssid_bin2str(payload, has_mask):
	if payload is None:
		return "dot11_ssid"
	
	if has_mask:
		split = len(payload)//2
		name = bytearray(payload[:split])
		while name[-1] == 0:
			name = name[:-1]
		name = bytes(name)
		
		return "dot11_ssid={:s}/{:s}".format(
			binascii.b2a_qp(name).decode("UTF-8"),
			binascii.b2a_hex(payload[split:]).decode("UTF-8"))
	else:
		return "dot11_ssid={:s}".format(binascii.b2a_qp(payload).decode("UTF-8"))
Example #38
0
        def upload(self, fw):
                # Make sure we are doing the right thing
                if self.board_type != fw.property('board_id'):
                        raise RuntimeError("Firmware not suitable for this board")
                if self.fw_maxsize < fw.property('image_size'):
                        raise RuntimeError("Firmware image is too large for this board")

                #print("OTP(first 5 blocks)")
                for byte in range(0,32*5,4):
                    x = self.__getOTP(byte)
                    self.otp  = self.otp + x
                #    print(" " + binascii.hexlify(x)),
                #print
                #according to src/modules/systemlib/otp.h in px4 code:  
                # first block is: 
                #char		id[4];		///4 bytes < 'P' 'X' '4' '\n'
		        #uint8_t	id_type;    ///1 byte < 0 for USB VID, 1 for generic VID
		        #uint32_t	vid;        ///4 bytes
		        #uint32_t	pid;        ///4 bytes
		        #char        unused[19];  ///19 bytes 
		        # next 4 blocks are: "Certificate Of Authenticity" aka "signature".
            	#char        signature[128];
                self.otp_id = self.otp[0:4]
                self.otp_idtype = self.otp[4:5]
                self.otp_vid = self.otp[8:4:-1]
                self.otp_pid = self.otp[12:8:-1]
                self.otp_coa = self.otp[32:160]
                # show user:
                print("type:" + self.otp_id)
                print("idtype:" +binascii.b2a_qp(self.otp_idtype))
                print("vid:" + binascii.hexlify(self.otp_vid))
                print("pid:"+ binascii.hexlify(self.otp_pid))
                #print("coa as hex:"+ binascii.hexlify(self.otp_coa))
                print("coa:"+ binascii.b2a_base64(self.otp_coa)),

                print("sn:"),
                for byte in range(0,12,4):
                    x = self.__getSN(byte)
                    x = x[::-1]  # reverse the bytes
                    self.sn  = self.sn + x
                    print(binascii.hexlify(x)), # show user
                print
                
 
                print("erase...")
                self.__erase()

                print("program...")
                self.__program(fw)

                print("verify...")
                if self.bl_rev == 2:
                        self.__verify_v2(fw)
                else:
                        self.__verify_v3(fw)

                print("done, rebooting.")
                self.__reboot()
                self.port.close()
Example #39
0
    def upload(self, fw):
        # Make sure we are doing the right thing
        if self.board_type != fw.property('board_id'):
            raise RuntimeError("Firmware not suitable for this board")
        if self.fw_maxsize < fw.property('image_size'):
            raise RuntimeError("Firmware image is too large for this board")

        # OTP added in v4:
        if self.bl_rev > 3:
            for byte in range(0, 32 * 6, 4):
                x = self.__getOTP(byte)
                self.otp = self.otp + x
                print(binascii.hexlify(x).decode('Latin-1') + ' ', end='')
            # see src/modules/systemlib/otp.h in px4 code:
            self.otp_id = self.otp[0:4]
            self.otp_idtype = self.otp[4:5]
            self.otp_vid = self.otp[8:4:-1]
            self.otp_pid = self.otp[12:8:-1]
            self.otp_coa = self.otp[32:160]
            print("")
            # show user:
            if (simulinkBuild != "y"):
                try:
                    print("type: " + self.otp_id.decode('Latin-1'))
                    print("idtype: " +
                          binascii.b2a_qp(self.otp_idtype).decode('Latin-1'))
                    print("vid: " +
                          binascii.hexlify(self.otp_vid).decode('Latin-1'))
                    print("pid: " +
                          binascii.hexlify(self.otp_pid).decode('Latin-1'))
                    print("coa: " +
                          binascii.b2a_base64(self.otp_coa).decode('Latin-1'))
                    print("sn: ", end='')
                    for byte in range(0, 12, 4):
                        x = self.__getSN(byte)
                        x = x[::-1]  # reverse the bytes
                        self.sn = self.sn + x
                        print(binascii.hexlify(x).decode('Latin-1'),
                              end='')  # show user
                    print('')
                except Exception:
                    # ignore bad character encodings
                    pass

        print("erase...")
        self.__erase()

        print("program...")
        self.__program(fw)

        print("verify...")
        if self.bl_rev == 2:
            self.__verify_v2(fw)
        else:
            self.__verify_v3(fw)

        print("done, rebooting.")
        self.__reboot()
        self.port.close()
Example #40
0
def encodestring(s, quotetabs=False, header=False):
    if b2a_qp is not None:
        return b2a_qp(s, quotetabs=quotetabs, header=header)
    from io import BytesIO
    infp = BytesIO(s)
    outfp = BytesIO()
    encode(infp, outfp, quotetabs, header)
    return outfp.getvalue()
Example #41
0
def encodestring(s, quotetabs=False, header=False):
    if b2a_qp is not None:
        return b2a_qp(s, quotetabs=quotetabs, header=header)
    from io import BytesIO
    infp = BytesIO(s)
    outfp = BytesIO()
    encode(infp, outfp, quotetabs, header)
    return outfp.getvalue()
Example #42
0
def encodestring(s, quotetabs=0, header=0):
    if b2a_qp is not None:
        return b2a_qp(s, quotetabs=quotetabs, header=header)
    from cStringIO import StringIO
    infp = StringIO(s)
    outfp = StringIO()
    encode(infp, outfp, quotetabs, header)
    return outfp.getvalue()
Example #43
0
 def test_qp(self):
     # A test for SF bug 534347 (segfaults without the proper fix)
     try:
         binascii.a2b_qp("", **{1: 1})
     except TypeError:
         pass
     else:
         self.fail("binascii.a2b_qp(**{1:1}) didn't raise TypeError")
     self.assertEqual(binascii.a2b_qp("= "), "")
     self.assertEqual(binascii.a2b_qp("=="), "=")
     self.assertEqual(binascii.a2b_qp("=AX"), "=AX")
     self.assertRaises(TypeError, binascii.b2a_qp, foo="bar")
     self.assertEqual(binascii.a2b_qp("=00\r\n=00"), "\x00\r\n\x00")
     self.assertEqual(binascii.b2a_qp("\xff\r\n\xff\n\xff"),
                      "=FF\r\n=FF\r\n=FF")
     self.assertEqual(binascii.b2a_qp("0" * 75 + "\xff\r\n\xff\r\n\xff"),
                      "0" * 75 + "=\r\n=FF\r\n=FF\r\n=FF")
Example #44
0
def fm_escape(s):
    """
    Escape binary FM data to string
    
    >>> fm_escape("ab\xffcd")
    'ab=FFcd'
    """
    return binascii.b2a_qp(str(s)).replace("=\n", "")
Example #45
0
def encodestring(s, quotetabs = 0, header = 0):
    if b2a_qp is not None:
        return b2a_qp(s, quotetabs = quotetabs, header = header)
    from cStringIO import StringIO
    infp = StringIO(s)
    outfp = StringIO()
    encode(infp, outfp, quotetabs, header)
    return outfp.getvalue()
Example #46
0
    def upload(self, fw):
        # Make sure we are doing the right thing
        if self.board_type != fw.property('board_id'):
            raise RuntimeError("Firmware not suitable for this board")
        if self.fw_maxsize < fw.property('image_size'):
            raise RuntimeError("Firmware image is too large for this board")

        #print("OTP(first 5 blocks)")
        for byte in range(0, 32 * 5, 4):
            x = self.__getOTP(byte)
            self.otp = self.otp + x
        #    print(" " + binascii.hexlify(x)),
        #print
        #according to src/modules/systemlib/otp.h in px4 code:
        # first block is:
        #char		id[4];		///4 bytes < 'P' 'X' '4' '\n'
#uint8_t	id_type;    ///1 byte < 0 for USB VID, 1 for generic VID
#uint32_t	vid;        ///4 bytes
#uint32_t	pid;        ///4 bytes
#char        unused[19];  ///19 bytes
# next 4 blocks are: "Certificate Of Authenticity" aka "signature".
#char        signature[128];
        self.otp_id = self.otp[0:4]
        self.otp_idtype = self.otp[4:5]
        self.otp_vid = self.otp[8:4:-1]
        self.otp_pid = self.otp[12:8:-1]
        self.otp_coa = self.otp[32:160]
        # show user:
        print("type:" + self.otp_id)
        print("idtype:" + binascii.b2a_qp(self.otp_idtype))
        print("vid:" + binascii.hexlify(self.otp_vid))
        print("pid:" + binascii.hexlify(self.otp_pid))
        #print("coa as hex:"+ binascii.hexlify(self.otp_coa))
        print("coa:" + binascii.b2a_base64(self.otp_coa)),

        print("sn:"),
        for byte in range(0, 12, 4):
            x = self.__getSN(byte)
            x = x[::-1]  # reverse the bytes
            self.sn = self.sn + x
            print(binascii.hexlify(x)),  # show user
        print

        print("erase...")
        self.__erase()

        print("program...")
        self.__program(fw)

        print("verify...")
        if self.bl_rev == 2:
            self.__verify_v2(fw)
        else:
            self.__verify_v3(fw)

        print("done, rebooting.")
        self.__reboot()
        self.port.close()
Example #47
0
    def __repr__(self):

        parts = []
        keys = list(self.items.keys())
        keys.sort()
        if self.commentblock != "":
            out = [self.commentblock]
        else:
            out = [""]
        keylast = []

        for key in keys:
            keynew = key.split(".")

            hrditem = self.items[key]
            if hrditem.temp:
                continue

            if False:  #  hrditem.comments!="":
                out.append("")
                out.append("%s" % (hrditem.comments.strip()))
            else:
                if keylast != [] and keynew[0] != keylast[0]:
                    if out[-1] != "":
                        out.append("")

            if not isinstance(hrditem.data, str):
                #@todo fix this bug
                # raise j.exceptions.RuntimeError("BUG SHOULD ALWAYS BE STR")
                pass

            if isinstance(hrditem.data,
                          str) and hrditem.data.find("@ASK") != -1:
                val = hrditem.value
                out.append("%-30s = %s" % (key, val))
            elif hrditem.ttype == "string":
                val = hrditem.getAsString()
                if val.find("\n") != -1:
                    out.append("%-30s = '%s'" % (key, val.strip("'")))
                else:
                    out.append("%-30s = %s" % (key, val))

            elif hrditem.ttype == "list" or hrditem.ttype == "dict":
                val = hrditem.getAsString()
                out.append("%-30s =\n%s" % (key, val))
                out.append("")

            elif hrditem.ttype != "binary":
                val = hrditem.getAsString()
                out.append("%-30s = %s" % (key, val))
            else:
                out.append("%-30s = bqp\n%s\n#BINARYEND#########\n\n" %
                           (key, binascii.b2a_qp(hrditem.data)))
            # print("%s'''%s'''"%(hrditem.ttype,val))
            keylast = key.split(".")
        out = out[1:]
        out = "\n".join(out).replace("\n\n\n", "\n\n")
        return out
def decode(data):
    """
    Return the base base64 decoded dataa else return '-'
    """
    try:
        decode = base64.b64decode(data)
        return binascii.b2a_qp(decode)
    except:
        return "-"
Example #49
0
    def __send_response(self, response):
      """Send data down the milter socket.

      Args:
        response: the data to send
      """
      logger.debug('  >>> %s', binascii.b2a_qp(response[0]))
      self.request.send(struct.pack('!I', len(response)))
      self.request.send(response)
def  base64code(s,d):
    global b64str
    global f
    if(d==len(b64str)):
        f.write(binascii.b2a_qp(base64.b64decode(s))+'\n')
    else:
        base64code(s+b64str[d],d+1)
        if b64str[d].isalpha():  
            base64code(s+b64str[d].lower(),d+1)
Example #51
0
def base64code(s, d):
    global b64str
    global f
    if (d == len(b64str)):
        f.write(binascii.b2a_qp(base64.b64decode(s)) + '\n')
    else:
        base64code(s + b64str[d], d + 1)
        if b64str[d].isalpha():
            base64code(s + b64str[d].lower(), d + 1)
Example #52
0
    def __repr__(self):

        parts=[]
        keys=list(self.items.keys())
        keys.sort()
        if self.commentblock!="":
            out=[self.commentblock]
        else:
            out=[""]
        keylast=[]

        for key in keys:
            keynew=key.split(".")

            hrditem=self.items[key]
            if hrditem.temp:
                continue

            if False:#  hrditem.comments!="":
                out.append("")
                out.append("%s" % (hrditem.comments.strip()))
            else:
                if keylast!=[] and keynew[0]!=keylast[0]:
                    if out[-1]!="":
                        out.append("")

            if not isinstance( hrditem.data,str):
                #@todo fix this bug
                # raise j.exceptions.RuntimeError("BUG SHOULD ALWAYS BE STR")
                pass


            if isinstance( hrditem.data,str) and hrditem.data.find("@ASK")!=-1:
                val=hrditem.value
                out.append("%-30s = %s" % (key, val))
            elif hrditem.ttype =="string":
                val=hrditem.getAsString()
                if val.find("\n")!=-1:
                    out.append("%-30s = '%s'" % (key, val.strip("'")))
                else:
                    out.append("%-30s = %s" % (key, val))

            elif hrditem.ttype =="list" or hrditem.ttype =="dict":
                val=hrditem.getAsString()
                out.append("%-30s =\n%s" % (key, val))
                out.append("")

            elif hrditem.ttype !="binary":
                val=hrditem.getAsString()
                out.append("%-30s = %s" % (key, val))
            else:
                out.append("%-30s = bqp\n%s\n#BINARYEND#########\n\n"%(key,binascii.b2a_qp(hrditem.data)))
            # print("%s'''%s'''"%(hrditem.ttype,val))
            keylast=key.split(".")
        out=out[1:]
        out="\n".join(out).replace("\n\n\n","\n\n")
        return out
Example #53
0
        def __send_response(self, response):
            """Send data down the milter socket.

      Args:
        response: the data to send
      """
            logger.debug('  >>> %s', binascii.b2a_qp(response[0]))
            self.request.send(struct.pack('!I', len(response)))
            self.request.send(response)
 def __call__(self, context):
     self.context = context
     self.catalog = getToolByName(context, "portal_catalog")
     if self.catalog is None:
         return SimpleVocabulary([])
     index = self.catalog._catalog.getIndex('Subject')
     # Vocabulary term tokens *must* be 7 bit values, titles *must* be unicode
     items = [SimpleTerm(i, b2a_qp(i), safe_unicode(i)) for i in index._index]
     return SimpleVocabulary(items)
Example #55
0
        def upload(self, fw):
                # Make sure we are doing the right thing
                if self.board_type != fw.property('board_id'):
                        msg = "Firmware not suitable for this board (board_type=%u board_id=%u)" % (
                                self.board_type, fw.property('board_id'))
                        if args.force:
                                print("WARNING: %s" % msg)
                        else:
                                raise RuntimeError(msg)
                if self.fw_maxsize < fw.property('image_size'):
                        raise RuntimeError("Firmware image is too large for this board")

                # OTP added in v4:
                if self.bl_rev > 3:
                    for byte in range(0,32*6,4):
                        x = self.__getOTP(byte)
                        self.otp  = self.otp + x
                        print(binascii.hexlify(x).decode('Latin-1') + ' ', end='')
                    # see src/modules/systemlib/otp.h in px4 code:
                    self.otp_id = self.otp[0:4]
                    self.otp_idtype = self.otp[4:5]
                    self.otp_vid = self.otp[8:4:-1]
                    self.otp_pid = self.otp[12:8:-1]
                    self.otp_coa = self.otp[32:160]
                    # show user:
                    try:
                            print("type: " + self.otp_id.decode('Latin-1'))
                            print("idtype: " + binascii.b2a_qp(self.otp_idtype).decode('Latin-1'))
                            print("vid: " + binascii.hexlify(self.otp_vid).decode('Latin-1'))
                            print("pid: "+ binascii.hexlify(self.otp_pid).decode('Latin-1'))
                            print("coa: "+ binascii.b2a_base64(self.otp_coa).decode('Latin-1'))
                            print("sn: ", end='')
                            for byte in range(0,12,4):
                                    x = self.__getSN(byte)
                                    x = x[::-1]  # reverse the bytes
                                    self.sn  = self.sn + x
                                    print(binascii.hexlify(x).decode('Latin-1'), end='') # show user
                            print('')
                            print("chip: %08x" % self.__getCHIP())
                    except Exception:
                            # ignore bad character encodings
                            pass
                
                self.__erase("Erase  ")
                self.__program("Program", fw)

                if self.bl_rev == 2:
                        self.__verify_v2("Verify ", fw)
                else:
                        self.__verify_v3("Verify ", fw)

                if args.boot_delay is not None:
                        self.__set_boot_delay(args.boot_delay)

                print("\nRebooting.\n")
                self.__reboot()
                self.port.close()
Example #56
0
    def __send_response(self, response):
      """Send data down the milter socket.

      Args:
        response: The data to send.
      """
      logging.debug('  >>> %s', binascii.b2a_qp(response[0]))
      self.push(struct.pack('!I', len(response)))
      self.push(response)
Example #57
0
    def default(self, preprocessing_object):
        """Converts a preprocessing object into a JSON dictionary.

    The resulting dictionary of the JSON serialized objects consists of:
    {
        '__type__': 'PreprocessObject'
        'collection_information': { ... }
        'counter': { ... }
        'plugin_counter': { ... }
        'store_range': { ... }
        'stores': { ... }
        'zone': { ... }
        ...
    }

    Here '__type__' indicates the object base type. In this case
    'PreprocessObject'. The rest of the elements of the dictionary
    make up the preprocessing object attributes.

    Args:
      preprocessing_object: a preprocessing object (instance of
                            PreprocessObject).

    Returns:
      A dictionary of the JSON serialized objects.

    Raises:
      TypeError: if not an instance of PreprocessObject.
    """
        if not isinstance(preprocessing_object, event.PreprocessObject):
            raise TypeError

        json_dict = {u"__type__": u"PreprocessObject"}
        for attribute_name in iter(preprocessing_object.__dict__.keys()):
            attribute_value = getattr(preprocessing_object, attribute_name, None)
            if attribute_value is None:
                continue

            if attribute_name == u"collection_information":
                attribute_value = self._ConvertCollectionInformationToDict(attribute_value)

            elif attribute_name in [u"counter", u"plugin_counter"]:
                attribute_value = self._ConvertCollectionsCounterToDict(attribute_value)

            elif attribute_name == u"store_range":
                attribute_value = {u"__type__": u"range", u"end": attribute_value[1], u"start": attribute_value[0]}

            elif attribute_name == u"zone":
                attribute_value = {u"__type__": u"timezone", u"zone": u"{0!s}".format(attribute_value)}

            elif isinstance(attribute_value, py2to3.BYTES_TYPE):
                attribute_value = {u"__type__": u"bytes", u"stream": u"{0:s}".format(binascii.b2a_qp(attribute_value))}

            json_dict[attribute_name] = attribute_value

        return json_dict
 def vocabulary_factory(context):
     values = get_vocabulary(field.__name__)
     terms = []
     for value in values:
         terms.append(SimpleTerm(
             value=value['id'],
             token=b2a_qp(value['id'].encode('utf-8')),
             title=value['title'],
             ))
     return SimpleVocabulary(terms)