Subversion Repositories HelenOS

Rev

Rev 4207 | Rev 4209 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4011 svoboda 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
 
4014 decky 29
/** @addtogroup generic
4011 svoboda 30
 * @{
31
 */
32
 
33
/**
34
 * @file
4014 decky 35
 * @brief Miscellaneous functions.
4011 svoboda 36
 */
37
 
38
#include <string.h>
39
#include <print.h>
40
#include <cpu.h>
41
#include <arch/asm.h>
42
#include <arch.h>
4208 svoboda 43
#include <errno.h>
4011 svoboda 44
#include <console/kconsole.h>
45
 
4179 decky 46
char invalch = '?';
47
 
4198 svoboda 48
/** Byte mask consisting of lowest @n bits (out of eight). */
4196 svoboda 49
#define LO_MASK_8(n) ((uint8_t)((1 << (n)) - 1))
50
 
4198 svoboda 51
/** Byte mask consisting of lowest @n bits (out of 32). */
52
#define LO_MASK_32(n) ((uint32_t)((1 << (n)) - 1))
53
 
54
/** Byte mask consisting of highest @n bits (out of eight). */
55
#define HI_MASK_8(n) (~LO_MASK_8(8 - (n)))
56
 
4196 svoboda 57
/** Number of data bits in a UTF-8 continuation byte. */
58
#define CONT_BITS 6
59
 
4200 svoboda 60
/** Decode a single character from a substring.
4175 decky 61
 *
4200 svoboda 62
 * Decode a single character from a substring of size @a sz. Decoding starts
63
 * at @a offset and this offset is moved to the beginning of the next
64
 * character. In case of decoding error, offset generally advances at least
65
 * by one. However, offset is never moved beyond (str + sz).
4175 decky 66
 *
4200 svoboda 67
 * @param str   String (not necessarily NULL-terminated).
4175 decky 68
 * @param index Index (counted in plain characters) where to start
69
 *              the decoding.
4200 svoboda 70
 * @param limit Size of the substring.
4175 decky 71
 *
4200 svoboda 72
 * @return  Value of decoded character or '?' on decoding error.
4175 decky 73
 */
4200 svoboda 74
wchar_t chr_decode(const char *str, size_t *offset, size_t sz)
4175 decky 75
{
4196 svoboda 76
    uint8_t b0, b;          /* Bytes read from str. */
77
    wchar_t ch;
78
 
79
    int b0_bits;        /* Data bits in first byte. */
80
    int cbytes;     /* Number of continuation bytes. */
81
 
4200 svoboda 82
    if (*offset + 1 > sz)
4179 decky 83
        return invalch;
4196 svoboda 84
 
4200 svoboda 85
    b0 = (uint8_t) str[(*offset)++];
4196 svoboda 86
 
87
    /* Determine code length. */
88
 
89
    if ((b0 & 0x80) == 0) {
90
        /* 0xxxxxxx (Plain ASCII) */
91
        b0_bits = 7;
92
        cbytes = 0;
93
    } else if ((b0 & 0xe0) == 0xc0) {
94
        /* 110xxxxx 10xxxxxx */
95
        b0_bits = 5;
96
        cbytes = 1;
97
    } else if ((b0 & 0xf0) == 0xe0) {
98
        /* 1110xxxx 10xxxxxx 10xxxxxx */
99
        b0_bits = 4;
100
        cbytes = 2;
101
    } else if ((b0 & 0xf8) == 0xf0) {
102
        /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
103
        b0_bits = 3;
104
        cbytes = 3;
105
    } else {
106
        /* 10xxxxxx -- unexpected continuation byte. */
107
        return invalch;
4179 decky 108
    }
4196 svoboda 109
 
4200 svoboda 110
    if (*offset + cbytes > sz) {
4196 svoboda 111
        return invalch;
4179 decky 112
    }
4196 svoboda 113
 
114
    ch = b0 & LO_MASK_8(b0_bits);
115
 
116
    /* Decode continuation bytes. */
117
    while (cbytes > 0) {
4200 svoboda 118
        b = (uint8_t) str[(*offset)++];
4196 svoboda 119
 
120
        /* Must be 10xxxxxx. */
121
        if ((b & 0xc0) != 0x80) {
4179 decky 122
            return invalch;
4196 svoboda 123
        }
124
 
125
        /* Shift data bits to ch. */
126
        ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
127
        --cbytes;
4179 decky 128
    }
4196 svoboda 129
 
130
    return ch;
4175 decky 131
}
132
 
