Esempio n. 1
0
def test_sanity():
    bites = b''.join([b.to_bytes(1, 'little') for b in range(256)])
    size, crc, outdata = encode(bites)
    size2, crc2, outdata2 = decode(outdata)
    assert (bites == outdata2)
    assert (crc == crc2)
    assert (size == size2)
Esempio n. 2
0
    def _testEncodeFile(self, filename):
        data, crc = self._readFile(filename)
        with open(filename, 'rb') as file_in, \
        open(filename + ".out", 'wb') as file_out:
            bytes_out, crc_out = yenc.encode(file_in, file_out, len(data))

        self.assertEqual(crc, crc_out)
Esempio n. 3
0
	def encode(self, inFilename, outFilename):
		try:
			fileIn = open(inFilename, 'r')
			fileOut = open(outFilename, 'w')

			crc = hex(binascii.crc32(open(inFilename,"r").read()))[2:]
			name = os.path.split(inFilename)[1]
			size = os.stat(inFilename)[stat.ST_SIZE]
			fileOut.write("=ybegin line=128 size=%d crc32=%s name=%s\r\n" % (size, crc, name) )
			encoded, crc = yenc.encode(fileIn, fileOut, size)
			fileOut.write("=yend size=%d crc32=%s\r\n" % (encoded, crc))
		finally:
			fileIn.close()
			fileOut.close()
Esempio n. 4
0
    def testCrcIn(self):
        """Exercise yenc.decode(crc_in=...) parameter"""
        with open(self.FILE_E, 'rb') as plain, \
        open(self.FILE_E + ".out", 'w+b') as encoded, \
        open(os.devnull, 'wb') as null:
            _, crc = yenc.encode(plain, encoded)

            # Correct CRC
            encoded.seek(0)
            yenc.decode(encoded, null, crc_in=crc)

            # Incorrect CRC
            crc = format(int(crc, 16) ^ 0xffffffff, "08x")
            encoded.seek(0)
            with self.assertRaises(yenc.Error):
                yenc.decode(encoded, null, crc_in=crc)
Esempio n. 5
0
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "o:")
    except getopt.GetoptError:
        usage()
    file_out = None
    for o, a in opts:
        if o == '-o':
            file_out = open(a, "wb")
    if not file_out:
        try:  # Python 3
            file_out = sys.stdout.detach()
        except AttributeError:  # Python < 3
            file_out = sys.stdout
        sys.stdout = None
    with file_out:
        if args:
            filename = args[0]
            if os.access(filename, os.F_OK | os.R_OK):
                file_in = open(filename, "rb")
            else:
                sys.stderr.write("couldn't access %s\n" % filename)
                sys.exit(2)
        else:
            usage()
        with file_in:
            with open(filename, "rb") as file:
                crc = "%08x" % (0xFFFFFFFF & crc32(file.read()))
            name = os.path.split(filename)[1]
            size = os.stat(filename)[ST_SIZE]
            line = "=ybegin line=128 size=%d crc32=%s name=%s\r\n"
            file_out.write((line % (size, crc, name)).encode("ascii"))
            try:
                encoded, crc_out = yenc.encode(file_in, file_out, size)
            except Exception as e:
                sys.stderr.write("{}\n".format(e))
                sys.exit(3)
        line = "=yend size=%d crc32=%s\r\n" % (encoded, crc_out)
        file_out.write(line.encode("ascii"))
Esempio n. 6
0
def test_encode():
    bites = [b.to_bytes(1, 'little') for b in range(256)]
    soln = [
        b'*', b'+', b',', b'-', b'=n', b'/', b'0', b'1', b'2', b'3', b'4',
        b'5', b'6', b'7', b'8', b'9', b':', b';', b'<', b'=}', b'>', b'?',
        b'@', b'A', b'B', b'C', b'D', b'E', b'F', b'G', b'H', b'I', b'J', b'K',
        b'L', b'M', b'N', b'O', b'P', b'Q', b'R', b'S', b'T', b'U', b'V', b'W',
        b'X', b'Y', b'Z', b'[', b'\\', b']', b'^', b'_', b'`', b'a', b'b',
        b'c', b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', b'n',
        b'o', b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', b'x', b'y', b'z',
        b'{', b'|', b'}', b'~', b'\x7f', b'\x80', b'\x81', b'\x82', b'\x83',
        b'\x84', b'\x85', b'\x86', b'\x87', b'\x88', b'\x89', b'\x8a', b'\x8b',
        b'\x8c', b'\x8d', b'\x8e', b'\x8f', b'\x90', b'\x91', b'\x92', b'\x93',
        b'\x94', b'\x95', b'\x96', b'\x97', b'\x98', b'\x99', b'\x9a', b'\x9b',
        b'\x9c', b'\x9d', b'\x9e', b'\x9f', b'\xa0', b'\xa1', b'\xa2', b'\xa3',
        b'\xa4', b'\xa5', b'\xa6', b'\xa7', b'\xa8', b'\xa9', b'\xaa', b'\xab',
        b'\xac', b'\xad', b'\xae', b'\xaf', b'\xb0', b'\xb1', b'\xb2', b'\xb3',
        b'\xb4', b'\xb5', b'\xb6', b'\xb7', b'\xb8', b'\xb9', b'\xba', b'\xbb',
        b'\xbc', b'\xbd', b'\xbe', b'\xbf', b'\xc0', b'\xc1', b'\xc2', b'\xc3',
        b'\xc4', b'\xc5', b'\xc6', b'\xc7', b'\xc8', b'\xc9', b'\xca', b'\xcb',
        b'\xcc', b'\xcd', b'\xce', b'\xcf', b'\xd0', b'\xd1', b'\xd2', b'\xd3',
        b'\xd4', b'\xd5', b'\xd6', b'\xd7', b'\xd8', b'\xd9', b'\xda', b'\xdb',
        b'\xdc', b'\xdd', b'\xde', b'\xdf', b'\xe0', b'\xe1', b'\xe2', b'\xe3',
        b'\xe4', b'\xe5', b'\xe6', b'\xe7', b'\xe8', b'\xe9', b'\xea', b'\xeb',
        b'\xec', b'\xed', b'\xee', b'\xef', b'\xf0', b'\xf1', b'\xf2', b'\xf3',
        b'\xf4', b'\xf5', b'\xf6', b'\xf7', b'\xf8', b'\xf9', b'\xfa', b'\xfb',
        b'\xfc', b'\xfd', b'\xfe', b'\xff', b'=@', b'\x01', b'\x02', b'\x03',
        b'\x04', b'\x05', b'\x06', b'\x07', b'\x08', b'=I', b'=J', b'\x0b',
        b'\x0c', b'=M', b'\x0e', b'\x0f', b'\x10', b'\x11', b'\x12', b'\x13',
        b'\x14', b'\x15', b'\x16', b'\x17', b'\x18', b'\x19', b'\x1a', b'\x1b',
        b'\x1c', b'\x1d', b'\x1e', b'\x1f', b'=`', b'!', b'"', b'#', b'$',
        b'%', b'&', b"'", b'(', b')'
    ]
    for i, b in enumerate(bites):
        size, crc32, data = encode(b)
        assert (data == soln[i])