The Pedigree Project 0.1
lib.h
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#ifndef KERNEL_UTILITIES_LIB_H
21#define KERNEL_UTILITIES_LIB_H
22#include <config.h>
23
24// IWYU pragma: private, include "pedigree/kernel/utilities/utility.h"
25
26#include "pedigree/kernel/compiler.h"
27#include "pedigree/kernel/processor/types.h"
28
29#include <stdarg.h>
30#if UTILITY_LINUX && defined(__APPLE__)
31#include <ctype.h>
32#endif
33
34// Host utilities can run on architectures other than the Pedigree target.
35#if X86_COMMON || HOSTED_X86_COMMON || (UTILITY_LINUX && (defined(__i386__) || defined(__x86_64__)))
36#define TARGET_IS_X86
37#endif
38
39#if defined(__cplusplus) && !defined(__APPLE__)
40#define NOTHROW noexcept
41#else
42#define NOTHROW
43#endif
44
45#ifdef __cplusplus
46extern "C" {
47#endif
48
49// This is not a prototype so doesn't fall into the block below.
50#define StringLength(x) (IS_CONSTANT(x) ? __builtin_strlen((x)) : _StringLength(x))
51
52// Bounded version of StringLength that avoids reading past the given
53// maximum length. Useful for buffers that might have a much smaller string in
54// them but doesn't overflow a buffer that doesn't have a null character at the
55// end of it.
56#define BoundedStringLength(x, y) \
57 (IS_CONSTANT(x) ? __builtin_strlen((x)) : _BoundedStringLength(x, y))
58
59#define ConstStringLength(x) static_assert(IS_CONSTANT((x))), __builtin_strlen((x))
60
61// String functions.
62EXPORTED_PUBLIC size_t _StringLength(const char* buf) PURE;
63EXPORTED_PUBLIC size_t _BoundedStringLength(const char* buf, size_t maxlen) PURE;
64EXPORTED_PUBLIC char* StringCopy(char* dest, const char* src);
65EXPORTED_PUBLIC char* StringCopyN(char* dest, const char* src, size_t len);
66EXPORTED_PUBLIC int StringCompare(const char* p1, const char* p2) PURE;
67EXPORTED_PUBLIC int StringCompareN(const char* p1, const char* p2, size_t n) PURE;
68EXPORTED_PUBLIC int StringCompareNOffset(const char* p1, const char* p2, size_t n,
69 size_t* offset) PURE;
70EXPORTED_PUBLIC char* StringConcat(char* dest, const char* src);
71EXPORTED_PUBLIC char* StringConcatN(char* dest, const char* src, size_t n);
72EXPORTED_PUBLIC char* StringFind(const char* str, int target) PURE;
73EXPORTED_PUBLIC char* StringReverseFind(const char* str, int target) PURE;
74EXPORTED_PUBLIC int StringContains(const char* str, const char* search) PURE;
75EXPORTED_PUBLIC int StringContainsN(const char* str, size_t len, const char* search,
76 size_t slen) PURE;
77EXPORTED_PUBLIC int VStringFormat(char* buf, const char* fmt, va_list arg);
78EXPORTED_PUBLIC int StringFormat(char* buf, const char* fmt, ...) FORMAT(printf, 2, 3);
79EXPORTED_PUBLIC unsigned long StringToUnsignedLong(const char* nptr, char** endptr, int base);
80
81// These work similarly to StringCompare, but only return 0 for match and -1
82// for a failed match (rather than returning the sign of the non-matching
83// comparison like StringCompare does) - use this if you only ever care that a
84// match is present, and don't care about the sign of the non-matching result.
85EXPORTED_PUBLIC int StringMatch(const char* p1, const char* p2) PURE;
86EXPORTED_PUBLIC int StringMatchN(const char* p1, const char* p2, size_t n) PURE;
87EXPORTED_PUBLIC int StringMatchNOffset(const char* p1, const char* p2, size_t n,
88 size_t* offset) PURE;
89
90// Performs NOTICE/ERROR/etc() on the formatted output, which allows the
91// Pedigree log to be used from C code.
92#if !defined(__cplusplus) || defined(IMPLEMENTING_LOG_FORMAT_FUNCTIONS)
93// DO NOT use in C++ code, except to implement.
94EXPORTED_PUBLIC int Debugf(const char* fmt, ...) FORMAT(printf, 1, 2);
95EXPORTED_PUBLIC int Noticef(const char* fmt, ...) FORMAT(printf, 1, 2);
96EXPORTED_PUBLIC int Warningf(const char* fmt, ...) FORMAT(printf, 1, 2);
97EXPORTED_PUBLIC int Errorf(const char* fmt, ...) FORMAT(printf, 1, 2);
98EXPORTED_PUBLIC int Fatalf(const char* fmt, ...) FORMAT(printf, 1, 2) NORETURN;
99#endif
100
101// Compares the two strings with optional case-sensitivity. The offset out
102// parameter holds the offset of a failed match in the case of a non-zero
103// return, or the length of the string otherwise.
104EXPORTED_PUBLIC int StringCompareCase(const char* s1, const char* s2, int sensitive, size_t length,
105 size_t* offset);
106
107// Memory functions.
108EXPORTED_PUBLIC void* ByteSet(void* buf, int c, size_t len);
109EXPORTED_PUBLIC void* WordSet(void* buf, int c, size_t len);
110EXPORTED_PUBLIC void* DoubleWordSet(void* buf, unsigned int c, size_t len);
111EXPORTED_PUBLIC void* QuadWordSet(void* buf, unsigned long long c, size_t len);
112EXPORTED_PUBLIC void* ForwardMemoryCopy(void* s1, const void* s2, size_t n);
113EXPORTED_PUBLIC void* MemoryCopy(void* s1, const void* s2, size_t n);
114EXPORTED_PUBLIC int MemoryCompare(const void* p1, const void* p2, size_t len) PURE;
115
116// Misc utilities for paths etc
117EXPORTED_PUBLIC const char* SDirectoryName(const char* path, char* buf, size_t buflen) PURE;
118EXPORTED_PUBLIC const char* SBaseName(const char* path, char* buf, size_t buflen) PURE;
119EXPORTED_PUBLIC const char* DirectoryName(const char* path) PURE;
120EXPORTED_PUBLIC const char* BaseName(const char* path) PURE;
121
122// Character checks.
123#if !(UTILITY_LINUX && defined(__APPLE__))
124EXPORTED_PUBLIC int isspace(int c) NOTHROW;
125EXPORTED_PUBLIC int isupper(int c) NOTHROW;
126EXPORTED_PUBLIC int islower(int c) NOTHROW;
127EXPORTED_PUBLIC int isdigit(int c) NOTHROW;
128EXPORTED_PUBLIC int isalpha(int c) NOTHROW;
129#endif
130
131// Built-in PRNG.
132void random_seed(uint64_t seed);
133EXPORTED_PUBLIC uint64_t random_next(void);
134
135// Hardware-backed random bytes. Returns the number of bytes produced and
136// returns zero when the active architecture has no usable hardware source.
137EXPORTED_PUBLIC size_t hardware_random_bytes(void* buffer, size_t length);
138
139// Fails closed until hardware or exactly 32 caller-validated entropy bytes seed it.
140EXPORTED_PUBLIC size_t secure_random_bytes(void* buffer, size_t length);
141EXPORTED_PUBLIC int secure_random_seed(const void* buffer, size_t length);
142
143EXPORTED_PUBLIC char toUpper(char c) PURE;
144EXPORTED_PUBLIC char toLower(char c) PURE;
145EXPORTED_PUBLIC int max(size_t a, size_t b) PURE;
146EXPORTED_PUBLIC int min(size_t a, size_t b) PURE;
147
148// Memory allocation for C code
149#if !(UTILITY_LINUX || HOSTED)
150EXPORTED_PUBLIC void* malloc(size_t) MALLOC ALLOC_SIZE(1) C_NOTHROW MUST_USE_RESULT;
151EXPORTED_PUBLIC void* calloc(size_t, size_t) MALLOC ALLOC_SIZE(1, 2) C_NOTHROW MUST_USE_RESULT;
152EXPORTED_PUBLIC void* realloc(void*, size_t) ALLOC_SIZE(2) C_NOTHROW MUST_USE_RESULT;
153EXPORTED_PUBLIC void free(void*) C_NOTHROW;
154#endif
155
156EXPORTED_PUBLIC size_t nextCharacter(const char* s, size_t i);
157EXPORTED_PUBLIC size_t prevCharacter(const char* s, size_t i);
158
160uint8_t checksum(const uint8_t* pMemory, size_t sMemory);
161
163uint16_t checksum16(const uint16_t* pMemory, size_t sMemory);
164
166uint32_t checksum32(const uint32_t* pMemory, size_t sMemory);
167
169uint32_t checksum32_naive(const uint32_t* pMemory, size_t sMemory);
170
172uint32_t checksumPage(uintptr_t address);
173
175uint32_t elfHash(const char* buffer, size_t length);
176
178uint32_t jenkinsHash(const char* buffer, size_t length);
179
181EXPORTED_PUBLIC uint32_t spookyHash(const char* buffer, size_t length);
182EXPORTED_PUBLIC uint64_t spookyHash64(const char* buffer, size_t length);
183EXPORTED_PUBLIC void spookyHash128(const char* buffer, size_t length, uint64_t* h1, uint64_t* h2);
184
186EXPORTED_PUBLIC int overlaps(const void* s1, const void* s2, size_t n) PURE;
187
188#ifdef __cplusplus
189}
190
191// Export C++ support library header.
192#include "pedigree/kernel/utilities/cpp.h"
193#endif
194
195#endif // KERNEL_UTILITIES_LIB_H
#define FORMAT(type, idx, first)