Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / openbios / fs / hfs / data.c
1 /*
2  * libhfs - library for reading and writing Macintosh HFS volumes
3  * Copyright (C) 1996-1998 Robert Leslie
4  *
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.
9  *
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.
14  *
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,
18  * MA 02110-1301, USA.
19  *
20  * $Id: data.c,v 1.7 1998/11/02 22:08:57 rob Exp $
21  */
22
23 #include "config.h"
24 #include "data.h"
25
26 #define TIMEDIFF  2082844800UL
27
28 static
29 time_t tzdiff = -1;
30
31 static const
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,
37
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,
42
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,
47
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,
52
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,
57
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,
62
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,
67
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
72 };
73
74 /*
75  * NAME:        data->getsb()
76  * DESCRIPTION: marshal 1 signed byte into local host format
77  */
78 signed char d_getsb(register const unsigned char *ptr)
79 {
80   return ptr[0];
81 }
82
83 /*
84  * NAME:        data->getub()
85  * DESCRIPTION: marshal 1 unsigned byte into local host format
86  */
87 unsigned char d_getub(register const unsigned char *ptr)
88 {
89   return ptr[0];
90 }
91
92 /*
93  * NAME:        data->getsw()
94  * DESCRIPTION: marshal 2 signed bytes into local host format
95  */
96 signed short d_getsw(register const unsigned char *ptr)
97 {
98   return
99     (((  signed short) ptr[0] << 8) |
100      ((unsigned short) ptr[1] << 0));
101 }
102
103 /*
104  * NAME:        data->getuw()
105  * DESCRIPTION: marshal 2 unsigned bytes into local host format
106  */
107 unsigned short d_getuw(register const unsigned char *ptr)
108 {
109   return
110     (((unsigned short) ptr[0] << 8) |
111      ((unsigned short) ptr[1] << 0));
112 }
113
114 /*
115  * NAME:        data->getsl()
116  * DESCRIPTION: marshal 4 signed bytes into local host format
117  */
118 signed long d_getsl(register const unsigned char *ptr)
119 {
120   return
121     (((  signed long) ptr[0] << 24) |
122      ((unsigned long) ptr[1] << 16) |
123      ((unsigned long) ptr[2] <<  8) |
124      ((unsigned long) ptr[3] <<  0));
125 }
126
127 /*
128  * NAME:        data->getul()
129  * DESCRIPTION: marshal 4 unsigned bytes into local host format
130  */
131 unsigned long d_getul(register const unsigned char *ptr)
132 {
133   return
134     (((unsigned long) ptr[0] << 24) |
135      ((unsigned long) ptr[1] << 16) |
136      ((unsigned long) ptr[2] <<  8) |
137      ((unsigned long) ptr[3] <<  0));
138 }
139
140 /*
141  * NAME:        data->putsb()
142  * DESCRIPTION: marshal 1 signed byte out in big-endian format
143  */
144 void d_putsb(register unsigned char *ptr,
145              register signed char data)
146 {
147   *ptr = data;
148 }
149
150 /*
151  * NAME:        data->putub()
152  * DESCRIPTION: marshal 1 unsigned byte out in big-endian format
153  */
154 void d_putub(register unsigned char *ptr,
155              register unsigned char data)
156 {
157   *ptr = data;
158 }
159
160 /*
161  * NAME:        data->putsw()
162  * DESCRIPTION: marshal 2 signed bytes out in big-endian format
163  */
164 void d_putsw(register unsigned char *ptr,
165              register signed short data)
166 {
167   *ptr++ = ((unsigned short) data & 0xff00) >> 8;
168   *ptr   = ((unsigned short) data & 0x00ff) >> 0;
169 }
170
171 /*
172  * NAME:        data->putuw()
173  * DESCRIPTION: marshal 2 unsigned bytes out in big-endian format
174  */
175 void d_putuw(register unsigned char *ptr,
176              register unsigned short data)
177 {
178   *ptr++ = (data & 0xff00) >> 8;
179   *ptr   = (data & 0x00ff) >> 0;
180 }
181
182 /*
183  * NAME:        data->putsl()
184  * DESCRIPTION: marshal 4 signed bytes out in big-endian format
185  */
186 void d_putsl(register unsigned char *ptr,
187              register signed long data)
188 {
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;
193 }
194
195 /*
196  * NAME:        data->putul()
197  * DESCRIPTION: marshal 4 unsigned bytes out in big-endian format
198  */
199 void d_putul(register unsigned char *ptr,
200              register unsigned long data)
201 {
202   *ptr++ = (data & 0xff000000UL) >> 24;
203   *ptr++ = (data & 0x00ff0000UL) >> 16;
204   *ptr++ = (data & 0x0000ff00UL) >>  8;
205   *ptr   = (data & 0x000000ffUL) >>  0;
206 }
207
208 /*
209  * NAME:        data->fetchsb()
210  * DESCRIPTION: incrementally retrieve a signed byte of data
211  */
212 void d_fetchsb(register const unsigned char **ptr,
213                register signed char *dest)
214 {
215   *dest = *(*ptr)++;
216 }
217
218 /*
219  * NAME:        data->fetchub()
220  * DESCRIPTION: incrementally retrieve an unsigned byte of data
221  */
222 void d_fetchub(register const unsigned char **ptr,
223                register unsigned char *dest)
224 {
225   *dest = *(*ptr)++;
226 }
227
228 /*
229  * NAME:        data->fetchsw()
230  * DESCRIPTION: incrementally retrieve a signed word of data
231  */
232 void d_fetchsw(register const unsigned char **ptr,
233                register signed short *dest)
234 {
235   *dest =
236     (((  signed short) (*ptr)[0] << 8) |
237      ((unsigned short) (*ptr)[1] << 0));
238   *ptr += 2;
239 }
240
241 /*
242  * NAME:        data->fetchuw()
243  * DESCRIPTION: incrementally retrieve an unsigned word of data
244  */
245 void d_fetchuw(register const unsigned char **ptr,
246                register unsigned short *dest)
247 {
248   *dest =
249     (((unsigned short) (*ptr)[0] << 8) |
250      ((unsigned short) (*ptr)[1] << 0));
251   *ptr += 2;
252 }
253
254 /*
255  * NAME:        data->fetchsl()
256  * DESCRIPTION: incrementally retrieve a signed long word of data
257  */
258 void d_fetchsl(register const unsigned char **ptr,
259                register signed long *dest)
260 {
261   *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));
266   *ptr += 4;
267 }
268
269 /*
270  * NAME:        data->fetchul()
271  * DESCRIPTION: incrementally retrieve an unsigned long word of data
272  */
273 void d_fetchul(register const unsigned char **ptr,
274                register unsigned long *dest)
275 {
276   *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));
281   *ptr += 4;
282 }
283
284 /*
285  * NAME:        data->storesb()
286  * DESCRIPTION: incrementally store a signed byte of data
287  */
288 void d_storesb(register unsigned char **ptr,
289                register signed char data)
290 {
291   *(*ptr)++ = data;
292 }
293
294 /*
295  * NAME:        data->storeub()
296  * DESCRIPTION: incrementally store an unsigned byte of data
297  */
298 void d_storeub(register unsigned char **ptr,
299                register unsigned char data)
300 {
301   *(*ptr)++ = data;
302 }
303
304 /*
305  * NAME:        data->storesw()
306  * DESCRIPTION: incrementally store a signed word of data
307  */
308 void d_storesw(register unsigned char **ptr,
309                register signed short data)
310 {
311   *(*ptr)++ = ((unsigned short) data & 0xff00) >> 8;
312   *(*ptr)++ = ((unsigned short) data & 0x00ff) >> 0;
313 }
314
315 /*
316  * NAME:        data->storeuw()
317  * DESCRIPTION: incrementally store an unsigned word of data
318  */
319 void d_storeuw(register unsigned char **ptr,
320                register unsigned short data)
321 {
322   *(*ptr)++ = (data & 0xff00) >> 8;
323   *(*ptr)++ = (data & 0x00ff) >> 0;
324 }
325
326 /*
327  * NAME:        data->storesl()
328  * DESCRIPTION: incrementally store a signed long word of data
329  */
330 void d_storesl(register unsigned char **ptr,
331                register signed long data)
332 {
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;
337 }
338
339 /*
340  * NAME:        data->storeul()
341  * DESCRIPTION: incrementally store an unsigned long word of data
342  */
343 void d_storeul(register unsigned char **ptr,
344                register unsigned long data)
345 {
346   *(*ptr)++ = (data & 0xff000000UL) >> 24;
347   *(*ptr)++ = (data & 0x00ff0000UL) >> 16;
348   *(*ptr)++ = (data & 0x0000ff00UL) >>  8;
349   *(*ptr)++ = (data & 0x000000ffUL) >>  0;
350 }
351
352 /*
353  * NAME:        data->fetchstr()
354  * DESCRIPTION: incrementally retrieve a string
355  */
356 void d_fetchstr(const unsigned char **ptr, char *dest, unsigned size)
357 {
358   unsigned len;
359
360   len = d_getub(*ptr);
361
362   if (len > 0 && len < size)
363     memcpy(dest, *ptr + 1, len);
364   else
365     len = 0;
366
367   dest[len] = 0;
368
369   *ptr += size;
370 }
371
372 /*
373  * NAME:        data->storestr()
374  * DESCRIPTION: incrementally store a string
375  */
376 void d_storestr(unsigned char **ptr, const char *src, unsigned size)
377 {
378   unsigned len;
379
380   len = strlen(src);
381   if (len > --size)
382     len = 0;
383
384   d_storeub(ptr, len);
385
386   memcpy(*ptr, src, len);
387   memset(*ptr + len, 0, size - len);
388
389   *ptr += size;
390 }
391
392 /*
393  * NAME:        data->relstring()
394  * DESCRIPTION: compare two strings as per MacOS for HFS
395  */
396 int d_relstring(const char *str1, const char *str2)
397 {
398   register int diff;
399
400   while (*str1 && *str2)
401     {
402       diff = hfs_charorder[(unsigned char) *str1] -
403              hfs_charorder[(unsigned char) *str2];
404
405       if (diff)
406         return diff;
407
408       ++str1, ++str2;
409     }
410
411   if (! *str1 && *str2)
412     return -1;
413   else if (*str1 && ! *str2)
414     return 1;
415
416   return 0;
417 }
418
419 /*
420  * NAME:        calctzdiff()
421  * DESCRIPTION: calculate the timezone difference between local time and UTC
422  */
423 static
424 void calctzdiff(void)
425 {
426 # ifdef HAVE_MKTIME
427
428   time_t t;
429   int isdst;
430   struct tm tm;
431   const struct tm *tmp;
432
433   time(&t);
434   isdst = localtime(&t)->tm_isdst;
435
436   tmp = gmtime(&t);
437   if (tmp)
438     {
439       tm = *tmp;
440       tm.tm_isdst = isdst;
441
442       tzdiff = t - mktime(&tm);
443     }
444   else
445     tzdiff = 0;
446
447 # else
448
449   tzdiff = 0;
450
451 # endif
452 }
453
454 /*
455  * NAME:        data->ltime()
456  * DESCRIPTION: convert MacOS time to local time
457  */
458 time_t d_ltime(unsigned long mtime)
459 {
460   if (tzdiff == -1)
461     calctzdiff();
462
463   return (time_t) (mtime - TIMEDIFF) - tzdiff;
464 }
465
466 /*
467  * NAME:        data->mtime()
468  * DESCRIPTION: convert local time to MacOS time
469  */
470 unsigned long d_mtime(time_t ltime)
471 {
472   if (tzdiff == -1)
473     calctzdiff();
474
475   return (unsigned long) (ltime + tzdiff) + TIMEDIFF;
476 }