1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
#include <sys/types.h>
#include <stdbool.h>
#include <stdarg.h>
#include <compel/plugins/std/syscall.h>
#include <compel/plugins/std/string.h>
#include "features.h"
static const char conv_tab[] = "0123456789abcdefghijklmnopqrstuvwxyz";
void std_dputc(int fd, char c)
{
sys_write(fd, &c, 1);
}
void std_dputs(int fd, const char *s)
{
for (; *s; s++)
std_dputc(fd, *s);
}
static size_t __std_vprint_long_hex(char *buf, size_t blen, unsigned long num, char **ps)
{
char *s = &buf[blen - 2];
buf[blen - 1] = '\0';
if (num == 0) {
*s = '0', s--;
goto done;
}
while (num > 0) {
*s = conv_tab[num % 16], s--;
num /= 16;
}
done:
s++;
*ps = s;
return blen - (s - buf);
}
static size_t __std_vprint_long(char *buf, size_t blen, long num, char **ps)
{
char *s = &buf[blen - 2];
int neg = 0;
buf[blen - 1] = '\0';
if (num < 0) {
neg = 1;
num = -num;
} else if (num == 0) {
*s = '0';
s--;
goto done;
}
while (num > 0) {
*s = (num % 10) + '0';
s--;
num /= 10;
}
if (neg) {
*s = '-';
s--;
}
done:
s++;
*ps = s;
return blen - (s - buf);
}
void std_vdprintf(int fd, const char *format, va_list args)
{
const char *s = format;
for (; *s != '\0'; s++) {
char buf[32], *t;
int along = 0;
if (*s != '%') {
std_dputc(fd, *s);
continue;
}
s++;
if (*s == 'l') {
along = 1;
s++;
if (*s == 'l')
s++;
}
switch (*s) {
case 's':
std_dputs(fd, va_arg(args, char *));
break;
case 'd':
__std_vprint_long(buf, sizeof(buf), along ? va_arg(args, long) : (long)va_arg(args, int), &t);
std_dputs(fd, t);
break;
case 'x':
__std_vprint_long_hex(buf, sizeof(buf), along ? va_arg(args, long) : (long)va_arg(args, int),
&t);
std_dputs(fd, t);
break;
}
}
}
void std_dprintf(int fd, const char *format, ...)
{
va_list args;
va_start(args, format);
std_vdprintf(fd, format, args);
va_end(args);
}
static inline bool __isspace(unsigned char c)
{
return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v';
}
static unsigned char __tolower(unsigned char c)
{
return (c <= 'Z' && c >= 'A') ? c - 'A' + 'a' : c;
}
static inline bool __isalpha(unsigned char c)
{
return ((c <= 'Z' && c >= 'A') || (c <= 'z' && c >= 'a'));
}
static inline bool __isdigit(unsigned char c)
{
return (c <= '9' && c >= '0');
}
static inline bool __isalnum(unsigned char c)
{
return (__isalpha(c) || __isdigit(c));
}
static unsigned int __conv_val(unsigned char c)
{
if (__isdigit(c))
return c - '0';
else if (__isalpha(c))
/**
* If we want the value of something which __isalpha() == true
* it has to be base > 10. 'A' = 10, 'B' = 11 ... 'Z' = 35
*/
return __tolower(c) - 'a' + 10;
return -1u;
}
unsigned long std_strtoul(const char *nptr, char **endptr, int base)
{
const char *s = nptr;
bool neg = false;
unsigned int v;
long num = 0;
if (base < 0 || base == 1 || base > 36)
goto fin;
while (__isspace(*s))
s++;
if (!*s)
goto fin;
if (*s == '-')
neg = true, s++;
if (base == 0) {
if (s[0] == '0') {
unsigned char p = __tolower(s[1]);
switch (p) {
case 'b':
base = 2, s += 2;
break;
case 'x':
base = 16, s += 2;
break;
default:
base = 8, s += 1;
break;
}
} else
base = 10;
} else if (base == 16) {
if (s[0] == '0' && __tolower(s[1]) == 'x')
s += 2;
}
for (; *s; s++) {
if (__isspace(*s))
continue;
if (!__isalnum(*s))
goto fin;
v = __conv_val(*s);
if (v == -1u || v > base)
goto fin;
num *= base;
num += v;
}
fin:
if (endptr)
*endptr = (char *)s;
return neg ? (unsigned long)-num : (unsigned long)num;
}
/*
* C compiler is free to insert implicit calls to memcmp, memset,
* memcpy and memmove, assuming they are available during linking.
* As the parasite code is not linked with libc, it must provide
* our own implementations of the above functions.
* Surely, these functions can also be called explicitly.
*
* Note: for now, not having memmove() seems OK for both gcc and clang.
*/
#ifndef ARCH_HAS_MEMCPY
void *memcpy(void *to, const void *from, size_t n)
{
size_t i;
unsigned char *cto = to;
const unsigned char *cfrom = from;
for (i = 0; i < n; ++i, ++cto, ++cfrom)
*cto = *cfrom;
return to;
}
#endif
#ifndef ARCH_HAS_MEMCMP
int memcmp(const void *cs, const void *ct, size_t count)
{
const unsigned char *su1, *su2;
int res = 0;
for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
if ((res = *su1 - *su2) != 0)
break;
return res;
}
#endif
#ifndef ARCH_HAS_MEMSET
void *memset(void *s, const int c, size_t count)
{
volatile char *dest = s;
size_t i = 0;
while (i < count)
dest[i++] = (char)c;
return s;
}
#endif
int std_strcmp(const char *cs, const char *ct)
{
unsigned char c1, c2;
while (1) {
c1 = *cs++;
c2 = *ct++;
if (c1 != c2)
return c1 < c2 ? -1 : 1;
if (!c1)
break;
}
return 0;
}
int std_strncmp(const char *cs, const char *ct, size_t count)
{
size_t i;
for (i = 0; i < count; i++) {
if (cs[i] != ct[i])
return cs[i] < ct[i] ? -1 : 1;
if (!cs[i])
break;
}
return 0;
}
|