]>
git.saurik.com Git - apple/hfs.git/blob - livefiles_hfs_plugin/lf_hfs_utils.c
1 /* Copyright © 2017-2018 Apple Inc. All rights reserved.
6 * Created by Yakov Ben Zaken on 19/03/2018.
9 #include "lf_hfs_utils.h"
10 #include "lf_hfs_vfsutils.h"
13 * General routine to allocate a hash table.
16 hashinit(int elements
, u_long
*hashmask
)
19 LIST_HEAD(generic
, generic
) *hashtbl
;
24 for (hashsize
= 1; hashsize
<= elements
; hashsize
<<= 1)
30 hashtbl
= hfs_malloc(hashsize
* sizeof(*hashtbl
));
33 for (i
= 0; i
< hashsize
; i
++)
35 LIST_INIT(&hashtbl
[i
]);
37 *hashmask
= hashsize
- 1;
43 * General routine to free a hash table.
46 hashDeinit(void* pvHashTbl
)
48 LIST_HEAD(generic
, generic
) *hashtbl
= pvHashTbl
;
53 * to_bsd_time - convert from Mac OS time (seconds since 1/1/1904)
54 * to BSD time (seconds since 1/1/1970)
57 to_bsd_time(u_int32_t hfs_time
)
59 u_int32_t gmt
= hfs_time
;
61 if (gmt
> MAC_GMT_FACTOR
)
62 gmt
-= MAC_GMT_FACTOR
;
64 gmt
= 0; /* don't let date go negative! */
70 * to_hfs_time - convert from BSD time (seconds since 1/1/1970)
71 * to Mac OS time (seconds since 1/1/1904)
74 to_hfs_time(time_t bsd_time
)
76 u_int32_t hfs_time
= (u_int32_t
)bsd_time
;
78 /* don't adjust zero - treat as uninitialzed */
80 hfs_time
+= MAC_GMT_FACTOR
;
86 microuptime(struct timeval
*tvp
)
89 clock_gettime( CLOCK_MONOTONIC
, &ts
);
90 TIMESPEC_TO_TIMEVAL(tvp
, &ts
);
94 microtime(struct timeval
*tvp
)
97 clock_gettime( CLOCK_REALTIME
, &ts
);
98 TIMESPEC_TO_TIMEVAL(tvp
, &ts
);
101 void* lf_hfs_utils_allocate_and_copy_string( char *pcName
, size_t uLen
)
103 //Check the validity of the uLen
104 if (uLen
> kHFSPlusMaxFileNameChars
) {
108 //Checkk the validity of the pcName
109 if (strlen(pcName
) != uLen
) {
113 void *pvTmp
= hfs_malloc( uLen
+1 );
114 if ( pvTmp
== NULL
) {
118 memcpy(pvTmp
, pcName
, uLen
);
119 //Add Null terminated at the end of the name
120 char *pcLastChar
= pvTmp
+ uLen
;
127 blk_to_bytes(uint32_t blk
, uint32_t blk_size
)
129 return (off_t
)blk
* blk_size
; // Avoid the overflow