]> git.saurik.com Git - apple/hfs.git/blame - livefiles_hfs_plugin/lf_hfs_utils.c
hfs-522.100.5.tar.gz
[apple/hfs.git] / livefiles_hfs_plugin / lf_hfs_utils.c
CommitLineData
de8ee011
A
1/* Copyright © 2017-2018 Apple Inc. All rights reserved.
2 *
3 * lf_hfs_utils.c
4 * livefiles_hfs
5 *
6 * Created by Yakov Ben Zaken on 19/03/2018.
7 */
8
9#include "lf_hfs_utils.h"
10#include "lf_hfs_vfsutils.h"
11
12/*
13 * General routine to allocate a hash table.
14 */
15void *
16hashinit(int elements, u_long *hashmask)
17{
18 int hashsize = 0;
19 LIST_HEAD(generic, generic) *hashtbl;
20 int i;
21
22 if (elements <= 0)
23 return NULL;
24 for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
25 {
26 continue;
27 }
28
29 hashsize >>= 1;
30 hashtbl = hfs_malloc(hashsize * sizeof(*hashtbl));
31 if (hashtbl != NULL)
32 {
33 for (i = 0; i < hashsize; i++)
34 {
35 LIST_INIT(&hashtbl[i]);
36 }
37 *hashmask = hashsize - 1;
38 }
39 return (hashtbl);
40}
41
42/*
43 * General routine to free a hash table.
44 */
45void
46hashDeinit(void* pvHashTbl)
47{
48 LIST_HEAD(generic, generic) *hashtbl = pvHashTbl;
49 hfs_free(hashtbl);
50}
51
52/*
53 * to_bsd_time - convert from Mac OS time (seconds since 1/1/1904)
54 * to BSD time (seconds since 1/1/1970)
55 */
56time_t
57to_bsd_time(u_int32_t hfs_time)
58{
59 u_int32_t gmt = hfs_time;
60
61 if (gmt > MAC_GMT_FACTOR)
62 gmt -= MAC_GMT_FACTOR;
63 else
64 gmt = 0; /* don't let date go negative! */
65
66 return (time_t)gmt;
67}
68
69/*
70 * to_hfs_time - convert from BSD time (seconds since 1/1/1970)
71 * to Mac OS time (seconds since 1/1/1904)
72 */
73u_int32_t
74to_hfs_time(time_t bsd_time)
75{
76 u_int32_t hfs_time = (u_int32_t)bsd_time;
77
78 /* don't adjust zero - treat as uninitialzed */
79 if (hfs_time != 0)
80 hfs_time += MAC_GMT_FACTOR;
81
82 return (hfs_time);
83}
84
85void
86microuptime(struct timeval *tvp)
87{
88 struct timespec ts;
89 clock_gettime( CLOCK_MONOTONIC, &ts );
90 TIMESPEC_TO_TIMEVAL(tvp, &ts);
91}
92
93void
94microtime(struct timeval *tvp)
95{
96 struct timespec ts;
97 clock_gettime( CLOCK_REALTIME, &ts );
98 TIMESPEC_TO_TIMEVAL(tvp, &ts);
99}
100
101void* lf_hfs_utils_allocate_and_copy_string( char *pcName, size_t uLen )
102{
103 //Check the validity of the uLen
104 if (uLen > kHFSPlusMaxFileNameChars) {
105 return NULL;
106 }
107
108 //Checkk the validity of the pcName
109 if (strlen(pcName) != uLen) {
110 return NULL;
111 }
112
113 void *pvTmp = hfs_malloc( uLen+1 );
114 if ( pvTmp == NULL ) {
115 return NULL;
116 }
117
118 memcpy(pvTmp, pcName, uLen);
119 //Add Null terminated at the end of the name
120 char *pcLastChar = pvTmp + uLen;
121 *pcLastChar = '\0';
122
123 return pvTmp;
124}
125
126off_t
127blk_to_bytes(uint32_t blk, uint32_t blk_size)
128{
129 return (off_t)blk * blk_size; // Avoid the overflow
130}