Example #1
0
    def test_textclipping_path_auto_data_fork(self) -> None:
        with tempfile.NamedTemporaryFile() as temp_data_fork:
            with TEXTCLIPPING_RSRC_FILE.open("rb") as source_file:
                shutil.copyfileobj(source_file, temp_data_fork)
                # Have to flush the temporary file manually so that the data is visible to the other reads below.
                # Normally this happens automatically as part of the close method, but that would also delete the temporary file, which we don't want.
                temp_data_fork.flush()

            with self.subTest(rsrc_fork="nonexistant"):
                # Data fork is selected when resource fork does not exist.

                with rsrcfork.open(temp_data_fork.name) as rf:
                    self.internal_test_textclipping(rf)

            with self.subTest(rsrc_fork="empty"):
                # Data fork is selected when resource fork exists, but is empty.

                with open_resource_fork(pathlib.Path(temp_data_fork.name),
                                        "wb") as temp_rsrc_fork:
                    temp_rsrc_fork.write(b"")

                with rsrcfork.open(temp_data_fork.name) as rf:
                    self.internal_test_textclipping(rf)

            with self.subTest(rsrc_fork="non-resource data"):
                # Data fork is selected when resource fork contains non-resource data.

                with open_resource_fork(pathlib.Path(temp_data_fork.name),
                                        "wb") as temp_rsrc_fork:
                    temp_rsrc_fork.write(
                        b"This is the file's resource fork. It contains junk, so it should be ignored in favor of the data fork."
                    )

                with rsrcfork.open(temp_data_fork.name) as rf:
                    self.internal_test_textclipping(rf)
Example #2
0
    def test_textclipping_path_auto_resource_fork(self) -> None:
        with tempfile.NamedTemporaryFile() as temp_data_fork:
            with TEXTCLIPPING_RSRC_FILE.open("rb") as source_file:
                with open_resource_fork(pathlib.Path(temp_data_fork.name),
                                        "wb") as temp_rsrc_fork:
                    shutil.copyfileobj(source_file, temp_rsrc_fork)

            with self.subTest(data_fork="empty"):
                # Resource fork is selected when data fork is empty.

                with rsrcfork.open(temp_data_fork.name) as rf:
                    self.internal_test_textclipping(rf)

            with self.subTest(data_fork="non-resource data"):
                # Resource fork is selected when data fork contains non-resource data.

                temp_data_fork.write(
                    b"This is the file's data fork. It should not be read, as the file has a resource fork."
                )

                with rsrcfork.open(temp_data_fork.name) as rf:
                    self.internal_test_textclipping(rf)

            with self.subTest(data_fork="valid resource data"):
                # Resource fork is selected even when data fork contains valid resource data.

                with EMPTY_RSRC_FILE.open("rb") as source_file:
                    shutil.copyfileobj(source_file, temp_data_fork)

                with rsrcfork.open(temp_data_fork.name) as rf:
                    self.internal_test_textclipping(rf)
Example #3
0
    def test_textclipping_path_resource_fork(self) -> None:
        with tempfile.NamedTemporaryFile() as tempf:
            with TEXTCLIPPING_RSRC_FILE.open("rb") as dataf:
                with open_resource_fork(pathlib.Path(tempf.name),
                                        "wb") as rsrcf:
                    shutil.copyfileobj(dataf, rsrcf)

            with rsrcfork.open(tempf.name, fork="rsrc") as rf:
                self.internal_test_textclipping(rf)
Example #4
0
    def test_testfile(self) -> None:
        with rsrcfork.open(TESTFILE_RSRC_FILE, fork="data") as rf:
            self.assertEqual(rf.header_system_data,
                             TESTFILE_HEADER_SYSTEM_DATA)
            self.assertEqual(rf.header_application_data,
                             TESTFILE_HEADER_APPLICATION_DATA)
            self.assertEqual(
                rf.file_attributes, rsrcfork.ResourceFileAttrs.
                mapPrinterDriverMultiFinderCompatible
                | rsrcfork.ResourceFileAttrs.mapReadOnly)
            self.assertEqual(list(rf), list(TESTFILE_RESOURCES))

            for (actual_type, actual_reses), (expected_type,
                                              expected_reses) in zip(
                                                  rf.items(),
                                                  TESTFILE_RESOURCES.items()):
                with self.subTest(type=expected_type):
                    self.assertEqual(actual_type, expected_type)
                    self.assertEqual(list(actual_reses), list(expected_reses))

                    for (actual_id,
                         actual_res), (expected_id,
                                       (expected_name, expected_attrs,
                                        expected_data)) in zip(
                                            actual_reses.items(),
                                            expected_reses.items()):
                        with self.subTest(id=expected_id):
                            self.assertEqual(actual_res.type, expected_type)
                            self.assertEqual(actual_id, expected_id)
                            self.assertEqual(actual_res.id, expected_id)
                            self.assertEqual(actual_res.name, expected_name)
                            self.assertEqual(actual_res.attributes,
                                             expected_attrs)
                            self.assertEqual(actual_res.data, expected_data)
                            with actual_res.open() as f:
                                self.assertEqual(f.read(), expected_data)
                            self.assertEqual(actual_res.compressed_info, None)
