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 
23 // IWYU pragma: private, include "pedigree/kernel/utilities/utility.h"
24 
25 #include "pedigree/kernel/compiler.h"
26 #include "pedigree/kernel/processor/types.h"
27 #include <stdarg.h>
28 
29 // Condense X86-ish systems into one define for utilities.
31 #if defined(X86_COMMON) || defined(HOSTED_X86_COMMON) || defined(UTILITY_LINUX)
32 #define TARGET_IS_X86
33 #endif
34 
35 #ifdef __cplusplus
36 #define NOTHROW noexcept
37 #else
38 #define NOTHROW
39 #endif
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 // This is not a prototype so doesn't fall into the block below.
46 #define StringLength(x) \
47  (IS_CONSTANT(x) ? __builtin_strlen((x)) : _StringLength(x))
48 
49 // String functions.
50 EXPORTED_PUBLIC size_t _StringLength(const char *buf) PURE;
51 EXPORTED_PUBLIC char *StringCopy(char *dest, const char *src);
52 EXPORTED_PUBLIC char *StringCopyN(char *dest, const char *src, size_t len);
53 EXPORTED_PUBLIC int StringCompare(const char *p1, const char *p2) PURE;
55 StringCompareN(const char *p1, const char *p2, size_t n) PURE;
56 EXPORTED_PUBLIC int StringCompareNOffset(
57  const char *p1, const char *p2, size_t n, size_t *offset) PURE;
58 EXPORTED_PUBLIC char *StringConcat(char *dest, const char *src);
59 EXPORTED_PUBLIC char *StringConcatN(char *dest, const char *src, size_t n);
60 EXPORTED_PUBLIC char *StringFind(const char *str, int target) PURE;
61 EXPORTED_PUBLIC char *StringReverseFind(const char *str, int target) PURE;
62 EXPORTED_PUBLIC int StringContains(const char *str, const char *search) PURE;
63 EXPORTED_PUBLIC int StringContainsN(
64  const char *str, size_t len, const char *search, size_t slen) PURE;
65 EXPORTED_PUBLIC int VStringFormat(char *buf, const char *fmt, va_list arg);
66 EXPORTED_PUBLIC int StringFormat(char *buf, const char *fmt, ...)
67  FORMAT(printf, 2, 3);
68 EXPORTED_PUBLIC unsigned long
69 StringToUnsignedLong(const char *nptr, char const **endptr, int base);
70 
71 // These work similarly to StringCompare, but only return 0 for match and -1
72 // for a failed match (rather than returning the sign of the non-matching
73 // comparison like StringCompare does) - use this if you only ever care that a
74 // match is present, and don't care about the sign of the non-matching result.
75 EXPORTED_PUBLIC int StringMatch(const char *p1, const char *p2) PURE;
77 StringMatchN(const char *p1, const char *p2, size_t n) PURE;
78 EXPORTED_PUBLIC int StringMatchNOffset(
79  const char *p1, const char *p2, size_t n, size_t *offset) PURE;
80 
81 // Performs NOTICE/ERROR/etc() on the formatted output, which allows the
82 // Pedigree log to be used from C code.
83 #if !defined(__cplusplus) || defined(IMPLEMENTING_LOG_FORMAT_FUNCTIONS)
84 // DO NOT use in C++ code, except to implement.
85 EXPORTED_PUBLIC int Debugf(const char *fmt, ...) FORMAT(printf, 1, 2);
86 EXPORTED_PUBLIC int Noticef(const char *fmt, ...) FORMAT(printf, 1, 2);
87 EXPORTED_PUBLIC int Warningf(const char *fmt, ...) FORMAT(printf, 1, 2);
88 EXPORTED_PUBLIC int Errorf(const char *fmt, ...) FORMAT(printf, 1, 2);
89 EXPORTED_PUBLIC int Fatalf(const char *fmt, ...) FORMAT(printf, 1, 2) NORETURN;
90 #endif
91 
92 // Compares the two strings with optional case-sensitivity. The offset out
93 // parameter holds the offset of a failed match in the case of a non-zero
94 // return, or the length of the string otherwise.
95 EXPORTED_PUBLIC int StringCompareCase(
96  const char *s1, const char *s2, int sensitive, size_t length,
97  size_t *offset);
98 
99 // Memory functions.
100 EXPORTED_PUBLIC void *ByteSet(void *buf, int c, size_t len);
101 EXPORTED_PUBLIC void *WordSet(void *buf, int c, size_t len);
102 EXPORTED_PUBLIC void *DoubleWordSet(void *buf, unsigned int c, size_t len);
103 EXPORTED_PUBLIC void *QuadWordSet(void *buf, unsigned long long c, size_t len);
104 EXPORTED_PUBLIC void *ForwardMemoryCopy(void *s1, const void *s2, size_t n);
105 EXPORTED_PUBLIC void *MemoryCopy(void *s1, const void *s2, size_t n);
106 EXPORTED_PUBLIC int
107 MemoryCompare(const void *p1, const void *p2, size_t len) PURE;
108 
109 // Misc utilities for paths etc
110 EXPORTED_PUBLIC const char *
111 SDirectoryName(const char *path, char *buf, size_t buflen) PURE;
112 EXPORTED_PUBLIC const char *
113 SBaseName(const char *path, char *buf, size_t buflen) PURE;
114 EXPORTED_PUBLIC const char *DirectoryName(const char *path) PURE;
115 EXPORTED_PUBLIC const char *BaseName(const char *path) PURE;
116 
117 // Character checks.
118 EXPORTED_PUBLIC int isspace(int c) NOTHROW;
119 EXPORTED_PUBLIC int isupper(int c) NOTHROW;
120 EXPORTED_PUBLIC int islower(int c) NOTHROW;
121 EXPORTED_PUBLIC int isdigit(int c) NOTHROW;
122 EXPORTED_PUBLIC int isalpha(int c) NOTHROW;
123 
124 // Built-in PRNG.
125 void random_seed(uint64_t seed);
126 EXPORTED_PUBLIC uint64_t random_next(void);
127 
128 EXPORTED_PUBLIC char toUpper(char c) PURE;
129 EXPORTED_PUBLIC char toLower(char c) PURE;
130 EXPORTED_PUBLIC int max(size_t a, size_t b) PURE;
131 EXPORTED_PUBLIC int min(size_t a, size_t b) PURE;
132 
133 // Memory allocation for C code
134 #ifndef UTILITY_LINUX
135 EXPORTED_PUBLIC void *malloc(size_t);
136 EXPORTED_PUBLIC void *calloc(size_t, size_t);
137 EXPORTED_PUBLIC void *realloc(void *, size_t);
138 EXPORTED_PUBLIC void free(void *);
139 #endif
140 
141 EXPORTED_PUBLIC size_t nextCharacter(const char *s, size_t i);
142 EXPORTED_PUBLIC size_t prevCharacter(const char *s, size_t i);
143 
145 uint8_t checksum(const uint8_t *pMemory, size_t sMemory);
146 
148 uint16_t checksum16(const uint8_t *pMemory, size_t sMemory);
149 
151 uint32_t checksum32(const uint8_t *pMemory, size_t sMemory);
152 
154 uint32_t checksum32_naive(const uint8_t *pMemory, size_t sMemory);
155 
157 uint32_t checksumPage(uintptr_t address);
158 
160 uint32_t elfHash(const char *buffer, size_t length);
161 
163 uint32_t jenkinsHash(const char *buffer, size_t length);
164 
166 uint32_t spookyHash(const char *buffer, size_t length);
167 uint64_t spookyHash64(const char *buffer, size_t length);
168 void spookyHash128(const char *buffer, size_t length, uint64_t *h1, uint64_t *h2);
169 
171 EXPORTED_PUBLIC int overlaps(const void *s1, const void *s2, size_t n) PURE;
172 
173 #ifdef __cplusplus
174 }
175 
176 // Export C++ support library header.
177 #include "pedigree/kernel/utilities/cpp.h"
178 #endif
179 
180 #endif // KERNEL_UTILITIES_LIB_H
#define FORMAT(type, idx, first)