blob: adb8198f84f44892cd5918c4766d6a8a96868426 [file] [log] [blame]
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -04001// String manipulation functions.
Kevin O'Connor9521e262008-07-04 13:04:29 -04002//
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -04003// Copyright (C) 2008-2013 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connor9521e262008-07-04 13:04:29 -04004//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05005// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connor9521e262008-07-04 13:04:29 -04006
Kevin O'Connor3df600b2013-09-14 19:28:55 -04007#include "stacks.h" // yield
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -04008#include "string.h" // memcpy
9#include "farptr.h" // SET_SEG
Kevin O'Connora5826b52009-10-24 17:57:29 -040010
Kevin O'Connor10ad7992009-10-24 11:06:08 -040011
12/****************************************************************
Kevin O'Connor10ad7992009-10-24 11:06:08 -040013 * String ops
14 ****************************************************************/
15
Kevin O'Connor2e7ab8b2008-03-29 14:29:35 -040016// Sum the bytes in the specified area.
17u8
Kevin O'Connor94fd47e2009-02-15 15:21:10 -050018checksum_far(u16 buf_seg, void *buf_far, u32 len)
Kevin O'Connor2e7ab8b2008-03-29 14:29:35 -040019{
Kevin O'Connor8b267cb2009-01-19 19:25:21 -050020 SET_SEG(ES, buf_seg);
Kevin O'Connor2e7ab8b2008-03-29 14:29:35 -040021 u32 i;
22 u8 sum = 0;
23 for (i=0; i<len; i++)
Kevin O'Connor94fd47e2009-02-15 15:21:10 -050024 sum += GET_VAR(ES, ((u8*)buf_far)[i]);
Kevin O'Connor2e7ab8b2008-03-29 14:29:35 -040025 return sum;
26}
27
Kevin O'Connor8b267cb2009-01-19 19:25:21 -050028u8
Kevin O'Connor94fd47e2009-02-15 15:21:10 -050029checksum(void *buf, u32 len)
Kevin O'Connor8b267cb2009-01-19 19:25:21 -050030{
31 return checksum_far(GET_SEG(SS), buf, len);
32}
33
Kevin O'Connor67823442009-04-13 14:14:51 -040034size_t
35strlen(const char *s)
36{
37 if (__builtin_constant_p(s))
38 return __builtin_strlen(s);
39 const char *p = s;
40 while (*p)
41 p++;
42 return p-s;
43}
44
Paolo Bonzini60e0e552015-01-02 12:32:25 -050045int
46memcmp_far(u16 s1seg, const void *s1, u16 s2seg, const void *s2, size_t n)
47{
48 while (n--) {
49 int d = GET_FARVAR(s1seg, *(u8*)s1) - GET_FARVAR(s2seg, *(u8*)s2);
50 if (d)
51 return d < 0 ? -1 : 1;
52 s1++;
53 s2++;
54 }
55 return 0;
56}
57
Kevin O'Connor67823442009-04-13 14:14:51 -040058// Compare two areas of memory.
59int
Kevin O'Connor38d1a342009-04-18 16:59:47 -040060memcmp(const void *s1, const void *s2, size_t n)
Kevin O'Connor67823442009-04-13 14:14:51 -040061{
62 while (n) {
63 if (*(u8*)s1 != *(u8*)s2)
Kevin O'Connor38d1a342009-04-18 16:59:47 -040064 return *(u8*)s1 < *(u8*)s2 ? -1 : 1;
Kevin O'Connor67823442009-04-13 14:14:51 -040065 s1++;
66 s2++;
67 n--;
68 }
Kevin O'Connor38d1a342009-04-18 16:59:47 -040069 return 0;
Kevin O'Connor67823442009-04-13 14:14:51 -040070}
71
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -040072// Compare two strings.
73int
Kevin O'Connor38d1a342009-04-18 16:59:47 -040074strcmp(const char *s1, const char *s2)
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -040075{
76 for (;;) {
77 if (*s1 != *s2)
Kevin O'Connor38d1a342009-04-18 16:59:47 -040078 return *s1 < *s2 ? -1 : 1;
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -040079 if (! *s1)
Kevin O'Connor38d1a342009-04-18 16:59:47 -040080 return 0;
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -040081 s1++;
82 s2++;
83 }
84}
85
Kevin O'Connor5b199ac2009-05-06 23:23:01 -040086inline void
87memset_far(u16 d_seg, void *d_far, u8 c, size_t len)
88{
89 SET_SEG(ES, d_seg);
90 asm volatile(
91 "rep stosb %%es:(%%di)"
92 : "+c"(len), "+D"(d_far)
Kevin O'Connor41966da2012-05-28 18:57:06 -040093 : "a"(c), "m" (__segment_ES)
Kevin O'Connor5b199ac2009-05-06 23:23:01 -040094 : "cc", "memory");
95}
96
97inline void
98memset16_far(u16 d_seg, void *d_far, u16 c, size_t len)
99{
100 len /= 2;
101 SET_SEG(ES, d_seg);
102 asm volatile(
103 "rep stosw %%es:(%%di)"
104 : "+c"(len), "+D"(d_far)
Kevin O'Connor41966da2012-05-28 18:57:06 -0400105 : "a"(c), "m" (__segment_ES)
Kevin O'Connor5b199ac2009-05-06 23:23:01 -0400106 : "cc", "memory");
107}
108
Kevin O'Connor5e4235f2008-04-12 09:00:04 -0400109void *
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400110memset(void *s, int c, size_t n)
111{
112 while (n)
113 ((char *)s)[--n] = c;
Kevin O'Connor5e4235f2008-04-12 09:00:04 -0400114 return s;
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400115}
116
Gerd Hoffmann0a806082010-11-29 09:42:11 +0100117void memset_fl(void *ptr, u8 val, size_t size)
118{
119 if (MODESEGMENT)
120 memset_far(FLATPTR_TO_SEG(ptr), (void*)(FLATPTR_TO_OFFSET(ptr)),
121 val, size);
122 else
123 memset(ptr, val, size);
124}
125
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500126inline void
127memcpy_far(u16 d_seg, void *d_far, u16 s_seg, const void *s_far, size_t len)
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400128{
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500129 SET_SEG(ES, d_seg);
130 u16 bkup_ds;
131 asm volatile(
132 "movw %%ds, %w0\n"
133 "movw %w4, %%ds\n"
134 "rep movsb (%%si),%%es:(%%di)\n"
Kevin O'Connor5b199ac2009-05-06 23:23:01 -0400135 "movw %w0, %%ds"
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500136 : "=&r"(bkup_ds), "+c"(len), "+S"(s_far), "+D"(d_far)
Kevin O'Connor41966da2012-05-28 18:57:06 -0400137 : "r"(s_seg), "m" (__segment_ES)
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500138 : "cc", "memory");
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400139}
140
Kevin O'Connor8f59aa32010-06-06 16:11:45 -0400141inline void
142memcpy_fl(void *d_fl, const void *s_fl, size_t len)
143{
144 if (MODESEGMENT)
145 memcpy_far(FLATPTR_TO_SEG(d_fl), (void*)FLATPTR_TO_OFFSET(d_fl)
146 , FLATPTR_TO_SEG(s_fl), (void*)FLATPTR_TO_OFFSET(s_fl)
147 , len);
148 else
149 memcpy(d_fl, s_fl, len);
150}
151
Kevin O'Connor942d4952009-06-10 22:44:06 -0400152void *
153#undef memcpy
154memcpy(void *d1, const void *s1, size_t len)
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500155#if MODESEGMENT == 0
Kevin O'Connor942d4952009-06-10 22:44:06 -0400156#define memcpy __builtin_memcpy
Kevin O'Connor5a1d0fc2009-06-15 23:35:30 -0400157#endif
Kevin O'Connor18b927e2008-08-29 21:14:36 -0400158{
Kevin O'Connor5a1d0fc2009-06-15 23:35:30 -0400159 SET_SEG(ES, GET_SEG(SS));
Kevin O'Connor942d4952009-06-10 22:44:06 -0400160 void *d = d1;
161 if (((u32)d1 | (u32)s1 | len) & 3) {
162 // non-aligned memcpy
163 asm volatile(
164 "rep movsb (%%esi),%%es:(%%edi)"
165 : "+c"(len), "+S"(s1), "+D"(d)
Kevin O'Connor41966da2012-05-28 18:57:06 -0400166 : "m" (__segment_ES) : "cc", "memory");
Kevin O'Connor942d4952009-06-10 22:44:06 -0400167 return d1;
168 }
169 // Common case - use 4-byte copy
Kevin O'Connor5d7b3f62009-04-19 20:05:50 -0400170 len /= 4;
171 asm volatile(
Kevin O'Connor5b199ac2009-05-06 23:23:01 -0400172 "rep movsl (%%esi),%%es:(%%edi)"
Kevin O'Connor942d4952009-06-10 22:44:06 -0400173 : "+c"(len), "+S"(s1), "+D"(d)
Kevin O'Connor41966da2012-05-28 18:57:06 -0400174 : "m" (__segment_ES) : "cc", "memory");
Kevin O'Connor942d4952009-06-10 22:44:06 -0400175 return d1;
Kevin O'Connor18b927e2008-08-29 21:14:36 -0400176}
177
Kevin O'Connorb4525a02010-07-27 01:14:11 -0400178// Copy to/from memory mapped IO. IO mem is very slow, so yield
179// periodically.
Kevin O'Connor34036962009-12-05 18:51:53 -0500180void
181iomemcpy(void *d, const void *s, u32 len)
182{
Kevin O'Connor41966da2012-05-28 18:57:06 -0400183 ASSERT32FLAT();
Kevin O'Connor34036962009-12-05 18:51:53 -0500184 yield();
Kevin O'Connorb4525a02010-07-27 01:14:11 -0400185 while (len > 3) {
Kevin O'Connor34036962009-12-05 18:51:53 -0500186 u32 copylen = len;
Kevin O'Connorad901592009-12-13 11:25:25 -0500187 if (copylen > 2048)
188 copylen = 2048;
Kevin O'Connor34036962009-12-05 18:51:53 -0500189 copylen /= 4;
Kevin O'Connorb4525a02010-07-27 01:14:11 -0400190 len -= copylen * 4;
Kevin O'Connor34036962009-12-05 18:51:53 -0500191 asm volatile(
192 "rep movsl (%%esi),%%es:(%%edi)"
193 : "+c"(copylen), "+S"(s), "+D"(d)
194 : : "cc", "memory");
195 yield();
196 }
Kevin O'Connorb4525a02010-07-27 01:14:11 -0400197 if (len)
198 // Copy any remaining bytes.
199 memcpy(d, s, len);
Kevin O'Connor34036962009-12-05 18:51:53 -0500200}
201
Kevin O'Connor18b927e2008-08-29 21:14:36 -0400202void *
Kevin O'Connorc7812932008-06-08 23:08:12 -0400203memmove(void *d, const void *s, size_t len)
204{
205 if (s >= d)
206 return memcpy(d, s, len);
207
208 d += len-1;
209 s += len-1;
210 while (len--) {
211 *(char*)d = *(char*)s;
212 d--;
213 s--;
214 }
215
216 return d;
217}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500218
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500219// Copy a string - truncating it if necessary.
220char *
221strtcpy(char *dest, const char *src, size_t len)
222{
223 char *d = dest;
Kevin O'Connordac46b12010-02-17 22:49:01 -0500224 while (--len && *src != '\0')
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500225 *d++ = *src++;
226 *d = '\0';
227 return dest;
228}
229
Stefan Weil6bcacf72015-10-02 08:46:40 +0200230// locate first occurrence of character c in the string s
Kevin O'Connor2e109a62010-12-24 10:39:32 -0500231char *
232strchr(const char *s, int c)
233{
234 for (; *s; s++)
235 if (*s == c)
236 return (char*)s;
237 return NULL;
238}
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400239
Kevin O'Connor9e881a32011-01-08 12:06:54 -0500240// Remove any trailing blank characters (spaces, new lines, carriage returns)
Kevin O'Connord15b0102014-01-31 19:38:36 -0500241char *
Kevin O'Connor9e881a32011-01-08 12:06:54 -0500242nullTrailingSpace(char *buf)
243{
244 int len = strlen(buf);
245 char *end = &buf[len-1];
246 while (end >= buf && *end <= ' ')
247 *(end--) = '\0';
Kevin O'Connord15b0102014-01-31 19:38:36 -0500248 while (*buf && *buf <= ' ')
249 buf++;
250 return buf;
Kevin O'Connor9e881a32011-01-08 12:06:54 -0500251}