1 /* @(#)data.c	1.9 13/04/30 joerg */
2 #include <schily/mconfig.h>
3 #ifndef lint
4 static	UConst char sccsid[] =
5 	"@(#)data.c	1.9 13/04/30 joerg";
6 #endif
7 /*
8  * hfsutils - tools for reading and writing Macintosh HFS volumes
9  * Copyright (C) 1996, 1997 Robert Leslie
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25 
26 #include <schily/string.h>
27 #include <schily/time.h>
28 
29 #include "internal.h"
30 #include "data.h"
31 #include "btree.h"
32 
33 #ifdef APPLE_HYB
34 /*
35  *	Depending on the version, AppleDouble/Single stores dates
36  *	relative to 1st Jan 1904 (v1) or 1st Jan 2000 (v2)
37  *
38  *	DUTDIFF is the difference between 1st Jan 2000 and
39  *	1st Jan 1970 (Unix epoch)).
40  */
41 
42 #define DUTDIFF 946684800L
43 #endif /* APPLE_HYB */
44 #define MUTDIFF	2082844800L
45 #define	TZNONE	0x0FFFFFFFL	/* A timezone diff that cannot occur */
46 
47 static	void	calctzdiff	__PR((void));
48 
49 static
50 unsigned long tzdiff = TZNONE;
51 
52 static
53 unsigned char hfs_charorder[256] = {
54   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
55   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
56   0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
57   0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
58 
59   0x20, 0x22, 0x23, 0x28, 0x29, 0x2a, 0x2b, 0x2c,
60   0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
61   0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e,
62   0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46,
63 
64   0x47, 0x48, 0x58, 0x5a, 0x5e, 0x60, 0x67, 0x69,
65   0x6b, 0x6d, 0x73, 0x75, 0x77, 0x79, 0x7b, 0x7f,
66   0x8d, 0x8f, 0x91, 0x93, 0x96, 0x98, 0x9f, 0xa1,
67   0xa3, 0xa5, 0xa8, 0xaa, 0xab, 0xac, 0xad, 0xae,
68 
69   0x54, 0x48, 0x58, 0x5a, 0x5e, 0x60, 0x67, 0x69,
70   0x6b, 0x6d, 0x73, 0x75, 0x77, 0x79, 0x7b, 0x7f,
71   0x8d, 0x8f, 0x91, 0x93, 0x96, 0x98, 0x9f, 0xa1,
72   0xa3, 0xa5, 0xa8, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
73 
74   0x4c, 0x50, 0x5c, 0x62, 0x7d, 0x81, 0x9a, 0x55,
75   0x4a, 0x56, 0x4c, 0x4e, 0x50, 0x5c, 0x62, 0x64,
76   0x65, 0x66, 0x6f, 0x70, 0x71, 0x72, 0x7d, 0x89,
77   0x8a, 0x8b, 0x81, 0x83, 0x9c, 0x9d, 0x9e, 0x9a,
78 
79   0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0x95,
80   0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0x52, 0x85,
81   0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8,
82   0xc9, 0xca, 0xcb, 0x57, 0x8c, 0xcc, 0x52, 0x85,
83 
84   0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0x26,
85   0x27, 0xd4, 0x20, 0x4a, 0x4e, 0x83, 0x87, 0x87,
86   0xd5, 0xd6, 0x24, 0x25, 0x2d, 0x2e, 0xd7, 0xd8,
87   0xa7, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
88 
89   0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
90   0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
91   0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
92   0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
93 };
94 
95 /*
96  * NAME:	data->getb()
97  * DESCRIPTION:	marshal 1 byte into local host format
98  */
d_getb(ptr)99 char d_getb(ptr)
100 	unsigned char	*ptr;
101 {
102   return (char) ptr[0];
103 }
104 
105 /*
106  * NAME:	data->getw()
107  * DESCRIPTION:	marshal 2 bytes into local host format
108  */
d_getw(ptr)109 short d_getw(ptr)
110 	unsigned char	*ptr;
111 {
112   return (short)
113     ((ptr[0] << 8) |
114      (ptr[1] << 0));
115 }
116 
117 /*
118  * NAME:	data->getl()
119  * DESCRIPTION:	marshal 4 bytes into local host format
120  */
d_getl(ptr)121 long d_getl(ptr)
122 	unsigned char	*ptr;
123 {
124   return (long)
125     ((ptr[0] << 24) |
126      (ptr[1] << 16) |
127      (ptr[2] <<  8) |
128      (ptr[3] <<  0));
129 }
130 
131 /*
132  * NAME:	data->putb()
133  * DESCRIPTION:	marshal 1 byte out to Macintosh (big-endian) format
134  */
135 #ifdef	PROTOTYPES
d_putb(unsigned char * ptr,char data)136 void d_putb(unsigned char *ptr, char data)
137 #else
138 void d_putb(ptr, data)
139 	unsigned char	*ptr;
140 	char		data;
141 #endif
142 {
143   ptr[0] = (unsigned char) data;
144 }
145 
146 /*
147  * NAME:	data->putw()
148  * DESCRIPTION:	marshal 2 bytes out to Macintosh (big-endian) format
149  */
150 #ifdef	PROTOTYPES
d_putw(unsigned char * ptr,short data)151 void d_putw(unsigned char *ptr, short data)
152 #else
153 void d_putw(ptr, data)
154 	unsigned char	*ptr;
155 	short		data;
156 #endif
157 {
158   ptr[0] = ((unsigned) data & 0xff00) >> 8;
159   ptr[1] = ((unsigned) data & 0x00ff) >> 0;
160 }
161 
162 /*
163  * NAME:	data->putl()
164  * DESCRIPTION:	marshal 4 bytes out to Macintosh (big-endian) format
165  */
d_putl(ptr,data)166 void d_putl(ptr, data)
167 	unsigned char	*ptr;
168 	long		data;
169 {
170   ptr[0] = ((unsigned long) data & 0xff000000) >> 24;
171   ptr[1] = ((unsigned long) data & 0x00ff0000) >> 16;
172   ptr[2] = ((unsigned long) data & 0x0000ff00) >>  8;
173   ptr[3] = ((unsigned long) data & 0x000000ff) >>  0;
174 }
175 
176 /*
177  * NAME:	data->fetchb()
178  * DESCRIPTION:	incrementally retrieve a byte of data
179  */
d_fetchb(ptr,dest)180 void d_fetchb(ptr, dest)
181 	unsigned char	**ptr;
182 	char		*dest;
183 {
184   *dest = d_getb(*ptr);
185   *ptr += 1;
186 }
187 
188 /*
189  * NAME:	data->fetchw()
190  * DESCRIPTION:	incrementally retrieve a word of data
191  */
d_fetchw(ptr,dest)192 void d_fetchw(ptr, dest)
193 	unsigned char	**ptr;
194 	short		*dest;
195 {
196   *dest = d_getw(*ptr);
197   *ptr += 2;
198 }
199 
200 /*
201  * NAME:	data->fetchl()
202  * DESCRIPTION:	incrementally retrieve a long word of data
203  */
d_fetchl(ptr,dest)204 void d_fetchl(ptr, dest)
205 	unsigned char	**ptr;
206 	long		*dest;
207 {
208   *dest = d_getl(*ptr);
209   *ptr += 4;
210 }
211 
212 /*
213  * NAME:	data->fetchs()
214  * DESCRIPTION:	incrementally retrieve a string
215  */
d_fetchs(ptr,dest,size)216 void d_fetchs(ptr, dest, size)
217 	unsigned char	**ptr;
218 	char		*dest;
219 	int		size;
220 {
221   int len;
222   char blen;
223 
224   d_fetchb(ptr, &blen);
225   len = blen;
226 
227   if (len > 0 && len < size)
228     memcpy(dest, *ptr, len);
229   else
230     len = 0;
231 
232   dest[len] = 0;
233 
234   *ptr += size - 1;
235 }
236 
237 /*
238  * NAME:	data->storeb()
239  * DESCRIPTION:	incrementally store a byte of data
240  */
241 #ifdef	PROTOTYPES
d_storeb(unsigned char ** ptr,char data)242 void d_storeb(unsigned char **ptr, char data)
243 #else
244 void d_storeb(ptr, data)
245 	unsigned char	**ptr;
246 	char		data;
247 #endif
248 {
249   d_putb(*ptr, data);
250   *ptr += 1;
251 }
252 
253 /*
254  * NAME:	data->storew()
255  * DESCRIPTION:	incrementally store a word of data
256  */
257 #ifdef	PROTOTYPES
d_storew(unsigned char ** ptr,short data)258 void d_storew(unsigned char **ptr, short data)
259 #else
260 void d_storew(ptr, data)
261 	unsigned char	**ptr;
262 	short		data;
263 #endif
264 {
265   d_putw(*ptr, data);
266   *ptr += 2;
267 }
268 
269 /*
270  * NAME:	data->storel()
271  * DESCRIPTION:	incrementally store a long word of data
272  */
d_storel(ptr,data)273 void d_storel(ptr, data)
274 	unsigned char	**ptr;
275 	long		data;
276 {
277   d_putl(*ptr, data);
278   *ptr += 4;
279 }
280 
281 /*
282  * NAME:	data->stores()
283  * DESCRIPTION:	incrementally store a string
284  */
d_stores(ptr,src,size)285 void d_stores(ptr, src, size)
286 	unsigned char	**ptr;
287 	char		*src;
288 	int		size;
289 {
290   int len;
291 
292   len = strlen(src);
293   if (len > --size)
294     len = 0;
295 
296   d_storeb(ptr, (unsigned char) len);
297 
298   memcpy(*ptr, src, len);
299   memset(*ptr + len, 0, size - len);
300 
301   *ptr += size;
302 }
303 
304 /*
305  * NAME:	calctzdiff()
306  * DESCRIPTION:	calculate the timezone difference between local time and UTC
307  */
308 static
calctzdiff()309 void calctzdiff()
310 {
311   time_t t;
312   int isdst;
313   struct tm tm, *tmp;
314 
315   time(&t);
316   isdst = localtime(&t)->tm_isdst;
317 
318   tmp = gmtime(&t);
319   if (tmp)
320     {
321       tm = *tmp;
322       tm.tm_isdst = isdst;
323 
324       tzdiff = t - mktime(&tm);
325     }
326   else
327     tzdiff = 0;
328 }
329 
330 /*
331  * NAME:	data->tomtime()
332  * DESCRIPTION:	convert UNIX time to Macintosh time
333  */
d_tomtime(secs)334 unsigned long d_tomtime(secs)
335 	unsigned long	secs;
336 {
337   time_t uxtime = secs;
338 
339   if (tzdiff == TZNONE)
340     calctzdiff();
341 
342   return uxtime + tzdiff + MUTDIFF;
343 }
344 
345 /*
346  * NAME:	data->toutime()
347  * DESCRIPTION:	convert Macintosh time to UNIX time
348  */
d_toutime(secs)349 unsigned long d_toutime(secs)
350 	unsigned long	secs;
351 {
352   time_t uxtime = secs;
353 
354   if (tzdiff == TZNONE)
355     calctzdiff();
356 
357   return uxtime - MUTDIFF - tzdiff;
358 }
359 
360 #ifdef APPLE_HYB
361 /*
362  * NAME:	data->dtoutime()
363  * DESCRIPTION:	convert Apple Double v2 time to UNIX time
364  */
d_dtoutime(secs)365 unsigned long d_dtoutime(secs)
366 	long		secs;
367 {
368   time_t uxtime = secs;
369 
370   if (tzdiff == TZNONE)
371     calctzdiff();
372 
373   return uxtime + DUTDIFF - tzdiff;
374 }
375 #endif /* APPLE_HYB */
376 
377 /*
378  * NAME:	data->relstring()
379  * DESCRIPTION:	compare two strings as per MacOS for HFS
380  */
d_relstring(str1,str2)381 int d_relstring(str1, str2)
382 	char	*str1;
383 	char	*str2;
384 {
385   int diff;
386 
387   while (*str1 && *str2)
388     {
389       diff = hfs_charorder[(unsigned char) *str1] -
390 	     hfs_charorder[(unsigned char) *str2];
391 
392       if (diff)
393 	return diff;
394 
395       ++str1, ++str2;
396     }
397 
398   if (! *str1 && *str2)
399     return -1;
400   else if (*str1 && ! *str2)
401     return 1;
402 
403   return 0;
404 }
405