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/Log.h"
23#include "pedigree/kernel/TargetInfo.h"
24#include "pedigree/kernel/utilities/spooky/SpookyV2.h"
25#include "pedigree/kernel/utilities/utility.h"
26
27#include <stdarg.h>
28
29void* page_align(void* p) {
30 return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(p) &
31 ~static_cast<uintptr_t>(TargetInfo::getPageOffsetMask()));
32}
33
34const char* SDirectoryName(const char* path, char* buf, size_t buflen) {
35 const char* last_slash = StringReverseFind(path, '/');
36 if (last_slash == nullptr) {
37 return nullptr;
38 }
39
40 size_t dirlength = last_slash - path;
41
42 size_t copylen = min(buflen, dirlength);
43 StringCopyN(buf, path, copylen);
44 buf[copylen] = '\0';
45
46 return buf;
47}
48
49const char* SBaseName(const char* path, char* buf, size_t buflen) {
50 size_t len = StringLength(path);
51
52 const char* last_slash = StringReverseFind(path, '/');
53 if (last_slash == nullptr) {
54 StringCopyN(buf, path, buflen);
55 return buf;
56 }
57
58 if (!last_slash[1]) {
59 // Trailing slash, no basename
60 return nullptr;
61 }
62
63 size_t baselength = len - (last_slash - path);
64 size_t copylen = min(buflen, baselength);
65 StringCopyN(buf, last_slash + 1, copylen);
66 buf[copylen] = '\0';
67
68 return buf;
69}
70
71const char* DirectoryName(const char* path) {
72 size_t len = StringLength(path);
73 char* buf = new char[len + 1];
74 const char* result = SDirectoryName(path, buf, len + 1);
75 if (!result) {
76 delete[] buf;
77 }
78 return result;
79}
80
81const char* BaseName(const char* path) {
82 size_t len = StringLength(path);
83 char* buf = new char[len + 1];
84 const char* result = SBaseName(path, buf, len + 1);
85 if (!result) {
86 delete[] buf;
87 }
88 return result;
89}
90
91uint8_t checksum(const uint8_t* pMemory, size_t sMemory) {
92 uint8_t sum = 0;
93 for (size_t i = 0; i < sMemory; i++)
94 sum += pMemory[i];
95 return (sum == 0);
96}
97
98uint16_t checksum16(const uint16_t* pMemory, size_t sMemory) {
99 uint16_t sum1 = 0, sum2 = 0;
100 const uint8_t* mem = reinterpret_cast<const uint8_t*>(pMemory);
101
102 for (size_t i = 0; i < sMemory; ++i) {
103 sum1 = (sum1 + mem[i]) % 255;
104 sum2 = (sum2 + sum1) % 255;
105 }
106
107 return (sum2 << 8) | sum1;
108}
109
110uint32_t checksum32(const uint32_t* pMemory, size_t sMemory) {
111 uint32_t sum1 = 0, sum2 = 0;
112 const uint16_t* mem = reinterpret_cast<const uint16_t*>(pMemory);
113
114 for (size_t i = 0; i < sMemory / 2; ++i) {
115 sum1 = (sum1 + mem[i]) % 65535;
116 sum2 = (sum2 + sum1) % 65535;
117 }
118
119 return (sum2 << 16) | sum1;
120}
121
122uint32_t checksum32_naive(const uint32_t* pMemory, size_t sMemory) {
123 uint32_t sum1 = 0, sum2 = 0;
124 const uint16_t* mem = reinterpret_cast<const uint16_t*>(pMemory);
125
126 for (size_t i = 0; i < sMemory / 2; ++i) {
127 sum1 = (sum1 + mem[i]) % 65535;
128 sum2 = (sum2 + sum1) % 65535;
129 }
130
131 return (sum2 << 16) | sum1;
132}
133
134uint32_t checksumPage(uintptr_t address) {
135 // may be able to be inlined with the knowledge of the constant size
136 return checksum32(reinterpret_cast<const uint32_t*>(address), TargetInfo::getPageSize());
137}
138
139uint32_t elfHash(const char* buffer, size_t length) {
140 uint32_t h = 0, g = 0;
141 for (size_t i = 0; i < length; ++i) {
142 h = (h << 4) + buffer[i];
143 g = h & 0xF0000000;
144 h ^= g;
145 h ^= g >> 24;
146 }
147
148 return h;
149}
150
151uint32_t jenkinsHash(const char* buffer, size_t length) {
152 uint32_t h = 0;
153 for (size_t i = 0; i < length; ++i) {
154 h += buffer[i];
155 h += h << 10;
156 h ^= h >> 6;
157 }
158
159 h += h << 3;
160 h ^= h >> 11;
161 h += h << 15;
162 return h;
163}
164
165uint32_t spookyHash(const char* buffer, size_t length) {
166 SpookyHash h;
167 h.Init(0, 0);
168 return h.Hash32(buffer, length, 0);
169}
170
171uint64_t spookyHash64(const char* buffer, size_t length) {
172 SpookyHash h;
173 h.Init(0, 0);
174 return h.Hash64(buffer, length, 0);
175}
176
177void spookyHash128(const char* buffer, size_t length, uint64_t* h1, uint64_t* h2) {
178 SpookyHash h;
179 h.Init(0, 0);
180 h.Hash128(buffer, length, h1, h2);
181}
182
183#define LOG_FORMAT_COMMON \
184 char buf[1024]; \
185 int i = 0; \
186 va_list ap; \
187 va_start(ap, fmt); \
188 i = VStringFormat(buf, fmt, ap); \
189 va_end(ap); \
190 if (i && (buf[i - 1] == '\n')) \
191 buf[i - 1] = '\0';
192
193int Debugf(const char* fmt, ...) {
194 LOG_FORMAT_COMMON
195
196 DEBUG_LOG("debugf: " << buf);
197
198 return i;
199}
200
201int Noticef(const char* fmt, ...) {
202 LOG_FORMAT_COMMON
203
204 NOTICE("noticef: " << buf);
205
206 return i;
207}
208
209int Warningf(const char* fmt, ...) {
210 LOG_FORMAT_COMMON
211
212 WARNING("warningf: " << buf);
213
214 return i;
215}
216
217int Errorf(const char* fmt, ...) {
218 LOG_FORMAT_COMMON
219
220 ERROR("errorf: " << buf);
221
222 return i;
223}
224
225int Fatalf(const char* fmt, ...) {
226 LOG_FORMAT_COMMON
227
228 FATAL("fatalf: " << buf);
229}
static constexpr size_t getPageSize() noexcept
Definition TargetInfo.h:40
void * page_align(void *p)
Definition utility.cc:29
Definition mem.c:283