trunk/src/mess/tools/imgtool/modules/mac.c
| r17925 | r17926 | |
| 803 | 803 | mac_str31 filename; /* file name (undefined for extent & catalog files) */ |
| 804 | 804 | }; |
| 805 | 805 | |
| 806 | struct mac_l2_imgref; |
| 807 | |
| 806 | 808 | /* |
| 807 | 809 | MFS/HFS open file ref |
| 808 | 810 | */ |
| 809 | | struct u |
| 811 | struct mac_fileref |
| 810 | 812 | { |
| 811 | | mac_l2_imgref *l2_img; /* image pointer */ |
| 813 | struct mac_l2_imgref *l2_img; /* image pointer */ |
| 812 | 814 | |
| 813 | 815 | UINT32 fileID; /* file ID (a.k.a. CNID in HFS/HFS+) */ |
| 814 | 816 | |
| r17925 | r17926 | |
| 827 | 829 | mfs_fileref mfs; |
| 828 | 830 | hfs_fileref hfs; |
| 829 | 831 | }; |
| 830 | | } mac_fileref; |
| 832 | }; |
| 831 | 833 | |
| 832 | 834 | /* |
| 833 | 835 | open BT ref |
| 834 | 836 | */ |
| 835 | 837 | struct mac_BTref |
| 836 | 838 | { |
| 837 | | mac_fileref fileref; /* open B-tree file ref */ |
| 839 | struct mac_fileref fileref; /* open B-tree file ref */ |
| 838 | 840 | |
| 839 | 841 | UINT16 nodeSize; /* size of a node, in bytes */ |
| 840 | 842 | UINT32 rootNode; /* node number of root node */ |
| r17925 | r17926 | |
| 901 | 903 | /* |
| 902 | 904 | MFS Master Directory Block |
| 903 | 905 | */ |
| 904 | | struct mfs_mdb_ |
| 906 | struct mfs_mdb |
| 905 | 907 | { |
| 906 | 908 | UINT8 sigWord[2]; /* volume signature - always $D2D7 */ |
| 907 | 909 | UINT32BE crDate; /* date and time of volume creation */ |
| r17925 | r17926 | |
| 941 | 943 | /* |
| 942 | 944 | HFS Master Directory Block |
| 943 | 945 | */ |
| 944 | | struct v1 |
| 946 | struct hfs_mdb |
| 945 | 947 | { |
| 946 | 948 | /* First fields are similar to MFS, though several fields have a different meaning */ |
| 947 | 949 | UINT8 sigWord[2]; /* volume signature - always $D2D7 */ |
| r17925 | r17926 | |
| 993 | 995 | hfs_extent_3 xtExtRec; /* extent record for extents overflow file */ |
| 994 | 996 | UINT32BE ctFlSize; /* size (in bytes) of catalog file */ |
| 995 | 997 | hfs_extent_3 ctExtRec; /* extent record for catalog file */ |
| 996 | | } hfs_mdb_t; |
| 998 | }; |
| 997 | 999 | |
| 998 | 1000 | /* to save a little stack space, we use the same buffer for MDB and next blocks */ |
| 999 | 1001 | union img_open_buf |
| 1000 | 1002 | { |
| 1001 | | mfs_mdb_t mfs_mdb; |
| 1002 | | hfs_mdb_t hfs_mdb; |
| 1003 | struct mfs_mdb mfs_mdb; |
| 1004 | struct hfs_mdb hfs_mdb; |
| 1003 | 1005 | UINT8 raw[512]; |
| 1004 | 1006 | }; |
| 1005 | 1007 | |
| r17925 | r17926 | |
| 1052 | 1054 | }; |
| 1053 | 1055 | |
| 1054 | 1056 | #ifdef UNUSED_FUNCTION |
| 1055 | | static void hfs_image_close(mac_l2_imgref *l2_img); |
| 1057 | static void hfs_image_close(struct mac_l2_imgref *l2_img); |
| 1056 | 1058 | #endif |
| 1057 | | static imgtoolerr_t mfs_file_get_nth_block_address(mac_fileref *fileref, UINT32 block_num, UINT32 *block_address); |
| 1058 | | static imgtoolerr_t hfs_file_get_nth_block_address(mac_fileref *fileref, UINT32 block_num, UINT32 *block_address); |
| 1059 | | static imgtoolerr_t mfs_lookup_path(mac_l2_imgref *l2_img, const char *fpath, mac_str255 filename, mac_dirent *cat_info, int create_it); |
| 1060 | | static imgtoolerr_t hfs_lookup_path(mac_l2_imgref *l2_img, const char *fpath, UINT32 *parID, mac_str255 filename, mac_dirent *cat_info); |
| 1061 | | static imgtoolerr_t mfs_file_open(mac_l2_imgref *l2_img, const mac_str255 filename, mac_forkID fork, mac_fileref *fileref); |
| 1062 | | static imgtoolerr_t hfs_file_open(mac_l2_imgref *l2_img, UINT32 parID, const mac_str255 filename, mac_forkID fork, mac_fileref *fileref); |
| 1063 | | static imgtoolerr_t mfs_file_setABeof(mac_fileref *fileref, UINT32 newABeof); |
| 1064 | | static imgtoolerr_t mfs_dir_update(mac_fileref *fileref); |
| 1059 | static imgtoolerr_t mfs_file_get_nth_block_address(struct mac_fileref *fileref, UINT32 block_num, UINT32 *block_address); |
| 1060 | static imgtoolerr_t hfs_file_get_nth_block_address(struct mac_fileref *fileref, UINT32 block_num, UINT32 *block_address); |
| 1061 | static imgtoolerr_t mfs_lookup_path(struct mac_l2_imgref *l2_img, const char *fpath, mac_str255 filename, mac_dirent *cat_info, int create_it); |
| 1062 | static imgtoolerr_t hfs_lookup_path(struct mac_l2_imgref *l2_img, const char *fpath, UINT32 *parID, mac_str255 filename, mac_dirent *cat_info); |
| 1063 | static imgtoolerr_t mfs_file_open(struct mac_l2_imgref *l2_img, const mac_str255 filename, mac_forkID fork, struct mac_fileref *fileref); |
| 1064 | static imgtoolerr_t hfs_file_open(struct mac_l2_imgref *l2_img, UINT32 parID, const mac_str255 filename, mac_forkID fork, struct mac_fileref *fileref); |
| 1065 | static imgtoolerr_t mfs_file_setABeof(struct mac_fileref *fileref, UINT32 newABeof); |
| 1066 | static imgtoolerr_t mfs_dir_update(struct mac_fileref *fileref); |
| 1065 | 1067 | |
| 1066 | | static mac_l2_imgref *get_imgref(imgtool_image *img) |
| 1068 | static struct mac_l2_imgref *get_imgref(imgtool_image *img) |
| 1067 | 1069 | { |
| 1068 | | return (mac_l2_imgref *) imgtool_floppy_extrabytes(img); |
| 1070 | return (struct mac_l2_imgref *) imgtool_floppy_extrabytes(img); |
| 1069 | 1071 | } |
| 1070 | 1072 | |
| 1071 | 1073 | |
| r17925 | r17926 | |
| 1077 | 1079 | |
| 1078 | 1080 | l2_img (I/O): level-2 image reference |
| 1079 | 1081 | */ |
| 1080 | | static void mac_image_close(mac_l2_imgref *l2_img) |
| 1082 | static void mac_image_close(struct mac_l2_imgref *l2_img) |
| 1081 | 1083 | { |
| 1082 | 1084 | switch (l2_img->format) |
| 1083 | 1085 | { |
| r17925 | r17926 | |
| 1111 | 1113 | |
| 1112 | 1114 | Return imgtool error code |
| 1113 | 1115 | */ |
| 1114 | | static imgtoolerr_t mac_lookup_path(mac_l2_imgref *l2_img, const char *fpath, UINT32 *parID, mac_str255 filename, mac_dirent *cat_info, int create_it) |
| 1116 | static imgtoolerr_t mac_lookup_path(struct mac_l2_imgref *l2_img, const char *fpath, UINT32 *parID, mac_str255 filename, mac_dirent *cat_info, int create_it) |
| 1115 | 1117 | { |
| 1116 | 1118 | imgtoolerr_t err = IMGTOOLERR_UNEXPECTED; |
| 1117 | 1119 | |
| r17925 | r17926 | |
| 1143 | 1145 | |
| 1144 | 1146 | Return imgtool error code |
| 1145 | 1147 | */ |
| 1146 | | static imgtoolerr_t mac_file_open(mac_l2_imgref *l2_img, UINT32 parID, const mac_str255 filename, mac_forkID fork, mac_fileref *fileref) |
| 1148 | static imgtoolerr_t mac_file_open(struct mac_l2_imgref *l2_img, UINT32 parID, const mac_str255 filename, mac_forkID fork, struct mac_fileref *fileref) |
| 1147 | 1149 | { |
| 1148 | 1150 | switch (l2_img->format) |
| 1149 | 1151 | { |
| r17925 | r17926 | |
| 1168 | 1170 | |
| 1169 | 1171 | Return imgtool error code |
| 1170 | 1172 | */ |
| 1171 | | static imgtoolerr_t mac_file_read(mac_fileref *fileref, UINT32 len, void *dest) |
| 1173 | static imgtoolerr_t mac_file_read(struct mac_fileref *fileref, UINT32 len, void *dest) |
| 1172 | 1174 | { |
| 1173 | 1175 | UINT32 block = 0; |
| 1174 | 1176 | floppy_tag_record tag; |
| r17925 | r17926 | |
| 1244 | 1246 | |
| 1245 | 1247 | Return imgtool error code |
| 1246 | 1248 | */ |
| 1247 | | static imgtoolerr_t mac_file_write(mac_fileref *fileref, UINT32 len, const void *src) |
| 1249 | static imgtoolerr_t mac_file_write(struct mac_fileref *fileref, UINT32 len, const void *src) |
| 1248 | 1250 | { |
| 1249 | 1251 | UINT32 block = 0; |
| 1250 | 1252 | floppy_tag_record tag; |
| r17925 | r17926 | |
| 1349 | 1351 | |
| 1350 | 1352 | Return imgtool error code |
| 1351 | 1353 | */ |
| 1352 | | static imgtoolerr_t mac_file_tell(mac_fileref *fileref, UINT32 *filePos) |
| 1354 | static imgtoolerr_t mac_file_tell(struct mac_fileref *fileref, UINT32 *filePos) |
| 1353 | 1355 | { |
| 1354 | 1356 | *filePos = fileref->crPs; |
| 1355 | 1357 | |
| r17925 | r17926 | |
| 1367 | 1369 | |
| 1368 | 1370 | Return imgtool error code |
| 1369 | 1371 | */ |
| 1370 | | static imgtoolerr_t mac_file_seek(mac_fileref *fileref, UINT32 filePos) |
| 1372 | static imgtoolerr_t mac_file_seek(struct mac_fileref *fileref, UINT32 filePos) |
| 1371 | 1373 | { |
| 1372 | 1374 | if ((fileref->crPs / 512) != (filePos / 512)) |
| 1373 | 1375 | fileref->reload_buf = TRUE; |
| r17925 | r17926 | |
| 1387 | 1389 | |
| 1388 | 1390 | Return imgtool error code |
| 1389 | 1391 | */ |
| 1390 | | static imgtoolerr_t mac_file_seteof(mac_fileref *fileref, UINT32 newEof) |
| 1392 | static imgtoolerr_t mac_file_seteof(struct mac_fileref *fileref, UINT32 newEof) |
| 1391 | 1393 | { |
| 1392 | 1394 | UINT32 newABEof; |
| 1393 | 1395 | imgtoolerr_t err = IMGTOOLERR_SUCCESS; |
| r17925 | r17926 | |
| 1527 | 1529 | */ |
| 1528 | 1530 | struct mfs_dirref |
| 1529 | 1531 | { |
| 1530 | | mac_l2_imgref *l2_img; /* image pointer */ |
| 1532 | struct mac_l2_imgref *l2_img; /* image pointer */ |
| 1531 | 1533 | UINT16 index; /* current file index in the disk directory */ |
| 1532 | 1534 | UINT16 cur_block; /* current block offset in directory file */ |
| 1533 | 1535 | UINT16 cur_offset; /* current byte offset in current block of directory file */ |
| r17925 | r17926 | |
| 1603 | 1605 | static imgtoolerr_t mfs_image_open(imgtool_image *image, imgtool_stream *stream) |
| 1604 | 1606 | { |
| 1605 | 1607 | imgtoolerr_t err; |
| 1606 | | mac_l2_imgref *l2_img; |
| 1608 | struct mac_l2_imgref *l2_img; |
| 1607 | 1609 | img_open_buf buf_local; |
| 1608 | 1610 | img_open_buf *buf; |
| 1609 | 1611 | |
| r17925 | r17926 | |
| 1692 | 1694 | |
| 1693 | 1695 | Return imgtool error code |
| 1694 | 1696 | */ |
| 1695 | | static imgtoolerr_t mfs_update_mdb(mac_l2_imgref *l2_img) |
| 1697 | static imgtoolerr_t mfs_update_mdb(struct mac_l2_imgref *l2_img) |
| 1696 | 1698 | { |
| 1697 | 1699 | imgtoolerr_t err; |
| 1698 | 1700 | union |
| 1699 | 1701 | { |
| 1700 | | mfs_mdb_t mfs_mdb; |
| 1702 | struct mfs_mdb mfs_mdb; |
| 1701 | 1703 | UINT8 raw[512]; |
| 1702 | 1704 | } buf; |
| 1703 | 1705 | |
| r17925 | r17926 | |
| 1788 | 1790 | |
| 1789 | 1791 | Return imgtool error code |
| 1790 | 1792 | */ |
| 1791 | | static imgtoolerr_t mfs_dir_open(mac_l2_imgref *l2_img, const char *path, mfs_dirref *dirref) |
| 1793 | static imgtoolerr_t mfs_dir_open(struct mac_l2_imgref *l2_img, const char *path, mfs_dirref *dirref) |
| 1792 | 1794 | { |
| 1793 | 1795 | imgtoolerr_t err; |
| 1794 | 1796 | |
| r17925 | r17926 | |
| 1882 | 1884 | |
| 1883 | 1885 | Return imgtool error code |
| 1884 | 1886 | */ |
| 1885 | | static imgtoolerr_t mfs_dir_insert(mac_l2_imgref *l2_img, mfs_dirref *dirref, const UINT8 *new_fname, mfs_dir_entry **dir_entry) |
| 1887 | static imgtoolerr_t mfs_dir_insert(struct mac_l2_imgref *l2_img, mfs_dirref *dirref, const UINT8 *new_fname, mfs_dir_entry **dir_entry) |
| 1886 | 1888 | { |
| 1887 | 1889 | size_t new_dir_entry_len; |
| 1888 | 1890 | mfs_dir_entry *cur_dir_entry; |
| r17925 | r17926 | |
| 1973 | 1975 | |
| 1974 | 1976 | Return imgtool error code |
| 1975 | 1977 | */ |
| 1976 | | static imgtoolerr_t mfs_dir_update(mac_fileref *fileref) |
| 1978 | static imgtoolerr_t mfs_dir_update(struct mac_fileref *fileref) |
| 1977 | 1979 | { |
| 1978 | 1980 | UINT16 cur_block; |
| 1979 | 1981 | UINT16 cur_offset; |
| r17925 | r17926 | |
| 1999 | 2001 | switch (fileref->forkType) |
| 2000 | 2002 | { |
| 2001 | 2003 | case data_fork: |
| 2002 | | set_UINT16BE(&cur_dir_entry->dataStartBlock, fileref->u.mfs.stBlk); |
| 2004 | set_UINT16BE(&cur_dir_entry->dataStartBlock, fileref->mfs.stBlk); |
| 2003 | 2005 | set_UINT32BE(&cur_dir_entry->dataLogicalSize, fileref->eof); |
| 2004 | 2006 | set_UINT32BE(&cur_dir_entry->dataPhysicalSize, fileref->pLen); |
| 2005 | 2007 | break; |
| 2006 | 2008 | |
| 2007 | 2009 | case rsrc_fork: |
| 2008 | | set_UINT16BE(&cur_dir_entry->rsrcStartBlock, fileref->u.mfs.stBlk); |
| 2010 | set_UINT16BE(&cur_dir_entry->rsrcStartBlock, fileref->mfs.stBlk); |
| 2009 | 2011 | set_UINT32BE(&cur_dir_entry->rsrcLogicalSize, fileref->eof); |
| 2010 | 2012 | set_UINT32BE(&cur_dir_entry->rsrcPhysicalSize, fileref->pLen); |
| 2011 | 2013 | break; |
| r17925 | r17926 | |
| 2092 | 2094 | |
| 2093 | 2095 | Return imgtool error code |
| 2094 | 2096 | */ |
| 2095 | | static imgtoolerr_t mfs_lookup_path(mac_l2_imgref *l2_img, const char *fpath, mac_str255 filename, mac_dirent *cat_info, int create_it) |
| 2097 | static imgtoolerr_t mfs_lookup_path(struct mac_l2_imgref *l2_img, const char *fpath, mac_str255 filename, mac_dirent *cat_info, int create_it) |
| 2096 | 2098 | { |
| 2097 | 2099 | mfs_dirref dirref; |
| 2098 | 2100 | mfs_dir_entry *dir_entry; |
| r17925 | r17926 | |
| 2161 | 2163 | |
| 2162 | 2164 | Return imgtool error code |
| 2163 | 2165 | */ |
| 2164 | | static imgtoolerr_t mfs_file_open_internal(mac_l2_imgref *l2_img, const mfs_dir_entry *dir_entry, mac_forkID fork, mac_fileref *fileref) |
| 2166 | static imgtoolerr_t mfs_file_open_internal(struct mac_l2_imgref *l2_img, const mfs_dir_entry *dir_entry, mac_forkID fork, struct mac_fileref *fileref) |
| 2165 | 2167 | { |
| 2166 | 2168 | assert(l2_img->format == L2I_MFS); |
| 2167 | 2169 | |
| r17925 | r17926 | |
| 2173 | 2175 | switch (fork) |
| 2174 | 2176 | { |
| 2175 | 2177 | case data_fork: |
| 2176 | | fileref->u.mfs.stBlk = get_UINT16BE(dir_entry->dataStartBlock); |
| 2178 | fileref->mfs.stBlk = get_UINT16BE(dir_entry->dataStartBlock); |
| 2177 | 2179 | fileref->eof = get_UINT32BE(dir_entry->dataLogicalSize); |
| 2178 | 2180 | fileref->pLen = get_UINT32BE(dir_entry->dataPhysicalSize); |
| 2179 | 2181 | break; |
| 2180 | 2182 | |
| 2181 | 2183 | case rsrc_fork: |
| 2182 | | fileref->u.mfs.stBlk = get_UINT16BE(dir_entry->rsrcStartBlock); |
| 2184 | fileref->mfs.stBlk = get_UINT16BE(dir_entry->rsrcStartBlock); |
| 2183 | 2185 | fileref->eof = get_UINT32BE(dir_entry->rsrcLogicalSize); |
| 2184 | 2186 | fileref->pLen = get_UINT32BE(dir_entry->rsrcPhysicalSize); |
| 2185 | 2187 | break; |
| r17925 | r17926 | |
| 2205 | 2207 | |
| 2206 | 2208 | Return imgtool error code |
| 2207 | 2209 | */ |
| 2208 | | static imgtoolerr_t mfs_file_open(mac_l2_imgref *l2_img, const mac_str255 filename, mac_forkID fork, mac_fileref *fileref) |
| 2210 | static imgtoolerr_t mfs_file_open(struct mac_l2_imgref *l2_img, const mac_str255 filename, mac_forkID fork, struct mac_fileref *fileref) |
| 2209 | 2211 | { |
| 2210 | 2212 | mfs_dirref dirref; |
| 2211 | 2213 | mfs_dir_entry *dir_entry; |
| r17925 | r17926 | |
| 2233 | 2235 | |
| 2234 | 2236 | Returns the 12-bit value read in array. |
| 2235 | 2237 | */ |
| 2236 | | static UINT16 mfs_get_ABlink(mac_l2_imgref *l2_img, UINT16 AB_address) |
| 2238 | static UINT16 mfs_get_ABlink(struct mac_l2_imgref *l2_img, UINT16 AB_address) |
| 2237 | 2239 | { |
| 2238 | 2240 | UINT16 reply; |
| 2239 | 2241 | int base; |
| r17925 | r17926 | |
| 2259 | 2261 | AB_address (I): index in the array, which is an AB address |
| 2260 | 2262 | data (I): 12-bit value to write in array |
| 2261 | 2263 | */ |
| 2262 | | static void mfs_set_ABlink(mac_l2_imgref *l2_img, UINT16 AB_address, UINT16 data) |
| 2264 | static void mfs_set_ABlink(struct mac_l2_imgref *l2_img, UINT16 AB_address, UINT16 data) |
| 2263 | 2265 | { |
| 2264 | 2266 | int base; |
| 2265 | 2267 | |
| r17925 | r17926 | |
| 2297 | 2299 | |
| 2298 | 2300 | Return imgtool error code |
| 2299 | 2301 | */ |
| 2300 | | static imgtoolerr_t mfs_file_get_nth_block_address(mac_fileref *fileref, UINT32 block_num, UINT32 *block_address) |
| 2302 | static imgtoolerr_t mfs_file_get_nth_block_address(struct mac_fileref *fileref, UINT32 block_num, UINT32 *block_address) |
| 2301 | 2303 | { |
| 2302 | 2304 | UINT32 AB_num; |
| 2303 | 2305 | UINT32 i; |
| r17925 | r17926 | |
| 2307 | 2309 | |
| 2308 | 2310 | AB_num = block_num / fileref->l2_img->blocksperAB; |
| 2309 | 2311 | |
| 2310 | | AB_address = fileref->u.mfs.stBlk; |
| 2312 | AB_address = fileref->mfs.stBlk; |
| 2311 | 2313 | if ((AB_address == 0) || (AB_address >= fileref->l2_img->numABs+2)) |
| 2312 | 2314 | /* 0 -> ??? */ |
| 2313 | 2315 | return IMGTOOLERR_CORRUPTIMAGE; |
| r17925 | r17926 | |
| 2341 | 2343 | |
| 2342 | 2344 | fileref (I/O): open mac file reference |
| 2343 | 2345 | lastAB (I): AB address on disk of last file AB (only if |
| 2344 | | fileref->u.mfs.stBlk != 1) |
| 2346 | fileref->mfs.stBlk != 1) |
| 2345 | 2347 | allocABs (I): number of ABs to allocate in addition to the current file |
| 2346 | 2348 | allocation |
| 2347 | 2349 | fblock (I): first file block to allocate (used for tag data) |
| 2348 | 2350 | |
| 2349 | 2351 | Return imgtool error code |
| 2350 | 2352 | */ |
| 2351 | | static imgtoolerr_t mfs_file_allocABs(mac_fileref *fileref, UINT16 lastAB, UINT32 allocABs, UINT32 fblock) |
| 2353 | static imgtoolerr_t mfs_file_allocABs(struct mac_fileref *fileref, UINT16 lastAB, UINT32 allocABs, UINT32 fblock) |
| 2352 | 2354 | { |
| 2353 | 2355 | int numABs = fileref->l2_img->numABs; |
| 2354 | 2356 | int free_ABs; |
| r17925 | r17926 | |
| 2399 | 2401 | if (free_ABs < allocABs) |
| 2400 | 2402 | return IMGTOOLERR_NOSPACE; |
| 2401 | 2403 | |
| 2402 | | if (fileref->u.mfs.stBlk != 1) |
| 2404 | if (fileref->mfs.stBlk != 1) |
| 2403 | 2405 | { /* try to extend last file extent */ |
| 2404 | 2406 | /* append free ABs after last AB */ |
| 2405 | 2407 | for (i=lastAB+1; (mfs_get_ABlink(fileref->l2_img, i) == 0) && (allocABs > 0) && (i < numABs); i++) |
| r17925 | r17926 | |
| 2499 | 2501 | extentABlen = allocABs; |
| 2500 | 2502 | for (i=0; i<allocABs; i++) |
| 2501 | 2503 | { |
| 2502 | | if (fileref->u.mfs.stBlk != 1) |
| 2504 | if (fileref->mfs.stBlk != 1) |
| 2503 | 2505 | mfs_set_ABlink(fileref->l2_img, lastAB, firstBestExtentBaseAB+i+2); |
| 2504 | 2506 | else |
| 2505 | | fileref->u.mfs.stBlk = firstBestExtentBaseAB+i+2; |
| 2507 | fileref->mfs.stBlk = firstBestExtentBaseAB+i+2; |
| 2506 | 2508 | lastAB = firstBestExtentBaseAB+i; |
| 2507 | 2509 | free_ABs--; |
| 2508 | 2510 | /* set tag to allocated */ |
| r17925 | r17926 | |
| 2537 | 2539 | { /* jeez, we need to fragment it. We use the largest smaller block to limit fragmentation. */ |
| 2538 | 2540 | for (i=0; i<secondBestExtentABlen; i++) |
| 2539 | 2541 | { |
| 2540 | | if (fileref->u.mfs.stBlk != 1) |
| 2542 | if (fileref->mfs.stBlk != 1) |
| 2541 | 2543 | mfs_set_ABlink(fileref->l2_img, lastAB, secondBestExtentBaseAB+i+2); |
| 2542 | 2544 | else |
| 2543 | | fileref->u.mfs.stBlk = secondBestExtentBaseAB+i+2; |
| 2545 | fileref->mfs.stBlk = secondBestExtentBaseAB+i+2; |
| 2544 | 2546 | lastAB = secondBestExtentBaseAB+i; |
| 2545 | 2547 | free_ABs--; |
| 2546 | 2548 | /* set tag to allocated */ |
| r17925 | r17926 | |
| 2588 | 2590 | |
| 2589 | 2591 | Return imgtool error code |
| 2590 | 2592 | */ |
| 2591 | | static imgtoolerr_t mfs_file_setABeof(mac_fileref *fileref, UINT32 newABeof) |
| 2593 | static imgtoolerr_t mfs_file_setABeof(struct mac_fileref *fileref, UINT32 newABeof) |
| 2592 | 2594 | { |
| 2593 | 2595 | UINT16 AB_address = 0; |
| 2594 | 2596 | UINT16 AB_link; |
| r17925 | r17926 | |
| 2601 | 2603 | assert(fileref->l2_img->format == L2I_MFS); |
| 2602 | 2604 | |
| 2603 | 2605 | /* run through link chain until we reach the old or the new EOF */ |
| 2604 | | AB_link = fileref->u.mfs.stBlk; |
| 2606 | AB_link = fileref->mfs.stBlk; |
| 2605 | 2607 | if ((AB_link == 0) || (AB_link >= fileref->l2_img->numABs+2)) |
| 2606 | 2608 | /* 0 -> ??? */ |
| 2607 | 2609 | return IMGTOOLERR_CORRUPTIMAGE; |
| r17925 | r17926 | |
| 2640 | 2642 | { /* new EOF is shorter than old one */ |
| 2641 | 2643 | /* mark new eof */ |
| 2642 | 2644 | if (i==0) |
| 2643 | | fileref->u.mfs.stBlk = 1; |
| 2645 | fileref->mfs.stBlk = 1; |
| 2644 | 2646 | else |
| 2645 | 2647 | { |
| 2646 | 2648 | mfs_set_ABlink(fileref->l2_img, AB_address, 1); |
| r17925 | r17926 | |
| 2907 | 2909 | |
| 2908 | 2910 | struct hfs_cat_enumerator |
| 2909 | 2911 | { |
| 2910 | | mac_l2_imgref *l2_img; |
| 2912 | struct mac_l2_imgref *l2_img; |
| 2911 | 2913 | BT_leaf_rec_enumerator BT_enumerator; |
| 2912 | 2914 | UINT32 parID; |
| 2913 | 2915 | }; |
| r17925 | r17926 | |
| 2923 | 2925 | |
| 2924 | 2926 | Return imgtool error code |
| 2925 | 2927 | */ |
| 2926 | | static imgtoolerr_t hfs_open_extents_file(mac_l2_imgref *l2_img, const hfs_mdb_t *mdb, mac_fileref *fileref) |
| 2928 | static imgtoolerr_t hfs_open_extents_file(struct mac_l2_imgref *l2_img, const struct hfs_mdb *mdb, struct mac_fileref *fileref) |
| 2927 | 2929 | { |
| 2928 | 2930 | assert(l2_img->format == L2I_HFS); |
| 2929 | 2931 | |
| r17925 | r17926 | |
| 2933 | 2935 | fileref->forkType = (mac_forkID)0x00; |
| 2934 | 2936 | |
| 2935 | 2937 | fileref->eof = fileref->pLen = get_UINT32BE(mdb->xtFlSize); |
| 2936 | | memcpy(fileref->u.hfs.extents, mdb->xtExtRec, sizeof(hfs_extent_3)); |
| 2938 | memcpy(fileref->hfs.extents, mdb->xtExtRec, sizeof(hfs_extent_3)); |
| 2937 | 2939 | |
| 2938 | 2940 | fileref->crPs = 0; |
| 2939 | 2941 | |
| r17925 | r17926 | |
| 2953 | 2955 | |
| 2954 | 2956 | Return imgtool error code |
| 2955 | 2957 | */ |
| 2956 | | static imgtoolerr_t hfs_open_cat_file(mac_l2_imgref *l2_img, const hfs_mdb_t *mdb, mac_fileref *fileref) |
| 2958 | static imgtoolerr_t hfs_open_cat_file(struct mac_l2_imgref *l2_img, const struct hfs_mdb *mdb, struct mac_fileref *fileref) |
| 2957 | 2959 | { |
| 2958 | 2960 | assert(l2_img->format == L2I_HFS); |
| 2959 | 2961 | |
| r17925 | r17926 | |
| 2963 | 2965 | fileref->forkType = (mac_forkID)0x00; |
| 2964 | 2966 | |
| 2965 | 2967 | fileref->eof = fileref->pLen = get_UINT32BE(mdb->ctFlSize); |
| 2966 | | memcpy(fileref->u.hfs.extents, mdb->ctExtRec, sizeof(hfs_extent_3)); |
| 2968 | memcpy(fileref->hfs.extents, mdb->ctExtRec, sizeof(hfs_extent_3)); |
| 2967 | 2969 | |
| 2968 | 2970 | fileref->crPs = 0; |
| 2969 | 2971 | |
| r17925 | r17926 | |
| 3030 | 3032 | static imgtoolerr_t hfs_image_open(imgtool_image *image, imgtool_stream *stream) |
| 3031 | 3033 | { |
| 3032 | 3034 | imgtoolerr_t err; |
| 3033 | | mac_l2_imgref *l2_img; |
| 3035 | struct mac_l2_imgref *l2_img; |
| 3034 | 3036 | img_open_buf buf_local; |
| 3035 | 3037 | img_open_buf *buf; |
| 3036 | 3038 | |
| r17925 | r17926 | |
| 3135 | 3137 | |
| 3136 | 3138 | l2_img (I/O): level-2 image reference |
| 3137 | 3139 | */ |
| 3138 | | static void hfs_image_close(mac_l2_imgref *l2_img) |
| 3140 | static void hfs_image_close(struct mac_l2_imgref *l2_img) |
| 3139 | 3141 | { |
| 3140 | 3142 | assert(l2_img->format == L2I_HFS); |
| 3141 | 3143 | |
| r17925 | r17926 | |
| 3159 | 3161 | |
| 3160 | 3162 | Return imgtool error code |
| 3161 | 3163 | */ |
| 3162 | | static imgtoolerr_t hfs_get_cat_record_data(mac_l2_imgref *l2_img, void *rec_raw, int rec_len, hfs_catKey **rec_key, hfs_catData **rec_data) |
| 3164 | static imgtoolerr_t hfs_get_cat_record_data(struct mac_l2_imgref *l2_img, void *rec_raw, int rec_len, hfs_catKey **rec_key, hfs_catData **rec_data) |
| 3163 | 3165 | { |
| 3164 | 3166 | hfs_catKey *lrec_key; |
| 3165 | 3167 | void *rec_data_raw; |
| r17925 | r17926 | |
| 3229 | 3231 | |
| 3230 | 3232 | Return imgtool error code |
| 3231 | 3233 | */ |
| 3232 | | static imgtoolerr_t hfs_cat_open(mac_l2_imgref *l2_img, const char *path, hfs_cat_enumerator *enumerator) |
| 3234 | static imgtoolerr_t hfs_cat_open(struct mac_l2_imgref *l2_img, const char *path, hfs_cat_enumerator *enumerator) |
| 3233 | 3235 | { |
| 3234 | 3236 | imgtoolerr_t err; |
| 3235 | 3237 | UINT32 parID; |
| r17925 | r17926 | |
| 3302 | 3304 | |
| 3303 | 3305 | Return imgtool error code |
| 3304 | 3306 | */ |
| 3305 | | static imgtoolerr_t hfs_cat_search(mac_l2_imgref *l2_img, UINT32 parID, const mac_str31 cName, hfs_catKey **rec_key, hfs_catData **rec_data) |
| 3307 | static imgtoolerr_t hfs_cat_search(struct mac_l2_imgref *l2_img, UINT32 parID, const mac_str31 cName, hfs_catKey **rec_key, hfs_catData **rec_data) |
| 3306 | 3308 | { |
| 3307 | 3309 | hfs_catKey search_key; |
| 3308 | 3310 | void *rec; |
| r17925 | r17926 | |
| 3350 | 3352 | |
| 3351 | 3353 | Return imgtool error code |
| 3352 | 3354 | */ |
| 3353 | | static imgtoolerr_t hfs_lookup_path(mac_l2_imgref *l2_img, const char *fpath, UINT32 *parID, mac_str255 filename, mac_dirent *cat_info) |
| 3355 | static imgtoolerr_t hfs_lookup_path(struct mac_l2_imgref *l2_img, const char *fpath, UINT32 *parID, mac_str255 filename, mac_dirent *cat_info) |
| 3354 | 3356 | { |
| 3355 | 3357 | const char *element_start; |
| 3356 | 3358 | int element_len; |
| r17925 | r17926 | |
| 3434 | 3436 | |
| 3435 | 3437 | Return imgtool error code |
| 3436 | 3438 | */ |
| 3437 | | static imgtoolerr_t hfs_file_open_internal(mac_l2_imgref *l2_img, const hfs_catFileData *file_rec, mac_forkID fork, mac_fileref *fileref) |
| 3439 | static imgtoolerr_t hfs_file_open_internal(struct mac_l2_imgref *l2_img, const hfs_catFileData *file_rec, mac_forkID fork, struct mac_fileref *fileref) |
| 3438 | 3440 | { |
| 3439 | 3441 | assert(l2_img->format == L2I_HFS); |
| 3440 | 3442 | |
| r17925 | r17926 | |
| 3448 | 3450 | case data_fork: |
| 3449 | 3451 | fileref->eof = get_UINT32BE(file_rec->dataLogicalSize); |
| 3450 | 3452 | fileref->pLen = get_UINT32BE(file_rec->dataPhysicalSize); |
| 3451 | | memcpy(fileref->u.hfs.extents, file_rec->dataExtents, sizeof(hfs_extent_3)); |
| 3453 | memcpy(fileref->hfs.extents, file_rec->dataExtents, sizeof(hfs_extent_3)); |
| 3452 | 3454 | break; |
| 3453 | 3455 | |
| 3454 | 3456 | case rsrc_fork: |
| 3455 | 3457 | fileref->eof = get_UINT32BE(file_rec->rsrcLogicalSize); |
| 3456 | 3458 | fileref->pLen = get_UINT32BE(file_rec->rsrcPhysicalSize); |
| 3457 | | memcpy(fileref->u.hfs.extents, file_rec->rsrcExtents, sizeof(hfs_extent_3)); |
| 3459 | memcpy(fileref->hfs.extents, file_rec->rsrcExtents, sizeof(hfs_extent_3)); |
| 3458 | 3460 | break; |
| 3459 | 3461 | } |
| 3460 | 3462 | |
| r17925 | r17926 | |
| 3479 | 3481 | |
| 3480 | 3482 | Return imgtool error code |
| 3481 | 3483 | */ |
| 3482 | | static imgtoolerr_t hfs_file_open(mac_l2_imgref *l2_img, UINT32 parID, const mac_str255 filename, mac_forkID fork, mac_fileref *fileref) |
| 3484 | static imgtoolerr_t hfs_file_open(struct mac_l2_imgref *l2_img, UINT32 parID, const mac_str255 filename, mac_forkID fork, struct mac_fileref *fileref) |
| 3483 | 3485 | { |
| 3484 | 3486 | hfs_catKey *catrec_key; |
| 3485 | 3487 | hfs_catData *catrec_data; |
| r17925 | r17926 | |
| 3497 | 3499 | if (dataRecType != hcrt_File) |
| 3498 | 3500 | return IMGTOOLERR_BADFILENAME; |
| 3499 | 3501 | |
| 3500 | | fileref->u.hfs.parID = get_UINT32BE(catrec_key->parID); |
| 3501 | | mac_strcpy(fileref->u.hfs.filename, catrec_key->cName); |
| 3502 | fileref->hfs.parID = get_UINT32BE(catrec_key->parID); |
| 3503 | mac_strcpy(fileref->hfs.filename, catrec_key->cName); |
| 3502 | 3504 | |
| 3503 | 3505 | /* open it */ |
| 3504 | 3506 | return hfs_file_open_internal(l2_img, &catrec_data->file, fork, fileref); |
| r17925 | r17926 | |
| 3516 | 3518 | |
| 3517 | 3519 | Return imgtool error code |
| 3518 | 3520 | */ |
| 3519 | | static imgtoolerr_t hfs_file_get_nth_block_address(mac_fileref *fileref, UINT32 block_num, UINT32 *block_address) |
| 3521 | static imgtoolerr_t hfs_file_get_nth_block_address(struct mac_fileref *fileref, UINT32 block_num, UINT32 *block_address) |
| 3520 | 3522 | { |
| 3521 | 3523 | UINT32 AB_num; |
| 3522 | 3524 | UINT32 cur_AB; |
| r17925 | r17926 | |
| 3533 | 3535 | |
| 3534 | 3536 | AB_num = block_num / fileref->l2_img->blocksperAB; |
| 3535 | 3537 | cur_AB = 0; |
| 3536 | | cur_extents = fileref->u.hfs.extents; |
| 3538 | cur_extents = fileref->hfs.extents; |
| 3537 | 3539 | |
| 3538 | 3540 | /* first look in catalog tree extents */ |
| 3539 | 3541 | for (i=0; i<3; i++) |
| r17925 | r17926 | |
| 3994 | 3996 | case L2I_HFS: |
| 3995 | 3997 | maxExtentAB = 0; |
| 3996 | 3998 | for (j=0; j<3; j++) |
| 3997 | | maxExtentAB += get_UINT16BE(BTref->fileref.u.hfs.extents[j].numABlks); |
| 3999 | maxExtentAB += get_UINT16BE(BTref->fileref.hfs.extents[j].numABlks); |
| 3998 | 4000 | maxExtentNode = (UINT64)maxExtentAB * 512 * BTref->fileref.l2_img->blocksperAB |
| 3999 | 4001 | / BTref->nodeSize; |
| 4000 | 4002 | extentEOL = FALSE; |
| r17925 | r17926 | |
| 5210 | 5212 | |
| 5211 | 5213 | Return imgtool error code |
| 5212 | 5214 | */ |
| 5213 | | static imgtoolerr_t get_comment(mac_l2_imgref *l2_img, UINT16 id, mac_str255 comment) |
| 5215 | static imgtoolerr_t get_comment(struct mac_l2_imgref *l2_img, UINT16 id, mac_str255 comment) |
| 5214 | 5216 | { |
| 5215 | 5217 | static const UINT8 desktop_fname[] = {'\7','D','e','s','k','t','o','p'}; |
| 5216 | 5218 | #define restype_FCMT (('F' << 24) | ('C' << 16) | ('M' << 8) | 'T') |
| r17925 | r17926 | |
| 5279 | 5281 | */ |
| 5280 | 5282 | static void mac_image_exit(imgtool_image *img) |
| 5281 | 5283 | { |
| 5282 | | mac_l2_imgref *image = get_imgref(img); |
| 5284 | struct mac_l2_imgref *image = get_imgref(img); |
| 5283 | 5285 | |
| 5284 | 5286 | mac_image_close(image); |
| 5285 | 5287 | } |
| r17925 | r17926 | |
| 5292 | 5294 | */ |
| 5293 | 5295 | static void mac_image_info(imgtool_image *img, char *string, size_t len) |
| 5294 | 5296 | { |
| 5295 | | mac_l2_imgref *image = get_imgref(img); |
| 5297 | struct mac_l2_imgref *image = get_imgref(img); |
| 5296 | 5298 | |
| 5297 | 5299 | switch (image->format) |
| 5298 | 5300 | { |
| r17925 | r17926 | |
| 5312 | 5314 | struct mac_iterator |
| 5313 | 5315 | { |
| 5314 | 5316 | mac_format format; |
| 5315 | | mac_l2_imgref *l2_img; |
| 5317 | struct mac_l2_imgref *l2_img; |
| 5316 | 5318 | union |
| 5317 | 5319 | { |
| 5318 | 5320 | struct |
| r17925 | r17926 | |
| 5331 | 5333 | */ |
| 5332 | 5334 | static imgtoolerr_t mac_image_beginenum(imgtool_directory *enumeration, const char *path) |
| 5333 | 5335 | { |
| 5334 | | mac_l2_imgref *image = get_imgref(imgtool_directory_image(enumeration)); |
| 5336 | struct mac_l2_imgref *image = get_imgref(imgtool_directory_image(enumeration)); |
| 5335 | 5337 | mac_iterator *iter = (mac_iterator *) imgtool_directory_extrabytes(enumeration); |
| 5336 | 5338 | imgtoolerr_t err = IMGTOOLERR_UNEXPECTED; |
| 5337 | 5339 | |
| r17925 | r17926 | |
| 5572 | 5574 | } |
| 5573 | 5575 | |
| 5574 | 5576 | #ifdef UNUSED_FUNCTION |
| 5575 | | static imgtoolerr_t mac_get_comment(mac_l2_imgref *image, mac_str255 filename, const mac_dirent *cat_info, mac_str255 comment) |
| 5577 | static imgtoolerr_t mac_get_comment(struct mac_l2_imgref *image, mac_str255 filename, const mac_dirent *cat_info, mac_str255 comment) |
| 5576 | 5578 | { |
| 5577 | 5579 | imgtoolerr_t err = IMGTOOLERR_SUCCESS; |
| 5578 | 5580 | UINT16 commentID; |
| r17925 | r17926 | |
| 5607 | 5609 | { |
| 5608 | 5610 | imgtoolerr_t err; |
| 5609 | 5611 | imgtool_image *img = imgtool_partition_image(partition); |
| 5610 | | mac_l2_imgref *image = get_imgref(img); |
| 5612 | struct mac_l2_imgref *image = get_imgref(img); |
| 5611 | 5613 | UINT32 parID; |
| 5612 | 5614 | mac_str255 filename; |
| 5613 | 5615 | mac_dirent cat_info; |
| r17925 | r17926 | |
| 5657 | 5659 | static imgtoolerr_t mac_image_writefile(imgtool_partition *partition, const char *fpath, const char *fork, imgtool_stream *sourcef, option_resolution *writeoptions) |
| 5658 | 5660 | { |
| 5659 | 5661 | imgtool_image *img = imgtool_partition_image(partition); |
| 5660 | | mac_l2_imgref *image = get_imgref(img); |
| 5662 | struct mac_l2_imgref *image = get_imgref(img); |
| 5661 | 5663 | UINT32 parID; |
| 5662 | 5664 | mac_str255 filename; |
| 5663 | 5665 | mac_dirent cat_info; |
| r17925 | r17926 | |
| 5739 | 5741 | mac_dirent cat_info; |
| 5740 | 5742 | int fork_num = 0; |
| 5741 | 5743 | imgtool_image *img = imgtool_partition_image(partition); |
| 5742 | | mac_l2_imgref *image = get_imgref(img); |
| 5744 | struct mac_l2_imgref *image = get_imgref(img); |
| 5743 | 5745 | |
| 5744 | 5746 | /* resolve path and fetch file info from directory/catalog */ |
| 5745 | 5747 | err = mac_lookup_path(image, path, &parID, filename, &cat_info, FALSE); |
| r17925 | r17926 | |
| 5776 | 5778 | UINT32 parID; |
| 5777 | 5779 | mac_str255 filename; |
| 5778 | 5780 | mac_dirent cat_info; |
| 5779 | | mac_l2_imgref *image = get_imgref(img); |
| 5781 | struct mac_l2_imgref *image = get_imgref(img); |
| 5780 | 5782 | int i; |
| 5781 | 5783 | |
| 5782 | 5784 | /* resolve path and fetch file info from directory/catalog */ |
| r17925 | r17926 | |
| 5844 | 5846 | mac_str255 filename; |
| 5845 | 5847 | mac_dirent cat_info; |
| 5846 | 5848 | imgtool_image *img = imgtool_partition_image(partition); |
| 5847 | | mac_l2_imgref *image = get_imgref(img); |
| 5849 | struct mac_l2_imgref *image = get_imgref(img); |
| 5848 | 5850 | int i; |
| 5849 | 5851 | |
| 5850 | 5852 | /* resolve path and fetch file info from directory/catalog */ |
| r17925 | r17926 | |
| 6317 | 6319 | mac_str255 filename; |
| 6318 | 6320 | mac_dirent cat_info; |
| 6319 | 6321 | imgtool_image *img = imgtool_partition_image(partition); |
| 6320 | | mac_l2_imgref *image = get_imgref(img); |
| 6322 | struct mac_l2_imgref *image = get_imgref(img); |
| 6321 | 6323 | mac_filecategory_t file_category = MAC_FILECATEGORY_DATA; |
| 6322 | 6324 | |
| 6323 | 6325 | if (path) |