The Pedigree Project  0.1
utility.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #define IMPLEMENTING_LOG_FORMAT_FUNCTIONS
21 
22 #include "pedigree/kernel/utilities/utility.h"
23 #include "pedigree/kernel/Log.h"
24 #include "pedigree/kernel/processor/PhysicalMemoryManager.h"
25 #include "pedigree/kernel/utilities/spooky/SpookyV2.h"
26 #include <stdarg.h>
27 
28 void *page_align(void *p)
29 {
30  return reinterpret_cast<void *>(
31  reinterpret_cast<uintptr_t>(p) &
33 }
34 
35 const char *SDirectoryName(const char *path, char *buf, size_t buflen)
36 {
37  const char *last_slash = StringReverseFind(path, '/');
38  if (last_slash == nullptr)
39  {
40  return nullptr;
41  }
42 
43  size_t dirlength = last_slash - path;
44 
45  size_t copylen = min(buflen, dirlength);
46  StringCopyN(buf, path, copylen);
47  buf[copylen] = '\0';
48 
49  return buf;
50 }
51 
52 const char *SBaseName(const char *path, char *buf, size_t buflen)
53 {
54  size_t len = StringLength(path);
55 
56  const char *last_slash = StringReverseFind(path, '/');
57  if (last_slash == nullptr)
58  {
59  StringCopyN(buf, path, buflen);
60  return buf;
61  }
62 
63  if (!last_slash[1])
64  {
65  // Trailing slash, no basename
66  return nullptr;
67  }
68 
69  size_t baselength = len - (last_slash - path);
70  size_t copylen = min(buflen, baselength);
71  StringCopyN(buf, last_slash + 1, copylen);
72  buf[copylen] = '\0';
73 
74  return buf;
75 }
76 
77 const char *DirectoryName(const char *path)
78 {
79  size_t len = StringLength(path);
80  char *buf = new char[len + 1];
81  const char *result = SDirectoryName(path, buf, len + 1);
82  if (!result)
83  {
84  delete[] buf;
85  }
86  return result;
87 }
88 
89 const char *BaseName(const char *path)
90 {
91  size_t len = StringLength(path);
92  char *buf = new char[len + 1];
93  const char *result = SBaseName(path, buf, len + 1);
94  if (!result)
95  {
96  delete[] buf;
97  }
98  return result;
99 }
100 
101 uint8_t checksum(const uint8_t *pMemory, size_t sMemory)
102 {
103  uint8_t sum = 0;
104  for (size_t i = 0; i < sMemory; i++)
105  sum += pMemory[i];
106  return (sum == 0);
107 }
108 
109 uint16_t checksum16(const uint8_t *pMemory, size_t sMemory)
110 {
111  uint16_t sum1 = 0, sum2 = 0;
112 
113  for (size_t i = 0; i < sMemory; ++i)
114  {
115  sum1 = (sum1 + pMemory[i]) % 255;
116  sum2 = (sum2 + sum1) % 255;
117  }
118 
119  return (sum2 << 8) | sum1;
120 }
121 
122 uint32_t checksum32(const uint8_t *pMemory, size_t sMemory)
123 {
124  uint32_t sum1 = 0, sum2 = 0;
125  const uint16_t *mem = reinterpret_cast<const uint16_t *>(pMemory);
126 
127  for (size_t i = 0; i < sMemory / 2; ++i)
128  {
129  sum1 = (sum1 + mem[i]) % 65535;
130  sum2 = (sum2 + sum1) % 65535;
131  }
132 
133  return (sum2 << 16) | sum1;
134 }
135 
136 uint32_t checksum32_naive(const uint8_t *pMemory, size_t sMemory)
137 {
138  uint32_t sum1 = 0, sum2 = 0;
139  const uint16_t *mem = reinterpret_cast<const uint16_t *>(pMemory);
140 
141  for (size_t i = 0; i < sMemory / 2; ++i)
142  {
143  sum1 = (sum1 + mem[i]) % 65535;
144  sum2 = (sum2 + sum1) % 65535;
145  }
146 
147  return (sum2 << 16) | sum1;
148 }
149 
150 uint32_t checksumPage(uintptr_t address)
151 {
152  // may be able to be inlined with the knowledge of the constant size
153  return checksum32(
154  reinterpret_cast<const uint8_t *>(address),
156 }
157 
158 uint32_t elfHash(const char *buffer, size_t length)
159 {
160  uint32_t h = 0, g = 0;
161  for (size_t i = 0; i < length; ++i)
162  {
163  h = (h << 4) + buffer[i];
164  g = h & 0xF0000000;
165  h ^= g;
166  h ^= g >> 24;
167  }
168 
169  return h;
170 }
171 
172 uint32_t jenkinsHash(const char *buffer, size_t length)
173 {
174  uint32_t h = 0;
175  for (size_t i = 0; i < length; ++i)
176  {
177  h += buffer[i];
178  h += h << 10;
179  h ^= h >> 6;
180  }
181 
182  h += h << 3;
183  h ^= h >> 11;
184  h += h << 15;
185  return h;
186 }
187 
188 uint32_t spookyHash(const char *buffer, size_t length)
189 {
190  SpookyHash h;
191  h.Init(0, 0);
192  return h.Hash32(buffer, length, 0);
193 }
194 
195 uint64_t spookyHash64(const char *buffer, size_t length)
196 {
197  SpookyHash h;
198  h.Init(0, 0);
199  return h.Hash64(buffer, length, 0);
200 }
201 
202 void spookyHash128(const char *buffer, size_t length, uint64_t *h1, uint64_t *h2)
203 {
204  SpookyHash h;
205  h.Init(0, 0);
206  h.Hash128(buffer, length, h1, h2);
207 }
208 
209 #define LOG_FORMAT_COMMON \
210  char buf[1024]; \
211  int i = 0; \
212  va_list ap; \
213  va_start(ap, fmt); \
214  i = VStringFormat(buf, fmt, ap); \
215  va_end(ap); \
216  if (i && (buf[i - 1] == '\n')) \
217  buf[i - 1] = '\0';
218 
219 int Debugf(const char *fmt, ...)
220 {
221  LOG_FORMAT_COMMON
222 
223  DEBUG_LOG("debugf: " << buf);
224 
225  return i;
226 }
227 
228 int Noticef(const char *fmt, ...)
229 {
230  LOG_FORMAT_COMMON
231 
232  NOTICE("noticef: " << buf);
233 
234  return i;
235 }
236 
237 int Warningf(const char *fmt, ...)
238 {
239  LOG_FORMAT_COMMON
240 
241  WARNING("warningf: " << buf);
242 
243  return i;
244 }
245 
246 int Errorf(const char *fmt, ...)
247 {
248  LOG_FORMAT_COMMON
249 
250  ERROR("errorf: " << buf);
251 
252  return i;
253 }
254 
255 int Fatalf(const char *fmt, ...)
256 {
257  LOG_FORMAT_COMMON
258 
259  FATAL("fatalf: " << buf);
260 }
#define WARNING(text)
Definition: Log.h:78
#define NOTICE(text)
Definition: Log.h:74
void * page_align(void *p)
Definition: utility.cc:28
#define ERROR(text)
Definition: Log.h:82
Definition: mem.c:283
#define FATAL(text)
Definition: Log.h:89
#define DEBUG_LOG(text)
Definition: Log.h:69