blob: 8556fe986293b3c0b6223305fe396f3404f6cbe2 [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
45// Compare two areas of memory.
46int
Kevin O'Connor38d1a342009-04-18 16:59:47 -040047memcmp(const void *s1, const void *s2, size_t n)
Kevin O'Connor67823442009-04-13 14:14:51 -040048{
49 while (n) {
50 if (*(u8*)s1 != *(u8*)s2)
Kevin O'Connor38d1a342009-04-18 16:59:47 -040051 return *(u8*)s1 < *(u8*)s2 ? -1 : 1;
Kevin O'Connor67823442009-04-13 14:14:51 -040052 s1++;
53 s2++;
54 n--;
55 }
Kevin O'Connor38d1a342009-04-18 16:59:47 -040056 return 0;
Kevin O'Connor67823442009-04-13 14:14:51 -040057}
58
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -040059// Compare two strings.
60int
Kevin O'Connor38d1a342009-04-18 16:59:47 -040061strcmp(const char *s1, const char *s2)
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -040062{
63 for (;;) {
64 if (*s1 != *s2)
Kevin O'Connor38d1a342009-04-18 16:59:47 -040065 return *s1 < *s2 ? -1 : 1;
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -040066 if (! *s1)
Kevin O'Connor38d1a342009-04-18 16:59:47 -040067 return 0;
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -040068 s1++;
69 s2++;
70 }
71}
72
Kevin O'Connor5b199ac2009-05-06 23:23:01 -040073inline void
74memset_far(u16 d_seg, void *d_far, u8 c, size_t len)
75{
76 SET_SEG(ES, d_seg);
77 asm volatile(
78 "rep stosb %%es:(%%di)"
79 : "+c"(len), "+D"(d_far)
Kevin O'Connor41966da2012-05-28 18:57:06 -040080 : "a"(c), "m" (__segment_ES)
Kevin O'Connor5b199ac2009-05-06 23:23:01 -040081 : "cc", "memory");
82}
83
84inline void
85memset16_far(u16 d_seg, void *d_far, u16 c, size_t len)
86{
87 len /= 2;
88 SET_SEG(ES, d_seg);
89 asm volatile(
90 "rep stosw %%es:(%%di)"
91 : "+c"(len), "+D"(d_far)
Kevin O'Connor41966da2012-05-28 18:57:06 -040092 : "a"(c), "m" (__segment_ES)
Kevin O'Connor5b199ac2009-05-06 23:23:01 -040093 : "cc", "memory");
94}
95
Kevin O'Connor5e4235f2008-04-12 09:00:04 -040096void *
Kevin O'Connor567e4e32008-04-05 11:37:51 -040097memset(void *s, int c, size_t n)
98{
99 while (n)
100 ((char *)s)[--n] = c;
Kevin O'Connor5e4235f2008-04-12 09:00:04 -0400101 return s;
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400102}
103
Gerd Hoffmann0a806082010-11-29 09:42:11 +0100104void memset_fl(void *ptr, u8 val, size_t size)
105{
106 if (MODESEGMENT)
107 memset_far(FLATPTR_TO_SEG(ptr), (void*)(FLATPTR_TO_OFFSET(ptr)),
108 val, size);
109 else
110 memset(ptr, val, size);
111}
112
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500113inline void
114memcpy_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 -0400115{
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500116 SET_SEG(ES, d_seg);
117 u16 bkup_ds;
118 asm volatile(
119 "movw %%ds, %w0\n"
120 "movw %w4, %%ds\n"
121 "rep movsb (%%si),%%es:(%%di)\n"
Kevin O'Connor5b199ac2009-05-06 23:23:01 -0400122 "movw %w0, %%ds"
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500123 : "=&r"(bkup_ds), "+c"(len), "+S"(s_far), "+D"(d_far)
Kevin O'Connor41966da2012-05-28 18:57:06 -0400124 : "r"(s_seg), "m" (__segment_ES)
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500125 : "cc", "memory");
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400126}
127
Kevin O'Connor8f59aa32010-06-06 16:11:45 -0400128inline void
129memcpy_fl(void *d_fl, const void *s_fl, size_t len)
130{
131 if (MODESEGMENT)
132 memcpy_far(FLATPTR_TO_SEG(d_fl), (void*)FLATPTR_TO_OFFSET(d_fl)
133 , FLATPTR_TO_SEG(s_fl), (void*)FLATPTR_TO_OFFSET(s_fl)
134 , len);
135 else
136 memcpy(d_fl, s_fl, len);
137}
138
Kevin O'Connor942d4952009-06-10 22:44:06 -0400139void *
140#undef memcpy
141memcpy(void *d1, const void *s1, size_t len)
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500142#if MODESEGMENT == 0
Kevin O'Connor942d4952009-06-10 22:44:06 -0400143#define memcpy __builtin_memcpy
Kevin O'Connor5a1d0fc2009-06-15 23:35:30 -0400144#endif
Kevin O'Connor18b927e2008-08-29 21:14:36 -0400145{
Kevin O'Connor5a1d0fc2009-06-15 23:35:30 -0400146 SET_SEG(ES, GET_SEG(SS));
Kevin O'Connor942d4952009-06-10 22:44:06 -0400147 void *d = d1;
148 if (((u32)d1 | (u32)s1 | len) & 3) {
149 // non-aligned memcpy
150 asm volatile(
151 "rep movsb (%%esi),%%es:(%%edi)"
152 : "+c"(len), "+S"(s1), "+D"(d)
Kevin O'Connor41966da2012-05-28 18:57:06 -0400153 : "m" (__segment_ES) : "cc", "memory");
Kevin O'Connor942d4952009-06-10 22:44:06 -0400154 return d1;
155 }
156 // Common case - use 4-byte copy
Kevin O'Connor5d7b3f62009-04-19 20:05:50 -0400157 len /= 4;
158 asm volatile(
Kevin O'Connor5b199ac2009-05-06 23:23:01 -0400159 "rep movsl (%%esi),%%es:(%%edi)"
Kevin O'Connor942d4952009-06-10 22:44:06 -0400160 : "+c"(len), "+S"(s1), "+D"(d)
Kevin O'Connor41966da2012-05-28 18:57:06 -0400161 : "m" (__segment_ES) : "cc", "memory");
Kevin O'Connor942d4952009-06-10 22:44:06 -0400162 return d1;
Kevin O'Connor18b927e2008-08-29 21:14:36 -0400163}
164
Kevin O'Connorb4525a02010-07-27 01:14:11 -0400165// Copy to/from memory mapped IO. IO mem is very slow, so yield
166// periodically.
Kevin O'Connor34036962009-12-05 18:51:53 -0500167void
168iomemcpy(void *d, const void *s, u32 len)
169{
Kevin O'Connor41966da2012-05-28 18:57:06 -0400170 ASSERT32FLAT();
Kevin O'Connor34036962009-12-05 18:51:53 -0500171 yield();
Kevin O'Connorb4525a02010-07-27 01:14:11 -0400172 while (len > 3) {
Kevin O'Connor34036962009-12-05 18:51:53 -0500173 u32 copylen = len;
Kevin O'Connorad901592009-12-13 11:25:25 -0500174 if (copylen > 2048)
175 copylen = 2048;
Kevin O'Connor34036962009-12-05 18:51:53 -0500176 copylen /= 4;
Kevin O'Connorb4525a02010-07-27 01:14:11 -0400177 len -= copylen * 4;
Kevin O'Connor34036962009-12-05 18:51:53 -0500178 asm volatile(
179 "rep movsl (%%esi),%%es:(%%edi)"
180 : "+c"(copylen), "+S"(s), "+D"(d)
181 : : "cc", "memory");
182 yield();
183 }
Kevin O'Connorb4525a02010-07-27 01:14:11 -0400184 if (len)
185 // Copy any remaining bytes.
186 memcpy(d, s, len);
Kevin O'Connor34036962009-12-05 18:51:53 -0500187}
188
Kevin O'Connor18b927e2008-08-29 21:14:36 -0400189void *
Kevin O'Connorc7812932008-06-08 23:08:12 -0400190memmove(void *d, const void *s, size_t len)
191{
192 if (s >= d)
193 return memcpy(d, s, len);
194
195 d += len-1;
196 s += len-1;
197 while (len--) {
198 *(char*)d = *(char*)s;
199 d--;
200 s--;
201 }
202
203 return d;
204}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500205
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500206// Copy a string - truncating it if necessary.
207char *
208strtcpy(char *dest, const char *src, size_t len)
209{
210 char *d = dest;
Kevin O'Connordac46b12010-02-17 22:49:01 -0500211 while (--len && *src != '\0')
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500212 *d++ = *src++;
213 *d = '\0';
214 return dest;
215}
216
Kevin O'Connor2e109a62010-12-24 10:39:32 -0500217// locate first occurance of character c in the string s
218char *
219strchr(const char *s, int c)
220{
221 for (; *s; s++)
222 if (*s == c)
223 return (char*)s;
224 return NULL;
225}
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400226
Kevin O'Connor9e881a32011-01-08 12:06:54 -0500227// Remove any trailing blank characters (spaces, new lines, carriage returns)
Kevin O'Connord15b0102014-01-31 19:38:36 -0500228char *
Kevin O'Connor9e881a32011-01-08 12:06:54 -0500229nullTrailingSpace(char *buf)
230{
231 int len = strlen(buf);
232 char *end = &buf[len-1];
233 while (end >= buf && *end <= ' ')
234 *(end--) = '\0';
Kevin O'Connord15b0102014-01-31 19:38:36 -0500235 while (*buf && *buf <= ' ')
236 buf++;
237 return buf;
Kevin O'Connor9e881a32011-01-08 12:06:54 -0500238}