4200 svoboda 133
/** Encode a single character to string representation.
4011 svoboda 134
 *
4200 svoboda 135
 * Encode a single character to string representation (i.e. UTF-8) and store
136
 * it into a buffer at @a offset. Encoding starts at @a offset and this offset
137
 * is moved to the position where the next character can be written to.
4011 svoboda 138
 *
4200 svoboda 139
 * @param ch        Input character.
140
 * @param str       Output buffer.
141
 * @param offset    Offset (in bytes) where to start writing.
142
 * @param sz        Size of the output buffer.
4179 decky 143
 *
4208 svoboda 144
 * @return EOK if the character was encoded successfully, EOVERFLOW if there
145
 *     was not enough space in the output buffer or EINVAL if the character
146
 *     code was invalid.
4179 decky 147
 */
4208 svoboda 148
int chr_encode(const wchar_t ch, char *str, size_t *offset, size_t sz)
4179 decky 149
{
4198 svoboda 150
    uint32_t cc;        /* Unsigned version of ch. */
151
 
152
    int cbytes;     /* Number of continuation bytes. */
153
    int b0_bits;        /* Number of data bits in first byte. */
154
    int i;
155
 
4200 svoboda 156
    if (*offset >= sz)
4208 svoboda 157
        return EOVERFLOW;
4198 svoboda 158
 
159
    if (ch < 0)
4208 svoboda 160
        return EINVAL;
4198 svoboda 161
 
162
    /* Bit operations should only be done on unsigned numbers. */
163
    cc = (uint32_t) ch;
164
 
165
    /* Determine how many continuation bytes are needed. */
166
    if ((cc & ~LO_MASK_32(7)) == 0) {
167
        b0_bits = 7;
168
        cbytes = 0;
169
    } else if ((cc & ~LO_MASK_32(11)) == 0) {
170
        b0_bits = 5;
171
        cbytes = 1;
172
    } else if ((cc & ~LO_MASK_32(16)) == 0) {
173
        b0_bits = 4;
174
        cbytes = 2;
175
    } else if ((cc & ~LO_MASK_32(21)) == 0) {
176
        b0_bits = 3;
177
        cbytes = 3;
178
    } else {
179
        /* Codes longer than 21 bits are not supported. */
4208 svoboda 180
        return EINVAL;
4179 decky 181
    }
4198 svoboda 182
 
183
    /* Check for available space in buffer. */
4200 svoboda 184
    if (*offset + cbytes >= sz)
4208 svoboda 185
        return EOVERFLOW;
4198 svoboda 186
 
187
    /* Encode continuation bytes. */
188
    for (i = cbytes; i > 0; --i) {
4200 svoboda 189
        str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));
4198 svoboda 190
        cc = cc >> CONT_BITS;
4179 decky 191
    }
4198 svoboda 192
 
193
    /* Encode first byte. */
4200 svoboda 194
    str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
4198 svoboda 195
 
4200 svoboda 196
    /* Advance offset. */
197
    *offset += (1 + cbytes);
4179 decky 198
 
4208 svoboda 199
    return EOK;
4179 decky 200
}
201
 
4205 svoboda 202
/** Get size of string, with length limit.
4179 decky 203
 *
4205 svoboda 204
 * Get the number of bytes which are used by up to @a max_len first
205
 * characters in the string @a str. If @a max_len is greater than
206
 * the length of @a str, the entire string is measured.
4179 decky 207
 *
4205 svoboda 208
 * @param str   String to consider.
209
 * @param count Maximum number of characters to measure.
4179 decky 210
 *
211
 * @return Number of bytes used by the characters.
212
 */
4205 svoboda 213
size_t str_lsize(const char *str, count_t max_len)
4179 decky 214
{
4205 svoboda 215
    count_t len = 0;
216
    size_t cur = 0;
217
    size_t prev;
4199 svoboda 218
    wchar_t ch;
4205 svoboda 219
 
4199 svoboda 220
    while (true) {
4205 svoboda 221
        prev = cur;
222
        if (len >= max_len)
4199 svoboda 223
            break;
4205 svoboda 224
        ch = chr_decode(str, &cur, UTF8_NO_LIMIT);
4199 svoboda 225
        if (ch == '\0') break;
226
 
4205 svoboda 227
        len++;
4179 decky 228
    }
4205 svoboda 229
 
230
    return prev;
4179 decky 231
}
232
 
233
/** Check whether character is plain ASCII.
234
 *
235
 * @return True if character is plain ASCII.
236
 *
237
 */
238
bool ascii_check(const wchar_t ch)
239
{
240
    if ((ch >= 0) && (ch <= 127))
241
        return true;
242
 
243
    return false;
244
}
245
 
246
/** Check whether character is Unicode.
247
 *
248
 * @return True if character is valid Unicode code point.
249
 */
250
bool unicode_check(const wchar_t ch)
251
{
252
    if ((ch >= 0) && (ch <= 1114111))
253
        return true;
254
 
255
    return false;
256
}
257
 
4207 svoboda 258
/** Return number of bytes the string occupies.
4179 decky 259
 *
4207 svoboda 260
 * @param str A string.
261
 * @return Number of bytes in @a str excluding the null terminator.
4011 svoboda 262
 */
