source: mainline/boot/generic/src/str.c@ 4872160

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4872160 was 4872160, checked in by Martin Decky <martin@…>, 15 years ago

new boot infrastructure

  • more code and metadata unification
  • import of up-to-date implementations from the kernel
  • the boot loaders should behave more similarly on all platforms
  • support for deflate compressed (LZ77) boot components
    • this again allows feasible boot images to be created on mips32
  • IA64 is still not booting
    • the broken forked GNU EFI library has been removed, a replacement of the functionality is on its way
  • Property mode set to 100644
File size: 11.0 KB
Line 
1/*
2 * Copyright (c) 2001-2004 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/**
30 * @file
31 * @brief String functions.
32 *
33 * Strings and characters use the Universal Character Set (UCS). The standard
34 * strings, called just strings are encoded in UTF-8. Wide strings (encoded
35 * in UTF-32) are supported to a limited degree. A single character is
36 * represented as wchar_t.@n
37 *
38 * Overview of the terminology:@n
39 *
40 * Term Meaning
41 * -------------------- ----------------------------------------------------
42 * byte 8 bits stored in uint8_t (unsigned 8 bit integer)
43 *
44 * character UTF-32 encoded Unicode character, stored in wchar_t
45 * (signed 32 bit integer), code points 0 .. 1114111
46 * are valid
47 *
48 * ASCII character 7 bit encoded ASCII character, stored in char
49 * (usually signed 8 bit integer), code points 0 .. 127
50 * are valid
51 *
52 * string UTF-8 encoded NULL-terminated Unicode string, char *
53 *
54 * wide string UTF-32 encoded NULL-terminated Unicode string,
55 * wchar_t *
56 *
57 * [wide] string size number of BYTES in a [wide] string (excluding
58 * the NULL-terminator), size_t
59 *
60 * [wide] string length number of CHARACTERS in a [wide] string (excluding
61 * the NULL-terminator), size_t
62 *
63 * [wide] string width number of display cells on a monospace display taken
64 * by a [wide] string, size_t
65 *
66 *
67 * Overview of string metrics:@n
68 *
69 * Metric Abbrev. Type Meaning
70 * ------ ------ ------ -------------------------------------------------
71 * size n size_t number of BYTES in a string (excluding the
72 * NULL-terminator)
73 *
74 * length l size_t number of CHARACTERS in a string (excluding the
75 * null terminator)
76 *
77 * width w size_t number of display cells on a monospace display
78 * taken by a string
79 *
80 *
81 * Function naming prefixes:@n
82 *
83 * chr_ operate on characters
84 * ascii_ operate on ASCII characters
85 * str_ operate on strings
86 * wstr_ operate on wide strings
87 *
88 * [w]str_[n|l|w] operate on a prefix limited by size, length
89 * or width
90 *
91 *
92 * A specific character inside a [wide] string can be referred to by:@n
93 *
94 * pointer (char *, wchar_t *)
95 * byte offset (size_t)
96 * character index (size_t)
97 *
98 */
99
100#include <str.h>
101#include <errno.h>
102
103/** Byte mask consisting of lowest @n bits (out of 8) */
104#define LO_MASK_8(n) ((uint8_t) ((1 << (n)) - 1))
105
106/** Byte mask consisting of lowest @n bits (out of 32) */
107#define LO_MASK_32(n) ((uint32_t) ((1 << (n)) - 1))
108
109/** Byte mask consisting of highest @n bits (out of 8) */
110#define HI_MASK_8(n) (~LO_MASK_8(8 - (n)))
111
112/** Number of data bits in a UTF-8 continuation byte */
113#define CONT_BITS 6
114
115/** Decode a single character from a string.
116 *
117 * Decode a single character from a string of size @a size. Decoding starts
118 * at @a offset and this offset is moved to the beginning of the next
119 * character. In case of decoding error, offset generally advances at least
120 * by one. However, offset is never moved beyond size.
121 *
122 * @param str String (not necessarily NULL-terminated).
123 * @param offset Byte offset in string where to start decoding.
124 * @param size Size of the string (in bytes).
125 *
126 * @return Value of decoded character, U_SPECIAL on decoding error or
127 * NULL if attempt to decode beyond @a size.
128 *
129 */
130wchar_t str_decode(const char *str, size_t *offset, size_t size)
131{
132 if (*offset + 1 > size)
133 return 0;
134
135 /* First byte read from string */
136 uint8_t b0 = (uint8_t) str[(*offset)++];
137
138 /* Determine code length */
139
140 unsigned int b0_bits; /* Data bits in first byte */
141 unsigned int cbytes; /* Number of continuation bytes */
142
143 if ((b0 & 0x80) == 0) {
144 /* 0xxxxxxx (Plain ASCII) */
145 b0_bits = 7;
146 cbytes = 0;
147 } else if ((b0 & 0xe0) == 0xc0) {
148 /* 110xxxxx 10xxxxxx */
149 b0_bits = 5;
150 cbytes = 1;
151 } else if ((b0 & 0xf0) == 0xe0) {
152 /* 1110xxxx 10xxxxxx 10xxxxxx */
153 b0_bits = 4;
154 cbytes = 2;
155 } else if ((b0 & 0xf8) == 0xf0) {
156 /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
157 b0_bits = 3;
158 cbytes = 3;
159 } else {
160 /* 10xxxxxx -- unexpected continuation byte */
161 return U_SPECIAL;
162 }
163
164 if (*offset + cbytes > size)
165 return U_SPECIAL;
166
167 wchar_t ch = b0 & LO_MASK_8(b0_bits);
168
169 /* Decode continuation bytes */
170 while (cbytes > 0) {
171 uint8_t b = (uint8_t) str[(*offset)++];
172
173 /* Must be 10xxxxxx */
174 if ((b & 0xc0) != 0x80)
175 return U_SPECIAL;
176
177 /* Shift data bits to ch */
178 ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
179 cbytes--;
180 }
181
182 return ch;
183}
184
185/** Encode a single character to string representation.
186 *
187 * Encode a single character to string representation (i.e. UTF-8) and store
188 * it into a buffer at @a offset. Encoding starts at @a offset and this offset
189 * is moved to the position where the next character can be written to.
190 *
191 * @param ch Input character.
192 * @param str Output buffer.
193 * @param offset Byte offset where to start writing.
194 * @param size Size of the output buffer (in bytes).
195 *
196 * @return EOK if the character was encoded successfully, EOVERFLOW if there
197 * was not enough space in the output buffer or EINVAL if the character
198 * code was invalid.
199 */
200int chr_encode(wchar_t ch, char *str, size_t *offset, size_t size)
201{
202 if (*offset >= size)
203 return EOVERFLOW;
204
205 if (!chr_check(ch))
206 return EINVAL;
207
208 /* Unsigned version of ch (bit operations should only be done
209 on unsigned types). */
210 uint32_t cc = (uint32_t) ch;
211
212 /* Determine how many continuation bytes are needed */
213
214 unsigned int b0_bits; /* Data bits in first byte */
215 unsigned int cbytes; /* Number of continuation bytes */
216
217 if ((cc & ~LO_MASK_32(7)) == 0) {
218 b0_bits = 7;
219 cbytes = 0;
220 } else if ((cc & ~LO_MASK_32(11)) == 0) {
221 b0_bits = 5;
222 cbytes = 1;
223 } else if ((cc & ~LO_MASK_32(16)) == 0) {
224 b0_bits = 4;
225 cbytes = 2;
226 } else if ((cc & ~LO_MASK_32(21)) == 0) {
227 b0_bits = 3;
228 cbytes = 3;
229 } else {
230 /* Codes longer than 21 bits are not supported */
231 return EINVAL;
232 }
233
234 /* Check for available space in buffer */
235 if (*offset + cbytes >= size)
236 return EOVERFLOW;
237
238 /* Encode continuation bytes */
239 unsigned int i;
240 for (i = cbytes; i > 0; i--) {
241 str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));
242 cc = cc >> CONT_BITS;
243 }
244
245 /* Encode first byte */
246 str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
247
248 /* Advance offset */
249 *offset += cbytes + 1;
250
251 return EOK;
252}
253
254/** Get size of string.
255 *
256 * Get the number of bytes which are used by the string @a str (excluding the
257 * NULL-terminator).
258 *
259 * @param str String to consider.
260 *
261 * @return Number of bytes used by the string
262 *
263 */
264size_t str_size(const char *str)
265{
266 size_t size = 0;
267
268 while (*str++ != 0)
269 size++;
270
271 return size;
272}
273
274/** Get size of string with length limit.
275 *
276 * Get the number of bytes which are used by up to @a max_len first
277 * characters in the string @a str. If @a max_len is greater than
278 * the length of @a str, the entire string is measured (excluding the
279 * NULL-terminator).
280 *
281 * @param str String to consider.
282 * @param max_len Maximum number of characters to measure.
283 *
284 * @return Number of bytes used by the characters.
285 *
286 */
287size_t str_lsize(const char *str, size_t max_len)
288{
289 size_t len = 0;
290 size_t offset = 0;
291
292 while (len < max_len) {
293 if (str_decode(str, &offset, STR_NO_LIMIT) == 0)
294 break;
295
296 len++;
297 }
298
299 return offset;
300}
301
302/** Get number of characters in a string.
303 *
304 * @param str NULL-terminated string.
305 *
306 * @return Number of characters in string.
307 *
308 */
309size_t str_length(const char *str)
310{
311 size_t len = 0;
312 size_t offset = 0;
313
314 while (str_decode(str, &offset, STR_NO_LIMIT) != 0)
315 len++;
316
317 return len;
318}
319
320/** Check whether character is plain ASCII.
321 *
322 * @return True if character is plain ASCII.
323 *
324 */
325bool ascii_check(wchar_t ch)
326{
327 if ((ch >= 0) && (ch <= 127))
328 return true;
329
330 return false;
331}
332
333/** Check whether character is valid
334 *
335 * @return True if character is a valid Unicode code point.
336 *
337 */
338bool chr_check(wchar_t ch)
339{
340 if ((ch >= 0) && (ch <= 1114111))
341 return true;
342
343 return false;
344}
345
346/** Compare two NULL terminated strings.
347 *
348 * Do a char-by-char comparison of two NULL-terminated strings.
349 * The strings are considered equal iff they consist of the same
350 * characters on the minimum of their lengths.
351 *
352 * @param s1 First string to compare.
353 * @param s2 Second string to compare.
354 *
355 * @return 0 if the strings are equal, -1 if first is smaller,
356 * 1 if second smaller.
357 *
358 */
359int str_cmp(const char *s1, const char *s2)
360{
361 wchar_t c1 = 0;
362 wchar_t c2 = 0;
363
364 size_t off1 = 0;
365 size_t off2 = 0;
366
367 while (true) {
368 c1 = str_decode(s1, &off1, STR_NO_LIMIT);
369 c2 = str_decode(s2, &off2, STR_NO_LIMIT);
370
371 if (c1 < c2)
372 return -1;
373
374 if (c1 > c2)
375 return 1;
376
377 if ((c1 == 0) || (c2 == 0))
378 break;
379 }
380
381 return 0;
382}
383
384/** Copy string.
385 *
386 * Copy source string @a src to destination buffer @a dest.
387 * No more than @a size bytes are written. If the size of the output buffer
388 * is at least one byte, the output string will always be well-formed, i.e.
389 * null-terminated and containing only complete characters.
390 *
391 * @param dest Destination buffer.
392 * @param count Size of the destination buffer (must be > 0).
393 * @param src Source string.
394 *
395 */
396void str_cpy(char *dest, size_t size, const char *src)
397{
398 size_t src_off = 0;
399 size_t dest_off = 0;
400
401 wchar_t ch;
402 while ((ch = str_decode(src, &src_off, STR_NO_LIMIT)) != 0) {
403 if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
404 break;
405 }
406
407 dest[dest_off] = '\0';
408}
409
410/** @}
411 */
Note: See TracBrowser for help on using the repository browser.