Add support to run SMM handler in TSEG instead of ASEG
[coreboot.git] / src / include / string.h
1 #ifndef STRING_H
2 #define STRING_H
3
4 #include <stddef.h>
5 #include <stdlib.h>
6
7 void *memcpy(void *dest, const void *src, size_t n);
8 void *memmove(void *dest, const void *src, size_t n);
9 void *memset(void *s, int c, size_t n);
10 int memcmp(const void *s1, const void *s2, size_t n);
11 void *memchr(const void *s, int c, size_t n);
12 #if !defined(__PRE_RAM__)
13 int sprintf(char * buf, const char *fmt, ...);
14 #endif
15
16 // simple string functions
17
18 static inline size_t strnlen(const char *src, size_t max)
19 {
20         size_t i = 0;
21         while((*src++) && (i < max)) {
22                 i++;
23         }
24         return i;
25 }
26
27 static inline size_t strlen(const char *src)
28 {
29         size_t i = 0;
30         while(*src++) {
31                 i++;
32         }
33         return i;
34 }
35
36 static inline char *strchr(const char *s, int c)
37 {
38         for (; *s; s++) {
39                 if (*s == c)
40                         return (char *) s;
41         }
42         return 0;
43 }
44
45 #if !defined(__PRE_RAM__)
46 static inline char *strdup(const char *s)
47 {
48         size_t sz = strlen(s) + 1;
49         char *d = malloc(sz);
50         memcpy(d, s, sz);
51         return d;
52 }
53 #endif
54
55 static inline char *strncpy(char *to, const char *from, int count)
56 {
57         register char *ret = to;
58
59         while (count > 0) {
60                 count--;
61                 if ((*to++ = *from++) == '\0')
62                         break;
63         }
64
65         while (count > 0) {
66                 count--;
67                 *to++ = '\0';
68         }
69         return ret;
70 }
71
72 static inline void strcpy(char *dst, const char *src)
73 {
74         while (*src)
75                 *dst++ = *src++;
76 }
77
78 static inline int strcmp(const char *s1, const char *s2)
79 {
80         int r;
81
82         while ((r = (*s1 - *s2)) == 0 && *s1) {
83                 s1++;
84                 s2++;
85         }
86         return r;
87 }
88
89 static inline int strncmp(const char *s1, const char *s2, int maxlen)
90 {
91         int i;
92
93         for (i = 0; i < maxlen; i++) {
94                 if (s1[i] != s2[i])
95                         return s1[i] - s2[i];
96         }
97
98         return 0;
99 }
100
101 static inline int isspace(int c)
102 {
103         switch (c) {
104         case ' ': case '\f': case '\n':
105         case '\r': case '\t': case '\v':
106                 return 1;
107         default:
108                 return 0;
109         }
110 }
111
112 static inline int isdigit(int c)
113 {
114         return (c >= '0' && c <= '9');
115 }
116
117 static inline int isxdigit(int c)
118 {
119         return ((c >= '0' && c <= '9') ||
120                 (c >= 'a' && c <= 'f') ||
121                 (c >= 'A' && c <= 'F'));
122 }
123
124 static inline int isupper(int c)
125 {
126         return (c >= 'A' && c <= 'Z');
127 }
128
129 static inline int islower(int c)
130 {
131         return (c >= 'a' && c <= 'z');
132 }
133
134 static inline int toupper(int c)
135 {
136         if (islower(c))
137                 c -= 'a'-'A';
138         return c;
139 }
140
141 static inline int tolower(int c)
142 {
143         if (isupper(c))
144                 c -= 'A'-'a';
145         return c;
146 }
147 #endif /* STRING_H */