4207 svoboda 263
size_t str_size(const char *str)
4011 svoboda 264
{
4179 decky 265
    size_t size;
4207 svoboda 266
 
267
    size = 0;
268
    while (*str++ != '\0')
269
        ++size;
270
 
4179 decky 271
    return size;
272
}
273
 
4205 svoboda 274
/** Return number of characters in a string.
4179 decky 275
 *
4205 svoboda 276
 * @param str NULL-terminated string.
277
 * @return Number of characters in string.
4179 decky 278
 */
4205 svoboda 279
count_t str_length(const char *str)
4179 decky 280
{
4205 svoboda 281
    count_t len = 0;
282
    size_t offset = 0;
283
 
284
    while (chr_decode(str, &offset, UTF8_NO_LIMIT) != 0) {
285
        len++;
4179 decky 286
    }
4205 svoboda 287
 
288
    return len;
4011 svoboda 289
}
290
 
4205 svoboda 291
/** Return number of characters in a wide string.
4179 decky 292
 *
4205 svoboda 293
 * @param str NULL-terminated wide string.
294
 * @return Number of characters in @a str.
4179 decky 295
 */
4205 svoboda 296
count_t wstr_length(const wchar_t *wstr)
4179 decky 297
{
4205 svoboda 298
    count_t len;
299
 
300
    len = 0;
301
    while (*wstr++ != '\0')
302
        ++len;
303
 
304
    return len;
4179 decky 305
}
306
 
4011 svoboda 307
/** Compare two NULL terminated strings
308
 *
309
 * Do a char-by-char comparison of two NULL terminated strings.
310
 * The strings are considered equal iff they consist of the same
311
 * characters on the minimum of their lengths.
312
 *
313
 * @param src First string to compare.
314
 * @param dst Second string to compare.
315
 *
316
 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
317
 *
318
 */
319
int strcmp(const char *src, const char *dst)
320
{
321
    for (; *src && *dst; src++, dst++) {
322
        if (*src < *dst)
323
            return -1;
324
        if (*src > *dst)
325
            return 1;
326
    }
327
    if (*src == *dst)
328
        return 0;
4014 decky 329
 
4011 svoboda 330
    if (!*src)
331
        return -1;
4014 decky 332
 
4011 svoboda 333
    return 1;
334
}
335
 
336
 
337
/** Compare two NULL terminated strings
338
 *
339
 * Do a char-by-char comparison of two NULL terminated strings.
340
 * The strings are considered equal iff they consist of the same
341
 * characters on the minimum of their lengths and specified maximal
342
 * length.
343
 *
344
 * @param src First string to compare.
345
 * @param dst Second string to compare.
346
 * @param len Maximal length for comparison.
347
 *
348
 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
349
 */
350
int strncmp(const char *src, const char *dst, size_t len)
351
{
352
    unsigned int i;
353
 
354
    for (i = 0; (*src) && (*dst) && (i < len); src++, dst++, i++) {
355
        if (*src < *dst)
356
            return -1;
4014 decky 357
 
4011 svoboda 358
        if (*src > *dst)
359
            return 1;
360
    }
4014 decky 361
 
4011 svoboda 362
    if (i == len || *src == *dst)
363
        return 0;
4014 decky 364
 
4011 svoboda 365
    if (!*src)
366
        return -1;
4014 decky 367
 
4011 svoboda 368
    return 1;
369
}
370
 
371
 
372
 
373
/** Copy NULL terminated string.
374
 *
375
 * Copy at most 'len' characters from string 'src' to 'dest'.
376
 * If 'src' is shorter than 'len', '\0' is inserted behind the
377
 * last copied character.
378
 *
4014 decky 379
 * @param src  Source string.
4011 svoboda 380
 * @param dest Destination buffer.
4014 decky 381
 * @param len  Size of destination buffer.
4011 svoboda 382
 */
383
void strncpy(char *dest, const char *src, size_t len)
384
{
385
    unsigned int i;
4014 decky 386
 
4011 svoboda 387
    for (i = 0; i < len; i++) {
388
        if (!(dest[i] = src[i]))
389
            return;
390
    }
4014 decky 391
 
4011 svoboda 392
    dest[i - 1] = '\0';
393
}
394
 
4012 svoboda 395
/** Find first occurence of character in string.
396
 *
4014 decky 397
 * @param s String to search.
398
 * @param i Character to look for.
4012 svoboda 399
 *
4014 decky 400
 * @return Pointer to character in @a s or NULL if not found.
4012 svoboda 401
 */
402
extern char *strchr(const char *s, int i)
403
{
404
    while (*s != '\0') {
4014 decky 405
        if (*s == i)
406
            return (char *) s;
4012 svoboda 407
        ++s;
408
    }
4014 decky 409
 
4012 svoboda 410
    return NULL;
411
}
412
 
4011 svoboda 413
/** @}
414
 */