2 * libhfs - library for reading and writing Macintosh HFS volumes
3 * Copyright (C) 1996-1998 Robert Leslie
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 * $Id: data.c,v 1.7 1998/11/02 22:08:57 rob Exp $
26 #define TIMEDIFF 2082844800UL
32 unsigned char hfs_charorder[256] = {
33 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
34 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
35 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
36 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
38 0x20, 0x22, 0x23, 0x28, 0x29, 0x2a, 0x2b, 0x2c,
39 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
40 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e,
41 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46,
43 0x47, 0x48, 0x58, 0x5a, 0x5e, 0x60, 0x67, 0x69,
44 0x6b, 0x6d, 0x73, 0x75, 0x77, 0x79, 0x7b, 0x7f,
45 0x8d, 0x8f, 0x91, 0x93, 0x96, 0x98, 0x9f, 0xa1,
46 0xa3, 0xa5, 0xa8, 0xaa, 0xab, 0xac, 0xad, 0xae,
48 0x54, 0x48, 0x58, 0x5a, 0x5e, 0x60, 0x67, 0x69,
49 0x6b, 0x6d, 0x73, 0x75, 0x77, 0x79, 0x7b, 0x7f,
50 0x8d, 0x8f, 0x91, 0x93, 0x96, 0x98, 0x9f, 0xa1,
51 0xa3, 0xa5, 0xa8, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
53 0x4c, 0x50, 0x5c, 0x62, 0x7d, 0x81, 0x9a, 0x55,
54 0x4a, 0x56, 0x4c, 0x4e, 0x50, 0x5c, 0x62, 0x64,
55 0x65, 0x66, 0x6f, 0x70, 0x71, 0x72, 0x7d, 0x89,
56 0x8a, 0x8b, 0x81, 0x83, 0x9c, 0x9d, 0x9e, 0x9a,
58 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0x95,
59 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0x52, 0x85,
60 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8,
61 0xc9, 0xca, 0xcb, 0x57, 0x8c, 0xcc, 0x52, 0x85,
63 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0x26,
64 0x27, 0xd4, 0x20, 0x4a, 0x4e, 0x83, 0x87, 0x87,
65 0xd5, 0xd6, 0x24, 0x25, 0x2d, 0x2e, 0xd7, 0xd8,
66 0xa7, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
68 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
69 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
70 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
71 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
76 * DESCRIPTION: marshal 1 signed byte into local host format
78 signed char d_getsb(register const unsigned char *ptr)
85 * DESCRIPTION: marshal 1 unsigned byte into local host format
87 unsigned char d_getub(register const unsigned char *ptr)
94 * DESCRIPTION: marshal 2 signed bytes into local host format
96 signed short d_getsw(register const unsigned char *ptr)
99 ((( signed short) ptr[0] << 8) |
100 ((unsigned short) ptr[1] << 0));
104 * NAME: data->getuw()
105 * DESCRIPTION: marshal 2 unsigned bytes into local host format
107 unsigned short d_getuw(register const unsigned char *ptr)
110 (((unsigned short) ptr[0] << 8) |
111 ((unsigned short) ptr[1] << 0));
115 * NAME: data->getsl()
116 * DESCRIPTION: marshal 4 signed bytes into local host format
118 signed long d_getsl(register const unsigned char *ptr)
121 ((( signed long) ptr[0] << 24) |
122 ((unsigned long) ptr[1] << 16) |
123 ((unsigned long) ptr[2] << 8) |
124 ((unsigned long) ptr[3] << 0));
128 * NAME: data->getul()
129 * DESCRIPTION: marshal 4 unsigned bytes into local host format
131 unsigned long d_getul(register const unsigned char *ptr)
134 (((unsigned long) ptr[0] << 24) |
135 ((unsigned long) ptr[1] << 16) |
136 ((unsigned long) ptr[2] << 8) |
137 ((unsigned long) ptr[3] << 0));
141 * NAME: data->putsb()
142 * DESCRIPTION: marshal 1 signed byte out in big-endian format
144 void d_putsb(register unsigned char *ptr,
145 register signed char data)
151 * NAME: data->putub()
152 * DESCRIPTION: marshal 1 unsigned byte out in big-endian format
154 void d_putub(register unsigned char *ptr,
155 register unsigned char data)
161 * NAME: data->putsw()
162 * DESCRIPTION: marshal 2 signed bytes out in big-endian format
164 void d_putsw(register unsigned char *ptr,
165 register signed short data)
167 *ptr++ = ((unsigned short) data & 0xff00) >> 8;
168 *ptr = ((unsigned short) data & 0x00ff) >> 0;
172 * NAME: data->putuw()
173 * DESCRIPTION: marshal 2 unsigned bytes out in big-endian format
175 void d_putuw(register unsigned char *ptr,
176 register unsigned short data)
178 *ptr++ = (data & 0xff00) >> 8;
179 *ptr = (data & 0x00ff) >> 0;
183 * NAME: data->putsl()
184 * DESCRIPTION: marshal 4 signed bytes out in big-endian format
186 void d_putsl(register unsigned char *ptr,
187 register signed long data)
189 *ptr++ = ((unsigned long) data & 0xff000000UL) >> 24;
190 *ptr++ = ((unsigned long) data & 0x00ff0000UL) >> 16;
191 *ptr++ = ((unsigned long) data & 0x0000ff00UL) >> 8;
192 *ptr = ((unsigned long) data & 0x000000ffUL) >> 0;
196 * NAME: data->putul()
197 * DESCRIPTION: marshal 4 unsigned bytes out in big-endian format
199 void d_putul(register unsigned char *ptr,
200 register unsigned long data)
202 *ptr++ = (data & 0xff000000UL) >> 24;
203 *ptr++ = (data & 0x00ff0000UL) >> 16;
204 *ptr++ = (data & 0x0000ff00UL) >> 8;
205 *ptr = (data & 0x000000ffUL) >> 0;
209 * NAME: data->fetchsb()
210 * DESCRIPTION: incrementally retrieve a signed byte of data
212 void d_fetchsb(register const unsigned char **ptr,
213 register signed char *dest)
219 * NAME: data->fetchub()
220 * DESCRIPTION: incrementally retrieve an unsigned byte of data
222 void d_fetchub(register const unsigned char **ptr,
223 register unsigned char *dest)
229 * NAME: data->fetchsw()
230 * DESCRIPTION: incrementally retrieve a signed word of data
232 void d_fetchsw(register const unsigned char **ptr,
233 register signed short *dest)
236 ((( signed short) (*ptr)[0] << 8) |
237 ((unsigned short) (*ptr)[1] << 0));
242 * NAME: data->fetchuw()
243 * DESCRIPTION: incrementally retrieve an unsigned word of data
245 void d_fetchuw(register const unsigned char **ptr,
246 register unsigned short *dest)
249 (((unsigned short) (*ptr)[0] << 8) |
250 ((unsigned short) (*ptr)[1] << 0));
255 * NAME: data->fetchsl()
256 * DESCRIPTION: incrementally retrieve a signed long word of data
258 void d_fetchsl(register const unsigned char **ptr,
259 register signed long *dest)
262 ((( signed long) (*ptr)[0] << 24) |
263 ((unsigned long) (*ptr)[1] << 16) |
264 ((unsigned long) (*ptr)[2] << 8) |
265 ((unsigned long) (*ptr)[3] << 0));
270 * NAME: data->fetchul()
271 * DESCRIPTION: incrementally retrieve an unsigned long word of data
273 void d_fetchul(register const unsigned char **ptr,
274 register unsigned long *dest)
277 (((unsigned long) (*ptr)[0] << 24) |
278 ((unsigned long) (*ptr)[1] << 16) |
279 ((unsigned long) (*ptr)[2] << 8) |
280 ((unsigned long) (*ptr)[3] << 0));
285 * NAME: data->storesb()
286 * DESCRIPTION: incrementally store a signed byte of data
288 void d_storesb(register unsigned char **ptr,
289 register signed char data)
295 * NAME: data->storeub()
296 * DESCRIPTION: incrementally store an unsigned byte of data
298 void d_storeub(register unsigned char **ptr,
299 register unsigned char data)
305 * NAME: data->storesw()
306 * DESCRIPTION: incrementally store a signed word of data
308 void d_storesw(register unsigned char **ptr,
309 register signed short data)
311 *(*ptr)++ = ((unsigned short) data & 0xff00) >> 8;
312 *(*ptr)++ = ((unsigned short) data & 0x00ff) >> 0;
316 * NAME: data->storeuw()
317 * DESCRIPTION: incrementally store an unsigned word of data
319 void d_storeuw(register unsigned char **ptr,
320 register unsigned short data)
322 *(*ptr)++ = (data & 0xff00) >> 8;
323 *(*ptr)++ = (data & 0x00ff) >> 0;
327 * NAME: data->storesl()
328 * DESCRIPTION: incrementally store a signed long word of data
330 void d_storesl(register unsigned char **ptr,
331 register signed long data)
333 *(*ptr)++ = ((unsigned long) data & 0xff000000UL) >> 24;
334 *(*ptr)++ = ((unsigned long) data & 0x00ff0000UL) >> 16;
335 *(*ptr)++ = ((unsigned long) data & 0x0000ff00UL) >> 8;
336 *(*ptr)++ = ((unsigned long) data & 0x000000ffUL) >> 0;
340 * NAME: data->storeul()
341 * DESCRIPTION: incrementally store an unsigned long word of data
343 void d_storeul(register unsigned char **ptr,
344 register unsigned long data)
346 *(*ptr)++ = (data & 0xff000000UL) >> 24;
347 *(*ptr)++ = (data & 0x00ff0000UL) >> 16;
348 *(*ptr)++ = (data & 0x0000ff00UL) >> 8;
349 *(*ptr)++ = (data & 0x000000ffUL) >> 0;
353 * NAME: data->fetchstr()
354 * DESCRIPTION: incrementally retrieve a string
356 void d_fetchstr(const unsigned char **ptr, char *dest, unsigned size)
362 if (len > 0 && len < size)
363 memcpy(dest, *ptr + 1, len);
373 * NAME: data->storestr()
374 * DESCRIPTION: incrementally store a string
376 void d_storestr(unsigned char **ptr, const char *src, unsigned size)
386 memcpy(*ptr, src, len);
387 memset(*ptr + len, 0, size - len);
393 * NAME: data->relstring()
394 * DESCRIPTION: compare two strings as per MacOS for HFS
396 int d_relstring(const char *str1, const char *str2)
400 while (*str1 && *str2)
402 diff = hfs_charorder[(unsigned char) *str1] -
403 hfs_charorder[(unsigned char) *str2];
411 if (! *str1 && *str2)
413 else if (*str1 && ! *str2)
421 * DESCRIPTION: calculate the timezone difference between local time and UTC
424 void calctzdiff(void)
431 const struct tm *tmp;
434 isdst = localtime(&t)->tm_isdst;
442 tzdiff = t - mktime(&tm);
455 * NAME: data->ltime()
456 * DESCRIPTION: convert MacOS time to local time
458 time_t d_ltime(unsigned long mtime)
463 return (time_t) (mtime - TIMEDIFF) - tzdiff;
467 * NAME: data->mtime()
468 * DESCRIPTION: convert local time to MacOS time
470 unsigned long d_mtime(time_t ltime)
475 return (unsigned long) (ltime + tzdiff) + TIMEDIFF;