Example #5
0
    run_cfg = RunConfig()

    rt = Runtime(cpu_cfg, mem_cfg, run_cfg)

    mem = rt.get_mem()

    if args.rom_path:
        # copy ROM image to the dedicated memory region
        for i in range(rom_len):
            mem.w8(args.rom_base + i, rom_data[i])

        # set lowmem global ROMBase to the starting address of the loaded ROM
        mem.w32(0x2AE, args.rom_base)

    with rsrcfork.open(args.path) as rf:
        if b'CODE' in rf and 0 in rf[b'CODE']:
            print("Found executable 68k code!")
            jt_res = rf[b'CODE'][0]
            #print(jt_res.length)
            jt_data = jt_res.data_raw

            # read jump table header
            jt_header = struct.unpack('>LLLL', jt_data[0:16])

            # read first entry of the jump table
            jt_1st_entry = struct.unpack('>HHHH', jt_data[16:24])
            if jt_1st_entry[1] != 0x3F3C or jt_1st_entry[3] != 0xA9F0:
                print("Invalid jump table! 1st entry is corrupted!")
                exit(1)
Example #6
0
    def test_compress_compare(self) -> None:
        # This test goes through pairs of resource files: one original file with both compressed and uncompressed resources, and one modified file where all compressed resources have been decompressed (using ResEdit on System 7.5.5).
        # It checks that the rsrcfork library performs automatic decompression on the compressed resources, so that the compressed resource file appears to the user like the uncompressed resource file (ignoring resource order, which was lost during decompression using ResEdit).

        for name in COMPRESS_RSRC_FILE_NAMES:
            with self.subTest(name=name):
                with rsrcfork.open(
                        COMPRESSED_DIR / name,
                        fork="data") as compressed_rf, rsrcfork.open(
                            UNCOMPRESSED_DIR / name,
                            fork="data") as uncompressed_rf:
                    self.assertEqual(sorted(compressed_rf),
                                     sorted(uncompressed_rf))

                    for (compressed_type,
                         compressed_reses), (uncompressed_type,
                                             uncompressed_reses) in zip(
                                                 sorted(compressed_rf.items()),
                                                 sorted(
                                                     uncompressed_rf.items())):
                        with self.subTest(type=compressed_type):
                            self.assertEqual(compressed_type,
                                             uncompressed_type)
                            self.assertEqual(sorted(compressed_reses),
                                             sorted(uncompressed_reses))

                            for (compressed_id, compressed_res), (
                                    uncompressed_id, uncompressed_res) in zip(
                                        sorted(compressed_reses.items()),
                                        sorted(uncompressed_reses.items())):
                                with self.subTest(id=compressed_id):
                                    # The metadata of the compressed and uncompressed resources must match.
                                    self.assertEqual(compressed_res.type,
                                                     uncompressed_res.type)
                                    self.assertEqual(compressed_id,
                                                     uncompressed_id)
                                    self.assertEqual(compressed_res.id,
                                                     compressed_id)
                                    self.assertEqual(compressed_res.id,
                                                     uncompressed_res.id)
                                    self.assertEqual(compressed_res.name,
                                                     uncompressed_res.name)
                                    self.assertEqual(
                                        compressed_res.attributes &
                                        ~rsrcfork.ResourceAttrs.resCompressed,
                                        uncompressed_res.attributes)

                                    # The uncompressed resource really has to be not compressed.
                                    self.assertNotIn(
                                        rsrcfork.ResourceAttrs.resCompressed,
                                        uncompressed_res.attributes)
                                    self.assertEqual(
                                        uncompressed_res.compressed_info, None)
                                    self.assertEqual(uncompressed_res.data,
                                                     uncompressed_res.data_raw)
                                    self.assertEqual(
                                        uncompressed_res.length,
                                        uncompressed_res.length_raw)

                                    # The compressed resource's (automatically decompressed) data must match the uncompressed data.
                                    self.assertEqual(compressed_res.data,
                                                     uncompressed_res.data)
                                    self.assertEqual(compressed_res.length,
                                                     uncompressed_res.length)
                                    with compressed_res.open(
                                    ) as compressed_f, uncompressed_res.open(
                                    ) as uncompressed_f:
                                        compressed_f.seek(15)
                                        uncompressed_f.seek(15)
                                        self.assertEqual(
                                            compressed_f.read(10),
                                            uncompressed_f.read(10))
                                        self.assertEqual(
                                            compressed_f.read(),
                                            uncompressed_f.read())
                                        compressed_f.seek(0)
                                        uncompressed_f.seek(0)
                                        self.assertEqual(
                                            compressed_f.read(),
                                            uncompressed_f.read())

                                    if rsrcfork.ResourceAttrs.resCompressed in compressed_res.attributes:
                                        # Resources with the compressed attribute must expose correct compression metadata.
                                        self.assertNotEqual(
                                            compressed_res.compressed_info,
                                            None)
                                        self.assertEqual(
                                            compressed_res.compressed_info.
                                            decompressed_length,
                                            compressed_res.length)
                                    else:
                                        # Some resources in the "compressed" files are not actually compressed, in which case there is no compression metadata.
                                        self.assertEqual(
                                            compressed_res.compressed_info,
                                            None)
                                        self.assertEqual(
                                            compressed_res.data,
                                            compressed_res.data_raw)
                                        self.assertEqual(
                                            compressed_res.length,
                                            compressed_res.length_raw)
Example #7
0
 def test_textclipping_path_data_fork(self) -> None:
     with rsrcfork.open(TEXTCLIPPING_RSRC_FILE, fork="data") as rf:
         self.internal_test_textclipping(rf)
Example #8
0
 def test_empty(self) -> None:
     with rsrcfork.open(EMPTY_RSRC_FILE, fork="data") as rf:
         self.assertEqual(rf.header_system_data, bytes(112))
         self.assertEqual(rf.header_application_data, bytes(128))
         self.assertEqual(rf.file_attributes, rsrcfork.ResourceFileAttrs(0))
         self.assertEqual(list(rf), [])