def extract_symbols_from_db(binarr): datapart = binarr[0x4A0:] # 494 bytes - 5bytes = 489 a = Struct("Symbol" / Bytes(5), "Rest" / Bytes(1172 - 5)) greedy_parser = GreedyRange(a) data_lines = greedy_parser.parse(datapart) return [data["Symbol"].decode("ascii").rstrip("\x00") for data in data_lines]
class TestGreedyRange(unittest.TestCase): def setUp(self): self.c = GreedyRange(UBInt8("foo")) def test_trivial(self): pass def test_empty_parse(self): self.assertRaises(RangeError, self.c.parse, b"") def test_parse(self): self.assertEqual(self.c.parse(b"\x01"), [1]) self.assertEqual(self.c.parse(b"\x01\x02\x03"), [1, 2, 3]) self.assertEqual(self.c.parse(b"\x01\x02\x03\x04\x05\x06"), [1, 2, 3, 4, 5, 6]) def test_empty_build(self): self.assertRaises(RangeError, self.c.build, []) def test_build(self): self.assertEqual(self.c.build([1, 2]), b"\x01\x02")
class TestGreedyRange(unittest.TestCase): def setUp(self): self.c = GreedyRange(UBInt8("foo")) def test_trivial(self): pass def test_empty_parse(self): self.assertRaises(RangeError, self.c.parse, six.b("")) def test_parse(self): self.assertEqual(self.c.parse(six.b("\x01")), [1]) self.assertEqual(self.c.parse(six.b("\x01\x02\x03")), [1, 2, 3]) self.assertEqual(self.c.parse(six.b("\x01\x02\x03\x04\x05\x06")), [1, 2, 3, 4, 5, 6]) def test_empty_build(self): self.assertRaises(RangeError, self.c.build, []) def test_build(self): self.assertEqual(self.c.build([1, 2]), six.b("\x01\x02"))
def get_files(data): files = {} fstruct = GreedyRange( Struct( "dsize" / Int32ul, "nsize" / Int32ul, "name" / Bytes(256), "fdata" / Bytes(this.dsize), ) ) fdata = fstruct.parse(data) for file in fdata: if file["nsize"] > 1 and ord(file["name"][1]) == 0: filename = CString("utf16").parse(file["name"]) else: filename = CString("ascii").parse(file["name"]) filename = filename.encode("ascii", "ignore") files[filename] = file["fdata"] return files
def read_symbol_file_data_part(binfile): datapart = binfile[0x4A0:] data = swapbitsinbytes(datapart) a = create_entry_chunk() chunksize = 40 parsed = a.parse(data[:chunksize]) assert parsed["DatePacked"][YEAR] == 2017 start = 0 end = chunksize values = { DAY: [], MONTH: [], YEAR: [], CLOSE: [], OPEN: [], HIGH: [], LOW: [], VOLUME: [], } packed_map = { DAY: lambda x: x[DATEPACKED][DAY], MONTH: lambda x: x[DATEPACKED][MONTH], YEAR: lambda x: x[DATEPACKED][YEAR], } greedy_parser = GreedyRange(a) data_lines = greedy_parser.parse(data) values = [ ( packed_map[DAY](el), packed_map[MONTH](el), packed_map[YEAR](el), el[OPEN], el[HIGH], el[LOW], el[CLOSE], el[VOLUME], ) for el in data_lines ] return chunksize, data, values