1 /* $NetBSD: libhfs.c,v 1.19 2023/08/11 05:51:34 mrg Exp $ */
2
3 /*-
4 * Copyright (c) 2005, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Yevgeny Binder, Dieter Baron, and Pelle Johansson.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * All functions and variable types have the prefix "hfs_". All constants
34 * have the prefix "HFS_".
35 *
36 * Naming convention for functions which read/write raw, linear data
37 * into/from a structured form:
38 *
39 * hfs_read/write[d][a]_foo_bar
40 * [d] - read/write from/to [d]isk instead of a memory buffer
41 * [a] - [a]llocate output buffer instead of using an existing one
42 * (not applicable for writing functions)
43 *
44 * Most functions do not have either of these options, so they will read from
45 * or write to a memory buffer, which has been previously allocated by the
46 * caller.
47 */
48
49 #include <sys/cdefs.h>
50 __KERNEL_RCSID(0, "$NetBSD: libhfs.c,v 1.19 2023/08/11 05:51:34 mrg Exp $");
51
52 #include "libhfs.h"
53
54 /* global private file/folder keys */
55 hfs_catalog_key_t hfs_gMetadataDirectoryKey; /* contains HFS+ inodes */
56 hfs_catalog_key_t hfs_gJournalInfoBlockFileKey;
57 hfs_catalog_key_t hfs_gJournalBufferFileKey;
58 hfs_catalog_key_t* hfs_gPrivateObjectKeys[4] = {
59 &hfs_gMetadataDirectoryKey,
60 &hfs_gJournalInfoBlockFileKey,
61 &hfs_gJournalBufferFileKey,
62 NULL
63 };
64
65
66 extern uint16_t be16tohp(void** inout_ptr);
67 extern uint32_t be32tohp(void** inout_ptr);
68 extern uint64_t be64tohp(void** inout_ptr);
69
70 hfs_callbacks hfs_gcb; /* global callbacks */
71
72 /*
73 * global case folding table
74 * (lazily initialized; see comments at bottom of hfs_open_volume())
75 */
76 unichar_t* hfs_gcft;
77
78
79 int hfslib_create_casefolding_table(void);
80
81 #ifdef DLO_DEBUG
82 #include <stdio.h>
83 void
dlo_print_key(hfs_catalog_key_t * key)84 dlo_print_key(hfs_catalog_key_t *key)
85 {
86 int i;
87
88 printf("%ld:[", (long)key->parent_cnid);
89 for (i=0; i<key->name.length; i++) {
90 if (key->name.unicode[i] < 256
91 && isprint(key->name.unicode[i]))
92 putchar(key->name.unicode[i]);
93 else
94 printf("<%04x>", key->name.unicode[i]);
95 }
96 printf("]");
97 }
98 #endif
99
100 void
hfslib_init(hfs_callbacks * in_callbacks)101 hfslib_init(hfs_callbacks* in_callbacks)
102 {
103 unichar_t temp[256];
104
105 if (in_callbacks != NULL)
106 memcpy(&hfs_gcb, in_callbacks, sizeof(hfs_callbacks));
107
108 hfs_gcft = NULL;
109
110 /*
111 * Create keys for the HFS+ "private" files so we can reuse them whenever
112 * we perform a user-visible operation, such as listing directory contents.
113 */
114
115 #define ATOU(str, len) /* quick & dirty ascii-to-unicode conversion */ \
116 do{ int i; for(i=0; i<len; i++) temp[i]=str[i]; } \
117 while( /*CONSTCOND*/ 0)
118
119 ATOU("\0\0\0\0HFS+ Private Data", 21);
120 hfslib_make_catalog_key(HFS_CNID_ROOT_FOLDER, 21, temp,
121 &hfs_gMetadataDirectoryKey);
122
123 ATOU(".journal_info_block", 19);
124 hfslib_make_catalog_key(HFS_CNID_ROOT_FOLDER, 19, temp,
125 &hfs_gJournalInfoBlockFileKey);
126
127 ATOU(".journal", 8);
128 hfslib_make_catalog_key(HFS_CNID_ROOT_FOLDER, 8, temp,
129 &hfs_gJournalBufferFileKey);
130
131 #undef ATOU
132 }
133
134 void
hfslib_done(void)135 hfslib_done(void)
136 {
137 hfs_callback_args cbargs;
138
139 if (hfs_gcft != NULL) {
140 hfslib_init_cbargs(&cbargs);
141 hfslib_free(hfs_gcft, &cbargs);
142 hfs_gcft = NULL;
143 }
144
145 return;
146 }
147
148 void
hfslib_init_cbargs(hfs_callback_args * ptr)149 hfslib_init_cbargs(hfs_callback_args* ptr)
150 {
151 memset(ptr, 0, sizeof(hfs_callback_args));
152 }
153
154 #if 0
155 #pragma mark -
156 #pragma mark High-Level Routines
157 #endif
158
159 int
hfslib_open_volume(const char * in_device,int in_readonly,hfs_volume * out_vol,hfs_callback_args * cbargs)160 hfslib_open_volume(
161 const char* in_device,
162 int in_readonly,
163 hfs_volume* out_vol,
164 hfs_callback_args* cbargs)
165 {
166 hfs_catalog_key_t rootkey;
167 hfs_thread_record_t rootthread;
168 hfs_hfs_master_directory_block_t mdb;
169 uint16_t node_rec_sizes[1];
170 void* node_recs[1];
171 void* buffer;
172 void* buffer2; /* used as temporary pointer for realloc() */
173 int result;
174 int isopen = 0;
175
176 result = 1;
177 buffer = NULL;
178
179 if (in_device == NULL || out_vol == NULL)
180 return 1;
181
182 out_vol->readonly = in_readonly;
183 out_vol->offset = 0;
184
185 if (hfslib_openvoldevice(out_vol, in_device, cbargs) != 0)
186 HFS_LIBERR("could not open device");
187 isopen = 1;
188
189 /*
190 * Read the volume header.
191 */
192 buffer = hfslib_malloc(max(sizeof(hfs_volume_header_t),
193 sizeof(hfs_hfs_master_directory_block_t)), cbargs);
194 if (buffer == NULL)
195 HFS_LIBERR("could not allocate volume header");
196 if (hfslib_readd(out_vol, buffer, max(sizeof(hfs_volume_header_t),
197 sizeof(hfs_hfs_master_directory_block_t)),
198 HFS_VOLUME_HEAD_RESERVE_SIZE, cbargs) != 0)
199 HFS_LIBERR("could not read volume header");
200
201 if (be16toh(*((uint16_t *)buffer)) == HFS_SIG_HFS) {
202 if (hfslib_read_master_directory_block(buffer, &mdb) == 0)
203 HFS_LIBERR("could not parse master directory block");
204 if (mdb.embedded_signature == HFS_SIG_HFSP) {
205 /* XXX: is 512 always correct? */
206 out_vol->offset =
207 mdb.first_block * 512
208 + mdb.embedded_extent.start_block
209 * (uint64_t)mdb.block_size;
210
211 if (hfslib_readd(out_vol, buffer,
212 sizeof(hfs_volume_header_t),
213 HFS_VOLUME_HEAD_RESERVE_SIZE, cbargs) != 0)
214 HFS_LIBERR("could not read volume header");
215 } else
216 HFS_LIBERR("Plain HFS volumes not currently supported");
217 }
218
219 if (hfslib_read_volume_header(buffer, &(out_vol->vh)) == 0)
220 HFS_LIBERR("could not parse volume header");
221
222 /*
223 * Check the volume signature to see if this is a legitimate HFS+ or HFSX
224 * volume. If so, set the key comparison function pointers appropriately.
225 */
226 switch(out_vol->vh.signature) {
227 case HFS_SIG_HFSP:
228 out_vol->keycmp = hfslib_compare_catalog_keys_cf;
229 break;
230 case HFS_SIG_HFSX:
231 out_vol->keycmp = NULL; /* will be set below */
232 break;
233 default:
234 /* HFS_LIBERR("unrecognized volume format"); */
235 goto error;
236 break;
237 }
238
239 /*
240 * Read the catalog header.
241 */
242 buffer2 = hfslib_realloc(buffer, 512, cbargs);
243 if (buffer2 == NULL)
244 HFS_LIBERR("could not allocate catalog header node");
245 buffer = buffer2;
246
247 /*
248 * We are only interested in the node header, so read the first
249 * 512 bytes and construct the node descriptor by hand.
250 */
251 if (hfslib_readd(out_vol, buffer, 512,
252 out_vol->vh.catalog_file.extents[0].start_block *
253 (uint64_t)out_vol->vh.block_size, cbargs) != 0)
254 HFS_LIBERR("could not read catalog header node");
255 node_recs[0] = (char *)buffer+14;
256 node_rec_sizes[0] = 120;
257 if (hfslib_read_header_node(node_recs, node_rec_sizes, 1,
258 &out_vol->chr, NULL, NULL) == 0)
259 HFS_LIBERR("could not parse catalog header node");
260
261 /*
262 * If this is an HFSX volume, the catalog header specifies the type of
263 * key comparison method (case-folding or binary compare) we should
264 * use.
265 */
266 if (out_vol->keycmp == NULL) {
267 if (out_vol->chr.keycomp_type == HFS_KEY_CASEFOLD)
268 out_vol->keycmp = hfslib_compare_catalog_keys_cf;
269 else if (out_vol->chr.keycomp_type == HFS_KEY_BINARY)
270 out_vol->keycmp = hfslib_compare_catalog_keys_bc;
271 else
272 HFS_LIBERR("undefined key compare method");
273 }
274
275 out_vol->catkeysizefieldsize
276 = (out_vol->chr.attributes & HFS_BIG_KEYS_MASK) ?
277 sizeof(uint16_t) : sizeof(uint8_t);
278
279 /*
280 * Read the extent overflow header.
281 */
282 /*
283 * We are only interested in the node header, so read the first
284 * 512 bytes and construct the node descriptor by hand.
285 * buffer is already 512 bytes long.
286 */
287 if (hfslib_readd(out_vol, buffer, 512,
288 out_vol->vh.extents_file.extents[0].start_block *
289 (uint64_t)out_vol->vh.block_size, cbargs) != 0)
290 HFS_LIBERR("could not read extent header node");
291
292 node_recs[0] = (char *)buffer+14;
293 node_rec_sizes[0] = 120;
294 if (hfslib_read_header_node(node_recs, node_rec_sizes, 1,
295 &out_vol->ehr, NULL, NULL) == 0)
296 HFS_LIBERR("could not parse extent header node");
297 out_vol->extkeysizefieldsize
298 = (out_vol->ehr.attributes & HFS_BIG_KEYS_MASK) ?
299 sizeof(uint16_t):sizeof(uint8_t);
300 /*
301 * Read the journal info block and journal header (if volume journaled).
302 */
303 if (out_vol->vh.attributes & (1<<HFS_VOL_JOURNALED)) {
304 /* journal info block */
305 buffer2 = hfslib_realloc(buffer, sizeof(hfs_journal_info_t), cbargs);
306 if (buffer2 == NULL)
307 HFS_LIBERR("could not allocate journal info block");
308 buffer = buffer2;
309
310 if (hfslib_readd(out_vol, buffer, sizeof(hfs_journal_info_t),
311 out_vol->vh.journal_info_block * out_vol->vh.block_size,
312 cbargs) != 0)
313 HFS_LIBERR("could not read journal info block");
314
315 if (hfslib_read_journal_info(buffer, &out_vol->jib) == 0)
316 HFS_LIBERR("could not parse journal info block");
317
318 /* journal header */
319 buffer2 = hfslib_realloc(buffer, sizeof(hfs_journal_header_t), cbargs);
320 if (buffer2 == NULL)
321 HFS_LIBERR("could not allocate journal header");
322 buffer = buffer2;
323
324 if (hfslib_readd(out_vol, buffer, sizeof(hfs_journal_header_t),
325 out_vol->jib.offset, cbargs) != 0)
326 HFS_LIBERR("could not read journal header");
327
328 if (hfslib_read_journal_header(buffer, &out_vol->jh) == 0)
329 HFS_LIBERR("could not parse journal header");
330
331 out_vol->journaled = 1;
332 } else {
333 out_vol->journaled = 0;
334 }
335
336 /*
337 * If this volume uses case-folding comparison and the folding table hasn't
338 * been created yet, do that here. (We don't do this in hfslib_init()
339 * because the table is large and we might never even need to use it.)
340 */
341 if (out_vol->keycmp == hfslib_compare_catalog_keys_cf && hfs_gcft == NULL)
342 result = hfslib_create_casefolding_table();
343 else
344 result = 0;
345
346 /*
347 * Find and store the volume name.
348 */
349 if (hfslib_make_catalog_key(HFS_CNID_ROOT_FOLDER, 0, NULL, &rootkey) == 0)
350 HFS_LIBERR("could not make root search key");
351
352 if (hfslib_find_catalog_record_with_key(out_vol, &rootkey,
353 (hfs_catalog_keyed_record_t*)&rootthread, cbargs)!=0)
354 HFS_LIBERR("could not find root parent");
355
356 memcpy(&out_vol->name, &rootthread.name, sizeof(hfs_unistr255_t));
357
358 /* FALLTHROUGH */
359 error:
360 if (result != 0 && isopen)
361 hfslib_close_volume(out_vol, cbargs);
362 if (buffer != NULL)
363 hfslib_free(buffer, cbargs);
364 return result;
365 }
366
367 void
hfslib_close_volume(hfs_volume * in_vol,hfs_callback_args * cbargs)368 hfslib_close_volume(hfs_volume* in_vol, hfs_callback_args* cbargs)
369 {
370 if (in_vol == NULL)
371 return;
372 hfslib_closevoldevice(in_vol, cbargs);
373 }
374
375 int
hfslib_path_to_cnid(hfs_volume * in_vol,hfs_cnid_t in_cnid,char ** out_unicode,uint16_t * out_length,hfs_callback_args * cbargs)376 hfslib_path_to_cnid(hfs_volume* in_vol,
377 hfs_cnid_t in_cnid,
378 char** out_unicode,
379 uint16_t* out_length,
380 hfs_callback_args* cbargs)
381 {
382 hfs_thread_record_t parent_thread;
383 hfs_cnid_t parent_cnid, child_cnid;
384 char* newpath;
385 char* path;
386 int path_offset = 0;
387 int result;
388 uint16_t* ptr; /* dummy var */
389 uint16_t uchar; /* dummy var */
390 uint16_t total_path_length;
391
392 if (in_vol == NULL || in_cnid == 0 || out_unicode == NULL ||
393 out_length == NULL)
394 return 1;
395
396 result = 1;
397 *out_unicode = NULL;
398 *out_length = 0;
399 path = NULL;
400 total_path_length = 0;
401
402 path = hfslib_malloc(514, cbargs); /* 256 unichars plus a forward slash */
403 if (path == NULL)
404 return 1;
405
406 child_cnid = in_cnid;
407 parent_cnid = child_cnid; /* skips loop in case in_cnid is root id */
408 while (parent_cnid != HFS_CNID_ROOT_FOLDER &&
409 parent_cnid != HFS_CNID_ROOT_PARENT)
410 {
411 if (child_cnid != in_cnid) {
412 newpath = hfslib_realloc(path, 514 + total_path_length*2, cbargs);
413 if (newpath == NULL)
414 goto exit;
415 path = newpath;
416 memmove(path + 514, path + path_offset, total_path_length*2);
417 }
418
419 parent_cnid = hfslib_find_parent_thread(in_vol, child_cnid,
420 &parent_thread, cbargs);
421 if (parent_cnid == 0)
422 goto exit;
423
424 path_offset = 512 - parent_thread.name.length*2;
425
426 memcpy(path + path_offset, parent_thread.name.unicode,
427 parent_thread.name.length*2);
428
429 /* Add a forward slash. The unicode string was specified in big endian
430 * format, so convert to core format if necessary. */
431 path[512] = 0x00;
432 path[513] = 0x2F;
433
434 ptr = (uint16_t*)path + 256;
435 uchar = be16tohp((void*)&ptr);
436 *(ptr-1) = uchar;
437
438 total_path_length += parent_thread.name.length + 1;
439 child_cnid = parent_cnid;
440 }
441
442 /*
443 * At this point, 'path' holds a sequence of unicode characters which
444 * represent the absolute path to the given cnid. This string is missing
445 * a terminating null char and an initial forward slash that represents
446 * the root of the filesystem. It most likely also has extra space in
447 * the beginning, due to the fact that we reserve 512 bytes for each path
448 * component and won't usually use all that space. So, we allocate the
449 * final string based on the actual length of the absolute path, plus four
450 * additional bytes (two unichars) for the forward slash and the null char.
451 */
452
453 *out_unicode = hfslib_malloc((total_path_length+2)*2, cbargs);
454 if (*out_unicode == NULL)
455 goto exit;
456
457 /* copy only the bytes that are actually used */
458 memcpy(*out_unicode + 2, path + path_offset, total_path_length*2);
459
460 /* insert forward slash at start */
461 uchar = be16toh(0x2F);
462 memcpy(*out_unicode, &uchar, sizeof(uchar));
463
464 /* insert null char at end */
465 (*out_unicode)[total_path_length*2+2] = 0x00;
466 (*out_unicode)[total_path_length*2+3] = 0x00;
467
468 *out_length = total_path_length + 1 /* extra for forward slash */ ;
469
470 result = 0;
471
472 exit:
473 if (path != NULL)
474 hfslib_free(path, cbargs);
475 return result;
476 }
477
478 hfs_cnid_t
hfslib_find_parent_thread(hfs_volume * in_vol,hfs_cnid_t in_child,hfs_thread_record_t * out_thread,hfs_callback_args * cbargs)479 hfslib_find_parent_thread(
480 hfs_volume* in_vol,
481 hfs_cnid_t in_child,
482 hfs_thread_record_t* out_thread,
483 hfs_callback_args* cbargs)
484 {
485 hfs_catalog_key_t childkey;
486
487 if (in_vol == NULL || in_child == 0 || out_thread == NULL)
488 return 0;
489
490 if (hfslib_make_catalog_key(in_child, 0, NULL, &childkey) == 0)
491 return 0;
492
493 if (hfslib_find_catalog_record_with_key(in_vol, &childkey,
494 (hfs_catalog_keyed_record_t*)out_thread, cbargs) != 0)
495 return 0;
496
497 return out_thread->parent_cnid;
498 }
499
500 /*
501 * hfslib_find_catalog_record_with_cnid()
502 *
503 * Looks up a catalog record by calling hfslib_find_parent_thread() and
504 * hfslib_find_catalog_record_with_key(). out_key may be NULL; if not, the key
505 * corresponding to this cnid is stuffed in it. Returns 0 on success.
506 */
507 int
hfslib_find_catalog_record_with_cnid(hfs_volume * in_vol,hfs_cnid_t in_cnid,hfs_catalog_keyed_record_t * out_rec,hfs_catalog_key_t * out_key,hfs_callback_args * cbargs)508 hfslib_find_catalog_record_with_cnid(
509 hfs_volume* in_vol,
510 hfs_cnid_t in_cnid,
511 hfs_catalog_keyed_record_t* out_rec,
512 hfs_catalog_key_t* out_key,
513 hfs_callback_args* cbargs)
514 {
515 hfs_cnid_t parentcnid;
516 hfs_thread_record_t parentthread;
517 hfs_catalog_key_t key;
518
519 if (in_vol == NULL || in_cnid == 0 || out_rec == NULL)
520 return 0;
521
522 parentcnid =
523 hfslib_find_parent_thread(in_vol, in_cnid, &parentthread, cbargs);
524 if (parentcnid == 0)
525 HFS_LIBERR("could not find parent thread for cnid %i", in_cnid);
526
527 if (hfslib_make_catalog_key(parentthread.parent_cnid,
528 parentthread.name.length, parentthread.name.unicode, &key) == 0)
529 HFS_LIBERR("could not make catalog search key");
530
531 if (out_key != NULL)
532 memcpy(out_key, &key, sizeof(key));
533
534 return hfslib_find_catalog_record_with_key(in_vol, &key, out_rec, cbargs);
535
536 error:
537 return 1;
538 }
539
540 /* Returns 0 on success, 1 on error, and -1 if record was not found. */
541 int
hfslib_find_catalog_record_with_key(hfs_volume * in_vol,hfs_catalog_key_t * in_key,hfs_catalog_keyed_record_t * out_rec,hfs_callback_args * cbargs)542 hfslib_find_catalog_record_with_key(
543 hfs_volume* in_vol,
544 hfs_catalog_key_t* in_key,
545 hfs_catalog_keyed_record_t* out_rec,
546 hfs_callback_args* cbargs)
547 {
548 hfs_node_descriptor_t nd = { .num_recs = 0 };
549 hfs_extent_descriptor_t* extents;
550 hfs_catalog_keyed_record_t lastrec;
551 hfs_catalog_key_t* curkey;
552 void** recs;
553 void* buffer;
554 uint64_t bytesread;
555 uint32_t curnode;
556 uint16_t* recsizes;
557 uint16_t numextents;
558 uint16_t recnum;
559 int16_t leaftype;
560 int keycompare;
561 int result;
562
563 if (in_key == NULL || out_rec == NULL || in_vol == NULL)
564 return 1;
565
566 result = 1;
567 buffer = NULL;
568 curkey = NULL;
569 extents = NULL;
570 recs = NULL;
571 recsizes = NULL;
572
573 /* The key takes up over half a kb of ram, which is a lot for the BSD
574 * kernel stack. So allocate it in the heap instead to play it safe. */
575 curkey = hfslib_malloc(sizeof(hfs_catalog_key_t), cbargs);
576 if (curkey == NULL)
577 HFS_LIBERR("could not allocate catalog search key");
578
579 buffer = hfslib_malloc(in_vol->chr.node_size, cbargs);
580 if (buffer == NULL)
581 HFS_LIBERR("could not allocate node buffer");
582
583 numextents = hfslib_get_file_extents(in_vol, HFS_CNID_CATALOG,
584 HFS_DATAFORK, &extents, cbargs);
585 if (numextents == 0)
586 HFS_LIBERR("could not locate fork extents");
587
588 curnode = in_vol->chr.root_node;
589
590 #ifdef DLO_DEBUG
591 printf("-> key ");
592 dlo_print_key(in_key);
593 printf("\n");
594 #endif
595
596 do {
597 #ifdef DLO_DEBUG
598 printf("--> node %d\n", curnode);
599 #endif
600
601 if (hfslib_readd_with_extents(in_vol, buffer,
602 &bytesread,in_vol->chr.node_size, curnode * in_vol->chr.node_size,
603 extents, numextents, cbargs) != 0)
604 HFS_LIBERR("could not read catalog node #%i", curnode);
605
606 if (hfslib_reada_node(buffer, &nd, &recs, &recsizes, HFS_CATALOG_FILE,
607 in_vol, cbargs) == 0)
608 HFS_LIBERR("could not parse catalog node #%i", curnode);
609
610 for (recnum = 0; recnum < nd.num_recs; recnum++)
611 {
612 leaftype = nd.kind;
613 if (hfslib_read_catalog_keyed_record(recs[recnum], out_rec,
614 &leaftype, curkey, in_vol) == 0)
615 HFS_LIBERR("could not read catalog record #%i",recnum);
616
617 #ifdef DLO_DEBUG
618 printf("---> record %d: ", recnum);
619 dlo_print_key(curkey);
620 fflush(stdout);
621 #endif
622 keycompare = in_vol->keycmp(in_key, curkey);
623 #ifdef DLO_DEBUG
624 printf(" %c\n",
625 keycompare < 0 ? '<'
626 : keycompare == 0 ? '=' : '>');
627 #endif
628
629 if (keycompare < 0) {
630 /* Check if key is less than *every* record, which should never
631 * happen if the volume is consistent and the key legit. */
632 if (recnum == 0)
633 HFS_LIBERR("all records greater than key");
634
635 /* Otherwise, we've found the first record that exceeds our key,
636 * so retrieve the previous record, which is still less... */
637 memcpy(out_rec, &lastrec,
638 sizeof(hfs_catalog_keyed_record_t));
639
640 /* ...unless this is a leaf node, which means we've gone from
641 * a key which is smaller than the search key, in the previous
642 * loop, to a key which is larger, in this loop, and that
643 * implies that our search key does not exist on the volume. */
644 if (nd.kind == HFS_LEAFNODE)
645 result = -1;
646 break;
647 } else if (keycompare == 0) {
648 /* If leaf node, found an exact match. */
649 result = 0;
650 break;
651 } else if (recnum == nd.num_recs-1 && keycompare > 0) {
652 /* If leaf node, we've reached the last record with no match,
653 * which means this key is not present on the volume. */
654 result = -1;
655 break;
656 }
657
658 memcpy(&lastrec, out_rec, sizeof(hfs_catalog_keyed_record_t));
659 }
660
661 if (nd.kind == HFS_INDEXNODE)
662 curnode = out_rec->child;
663 else if (nd.kind == HFS_LEAFNODE)
664 break;
665 hfslib_free_recs(&recs, &recsizes, &nd.num_recs, cbargs);
666 } while (nd.kind != HFS_LEAFNODE);
667
668 /* FALLTHROUGH */
669 error:
670 if (extents != NULL)
671 hfslib_free(extents, cbargs);
672 hfslib_free_recs(&recs, &recsizes, &nd.num_recs, cbargs);
673 if (curkey != NULL)
674 hfslib_free(curkey, cbargs);
675 if (buffer != NULL)
676 hfslib_free(buffer, cbargs);
677 return result;
678 }
679
680 /* returns 0 on success */
681 /* XXX Need to look this over and make sure it gracefully handles cases where
682 * XXX the key is not found. */
683 int
hfslib_find_extent_record_with_key(hfs_volume * in_vol,hfs_extent_key_t * in_key,hfs_extent_record_t * out_rec,hfs_callback_args * cbargs)684 hfslib_find_extent_record_with_key(hfs_volume* in_vol,
685 hfs_extent_key_t* in_key,
686 hfs_extent_record_t* out_rec,
687 hfs_callback_args* cbargs)
688 {
689 hfs_node_descriptor_t nd = { .num_recs = 0 };
690 hfs_extent_descriptor_t* extents;
691 hfs_extent_record_t lastrec;
692 hfs_extent_key_t curkey;
693 void** recs;
694 void* buffer;
695 uint64_t bytesread;
696 uint32_t curnode;
697 uint16_t* recsizes;
698 uint16_t numextents;
699 uint16_t recnum;
700 int keycompare;
701 int result;
702
703 if (in_vol == NULL || in_key == NULL || out_rec == NULL)
704 return 1;
705
706 result = 1;
707 buffer = NULL;
708 extents = NULL;
709 recs = NULL;
710 recsizes = NULL;
711
712 buffer = hfslib_malloc(in_vol->ehr.node_size, cbargs);
713 if (buffer == NULL)
714 HFS_LIBERR("could not allocate node buffer");
715
716 numextents = hfslib_get_file_extents(in_vol, HFS_CNID_EXTENTS,
717 HFS_DATAFORK, &extents, cbargs);
718 if (numextents == 0)
719 HFS_LIBERR("could not locate fork extents");
720
721 nd.num_recs = 0;
722 curnode = in_vol->ehr.root_node;
723
724 do {
725 hfslib_free_recs(&recs, &recsizes, &nd.num_recs, cbargs);
726 recnum = 0;
727
728 if (hfslib_readd_with_extents(in_vol, buffer, &bytesread,
729 in_vol->ehr.node_size, curnode * in_vol->ehr.node_size, extents,
730 numextents, cbargs) != 0)
731 HFS_LIBERR("could not read extents overflow node #%i", curnode);
732
733 if (hfslib_reada_node(buffer, &nd, &recs, &recsizes, HFS_EXTENTS_FILE,
734 in_vol, cbargs) == 0)
735 HFS_LIBERR("could not parse extents overflow node #%i",curnode);
736
737 for (recnum = 0; recnum < nd.num_recs; recnum++) {
738 memcpy(&lastrec, out_rec, sizeof(hfs_extent_record_t));
739
740 if (hfslib_read_extent_record(recs[recnum], out_rec, nd.kind,
741 &curkey, in_vol) == 0)
742 HFS_LIBERR("could not read extents record #%i",recnum);
743
744 keycompare = hfslib_compare_extent_keys(in_key, &curkey);
745 if (keycompare < 0) {
746 /* this should never happen for any legitimate key */
747 if (recnum == 0)
748 return 1;
749 memcpy(out_rec, &lastrec, sizeof(hfs_extent_record_t));
750 break;
751 } else if (keycompare == 0 ||
752 (recnum == nd.num_recs-1 && keycompare > 0))
753 break;
754 }
755
756 if (nd.kind == HFS_INDEXNODE)
757 curnode = *((uint32_t *)out_rec); /* out_rec is a node ptr in this case */
758 else if (nd.kind == HFS_LEAFNODE)
759 break;
760 else
761 HFS_LIBERR("unknown node type for extents overflow node #%i",curnode);
762 } while (nd.kind != HFS_LEAFNODE);
763
764 result = 0;
765
766 /* FALLTHROUGH */
767
768 error:
769 if (buffer != NULL)
770 hfslib_free(buffer, cbargs);
771 if (extents != NULL)
772 hfslib_free(extents, cbargs);
773 hfslib_free_recs(&recs, &recsizes, &nd.num_recs, cbargs);
774 return result;
775 }
776
777 /* out_extents may be NULL. */
778 uint16_t
hfslib_get_file_extents(hfs_volume * in_vol,hfs_cnid_t in_cnid,uint8_t in_forktype,hfs_extent_descriptor_t ** out_extents,hfs_callback_args * cbargs)779 hfslib_get_file_extents(hfs_volume* in_vol,
780 hfs_cnid_t in_cnid,
781 uint8_t in_forktype,
782 hfs_extent_descriptor_t** out_extents,
783 hfs_callback_args* cbargs)
784 {
785 hfs_extent_descriptor_t* dummy;
786 hfs_extent_key_t extentkey;
787 hfs_file_record_t file;
788 hfs_catalog_key_t filekey;
789 hfs_thread_record_t fileparent;
790 hfs_fork_t fork = {.logical_size = 0};
791 hfs_extent_record_t nextextentrec;
792 uint32_t numblocks;
793 uint16_t numextents, n;
794
795 if (in_vol == NULL || in_cnid == 0)
796 return 0;
797
798 if (out_extents != NULL) {
799 *out_extents = hfslib_malloc(sizeof(hfs_extent_descriptor_t), cbargs);
800 if (*out_extents == NULL)
801 return 0;
802 }
803
804 switch(in_cnid)
805 {
806 case HFS_CNID_CATALOG:
807 fork = in_vol->vh.catalog_file;
808 break;
809
810 case HFS_CNID_EXTENTS:
811 fork = in_vol->vh.extents_file;
812 break;
813
814 case HFS_CNID_ALLOCATION:
815 fork = in_vol->vh.allocation_file;
816 break;
817
818 case HFS_CNID_ATTRIBUTES:
819 fork = in_vol->vh.attributes_file;
820 break;
821
822 case HFS_CNID_STARTUP:
823 fork = in_vol->vh.startup_file;
824 break;
825
826 default:
827 if (hfslib_find_parent_thread(in_vol, in_cnid, &fileparent,
828 cbargs) == 0)
829 goto error;
830
831 if (hfslib_make_catalog_key(fileparent.parent_cnid,
832 fileparent.name.length, fileparent.name.unicode, &filekey) == 0)
833 goto error;
834
835 if (hfslib_find_catalog_record_with_key(in_vol, &filekey,
836 (hfs_catalog_keyed_record_t*)&file, cbargs) != 0)
837 goto error;
838
839 /* only files have extents, not folders or threads */
840 if (file.rec_type != HFS_REC_FILE)
841 goto error;
842
843 if (in_forktype == HFS_DATAFORK)
844 fork = file.data_fork;
845 else if (in_forktype == HFS_RSRCFORK)
846 fork = file.rsrc_fork;
847 }
848
849 numextents = 0;
850 numblocks = 0;
851 memcpy(&nextextentrec, &fork.extents, sizeof(hfs_extent_record_t));
852
853 while (1) {
854 for (n = 0; n < 8; n++) {
855 if (nextextentrec[n].block_count == 0)
856 break;
857 numblocks += nextextentrec[n].block_count;
858 }
859 if (out_extents != NULL) {
860 dummy = hfslib_realloc(*out_extents,
861 (numextents+n) * sizeof(hfs_extent_descriptor_t),
862 cbargs);
863 if (dummy == NULL)
864 goto error;
865 *out_extents = dummy;
866
867 memcpy(*out_extents + numextents,
868 &nextextentrec, n*sizeof(hfs_extent_descriptor_t));
869 }
870 numextents += n;
871
872 if (numblocks >= fork.total_blocks)
873 break;
874
875 if (hfslib_make_extent_key(in_cnid, in_forktype, numblocks,
876 &extentkey) == 0)
877 goto error;
878
879 if (hfslib_find_extent_record_with_key(in_vol, &extentkey,
880 &nextextentrec, cbargs) != 0)
881 goto error;
882 }
883
884 goto exit;
885
886 error:
887 if (out_extents != NULL && *out_extents != NULL) {
888 hfslib_free(*out_extents, cbargs);
889 *out_extents = NULL;
890 }
891 return 0;
892
893 exit:
894 return numextents;
895 }
896
897 /*
898 * hfslib_get_directory_contents()
899 *
900 * Finds the immediate children of a given directory CNID and places their
901 * CNIDs in an array allocated here. The first child is found by doing a
902 * catalog search that only compares parent CNIDs (ignoring file/folder names)
903 * and skips over thread records. Then the remaining children are listed in
904 * ascending order by name, according to the HFS+ spec, so just read off each
905 * successive leaf node until a different parent CNID is found.
906 *
907 * If out_childnames is not NULL, it will be allocated and set to an array of
908 * hfs_unistr255_t's which correspond to the name of the child with that same
909 * index.
910 *
911 * out_children may be NULL.
912 *
913 * Returns 0 on success.
914 */
915 int
hfslib_get_directory_contents(hfs_volume * in_vol,hfs_cnid_t in_dir,hfs_catalog_keyed_record_t ** out_children,hfs_unistr255_t ** out_childnames,uint32_t * out_numchildren,hfs_callback_args * cbargs)916 hfslib_get_directory_contents(
917 hfs_volume* in_vol,
918 hfs_cnid_t in_dir,
919 hfs_catalog_keyed_record_t** out_children,
920 hfs_unistr255_t** out_childnames,
921 uint32_t* out_numchildren,
922 hfs_callback_args* cbargs)
923 {
924 hfs_node_descriptor_t nd = { .num_recs = 0 };
925 hfs_extent_descriptor_t* extents;
926 hfs_catalog_keyed_record_t currec;
927 hfs_catalog_key_t curkey;
928 void** recs;
929 void* buffer;
930 void* ptr; /* temporary pointer for realloc() */
931 uint64_t bytesread;
932 uint32_t curnode;
933 uint32_t lastnode;
934 uint16_t* recsizes;
935 uint16_t numextents;
936 uint16_t recnum;
937 int16_t leaftype;
938 int keycompare;
939 int result;
940
941 if (in_vol == NULL || in_dir == 0 || out_numchildren == NULL)
942 return 1;
943
944 result = 1;
945 buffer = NULL;
946 extents = NULL;
947 lastnode = 0;
948 recs = NULL;
949 recsizes = NULL;
950 *out_numchildren = 0;
951 if (out_children != NULL)
952 *out_children = NULL;
953 if (out_childnames != NULL)
954 *out_childnames = NULL;
955
956 buffer = hfslib_malloc(in_vol->chr.node_size, cbargs);
957 if (buffer == NULL)
958 HFS_LIBERR("could not allocate node buffer");
959
960 numextents = hfslib_get_file_extents(in_vol, HFS_CNID_CATALOG,
961 HFS_DATAFORK, &extents, cbargs);
962 if (numextents == 0)
963 HFS_LIBERR("could not locate fork extents");
964
965 nd.num_recs = 0;
966 curnode = in_vol->chr.root_node;
967
968 while (1)
969 {
970 hfslib_free_recs(&recs, &recsizes, &nd.num_recs, cbargs);
971 recnum = 0;
972
973 if (hfslib_readd_with_extents(in_vol, buffer, &bytesread,
974 in_vol->chr.node_size, curnode * in_vol->chr.node_size, extents,
975 numextents, cbargs) != 0)
976 HFS_LIBERR("could not read catalog node #%i", curnode);
977
978 if (hfslib_reada_node(buffer, &nd, &recs, &recsizes, HFS_CATALOG_FILE,
979 in_vol, cbargs) == 0)
980 HFS_LIBERR("could not parse catalog node #%i", curnode);
981
982 for (recnum = 0; recnum < nd.num_recs; recnum++)
983 {
984 leaftype = nd.kind; /* needed b/c leaftype might be modified now */
985 if (hfslib_read_catalog_keyed_record(recs[recnum], &currec,
986 &leaftype, &curkey, in_vol) == 0)
987 HFS_LIBERR("could not read cat record %i:%i", curnode, recnum);
988
989 if (nd.kind == HFS_INDEXNODE)
990 {
991 keycompare = in_dir - curkey.parent_cnid;
992 if (keycompare < 0) {
993 /* Check if key is less than *every* record, which should
994 * never happen if the volume and key are good. */
995 if (recnum == 0)
996 HFS_LIBERR("all records greater than key");
997
998 /* Otherwise, we've found the first record that exceeds our
999 * key, so retrieve the previous, lesser record. */
1000 curnode = lastnode;
1001 break;
1002 } else if (keycompare == 0) {
1003 /*
1004 * Normally, if we were doing a typical catalog lookup with
1005 * both a parent cnid AND a name, keycompare==0 would be an
1006 * exact match. However, since we are ignoring object names
1007 * in this case and only comparing parent cnids, a direct
1008 * match on only a parent cnid could mean that we've found
1009 * an object with that parent cnid BUT which is NOT the
1010 * first object (according to the HFS+ spec) with that
1011 * parent cnid. Thus, when we find a parent cnid match, we
1012 * still go back to the previously found leaf node and start
1013 * checking it for a possible prior instance of an object
1014 * with our desired parent cnid.
1015 */
1016 curnode = lastnode;
1017 break;
1018 } else if (recnum == nd.num_recs-1 && keycompare > 0) {
1019 /* Descend to child node if we found an exact match, or if
1020 * this is the last pointer record. */
1021 curnode = currec.child;
1022 break;
1023 }
1024
1025 lastnode = currec.child;
1026 } else {
1027 /*
1028 * We have now descended down the hierarchy of index nodes into
1029 * the leaf node that contains the first catalog record with a
1030 * matching parent CNID. Since all leaf nodes are chained
1031 * through their flink/blink, we can simply walk forward through
1032 * this chain, copying every matching non-thread record, until
1033 * we hit a record with a different parent CNID. At that point,
1034 * we've retrieved all of our directory's items, if any.
1035 */
1036 curnode = nd.flink;
1037
1038 if (curkey.parent_cnid < in_dir) {
1039 continue;
1040 } else if (curkey.parent_cnid == in_dir) {
1041 /* Hide files/folders which are supposed to be invisible
1042 * to users, according to the hfs+ spec. */
1043 if (hfslib_is_private_file(&curkey))
1044 continue;
1045
1046 /* leaftype has now been set to the catalog record type */
1047 if (leaftype == HFS_REC_FLDR || leaftype == HFS_REC_FILE)
1048 {
1049 (*out_numchildren)++;
1050
1051 if (out_children != NULL) {
1052 ptr = hfslib_realloc(*out_children,
1053 *out_numchildren *
1054 sizeof(hfs_catalog_keyed_record_t), cbargs);
1055 if (ptr == NULL)
1056 HFS_LIBERR("could not allocate child record");
1057 *out_children = ptr;
1058
1059 memcpy(&((*out_children)[*out_numchildren-1]),
1060 &currec, sizeof(hfs_catalog_keyed_record_t));
1061 }
1062
1063 if (out_childnames != NULL) {
1064 ptr = hfslib_realloc(*out_childnames,
1065 *out_numchildren * sizeof(hfs_unistr255_t),
1066 cbargs);
1067 if (ptr == NULL)
1068 HFS_LIBERR("could not allocate child name");
1069 *out_childnames = ptr;
1070
1071 memcpy(&((*out_childnames)[*out_numchildren-1]),
1072 &curkey.name, sizeof(hfs_unistr255_t));
1073 }
1074 }
1075 } else {
1076 result = 0;
1077 /* We have just now passed the last item in the desired
1078 * folder (or the folder was empty), so exit. */
1079 goto exit;
1080 }
1081 }
1082 }
1083 }
1084
1085 result = 0;
1086 goto exit;
1087
1088 error:
1089 if (out_children != NULL && *out_children != NULL)
1090 hfslib_free(*out_children, cbargs);
1091 if (out_childnames != NULL && *out_childnames != NULL)
1092 hfslib_free(*out_childnames, cbargs);
1093 /* FALLTHROUGH */
1094
1095 exit:
1096 if (extents != NULL)
1097 hfslib_free(extents, cbargs);
1098 hfslib_free_recs(&recs, &recsizes, &nd.num_recs, cbargs);
1099 if (buffer != NULL)
1100 hfslib_free(buffer, cbargs);
1101 return result;
1102 }
1103
1104 int
hfslib_is_journal_clean(hfs_volume * in_vol)1105 hfslib_is_journal_clean(hfs_volume* in_vol)
1106 {
1107 if (in_vol == NULL)
1108 return 0;
1109
1110 /* return true if no journal */
1111 if (!(in_vol->vh.attributes & (1<<HFS_VOL_JOURNALED)))
1112 return 1;
1113
1114 return (in_vol->jh.start == in_vol->jh.end);
1115 }
1116
1117 /*
1118 * hfslib_is_private_file()
1119 *
1120 * Given a file/folder's key and parent CNID, determines if it should be hidden
1121 * from the user (e.g., the journal header file or the HFS+ Private Data folder)
1122 */
1123 int
hfslib_is_private_file(hfs_catalog_key_t * filekey)1124 hfslib_is_private_file(hfs_catalog_key_t *filekey)
1125 {
1126 hfs_catalog_key_t* curkey = NULL;
1127 int i = 0;
1128
1129 /*
1130 * According to the HFS+ spec to date, all special objects are located in
1131 * the root directory of the volume, so don't bother going further if the
1132 * requested object is not.
1133 */
1134 if (filekey->parent_cnid != HFS_CNID_ROOT_FOLDER)
1135 return 0;
1136
1137 while ((curkey = hfs_gPrivateObjectKeys[i]) != NULL) {
1138 /* XXX Always use binary compare here, or use volume's specific key
1139 * XXX comparison routine? */
1140 if (filekey->name.length == curkey->name.length &&
1141 memcmp(filekey->name.unicode, curkey->name.unicode,
1142 2 * curkey->name.length) == 0)
1143 return 1;
1144 i++;
1145 }
1146
1147 return 0;
1148 }
1149
1150
1151 /* bool
1152 hfslib_is_journal_valid(hfs_volume* in_vol)
1153 {
1154 - check magic numbers
1155 - check Other Things
1156 }*/
1157
1158 #if 0
1159 #pragma mark -
1160 #pragma mark Major Structures
1161 #endif
1162
1163 /*
1164 * hfslib_read_volume_header()
1165 *
1166 * Reads in_bytes, formats the data appropriately, and places the result
1167 * in out_header, which is assumed to be previously allocated. Returns number
1168 * of bytes read, 0 if failed.
1169 */
1170
1171 size_t
hfslib_read_volume_header(void * in_bytes,hfs_volume_header_t * out_header)1172 hfslib_read_volume_header(void* in_bytes, hfs_volume_header_t* out_header)
1173 {
1174 void* ptr;
1175 size_t last_bytes_read;
1176 int i;
1177
1178 if (in_bytes == NULL || out_header == NULL)
1179 return 0;
1180
1181 ptr = in_bytes;
1182
1183 out_header->signature = be16tohp(&ptr);
1184 out_header->version = be16tohp(&ptr);
1185 out_header->attributes = be32tohp(&ptr);
1186 out_header->last_mounting_version = be32tohp(&ptr);
1187 out_header->journal_info_block = be32tohp(&ptr);
1188
1189 out_header->date_created = be32tohp(&ptr);
1190 out_header->date_modified = be32tohp(&ptr);
1191 out_header->date_backedup = be32tohp(&ptr);
1192 out_header->date_checked = be32tohp(&ptr);
1193
1194 out_header->file_count = be32tohp(&ptr);
1195 out_header->folder_count = be32tohp(&ptr);
1196
1197 out_header->block_size = be32tohp(&ptr);
1198 out_header->total_blocks = be32tohp(&ptr);
1199 out_header->free_blocks = be32tohp(&ptr);
1200 out_header->next_alloc_block = be32tohp(&ptr);
1201 out_header->rsrc_clump_size = be32tohp(&ptr);
1202 out_header->data_clump_size = be32tohp(&ptr);
1203 out_header->next_cnid = be32tohp(&ptr);
1204
1205 out_header->write_count = be32tohp(&ptr);
1206 out_header->encodings = be64tohp(&ptr);
1207
1208 for (i =0 ; i < 8; i++)
1209 out_header->finder_info[i] = be32tohp(&ptr);
1210
1211 if ((last_bytes_read = hfslib_read_fork_descriptor(ptr,
1212 &out_header->allocation_file)) == 0)
1213 return 0;
1214 ptr = (uint8_t*)ptr + last_bytes_read;
1215
1216 if ((last_bytes_read = hfslib_read_fork_descriptor(ptr,
1217 &out_header->extents_file)) == 0)
1218 return 0;
1219 ptr = (uint8_t*)ptr + last_bytes_read;
1220
1221 if ((last_bytes_read = hfslib_read_fork_descriptor(ptr,
1222 &out_header->catalog_file)) == 0)
1223 return 0;
1224 ptr = (uint8_t*)ptr + last_bytes_read;
1225
1226 if ((last_bytes_read = hfslib_read_fork_descriptor(ptr,
1227 &out_header->attributes_file)) == 0)
1228 return 0;
1229 ptr = (uint8_t*)ptr + last_bytes_read;
1230
1231 if ((last_bytes_read = hfslib_read_fork_descriptor(ptr,
1232 &out_header->startup_file)) == 0)
1233 return 0;
1234 ptr = (uint8_t*)ptr + last_bytes_read;
1235
1236 return ((uint8_t*)ptr - (uint8_t*)in_bytes);
1237 }
1238
1239 /*
1240 * hfsplib_read_master_directory_block()
1241 *
1242 * Reads in_bytes, formats the data appropriately, and places the result
1243 * in out_header, which is assumed to be previously allocated. Returns numb
1244 er
1245 * of bytes read, 0 if failed.
1246 */
1247
1248 size_t
hfslib_read_master_directory_block(void * in_bytes,hfs_hfs_master_directory_block_t * out_mdr)1249 hfslib_read_master_directory_block(void* in_bytes,
1250 hfs_hfs_master_directory_block_t* out_mdr)
1251 {
1252 void* ptr;
1253 int i;
1254
1255 if (in_bytes == NULL || out_mdr == NULL)
1256 return 0;
1257
1258 ptr = in_bytes;
1259
1260 out_mdr->signature = be16tohp(&ptr);
1261
1262 out_mdr->date_created = be32tohp(&ptr);
1263 out_mdr->date_modified = be32tohp(&ptr);
1264
1265 out_mdr->attributes = be16tohp(&ptr);
1266 out_mdr->root_file_count = be16tohp(&ptr);
1267 out_mdr->volume_bitmap = be16tohp(&ptr);
1268
1269 out_mdr->next_alloc_block = be16tohp(&ptr);
1270 out_mdr->total_blocks = be16tohp(&ptr);
1271 out_mdr->block_size = be32tohp(&ptr);
1272
1273 out_mdr->clump_size = be32tohp(&ptr);
1274 out_mdr->first_block = be16tohp(&ptr);
1275 out_mdr->next_cnid = be32tohp(&ptr);
1276 out_mdr->free_blocks = be16tohp(&ptr);
1277
1278 memcpy(out_mdr->volume_name, ptr, 28);
1279 ptr = (char *)ptr + 28;
1280
1281 out_mdr->date_backedup = be32tohp(&ptr);
1282 out_mdr->backup_seqnum = be16tohp(&ptr);
1283
1284 out_mdr->write_count = be32tohp(&ptr);
1285
1286 out_mdr->extents_clump_size = be32tohp(&ptr);
1287 out_mdr->catalog_clump_size = be32tohp(&ptr);
1288
1289 out_mdr->root_folder_count = be16tohp(&ptr);
1290 out_mdr->file_count = be32tohp(&ptr);
1291 out_mdr->folder_count = be32tohp(&ptr);
1292
1293 for (i = 0; i < 8; i++)
1294 out_mdr->finder_info[i] = be32tohp(&ptr);
1295
1296 out_mdr->embedded_signature = be16tohp(&ptr);
1297 out_mdr->embedded_extent.start_block = be16tohp(&ptr);
1298 out_mdr->embedded_extent.block_count = be16tohp(&ptr);
1299
1300 out_mdr->extents_size = be32tohp(&ptr);
1301 for (i = 0; i < 3; i++) {
1302 out_mdr->extents_extents[i].start_block = be16tohp(&ptr);
1303 out_mdr->extents_extents[i].block_count = be16tohp(&ptr);
1304 }
1305
1306 out_mdr->catalog_size = be32tohp(&ptr);
1307 for (i = 0; i < 3; i++) {
1308 out_mdr->catalog_extents[i].start_block = be16tohp(&ptr);
1309 out_mdr->catalog_extents[i].block_count = be16tohp(&ptr);
1310 }
1311
1312 return ((uint8_t*)ptr - (uint8_t*)in_bytes);
1313 }
1314
1315 /*
1316 * hfslib_reada_node()
1317 *
1318 * Given the pointer to and size of a buffer containing the entire, raw
1319 * contents of any b-tree node from the disk, this function will:
1320 *
1321 * 1. determine the type of node and read its contents
1322 * 2. allocate memory for each record and fill it appropriately
1323 * 3. set out_record_ptrs_array to point to an array (which it allocates)
1324 * which has out_node_descriptor->num_recs many pointers to the
1325 * records themselves
1326 * 4. allocate out_record_ptr_sizes_array and fill it with the sizes of
1327 * each record
1328 * 5. return the number of bytes read (i.e., the size of the node)
1329 * or 0 on failure
1330 *
1331 * out_node_descriptor must be allocated by the caller and may not be NULL.
1332 *
1333 * out_record_ptrs_array and out_record_ptr_sizes_array must both be specified,
1334 * or both be NULL if the caller is not interested in reading the records.
1335 *
1336 * out_record_ptr_sizes_array may be NULL if the caller is not interested in
1337 * reading the records, but must not be NULL if out_record_ptrs_array is not.
1338 *
1339 * in_parent_file is HFS_CATALOG_FILE, HFS_EXTENTS_FILE, or
1340 * HFS_ATTRIBUTES_FILE, depending on the special file in which this node
1341 * resides.
1342 *
1343 * inout_volume must have its catnodesize or extnodesize field (depending on
1344 * the parent file) set to the correct value if this is an index, leaf, or map
1345 * node. If this is a header node, the field will be set to its correct value.
1346 */
1347 size_t
hfslib_reada_node(void * in_bytes,hfs_node_descriptor_t * out_node_descriptor,void ** out_record_ptrs_array[],uint16_t * out_record_ptr_sizes_array[],hfs_btree_file_type in_parent_file,hfs_volume * inout_volume,hfs_callback_args * cbargs)1348 hfslib_reada_node(void* in_bytes,
1349 hfs_node_descriptor_t* out_node_descriptor,
1350 void** out_record_ptrs_array[],
1351 uint16_t* out_record_ptr_sizes_array[],
1352 hfs_btree_file_type in_parent_file,
1353 hfs_volume* inout_volume,
1354 hfs_callback_args* cbargs)
1355 {
1356 void* ptr;
1357 uint16_t* rec_offsets;
1358 size_t last_bytes_read;
1359 uint16_t nodesize;
1360 uint16_t numrecords;
1361 uint16_t free_space_offset; /* offset to free space in node */
1362 int keysizefieldsize;
1363 int i;
1364
1365 numrecords = 0;
1366 rec_offsets = NULL;
1367 if (out_record_ptrs_array != NULL)
1368 *out_record_ptrs_array = NULL;
1369 if (out_record_ptr_sizes_array != NULL)
1370 *out_record_ptr_sizes_array = NULL;
1371
1372 if (in_bytes == NULL || inout_volume == NULL || out_node_descriptor == NULL
1373 || (out_record_ptrs_array == NULL && out_record_ptr_sizes_array != NULL)
1374 || (out_record_ptrs_array != NULL && out_record_ptr_sizes_array == NULL) )
1375 goto error;
1376
1377 ptr = in_bytes;
1378
1379 out_node_descriptor->flink = be32tohp(&ptr);
1380 out_node_descriptor->blink = be32tohp(&ptr);
1381 out_node_descriptor->kind = *(((int8_t*)ptr));
1382 ptr = (uint8_t*)ptr + 1;
1383 out_node_descriptor->height = *(((uint8_t*)ptr));
1384 ptr = (uint8_t*)ptr + 1;
1385 out_node_descriptor->num_recs = be16tohp(&ptr);
1386 out_node_descriptor->reserved = be16tohp(&ptr);
1387
1388 numrecords = out_node_descriptor->num_recs;
1389
1390 /*
1391 * To go any further, we will need to know the size of this node, as well
1392 * as the width of keyed records' key_len parameters for this btree. If
1393 * this is an index, leaf, or map node, inout_volume already has the node
1394 * size set in its catnodesize or extnodesize field and the key length set
1395 * in the catkeysizefieldsize or extkeysizefieldsize for catalog files and
1396 * extent files, respectively. However, if this is a header node, this
1397 * information has not yet been determined, so this is the place to do it.
1398 */
1399 if (out_node_descriptor->kind == HFS_HEADERNODE)
1400 {
1401 hfs_header_record_t hr;
1402 void* header_rec_offset[1];
1403 uint16_t header_rec_size[1];
1404
1405 /* sanity check to ensure this is a good header node */
1406 if (numrecords != 3)
1407 HFS_LIBERR("header node does not have exactly 3 records");
1408
1409 header_rec_offset[0] = ptr;
1410 header_rec_size[0] = sizeof(hfs_header_record_t);
1411
1412 last_bytes_read = hfslib_read_header_node(header_rec_offset,
1413 header_rec_size, 1, &hr, NULL, NULL);
1414 if (last_bytes_read == 0)
1415 HFS_LIBERR("could not read header node");
1416
1417 switch(in_parent_file)
1418 {
1419 case HFS_CATALOG_FILE:
1420 inout_volume->chr.node_size = hr.node_size;
1421 inout_volume->catkeysizefieldsize =
1422 (hr.attributes & HFS_BIG_KEYS_MASK) ?
1423 sizeof(uint16_t):sizeof(uint8_t);
1424 break;
1425
1426 case HFS_EXTENTS_FILE:
1427 inout_volume->ehr.node_size = hr.node_size;
1428 inout_volume->extkeysizefieldsize =
1429 (hr.attributes & HFS_BIG_KEYS_MASK) ?
1430 sizeof(uint16_t):sizeof(uint8_t);
1431 break;
1432
1433 case HFS_ATTRIBUTES_FILE:
1434 default:
1435 HFS_LIBERR("invalid parent file type specified");
1436 /* NOTREACHED */
1437 }
1438 }
1439
1440 switch (in_parent_file)
1441 {
1442 case HFS_CATALOG_FILE:
1443 nodesize = inout_volume->chr.node_size;
1444 keysizefieldsize = inout_volume->catkeysizefieldsize;
1445 break;
1446
1447 case HFS_EXTENTS_FILE:
1448 nodesize = inout_volume->ehr.node_size;
1449 keysizefieldsize = inout_volume->extkeysizefieldsize;
1450 break;
1451
1452 case HFS_ATTRIBUTES_FILE:
1453 default:
1454 HFS_LIBERR("invalid parent file type specified");
1455 /* NOTREACHED */
1456 }
1457
1458 /*
1459 * Don't care about records so just exit after getting the node descriptor.
1460 * Note: This happens after the header node code, and not before it, in
1461 * case the caller calls this function and ignores the record data just to
1462 * get at the node descriptor, but then tries to call it again on a non-
1463 * header node without first setting inout_volume->cat/extnodesize.
1464 */
1465 if (out_record_ptrs_array == NULL)
1466 return ((uint8_t*)ptr - (uint8_t*)in_bytes);
1467
1468 rec_offsets = hfslib_malloc(numrecords * sizeof(uint16_t), cbargs);
1469 *out_record_ptr_sizes_array =
1470 hfslib_malloc(numrecords * sizeof(uint16_t), cbargs);
1471 if (rec_offsets == NULL || *out_record_ptr_sizes_array == NULL)
1472 HFS_LIBERR("could not allocate node record offsets");
1473
1474 *out_record_ptrs_array = hfslib_malloc(numrecords * sizeof(void*), cbargs);
1475 if (*out_record_ptrs_array == NULL)
1476 HFS_LIBERR("could not allocate node records");
1477
1478 last_bytes_read = hfslib_reada_node_offsets((uint8_t*)in_bytes + nodesize -
1479 numrecords * sizeof(uint16_t), rec_offsets, numrecords);
1480 if (last_bytes_read == 0)
1481 HFS_LIBERR("could not read node record offsets");
1482
1483 /* The size of the last record (i.e. the first one listed in the offsets)
1484 * must be determined using the offset to the node's free space. */
1485 free_space_offset = be16toh(*(uint16_t*)((uint8_t*)in_bytes + nodesize -
1486 (numrecords+1) * sizeof(uint16_t)));
1487
1488 (*out_record_ptr_sizes_array)[numrecords-1] =
1489 free_space_offset - rec_offsets[0];
1490 for (i = 1; i < numrecords; i++) {
1491 (*out_record_ptr_sizes_array)[numrecords-i-1] =
1492 rec_offsets[i-1] - rec_offsets[i];
1493 }
1494
1495 for (i = 0; i < numrecords; i++)
1496 {
1497 (*out_record_ptrs_array)[i] =
1498 hfslib_malloc((*out_record_ptr_sizes_array)[i], cbargs);
1499
1500 if ((*out_record_ptrs_array)[i] == NULL)
1501 HFS_LIBERR("could not allocate node record #%i",i);
1502
1503 /*
1504 * If this is a keyed node (i.e., a leaf or index node), there are two
1505 * boundary rules that each record must obey:
1506 *
1507 * 1. A pad byte must be placed between the key and data if the
1508 * size of the key plus the size of the key_len field is odd.
1509 *
1510 * 2. A pad byte must be placed after the data if the data size
1511 * is odd.
1512 *
1513 * So in the first case we increment the starting point of the data
1514 * and correspondingly decrement the record size. In the second case
1515 * we decrement the record size.
1516 */
1517 if (out_node_descriptor->kind == HFS_LEAFNODE ||
1518 out_node_descriptor->kind == HFS_INDEXNODE)
1519 {
1520 hfs_catalog_key_t reckey;
1521 uint16_t rectype;
1522
1523 rectype = out_node_descriptor->kind;
1524 last_bytes_read = hfslib_read_catalog_keyed_record(ptr, NULL,
1525 &rectype, &reckey, inout_volume);
1526 if (last_bytes_read == 0)
1527 HFS_LIBERR("could not read node record");
1528
1529 if ((reckey.key_len + keysizefieldsize) % 2 == 1) {
1530 ptr = (uint8_t*)ptr + 1;
1531 (*out_record_ptr_sizes_array)[i]--;
1532 }
1533
1534 if ((*out_record_ptr_sizes_array)[i] % 2 == 1)
1535 (*out_record_ptr_sizes_array)[i]--;
1536 }
1537
1538 memcpy((*out_record_ptrs_array)[i], ptr,
1539 (*out_record_ptr_sizes_array)[i]);
1540 ptr = (uint8_t*)ptr + (*out_record_ptr_sizes_array)[i];
1541 }
1542
1543 goto exit;
1544
1545 error:
1546 hfslib_free_recs(out_record_ptrs_array, out_record_ptr_sizes_array,
1547 &numrecords, cbargs);
1548
1549 ptr = in_bytes;
1550
1551 /* warn("error occurred in hfslib_reada_node()"); */
1552
1553 /* FALLTHROUGH */
1554
1555 exit:
1556 if (rec_offsets != NULL)
1557 hfslib_free(rec_offsets, cbargs);
1558 return ((uint8_t*)ptr - (uint8_t*)in_bytes);
1559 }
1560
1561 /*
1562 * hfslib_reada_node_offsets()
1563 *
1564 * Sets out_offset_array to contain the offsets to each record in the node,
1565 * in reverse order. Does not read the free space offset.
1566 */
1567 size_t
hfslib_reada_node_offsets(void * in_bytes,uint16_t * out_offset_array,uint16_t numrecords)1568 hfslib_reada_node_offsets(void* in_bytes, uint16_t* out_offset_array,
1569 uint16_t numrecords)
1570 {
1571 void* ptr;
1572
1573 if (in_bytes == NULL || out_offset_array == NULL)
1574 return 0;
1575
1576 ptr = in_bytes;
1577
1578 /*
1579 * The offset for record 0 (which is the very last offset in the node) is
1580 * always equal to 14, the size of the node descriptor. So, once we hit
1581 * offset=14, we know this is the last offset. In this way, we don't need
1582 * to know the number of records beforehand.
1583 */
1584 do {
1585 if (numrecords-- == 0)
1586 return 0;
1587 *out_offset_array = be16tohp(&ptr);
1588 } while (*out_offset_array++ != (uint16_t)14);
1589
1590 return ((uint8_t*)ptr - (uint8_t*)in_bytes);
1591 }
1592
1593 /* hfslib_read_header_node()
1594 *
1595 * out_header_record and/or out_map_record may be NULL if the caller doesn't
1596 * care about their contents.
1597 */
1598 size_t
hfslib_read_header_node(void ** in_recs,uint16_t * in_rec_sizes,uint16_t in_num_recs,hfs_header_record_t * out_hr,void * out_userdata,void * out_map)1599 hfslib_read_header_node(void** in_recs,
1600 uint16_t* in_rec_sizes,
1601 uint16_t in_num_recs,
1602 hfs_header_record_t* out_hr,
1603 void* out_userdata,
1604 void* out_map)
1605 {
1606 void* ptr;
1607 int i;
1608
1609 KASSERT(out_hr != NULL);
1610
1611 if (in_recs == NULL || in_rec_sizes == NULL)
1612 return 0;
1613
1614 ptr = in_recs[0];
1615 out_hr->tree_depth = be16tohp(&ptr);
1616 out_hr->root_node = be32tohp(&ptr);
1617 out_hr->leaf_recs = be32tohp(&ptr);
1618 out_hr->first_leaf = be32tohp(&ptr);
1619 out_hr->last_leaf = be32tohp(&ptr);
1620 out_hr->node_size = be16tohp(&ptr);
1621 out_hr->max_key_len = be16tohp(&ptr);
1622 out_hr->total_nodes = be32tohp(&ptr);
1623 out_hr->free_nodes = be32tohp(&ptr);
1624 out_hr->reserved = be16tohp(&ptr);
1625 out_hr->clump_size = be32tohp(&ptr);
1626 out_hr->btree_type = *(((uint8_t*)ptr));
1627 ptr = (uint8_t*)ptr + 1;
1628 out_hr->keycomp_type = *(((uint8_t*)ptr));
1629 ptr = (uint8_t*)ptr + 1;
1630 out_hr->attributes = be32tohp(&ptr);
1631 for (i = 0; i < 16; i++)
1632 out_hr->reserved2[i] = be32tohp(&ptr);
1633
1634 if (out_userdata != NULL) {
1635 memcpy(out_userdata, in_recs[1], in_rec_sizes[1]);
1636 }
1637 ptr = (uint8_t*)ptr + in_rec_sizes[1]; /* size of user data record */
1638
1639 if (out_map != NULL) {
1640 memcpy(out_map, in_recs[2], in_rec_sizes[2]);
1641 }
1642 ptr = (uint8_t*)ptr + in_rec_sizes[2]; /* size of map record */
1643
1644 return ((uint8_t*)ptr - (uint8_t*)in_recs[0]);
1645 }
1646
1647 /*
1648 * hfslib_read_catalog_keyed_record()
1649 *
1650 * out_recdata can be NULL. inout_rectype must be set to either HFS_LEAFNODE
1651 * or HFS_INDEXNODE upon calling this function, and will be set by the
1652 * function to one of HFS_REC_FLDR, HFS_REC_FILE, HFS_REC_FLDR_THREAD, or
1653 * HFS_REC_FLDR_THREAD upon return if the node is a leaf node. If it is an
1654 * index node, inout_rectype will not be changed.
1655 */
1656 size_t
hfslib_read_catalog_keyed_record(void * in_bytes,hfs_catalog_keyed_record_t * out_recdata,int16_t * inout_rectype,hfs_catalog_key_t * out_key,hfs_volume * in_volume)1657 hfslib_read_catalog_keyed_record(
1658 void* in_bytes,
1659 hfs_catalog_keyed_record_t* out_recdata,
1660 int16_t* inout_rectype,
1661 hfs_catalog_key_t* out_key,
1662 hfs_volume* in_volume)
1663 {
1664 void* ptr;
1665 size_t last_bytes_read;
1666
1667 if (in_bytes == NULL || out_key == NULL || inout_rectype == NULL)
1668 return 0;
1669
1670 ptr = in_bytes;
1671
1672 /* For HFS+, the key length is always a 2-byte number. This is indicated
1673 * by the HFS_BIG_KEYS_MASK bit in the attributes field of the catalog
1674 * header record. However, we just assume this bit is set, since all HFS+
1675 * volumes should have it set anyway. */
1676 if (in_volume->catkeysizefieldsize == sizeof(uint16_t))
1677 out_key->key_len = be16tohp(&ptr);
1678 else if (in_volume->catkeysizefieldsize == sizeof(uint8_t)) {
1679 out_key->key_len = *(((uint8_t*)ptr));
1680 ptr = (uint8_t*)ptr + 1;
1681 }
1682
1683 out_key->parent_cnid = be32tohp(&ptr);
1684
1685 last_bytes_read = hfslib_read_unistr255(ptr, &out_key->name);
1686 if (last_bytes_read == 0)
1687 return 0;
1688 ptr = (uint8_t*)ptr + last_bytes_read;
1689
1690 /* don't waste time if the user just wanted the key and/or record type */
1691 if (out_recdata == NULL) {
1692 if (*inout_rectype == HFS_LEAFNODE)
1693 *inout_rectype = be16tohp(&ptr);
1694 else if (*inout_rectype != HFS_INDEXNODE)
1695 return 0; /* should not happen if we were given valid arguments */
1696
1697 return ((uint8_t*)ptr - (uint8_t*)in_bytes);
1698 }
1699
1700 if (*inout_rectype == HFS_INDEXNODE) {
1701 out_recdata->child = be32tohp(&ptr);
1702 } else {
1703 /* first need to determine what kind of record this is */
1704 *inout_rectype = be16tohp(&ptr);
1705 out_recdata->type = *inout_rectype;
1706
1707 switch(out_recdata->type)
1708 {
1709 case HFS_REC_FLDR:
1710 {
1711 out_recdata->folder.flags = be16tohp(&ptr);
1712 out_recdata->folder.valence = be32tohp(&ptr);
1713 out_recdata->folder.cnid = be32tohp(&ptr);
1714 out_recdata->folder.date_created = be32tohp(&ptr);
1715 out_recdata->folder.date_content_mod = be32tohp(&ptr);
1716 out_recdata->folder.date_attrib_mod = be32tohp(&ptr);
1717 out_recdata->folder.date_accessed = be32tohp(&ptr);
1718 out_recdata->folder.date_backedup = be32tohp(&ptr);
1719
1720 last_bytes_read = hfslib_read_bsd_data(ptr,
1721 &out_recdata->folder.bsd);
1722 if (last_bytes_read == 0)
1723 return 0;
1724 ptr = (uint8_t*)ptr + last_bytes_read;
1725
1726 last_bytes_read = hfslib_read_folder_userinfo(ptr,
1727 &out_recdata->folder.user_info);
1728 if (last_bytes_read == 0)
1729 return 0;
1730 ptr = (uint8_t*)ptr + last_bytes_read;
1731
1732 last_bytes_read = hfslib_read_folder_finderinfo(ptr,
1733 &out_recdata->folder.finder_info);
1734 if (last_bytes_read == 0)
1735 return 0;
1736 ptr = (uint8_t*)ptr + last_bytes_read;
1737
1738 out_recdata->folder.text_encoding = be32tohp(&ptr);
1739 out_recdata->folder.reserved = be32tohp(&ptr);
1740 }
1741 break;
1742
1743 case HFS_REC_FILE:
1744 {
1745 out_recdata->file.flags = be16tohp(&ptr);
1746 out_recdata->file.reserved = be32tohp(&ptr);
1747 out_recdata->file.cnid = be32tohp(&ptr);
1748 out_recdata->file.date_created = be32tohp(&ptr);
1749 out_recdata->file.date_content_mod = be32tohp(&ptr);
1750 out_recdata->file.date_attrib_mod = be32tohp(&ptr);
1751 out_recdata->file.date_accessed = be32tohp(&ptr);
1752 out_recdata->file.date_backedup = be32tohp(&ptr);
1753
1754 last_bytes_read = hfslib_read_bsd_data(ptr,
1755 &out_recdata->file.bsd);
1756 if (last_bytes_read == 0)
1757 return 0;
1758 ptr = (uint8_t*)ptr + last_bytes_read;
1759
1760 last_bytes_read = hfslib_read_file_userinfo(ptr,
1761 &out_recdata->file.user_info);
1762 if (last_bytes_read == 0)
1763 return 0;
1764 ptr = (uint8_t*)ptr + last_bytes_read;
1765
1766 last_bytes_read = hfslib_read_file_finderinfo(ptr,
1767 &out_recdata->file.finder_info);
1768 if (last_bytes_read == 0)
1769 return 0;
1770 ptr = (uint8_t*)ptr + last_bytes_read;
1771
1772 out_recdata->file.text_encoding = be32tohp(&ptr);
1773 out_recdata->file.reserved2 = be32tohp(&ptr);
1774
1775 last_bytes_read = hfslib_read_fork_descriptor(ptr,
1776 &out_recdata->file.data_fork);
1777 if (last_bytes_read == 0)
1778 return 0;
1779 ptr = (uint8_t*)ptr + last_bytes_read;
1780
1781 last_bytes_read = hfslib_read_fork_descriptor(ptr,
1782 &out_recdata->file.rsrc_fork);
1783 if (last_bytes_read == 0)
1784 return 0;
1785 ptr = (uint8_t*)ptr + last_bytes_read;
1786 }
1787 break;
1788
1789 case HFS_REC_FLDR_THREAD:
1790 case HFS_REC_FILE_THREAD:
1791 {
1792 out_recdata->thread.reserved = be16tohp(&ptr);
1793 out_recdata->thread.parent_cnid = be32tohp(&ptr);
1794
1795 last_bytes_read = hfslib_read_unistr255(ptr,
1796 &out_recdata->thread.name);
1797 if (last_bytes_read == 0)
1798 return 0;
1799 ptr = (uint8_t*)ptr + last_bytes_read;
1800 }
1801 break;
1802
1803 default:
1804 return 1;
1805 /* NOTREACHED */
1806 }
1807 }
1808
1809 return ((uint8_t*)ptr - (uint8_t*)in_bytes);
1810 }
1811
1812 /* out_rec may be NULL */
1813 size_t
hfslib_read_extent_record(void * in_bytes,hfs_extent_record_t * out_rec,hfs_node_kind in_nodekind,hfs_extent_key_t * out_key,hfs_volume * in_volume)1814 hfslib_read_extent_record(
1815 void* in_bytes,
1816 hfs_extent_record_t* out_rec,
1817 hfs_node_kind in_nodekind,
1818 hfs_extent_key_t* out_key,
1819 hfs_volume* in_volume)
1820 {
1821 void* ptr;
1822 size_t last_bytes_read;
1823
1824 if (in_bytes == NULL || out_key == NULL
1825 || (in_nodekind!=HFS_LEAFNODE && in_nodekind!=HFS_INDEXNODE))
1826 return 0;
1827
1828 ptr = in_bytes;
1829
1830 /* For HFS+, the key length is always a 2-byte number. This is indicated
1831 * by the HFS_BIG_KEYS_MASK bit in the attributes field of the extent
1832 * overflow header record. However, we just assume this bit is set, since
1833 * all HFS+ volumes should have it set anyway. */
1834 if (in_volume->extkeysizefieldsize == sizeof(uint16_t))
1835 out_key->key_length = be16tohp(&ptr);
1836 else if (in_volume->extkeysizefieldsize == sizeof(uint8_t)) {
1837 out_key->key_length = *(((uint8_t*)ptr));
1838 ptr = (uint8_t*)ptr + 1;
1839 }
1840
1841 out_key->fork_type = *(((uint8_t*)ptr));
1842 ptr = (uint8_t*)ptr + 1;
1843 out_key->padding = *(((uint8_t*)ptr));
1844 ptr = (uint8_t*)ptr + 1;
1845 out_key->file_cnid = be32tohp(&ptr);
1846 out_key->start_block = be32tohp(&ptr);
1847
1848 /* don't waste time if the user just wanted the key */
1849 if (out_rec == NULL)
1850 return ((uint8_t*)ptr - (uint8_t*)in_bytes);
1851
1852 if (in_nodekind == HFS_LEAFNODE) {
1853 last_bytes_read = hfslib_read_extent_descriptors(ptr, out_rec);
1854 if (last_bytes_read == 0)
1855 return 0;
1856 ptr = (uint8_t*)ptr + last_bytes_read;
1857 } else {
1858 /* XXX: this is completely bogus */
1859 /* (uint32_t*)*out_rec = be32tohp(&ptr); */
1860 uint32_t *ptr_32 = (uint32_t *)out_rec;
1861 *ptr_32 = be32tohp(&ptr);
1862 /* (*out_rec)[0].start_block = be32tohp(&ptr); */
1863 }
1864
1865 return ((uint8_t*)ptr - (uint8_t*)in_bytes);
1866 }
1867
1868 void
hfslib_free_recs(void *** inout_node_recs,uint16_t ** inout_rec_sizes,uint16_t * inout_num_recs,hfs_callback_args * cbargs)1869 hfslib_free_recs(
1870 void*** inout_node_recs,
1871 uint16_t** inout_rec_sizes,
1872 uint16_t* inout_num_recs,
1873 hfs_callback_args* cbargs)
1874 {
1875 uint16_t i;
1876
1877 if (inout_num_recs == NULL || *inout_num_recs == 0)
1878 return;
1879
1880 if (inout_node_recs != NULL && *inout_node_recs != NULL) {
1881 for (i = 0 ; i < *inout_num_recs; i++) {
1882 if ((*inout_node_recs)[i] != NULL) {
1883 hfslib_free((*inout_node_recs)[i], cbargs);
1884 (*inout_node_recs)[i] = NULL;
1885 }
1886 }
1887 hfslib_free(*inout_node_recs, cbargs);
1888 *inout_node_recs = NULL;
1889 }
1890
1891 if (inout_rec_sizes != NULL && *inout_rec_sizes != NULL) {
1892 hfslib_free(*inout_rec_sizes, cbargs);
1893 *inout_rec_sizes = NULL;
1894 }
1895
1896 *inout_num_recs = 0;
1897 }
1898
1899 #if 0
1900 #pragma mark -
1901 #pragma mark Individual Fields
1902 #endif
1903
1904 size_t
hfslib_read_fork_descriptor(void * in_bytes,hfs_fork_t * out_forkdata)1905 hfslib_read_fork_descriptor(void* in_bytes, hfs_fork_t* out_forkdata)
1906 {
1907 void* ptr;
1908 size_t last_bytes_read;
1909
1910 if (in_bytes == NULL || out_forkdata == NULL)
1911 return 0;
1912
1913 ptr = in_bytes;
1914
1915 out_forkdata->logical_size = be64tohp(&ptr);
1916 out_forkdata->clump_size = be32tohp(&ptr);
1917 out_forkdata->total_blocks = be32tohp(&ptr);
1918
1919 if ((last_bytes_read = hfslib_read_extent_descriptors(ptr,
1920 &out_forkdata->extents)) == 0)
1921 return 0;
1922 ptr = (uint8_t*)ptr + last_bytes_read;
1923
1924 return ((uint8_t*)ptr - (uint8_t*)in_bytes);
1925 }
1926
1927 size_t
hfslib_read_extent_descriptors(void * in_bytes,hfs_extent_record_t * out_extentrecord)1928 hfslib_read_extent_descriptors(
1929 void* in_bytes,
1930 hfs_extent_record_t* out_extentrecord)
1931 {
1932 void* ptr;
1933 int i;
1934
1935 if (in_bytes == NULL || out_extentrecord == NULL)
1936 return 0;
1937
1938 ptr = in_bytes;
1939
1940 for (i = 0; i < 8; i++) {
1941 (((hfs_extent_descriptor_t*)*out_extentrecord)[i]).start_block =
1942 be32tohp(&ptr);
1943 (((hfs_extent_descriptor_t*)*out_extentrecord)[i]).block_count =
1944 be32tohp(&ptr);
1945 }
1946
1947 return ((uint8_t*)ptr - (uint8_t*)in_bytes);
1948 }
1949
1950 size_t
hfslib_read_unistr255(void * in_bytes,hfs_unistr255_t * out_string)1951 hfslib_read_unistr255(void* in_bytes, hfs_unistr255_t* out_string)
1952 {
1953 void* ptr;
1954 uint16_t i, length;
1955
1956 if (in_bytes == NULL || out_string == NULL)
1957 return 0;
1958
1959 ptr = in_bytes;
1960
1961 length = be16tohp(&ptr);
1962 if (length > 255)
1963 length = 255; /* hfs+ folder/file names have a limit of 255 chars */
1964 out_string->length = length;
1965
1966 for (i = 0; i < length; i++) {
1967 out_string->unicode[i] = be16tohp(&ptr);
1968 }
1969
1970 return ((uint8_t*)ptr - (uint8_t*)in_bytes);
1971 }
1972
1973 size_t
hfslib_read_bsd_data(void * in_bytes,hfs_bsd_data_t * out_perms)1974 hfslib_read_bsd_data(void* in_bytes, hfs_bsd_data_t* out_perms)
1975 {
1976 void* ptr;
1977
1978 if (in_bytes == NULL || out_perms == NULL)
1979 return 0;
1980
1981 ptr = in_bytes;
1982
1983 out_perms->owner_id = be32tohp(&ptr);
1984 out_perms->group_id = be32tohp(&ptr);
1985 out_perms->admin_flags = *(((uint8_t*)ptr));
1986 ptr = (uint8_t*)ptr + 1;
1987 out_perms->owner_flags = *(((uint8_t*)ptr));
1988 ptr = (uint8_t*)ptr + 1;
1989 out_perms->file_mode = be16tohp(&ptr);
1990 out_perms->special.inode_num = be32tohp(&ptr); /* this field is a union */
1991
1992 return ((uint8_t*)ptr - (uint8_t*)in_bytes);
1993 }
1994
1995 size_t
hfslib_read_file_userinfo(void * in_bytes,hfs_macos_file_info_t * out_info)1996 hfslib_read_file_userinfo(void* in_bytes, hfs_macos_file_info_t* out_info)
1997 {
1998 void* ptr;
1999
2000 if (in_bytes == NULL || out_info == NULL)
2001 return 0;
2002
2003 ptr = in_bytes;
2004
2005 out_info->file_type = be32tohp(&ptr);
2006 out_info->file_creator = be32tohp(&ptr);
2007 out_info->finder_flags = be16tohp(&ptr);
2008 out_info->location.v = be16tohp(&ptr);
2009 out_info->location.h = be16tohp(&ptr);
2010 out_info->reserved = be16tohp(&ptr);
2011
2012 return ((uint8_t*)ptr - (uint8_t*)in_bytes);
2013 }
2014
2015 size_t
hfslib_read_file_finderinfo(void * in_bytes,hfs_macos_extended_file_info_t * out_info)2016 hfslib_read_file_finderinfo(
2017 void* in_bytes,
2018 hfs_macos_extended_file_info_t* out_info)
2019 {
2020 void* ptr;
2021
2022 if (in_bytes == NULL || out_info == NULL)
2023 return 0;
2024
2025 ptr = in_bytes;
2026
2027 #if 0
2028 #pragma warn Fill in with real code!
2029 #endif
2030 /* FIXME: Fill in with real code! */
2031 memset(out_info, 0, sizeof(*out_info));
2032 ptr = (uint8_t*)ptr + sizeof(hfs_macos_extended_file_info_t);
2033
2034 return ((uint8_t*)ptr - (uint8_t*)in_bytes);
2035 }
2036
2037 size_t
hfslib_read_folder_userinfo(void * in_bytes,hfs_macos_folder_info_t * out_info)2038 hfslib_read_folder_userinfo(void* in_bytes, hfs_macos_folder_info_t* out_info)
2039 {
2040 void* ptr;
2041
2042 if (in_bytes == NULL || out_info == NULL)
2043 return 0;
2044
2045 ptr = in_bytes;
2046
2047 #if 0
2048 #pragma warn Fill in with real code!
2049 #endif
2050 /* FIXME: Fill in with real code! */
2051 memset(out_info, 0, sizeof(*out_info));
2052 ptr = (uint8_t*)ptr + sizeof(hfs_macos_folder_info_t);
2053
2054 return ((uint8_t*)ptr - (uint8_t*)in_bytes);
2055 }
2056
2057 size_t
hfslib_read_folder_finderinfo(void * in_bytes,hfs_macos_extended_folder_info_t * out_info)2058 hfslib_read_folder_finderinfo(
2059 void* in_bytes,
2060 hfs_macos_extended_folder_info_t* out_info)
2061 {
2062 void* ptr;
2063
2064 if (in_bytes == NULL || out_info == NULL)
2065 return 0;
2066
2067 ptr = in_bytes;
2068
2069 #if 0
2070 #pragma warn Fill in with real code!
2071 #endif
2072 /* FIXME: Fill in with real code! */
2073 memset(out_info, 0, sizeof(*out_info));
2074 ptr = (uint8_t*)ptr + sizeof(hfs_macos_extended_folder_info_t);
2075
2076 return ((uint8_t*)ptr - (uint8_t*)in_bytes);
2077 }
2078
2079 size_t
hfslib_read_journal_info(void * in_bytes,hfs_journal_info_t * out_info)2080 hfslib_read_journal_info(void* in_bytes, hfs_journal_info_t* out_info)
2081 {
2082 void* ptr;
2083 int i;
2084
2085 if (in_bytes == NULL || out_info == NULL)
2086 return 0;
2087
2088 ptr = in_bytes;
2089
2090 out_info->flags = be32tohp(&ptr);
2091 for (i = 0; i < 8; i++) {
2092 out_info->device_signature[i] = be32tohp(&ptr);
2093 }
2094 out_info->offset = be64tohp(&ptr);
2095 out_info->size = be64tohp(&ptr);
2096 for (i = 0; i < 32; i++) {
2097 out_info->reserved[i] = be64tohp(&ptr);
2098 }
2099
2100 return ((uint8_t*)ptr - (uint8_t*)in_bytes);
2101 }
2102
2103 size_t
hfslib_read_journal_header(void * in_bytes,hfs_journal_header_t * out_header)2104 hfslib_read_journal_header(void* in_bytes, hfs_journal_header_t* out_header)
2105 {
2106 void* ptr;
2107
2108 if (in_bytes == NULL || out_header == NULL)
2109 return 0;
2110
2111 ptr = in_bytes;
2112
2113 out_header->magic = be32tohp(&ptr);
2114 out_header->endian = be32tohp(&ptr);
2115 out_header->start = be64tohp(&ptr);
2116 out_header->end = be64tohp(&ptr);
2117 out_header->size = be64tohp(&ptr);
2118 out_header->blocklist_header_size = be32tohp(&ptr);
2119 out_header->checksum = be32tohp(&ptr);
2120 out_header->journal_header_size = be32tohp(&ptr);
2121
2122 return ((uint8_t*)ptr - (uint8_t*)in_bytes);
2123 }
2124
2125 #if 0
2126 #pragma mark -
2127 #pragma mark Disk Access
2128 #endif
2129
2130 /*
2131 * hfslib_readd_with_extents()
2132 *
2133 * This function reads the contents of a file from the volume, given an array
2134 * of extent descriptors which specify where every extent of the file is
2135 * located (in addition to the usual pread() arguments). out_bytes is presumed
2136 * to exist and be large enough to hold in_length number of bytes. Returns 0
2137 * on success.
2138 */
2139 int
hfslib_readd_with_extents(hfs_volume * in_vol,void * out_bytes,uint64_t * out_bytesread,uint64_t in_length,uint64_t in_offset,hfs_extent_descriptor_t in_extents[],uint16_t in_numextents,hfs_callback_args * cbargs)2140 hfslib_readd_with_extents(
2141 hfs_volume* in_vol,
2142 void* out_bytes,
2143 uint64_t* out_bytesread,
2144 uint64_t in_length,
2145 uint64_t in_offset,
2146 hfs_extent_descriptor_t in_extents[],
2147 uint16_t in_numextents,
2148 hfs_callback_args* cbargs)
2149 {
2150 uint64_t ext_length, last_offset;
2151 uint16_t i;
2152 int error;
2153
2154 if (in_vol == NULL || out_bytes == NULL || in_extents == NULL ||
2155 in_numextents == 0 || out_bytesread == NULL)
2156 return -1;
2157
2158 *out_bytesread = 0;
2159 last_offset = 0;
2160
2161 for (i = 0; i < in_numextents; i++)
2162 {
2163 if (in_extents[i].block_count == 0)
2164 continue;
2165
2166 ext_length = in_extents[i].block_count * in_vol->vh.block_size;
2167
2168 if (in_offset < last_offset+ext_length
2169 && in_offset+in_length >= last_offset)
2170 {
2171 uint64_t isect_start, isect_end;
2172
2173 isect_start = max(in_offset, last_offset);
2174 isect_end = min(in_offset+in_length, last_offset+ext_length);
2175 error = hfslib_readd(in_vol, out_bytes, isect_end-isect_start,
2176 isect_start - last_offset + (uint64_t)in_extents[i].start_block
2177 * in_vol->vh.block_size, cbargs);
2178
2179 if (error != 0)
2180 return error;
2181
2182 *out_bytesread += isect_end-isect_start;
2183 out_bytes = (uint8_t*)out_bytes + isect_end-isect_start;
2184 }
2185
2186 last_offset += ext_length;
2187 }
2188
2189 return 0;
2190 }
2191
2192 #if 0
2193 #pragma mark -
2194 #pragma mark Callback Wrappers
2195 #endif
2196
2197 void
hfslib_error(const char * in_format,const char * in_file,int in_line,...)2198 hfslib_error(const char* in_format, const char* in_file, int in_line, ...)
2199 {
2200 va_list ap;
2201
2202 if (in_format == NULL)
2203 return;
2204
2205 if (hfs_gcb.error != NULL) {
2206 va_start(ap, in_line);
2207 hfs_gcb.error(in_format, in_file, in_line, ap);
2208 va_end(ap);
2209 }
2210 }
2211
2212 void*
hfslib_malloc(size_t size,hfs_callback_args * cbargs)2213 hfslib_malloc(size_t size, hfs_callback_args* cbargs)
2214 {
2215 if (hfs_gcb.allocmem != NULL)
2216 return hfs_gcb.allocmem(size, cbargs);
2217
2218 return NULL;
2219 }
2220
2221 void*
hfslib_realloc(void * ptr,size_t size,hfs_callback_args * cbargs)2222 hfslib_realloc(void* ptr, size_t size, hfs_callback_args* cbargs)
2223 {
2224 if (hfs_gcb.reallocmem != NULL)
2225 return hfs_gcb.reallocmem(ptr, size, cbargs);
2226
2227 return NULL;
2228 }
2229
2230 void
hfslib_free(void * ptr,hfs_callback_args * cbargs)2231 hfslib_free(void* ptr, hfs_callback_args* cbargs)
2232 {
2233 if (hfs_gcb.freemem != NULL && ptr != NULL)
2234 hfs_gcb.freemem(ptr, cbargs);
2235 }
2236
2237 int
hfslib_openvoldevice(hfs_volume * in_vol,const char * in_device,hfs_callback_args * cbargs)2238 hfslib_openvoldevice(
2239 hfs_volume* in_vol,
2240 const char* in_device,
2241 hfs_callback_args* cbargs)
2242 {
2243 if (hfs_gcb.openvol != NULL && in_device != NULL)
2244 return hfs_gcb.openvol(in_vol, in_device, cbargs);
2245
2246 return 1;
2247 }
2248
2249 void
hfslib_closevoldevice(hfs_volume * in_vol,hfs_callback_args * cbargs)2250 hfslib_closevoldevice(hfs_volume* in_vol, hfs_callback_args* cbargs)
2251 {
2252 if (hfs_gcb.closevol != NULL)
2253 hfs_gcb.closevol(in_vol, cbargs);
2254 }
2255
2256 int
hfslib_readd(hfs_volume * in_vol,void * out_bytes,uint64_t in_length,uint64_t in_offset,hfs_callback_args * cbargs)2257 hfslib_readd(
2258 hfs_volume* in_vol,
2259 void* out_bytes,
2260 uint64_t in_length,
2261 uint64_t in_offset,
2262 hfs_callback_args* cbargs)
2263 {
2264 if (in_vol == NULL || out_bytes == NULL)
2265 return -1;
2266
2267 if (hfs_gcb.read != NULL)
2268 return hfs_gcb.read(in_vol, out_bytes, in_length, in_offset, cbargs);
2269
2270 return -1;
2271 }
2272
2273 #if 0
2274 #pragma mark -
2275 #pragma mark Other
2276 #endif
2277
2278 /* returns key length */
2279 uint16_t
hfslib_make_catalog_key(hfs_cnid_t in_parent_cnid,uint16_t in_name_len,unichar_t * in_unicode,hfs_catalog_key_t * out_key)2280 hfslib_make_catalog_key(
2281 hfs_cnid_t in_parent_cnid,
2282 uint16_t in_name_len,
2283 unichar_t* in_unicode,
2284 hfs_catalog_key_t* out_key)
2285 {
2286 if (in_parent_cnid == 0 || (in_name_len > 0 && in_unicode == NULL) ||
2287 out_key == 0)
2288 return 0;
2289
2290 if (in_name_len > 255)
2291 in_name_len = 255;
2292
2293 out_key->key_len = 6 + 2 * in_name_len;
2294 out_key->parent_cnid = in_parent_cnid;
2295 out_key->name.length = in_name_len;
2296 if (in_name_len > 0)
2297 memcpy(&out_key->name.unicode, in_unicode, in_name_len*2);
2298
2299 return out_key->key_len;
2300 }
2301
2302 /* returns key length */
2303 uint16_t
hfslib_make_extent_key(hfs_cnid_t in_cnid,uint8_t in_forktype,uint32_t in_startblock,hfs_extent_key_t * out_key)2304 hfslib_make_extent_key(
2305 hfs_cnid_t in_cnid,
2306 uint8_t in_forktype,
2307 uint32_t in_startblock,
2308 hfs_extent_key_t* out_key)
2309 {
2310 if (in_cnid == 0 || out_key == 0)
2311 return 0;
2312
2313 out_key->key_length = HFS_MAX_EXT_KEY_LEN;
2314 out_key->fork_type = in_forktype;
2315 out_key->padding = 0;
2316 out_key->file_cnid = in_cnid;
2317 out_key->start_block = in_startblock;
2318
2319 return out_key->key_length;
2320 }
2321
2322 /* case-folding */
2323 int
hfslib_compare_catalog_keys_cf(const void * ap,const void * bp)2324 hfslib_compare_catalog_keys_cf (
2325 const void *ap,
2326 const void *bp)
2327 {
2328 const hfs_catalog_key_t *a, *b;
2329 unichar_t ac, bc; /* current character from a, b */
2330 unichar_t lc; /* lowercase version of current character */
2331 uint8_t apos, bpos; /* current character indices */
2332
2333 a = (const hfs_catalog_key_t*)ap;
2334 b = (const hfs_catalog_key_t*)bp;
2335
2336 if (a->parent_cnid != b->parent_cnid) {
2337 return (a->parent_cnid - b->parent_cnid);
2338 } else {
2339 /*
2340 * The following code implements the pseudocode suggested by
2341 * the HFS+ technote.
2342 */
2343
2344 /*
2345 * XXX These need to be revised to be endian-independent!
2346 */
2347 #define hbyte(x) ((x) >> 8)
2348 #define lbyte(x) ((x) & 0x00FF)
2349
2350 apos = bpos = 0;
2351 while (1)
2352 {
2353 /* get next valid character from a */
2354 for (lc = 0; lc == 0 && apos < a->name.length; apos++) {
2355 ac = a->name.unicode[apos];
2356 lc = hfs_gcft[hbyte(ac)];
2357 if (lc == 0)
2358 lc = ac;
2359 else
2360 lc = hfs_gcft[lc + lbyte(ac)];
2361 };
2362 ac = lc;
2363
2364 /* get next valid character from b */
2365 for (lc = 0; lc == 0 && bpos < b->name.length; bpos++) {
2366 bc = b->name.unicode[bpos];
2367 lc = hfs_gcft[hbyte(bc)];
2368 if (lc == 0)
2369 lc = bc;
2370 else
2371 lc = hfs_gcft[lc + lbyte(bc)];
2372 };
2373 bc = lc;
2374
2375 /* on end of string ac/bc are 0, otherwise > 0 */
2376 if (ac != bc || (ac == 0 && bc == 0))
2377 return ac - bc;
2378 }
2379 #undef hbyte
2380 #undef lbyte
2381 }
2382 }
2383
2384 /* binary compare (i.e., not case folding) */
2385 int
hfslib_compare_catalog_keys_bc(const void * ap,const void * bp)2386 hfslib_compare_catalog_keys_bc (
2387 const void *ap,
2388 const void *bp)
2389 {
2390 int c;
2391 const hfs_catalog_key_t *a, *b;
2392
2393 a = (const hfs_catalog_key_t *) ap;
2394 b = (const hfs_catalog_key_t *) bp;
2395
2396 if (a->parent_cnid == b->parent_cnid)
2397 {
2398 if (a->name.length == 0 && b->name.length == 0)
2399 return 0;
2400
2401 if (a->name.length == 0)
2402 return -1;
2403 if (b->name.length == 0)
2404 return 1;
2405
2406 /* FIXME: This does a byte-per-byte comparison, whereas the HFS spec
2407 * mandates a uint16_t chunk comparison. */
2408 c = memcmp(a->name.unicode, b->name.unicode,
2409 sizeof(unichar_t)*min(a->name.length, b->name.length));
2410 if (c != 0)
2411 return c;
2412 else
2413 return (a->name.length - b->name.length);
2414 } else {
2415 return (a->parent_cnid - b->parent_cnid);
2416 }
2417 }
2418
2419 int
hfslib_compare_extent_keys(const void * ap,const void * bp)2420 hfslib_compare_extent_keys (
2421 const void *ap,
2422 const void *bp)
2423 {
2424 /*
2425 * Comparison order, in descending importance:
2426 *
2427 * CNID -> fork type -> start block
2428 */
2429
2430 const hfs_extent_key_t *a, *b;
2431 a = (const hfs_extent_key_t *) ap;
2432 b = (const hfs_extent_key_t *) bp;
2433
2434 if (a->file_cnid == b->file_cnid)
2435 {
2436 if (a->fork_type == b->fork_type)
2437 {
2438 if (a->start_block == b->start_block)
2439 {
2440 return 0;
2441 } else {
2442 return (a->start_block - b->start_block);
2443 }
2444 } else {
2445 return (a->fork_type - b->fork_type);
2446 }
2447 } else {
2448 return (a->file_cnid - b->file_cnid);
2449 }
2450 }
2451
2452 /* 1+10 tables of 16 rows and 16 columns, each 2 bytes wide = 5632 bytes */
2453 int
hfslib_create_casefolding_table(void)2454 hfslib_create_casefolding_table(void)
2455 {
2456 hfs_callback_args cbargs;
2457 unichar_t* t; /* convenience */
2458 uint16_t s; /* current subtable * 256 */
2459 uint16_t i; /* current subtable index (0 to 255) */
2460
2461 if (hfs_gcft != NULL)
2462 return 0; /* no sweat, table already exists */
2463
2464 hfslib_init_cbargs(&cbargs);
2465 hfs_gcft = hfslib_malloc(5632, &cbargs);
2466 if (hfs_gcft == NULL)
2467 HFS_LIBERR("could not allocate case folding table");
2468
2469 t = hfs_gcft; /* easier to type :) */
2470
2471 /*
2472 * high byte indices
2473 */
2474 s = 0 * 256;
2475 memset(t, 0x00, 512);
2476 t[s+ 0] = 0x0100;
2477 t[s+ 1] = 0x0200;
2478 t[s+ 3] = 0x0300;
2479 t[s+ 4] = 0x0400;
2480 t[s+ 5] = 0x0500;
2481 t[s+ 16] = 0x0600;
2482 t[s+ 32] = 0x0700;
2483 t[s+ 33] = 0x0800;
2484 t[s+254] = 0x0900;
2485 t[s+255] = 0x0a00;
2486
2487 /*
2488 * table 1 (high byte 0x00)
2489 */
2490 s = 1 * 256;
2491 for (i = 0; i < 65; i++)
2492 t[s+i] = i;
2493 t[s+ 0] = 0xffff;
2494 for (i = 65; i < 91; i++)
2495 t[s+i] = i + 0x20;
2496 for (i = 91; i < 256; i++)
2497 t[s+i] = i;
2498 t[s+198] = 0x00e6;
2499 t[s+208] = 0x00f0;
2500 t[s+216] = 0x00f8;
2501 t[s+222] = 0x00fe;
2502
2503 /*
2504 * table 2 (high byte 0x01)
2505 */
2506 s = 2 * 256;
2507 for (i = 0; i < 256; i++)
2508 t[s+i] = i + 0x0100;
2509 t[s+ 16] = 0x0111;
2510 t[s+ 38] = 0x0127;
2511 t[s+ 50] = 0x0133;
2512 t[s+ 63] = 0x0140;
2513 t[s+ 65] = 0x0142;
2514 t[s+ 74] = 0x014b;
2515 t[s+ 82] = 0x0153;
2516 t[s+102] = 0x0167;
2517 t[s+129] = 0x0253;
2518 t[s+130] = 0x0183;
2519 t[s+132] = 0x0185;
2520 t[s+134] = 0x0254;
2521 t[s+135] = 0x0188;
2522 t[s+137] = 0x0256;
2523 t[s+138] = 0x0257;
2524 t[s+139] = 0x018c;
2525 t[s+142] = 0x01dd;
2526 t[s+143] = 0x0259;
2527 t[s+144] = 0x025b;
2528 t[s+145] = 0x0192;
2529 t[s+147] = 0x0260;
2530 t[s+148] = 0x0263;
2531 t[s+150] = 0x0269;
2532 t[s+151] = 0x0268;
2533 t[s+152] = 0x0199;
2534 t[s+156] = 0x026f;
2535 t[s+157] = 0x0272;
2536 t[s+159] = 0x0275;
2537 t[s+162] = 0x01a3;
2538 t[s+164] = 0x01a5;
2539 t[s+167] = 0x01a8;
2540 t[s+169] = 0x0283;
2541 t[s+172] = 0x01ad;
2542 t[s+174] = 0x0288;
2543 t[s+177] = 0x028a;
2544 t[s+178] = 0x028b;
2545 t[s+179] = 0x01b4;
2546 t[s+181] = 0x01b6;
2547 t[s+183] = 0x0292;
2548 t[s+184] = 0x01b9;
2549 t[s+188] = 0x01bd;
2550 t[s+196] = 0x01c6;
2551 t[s+197] = 0x01c6;
2552 t[s+199] = 0x01c9;
2553 t[s+200] = 0x01c9;
2554 t[s+202] = 0x01cc;
2555 t[s+203] = 0x01cc;
2556 t[s+228] = 0x01e5;
2557 t[s+241] = 0x01f3;
2558 t[s+242] = 0x01f3;
2559
2560 /*
2561 * table 3 (high byte 0x03)
2562 */
2563 s = 3 * 256;
2564 for (i = 0; i < 145; i++)
2565 t[s+i] = i + 0x0300;
2566 for (i = 145; i < 170; i++)
2567 t[s+i] = i + 0x0320;
2568 t[s+162] = 0x03a2;
2569 for (i = 170; i < 256; i++)
2570 t[s+i] = i + 0x0300;
2571
2572 for (i = 226; i < 239; i += 2)
2573 t[s+i] = i + 0x0301;
2574
2575 /*
2576 * table 4 (high byte 0x04)
2577 */
2578 s = 4 * 256;
2579 for (i = 0; i < 16; i++)
2580 t[s+i] = i + 0x0400;
2581 t[s+ 2] = 0x0452;
2582 t[s+ 4] = 0x0454;
2583 t[s+ 5] = 0x0455;
2584 t[s+ 6] = 0x0456;
2585 t[s+ 8] = 0x0458;
2586 t[s+ 9] = 0x0459;
2587 t[s+ 10] = 0x045a;
2588 t[s+ 11] = 0x045b;
2589 t[s+ 15] = 0x045f;
2590
2591 for (i = 16; i < 48; i++)
2592 t[s+i] = i + 0x0420;
2593 t[s+ 25] = 0x0419;
2594 for (i = 48; i < 256; i++)
2595 t[s+i] = i + 0x0400;
2596 t[s+195] = 0x04c4;
2597 t[s+199] = 0x04c8;
2598 t[s+203] = 0x04cc;
2599
2600 for (i = 96; i < 129; i += 2)
2601 t[s+i] = i + 0x0401;
2602 t[s+118] = 0x0476;
2603 for (i = 144; i < 191; i += 2)
2604 t[s+i] = i + 0x0401;
2605
2606 /*
2607 * table 5 (high byte 0x05)
2608 */
2609 s = 5 * 256;
2610 for (i = 0; i < 49; i++)
2611 t[s+i] = i + 0x0500;
2612 for (i = 49; i < 87; i++)
2613 t[s+i] = i + 0x0530;
2614 for (i = 87; i < 256; i++)
2615 t[s+i] = i + 0x0500;
2616
2617 /*
2618 * table 6 (high byte 0x10)
2619 */
2620 s = 6 * 256;
2621 for (i = 0; i < 160; i++)
2622 t[s+i] = i + 0x1000;
2623 for (i = 160; i < 198; i++)
2624 t[s+i] = i + 0x1030;
2625 for (i = 198; i < 256; i++)
2626 t[s+i] = i + 0x1000;
2627
2628 /*
2629 * table 7 (high byte 0x20)
2630 */
2631 s = 7 * 256;
2632 for (i = 0; i < 256; i++)
2633 t[s+i] = i + 0x2000;
2634 {
2635 uint8_t zi[15] = { 12, 13, 14, 15,
2636 42, 43, 44, 45, 46,
2637 106, 107, 108, 109, 110, 111};
2638
2639 for (i = 0; i < 15; i++)
2640 t[s+zi[i]] = 0x0000;
2641 }
2642
2643 /*
2644 * table 8 (high byte 0x21)
2645 */
2646 s = 8 * 256;
2647 for (i = 0; i < 96; i++)
2648 t[s+i] = i + 0x2100;
2649 for (i = 96; i < 112; i++)
2650 t[s+i] = i + 0x2110;
2651 for (i = 112; i < 256; i++)
2652 t[s+i] = i + 0x2100;
2653
2654 /*
2655 * table 9 (high byte 0xFE)
2656 */
2657 s = 9 * 256;
2658 for (i = 0; i < 256; i++)
2659 t[s+i] = i + 0xFE00;
2660 t[s+255] = 0x0000;
2661
2662 /*
2663 * table 10 (high byte 0xFF)
2664 */
2665 s = 10 * 256;
2666 for (i = 0; i < 33; i++)
2667 t[s+i] = i + 0xFF00;
2668 for (i = 33; i < 59; i++)
2669 t[s+i] = i + 0xFF20;
2670 for (i = 59; i < 256; i++)
2671 t[s+i] = i + 0xFF00;
2672
2673 return 0;
2674
2675 error:
2676 return 1;
2677 }
2678
2679 int
hfslib_get_hardlink(hfs_volume * vol,uint32_t inode_num,hfs_catalog_keyed_record_t * rec,hfs_callback_args * cbargs)2680 hfslib_get_hardlink(hfs_volume *vol, uint32_t inode_num,
2681 hfs_catalog_keyed_record_t *rec,
2682 hfs_callback_args *cbargs)
2683 {
2684 hfs_catalog_keyed_record_t metadata;
2685 hfs_catalog_key_t key;
2686 char name[16];
2687 unichar_t name_uni[16];
2688 int i, len;
2689
2690 /* XXX: cache this */
2691 if (hfslib_find_catalog_record_with_key(vol,
2692 &hfs_gMetadataDirectoryKey,
2693 &metadata, cbargs) != 0
2694 || metadata.type != HFS_REC_FLDR)
2695 return -1;
2696
2697 len = snprintf(name, sizeof(name), "iNode%d", inode_num);
2698 for (i = 0; i < len; i++)
2699 name_uni[i] = name[i];
2700
2701 if (hfslib_make_catalog_key(metadata.folder.cnid, len, name_uni,
2702 &key) == 0)
2703 return -1;
2704
2705 return hfslib_find_catalog_record_with_key(vol, &key, rec, cbargs);
2706 }
2707