/[pearpc]/src/io/prom/fs/hfs/data.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /src/io/prom/fs/hfs/data.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations)
Wed Sep 5 17:11:21 2007 UTC (16 years, 7 months ago) by dpavlin
File MIME type: text/plain
File size: 11217 byte(s)
import upstream CVS
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., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 */
20
21 # ifdef HAVE_CONFIG_H
22 # include "config.h"
23 # endif
24
25 # include <string.h>
26 # include <time.h>
27
28 # ifdef TM_IN_SYS_TIME
29 # include <sys/time.h>
30 # endif
31
32 # include "data.h"
33
34 # define TIMEDIFF 2082844800UL
35
36 static
37 time_t tzdiff = -1;
38
39 const
40 unsigned char hfs_charorder[256] = {
41 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
42 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
43 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
44 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
45
46 0x20, 0x22, 0x23, 0x28, 0x29, 0x2a, 0x2b, 0x2c,
47 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
48 0x37, 0x38, 0x39, 0x3a, 0x3b, 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, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
77 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
78 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
79 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
80 };
81
82 /*
83 * NAME: data->getsb()
84 * DESCRIPTION: marshal 1 signed byte into local host format
85 */
86 signed char d_getsb(register const unsigned char *ptr)
87 {
88 return ptr[0];
89 }
90
91 /*
92 * NAME: data->getub()
93 * DESCRIPTION: marshal 1 unsigned byte into local host format
94 */
95 unsigned char d_getub(register const unsigned char *ptr)
96 {
97 return ptr[0];
98 }
99
100 /*
101 * NAME: data->getsw()
102 * DESCRIPTION: marshal 2 signed bytes into local host format
103 */
104 signed short d_getsw(register const unsigned char *ptr)
105 {
106 return
107 ((( signed short) ptr[0] << 8) |
108 ((unsigned short) ptr[1] << 0));
109 }
110
111 /*
112 * NAME: data->getuw()
113 * DESCRIPTION: marshal 2 unsigned bytes into local host format
114 */
115 unsigned short d_getuw(register const unsigned char *ptr)
116 {
117 return
118 (((unsigned short) ptr[0] << 8) |
119 ((unsigned short) ptr[1] << 0));
120 }
121
122 /*
123 * NAME: data->getsl()
124 * DESCRIPTION: marshal 4 signed bytes into local host format
125 */
126 signed long d_getsl(register const unsigned char *ptr)
127 {
128 return
129 ((( signed long) ptr[0] << 24) |
130 ((unsigned long) ptr[1] << 16) |
131 ((unsigned long) ptr[2] << 8) |
132 ((unsigned long) ptr[3] << 0));
133 }
134
135 /*
136 * NAME: data->getul()
137 * DESCRIPTION: marshal 4 unsigned bytes into local host format
138 */
139 unsigned long d_getul(register const unsigned char *ptr)
140 {
141 return
142 (((unsigned long) ptr[0] << 24) |
143 ((unsigned long) ptr[1] << 16) |
144 ((unsigned long) ptr[2] << 8) |
145 ((unsigned long) ptr[3] << 0));
146 }
147
148 /*
149 * NAME: data->putsb()
150 * DESCRIPTION: marshal 1 signed byte out in big-endian format
151 */
152 void d_putsb(register unsigned char *ptr,
153 register signed char data)
154 {
155 *ptr = data;
156 }
157
158 /*
159 * NAME: data->putub()
160 * DESCRIPTION: marshal 1 unsigned byte out in big-endian format
161 */
162 void d_putub(register unsigned char *ptr,
163 register unsigned char data)
164 {
165 *ptr = data;
166 }
167
168 /*
169 * NAME: data->putsw()
170 * DESCRIPTION: marshal 2 signed bytes out in big-endian format
171 */
172 void d_putsw(register unsigned char *ptr,
173 register signed short data)
174 {
175 *ptr++ = ((unsigned short) data & 0xff00) >> 8;
176 *ptr = ((unsigned short) data & 0x00ff) >> 0;
177 }
178
179 /*
180 * NAME: data->putuw()
181 * DESCRIPTION: marshal 2 unsigned bytes out in big-endian format
182 */
183 void d_putuw(register unsigned char *ptr,
184 register unsigned short data)
185 {
186 *ptr++ = (data & 0xff00) >> 8;
187 *ptr = (data & 0x00ff) >> 0;
188 }
189
190 /*
191 * NAME: data->putsl()
192 * DESCRIPTION: marshal 4 signed bytes out in big-endian format
193 */
194 void d_putsl(register unsigned char *ptr,
195 register signed long data)
196 {
197 *ptr++ = ((unsigned long) data & 0xff000000UL) >> 24;
198 *ptr++ = ((unsigned long) data & 0x00ff0000UL) >> 16;
199 *ptr++ = ((unsigned long) data & 0x0000ff00UL) >> 8;
200 *ptr = ((unsigned long) data & 0x000000ffUL) >> 0;
201 }
202
203 /*
204 * NAME: data->putul()
205 * DESCRIPTION: marshal 4 unsigned bytes out in big-endian format
206 */
207 void d_putul(register unsigned char *ptr,
208 register unsigned long data)
209 {
210 *ptr++ = (data & 0xff000000UL) >> 24;
211 *ptr++ = (data & 0x00ff0000UL) >> 16;
212 *ptr++ = (data & 0x0000ff00UL) >> 8;
213 *ptr = (data & 0x000000ffUL) >> 0;
214 }
215
216 /*
217 * NAME: data->fetchsb()
218 * DESCRIPTION: incrementally retrieve a signed byte of data
219 */
220 void d_fetchsb(register const unsigned char **ptr,
221 register signed char *dest)
222 {
223 *dest = *(*ptr)++;
224 }
225
226 /*
227 * NAME: data->fetchub()
228 * DESCRIPTION: incrementally retrieve an unsigned byte of data
229 */
230 void d_fetchub(register const unsigned char **ptr,
231 register unsigned char *dest)
232 {
233 *dest = *(*ptr)++;
234 }
235
236 /*
237 * NAME: data->fetchsw()
238 * DESCRIPTION: incrementally retrieve a signed word of data
239 */
240 void d_fetchsw(register const unsigned char **ptr,
241 register signed short *dest)
242 {
243 *dest =
244 ((( signed short) (*ptr)[0] << 8) |
245 ((unsigned short) (*ptr)[1] << 0));
246 *ptr += 2;
247 }
248
249 /*
250 * NAME: data->fetchuw()
251 * DESCRIPTION: incrementally retrieve an unsigned word of data
252 */
253 void d_fetchuw(register const unsigned char **ptr,
254 register unsigned short *dest)
255 {
256 *dest =
257 (((unsigned short) (*ptr)[0] << 8) |
258 ((unsigned short) (*ptr)[1] << 0));
259 *ptr += 2;
260 }
261
262 /*
263 * NAME: data->fetchsl()
264 * DESCRIPTION: incrementally retrieve a signed long word of data
265 */
266 void d_fetchsl(register const unsigned char **ptr,
267 register signed long *dest)
268 {
269 *dest =
270 ((( signed long) (*ptr)[0] << 24) |
271 ((unsigned long) (*ptr)[1] << 16) |
272 ((unsigned long) (*ptr)[2] << 8) |
273 ((unsigned long) (*ptr)[3] << 0));
274 *ptr += 4;
275 }
276
277 /*
278 * NAME: data->fetchul()
279 * DESCRIPTION: incrementally retrieve an unsigned long word of data
280 */
281 void d_fetchul(register const unsigned char **ptr,
282 register unsigned long *dest)
283 {
284 *dest =
285 (((unsigned long) (*ptr)[0] << 24) |
286 ((unsigned long) (*ptr)[1] << 16) |
287 ((unsigned long) (*ptr)[2] << 8) |
288 ((unsigned long) (*ptr)[3] << 0));
289 *ptr += 4;
290 }
291
292 /*
293 * NAME: data->storesb()
294 * DESCRIPTION: incrementally store a signed byte of data
295 */
296 void d_storesb(register unsigned char **ptr,
297 register signed char data)
298 {
299 *(*ptr)++ = data;
300 }
301
302 /*
303 * NAME: data->storeub()
304 * DESCRIPTION: incrementally store an unsigned byte of data
305 */
306 void d_storeub(register unsigned char **ptr,
307 register unsigned char data)
308 {
309 *(*ptr)++ = data;
310 }
311
312 /*
313 * NAME: data->storesw()
314 * DESCRIPTION: incrementally store a signed word of data
315 */
316 void d_storesw(register unsigned char **ptr,
317 register signed short data)
318 {
319 *(*ptr)++ = ((unsigned short) data & 0xff00) >> 8;
320 *(*ptr)++ = ((unsigned short) data & 0x00ff) >> 0;
321 }
322
323 /*
324 * NAME: data->storeuw()
325 * DESCRIPTION: incrementally store an unsigned word of data
326 */
327 void d_storeuw(register unsigned char **ptr,
328 register unsigned short data)
329 {
330 *(*ptr)++ = (data & 0xff00) >> 8;
331 *(*ptr)++ = (data & 0x00ff) >> 0;
332 }
333
334 /*
335 * NAME: data->storesl()
336 * DESCRIPTION: incrementally store a signed long word of data
337 */
338 void d_storesl(register unsigned char **ptr,
339 register signed long data)
340 {
341 *(*ptr)++ = ((unsigned long) data & 0xff000000UL) >> 24;
342 *(*ptr)++ = ((unsigned long) data & 0x00ff0000UL) >> 16;
343 *(*ptr)++ = ((unsigned long) data & 0x0000ff00UL) >> 8;
344 *(*ptr)++ = ((unsigned long) data & 0x000000ffUL) >> 0;
345 }
346
347 /*
348 * NAME: data->storeul()
349 * DESCRIPTION: incrementally store an unsigned long word of data
350 */
351 void d_storeul(register unsigned char **ptr,
352 register unsigned long data)
353 {
354 *(*ptr)++ = (data & 0xff000000UL) >> 24;
355 *(*ptr)++ = (data & 0x00ff0000UL) >> 16;
356 *(*ptr)++ = (data & 0x0000ff00UL) >> 8;
357 *(*ptr)++ = (data & 0x000000ffUL) >> 0;
358 }
359
360 /*
361 * NAME: data->fetchstr()
362 * DESCRIPTION: incrementally retrieve a string
363 */
364 void d_fetchstr(const unsigned char **ptr, char *dest, unsigned size)
365 {
366 unsigned len;
367
368 len = d_getub(*ptr);
369
370 if (len > 0 && len < size)
371 memcpy(dest, *ptr + 1, len);
372 else
373 len = 0;
374
375 dest[len] = 0;
376
377 *ptr += size;
378 }
379
380 /*
381 * NAME: data->storestr()
382 * DESCRIPTION: incrementally store a string
383 */
384 void d_storestr(unsigned char **ptr, const char *src, unsigned size)
385 {
386 unsigned len;
387
388 len = strlen(src);
389 if (len > --size)
390 len = 0;
391
392 d_storeub(ptr, len);
393
394 memcpy(*ptr, src, len);
395 memset(*ptr + len, 0, size - len);
396
397 *ptr += size;
398 }
399
400 /*
401 * NAME: data->relstring()
402 * DESCRIPTION: compare two strings as per MacOS for HFS
403 */
404 int d_relstring(const char *str1, const char *str2)
405 {
406 register int diff;
407
408 while (*str1 && *str2)
409 {
410 diff = hfs_charorder[(unsigned char) *str1] -
411 hfs_charorder[(unsigned char) *str2];
412
413 if (diff)
414 return diff;
415
416 ++str1, ++str2;
417 }
418
419 if (! *str1 && *str2)
420 return -1;
421 else if (*str1 && ! *str2)
422 return 1;
423
424 return 0;
425 }
426
427 /*
428 * NAME: calctzdiff()
429 * DESCRIPTION: calculate the timezone difference between local time and UTC
430 */
431 static
432 void calctzdiff(void)
433 {
434 # ifdef HAVE_MKTIME
435
436 time_t t;
437 int isdst;
438 struct tm tm;
439 const struct tm *tmp;
440
441 time(&t);
442 isdst = localtime(&t)->tm_isdst;
443
444 tmp = gmtime(&t);
445 if (tmp)
446 {
447 tm = *tmp;
448 tm.tm_isdst = isdst;
449
450 tzdiff = t - mktime(&tm);
451 }
452 else
453 tzdiff = 0;
454
455 # else
456
457 tzdiff = 0;
458
459 # endif
460 }
461
462 /*
463 * NAME: data->ltime()
464 * DESCRIPTION: convert MacOS time to local time
465 */
466 time_t d_ltime(unsigned long mtime)
467 {
468 if (tzdiff == -1)
469 calctzdiff();
470
471 return (time_t) (mtime - TIMEDIFF) - tzdiff;
472 }
473
474 /*
475 * NAME: data->mtime()
476 * DESCRIPTION: convert local time to MacOS time
477 */
478 unsigned long d_mtime(time_t ltime)
479 {
480 if (tzdiff == -1)
481 calctzdiff();
482
483 return (unsigned long) (ltime + tzdiff) + TIMEDIFF;
484 }

  ViewVC Help
Powered by ViewVC 1.1.26