Changeset abf09311 in mainline


Ignore:
Timestamp:
2010-04-17T01:12:35Z (14 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
46416ab
Parents:
d7f8796c
Message:

much safer implementation of str_dup() and str_ndup()
port str_dup() and str_ndup() into the kernel

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/include/str.h

    rd7f8796c rabf09311  
    8989extern void wstr_to_str(char *dest, size_t size, const wchar_t *src);
    9090
     91extern char *str_dup(const char *src);
     92extern char *str_ndup(const char *src, size_t n);
     93
    9194extern char *str_chr(const char *str, wchar_t ch);
    9295
  • kernel/generic/src/lib/str.c

    rd7f8796c rabf09311  
    537537 * null-terminated and containing only complete characters.
    538538 *
    539  * @param dest   Destination buffer.
     539 * @param dest  Destination buffer.
    540540 * @param count Size of the destination buffer (must be > 0).
    541541 * @param src   Source string.
     542 *
    542543 */
    543544void str_cpy(char *dest, size_t size, const char *src)
    544545{
    545         wchar_t ch;
    546         size_t src_off;
    547         size_t dest_off;
    548 
    549546        /* There must be space for a null terminator in the buffer. */
    550547        ASSERT(size > 0);
    551548       
    552         src_off = 0;
    553         dest_off = 0;
    554 
     549        size_t src_off = 0;
     550        size_t dest_off = 0;
     551       
     552        wchar_t ch;
    555553        while ((ch = str_decode(src, &src_off, STR_NO_LIMIT)) != 0) {
    556554                if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
    557555                        break;
    558556        }
    559 
     557       
    560558        dest[dest_off] = '\0';
    561559}
     
    571569 * have to be null-terminated.
    572570 *
    573  * @param dest   Destination buffer.
     571 * @param dest  Destination buffer.
    574572 * @param count Size of the destination buffer (must be > 0).
    575573 * @param src   Source string.
    576  * @param n     Maximum number of bytes to read from @a src.
     574 * @param n     Maximum number of bytes to read from @a src.
     575 *
    577576 */
    578577void str_ncpy(char *dest, size_t size, const char *src, size_t n)
    579578{
    580         wchar_t ch;
    581         size_t src_off;
    582         size_t dest_off;
    583 
    584579        /* There must be space for a null terminator in the buffer. */
    585580        ASSERT(size > 0);
    586581       
    587         src_off = 0;
    588         dest_off = 0;
    589 
     582        size_t src_off = 0;
     583        size_t dest_off = 0;
     584       
     585        wchar_t ch;
    590586        while ((ch = str_decode(src, &src_off, n)) != 0) {
    591587                if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
    592588                        break;
    593589        }
    594 
     590       
    595591        dest[dest_off] = '\0';
     592}
     593
     594/** Duplicate string.
     595 *
     596 * Allocate a new string and copy characters from the source
     597 * string into it. The duplicate string is allocated via sleeping
     598 * malloc(), thus this function can sleep in no memory conditions.
     599 *
     600 * The allocation cannot fail and the return value is always
     601 * a valid pointer. The duplicate string is always a well-formed
     602 * null-terminated UTF-8 string, but it can differ from the source
     603 * string on the byte level.
     604 *
     605 * @param src Source string.
     606 *
     607 * @return Duplicate string.
     608 *
     609 */
     610char *str_dup(const char *src)
     611{
     612        size_t size = str_size(src) + 1;
     613        char *dest = malloc(size, 0);
     614        ASSERT(dest);
     615       
     616        str_cpy(dest, size, src);
     617        return dest;
     618}
     619
     620/** Duplicate string with size limit.
     621 *
     622 * Allocate a new string and copy up to @max_size bytes from the source
     623 * string into it. The duplicate string is allocated via sleeping
     624 * malloc(), thus this function can sleep in no memory conditions.
     625 * No more than @max_size + 1 bytes is allocated, but if the size
     626 * occupied by the source string is smaller than @max_size + 1,
     627 * less is allocated.
     628 *
     629 * The allocation cannot fail and the return value is always
     630 * a valid pointer. The duplicate string is always a well-formed
     631 * null-terminated UTF-8 string, but it can differ from the source
     632 * string on the byte level.
     633 *
     634 * @param src Source string.
     635 * @param n   Maximum number of bytes to duplicate.
     636 *
     637 * @return Duplicate string.
     638 *
     639 */
     640char *str_ndup(const char *src, size_t n)
     641{
     642        size_t size = str_size(src);
     643        if (size > n)
     644                size = n;
     645       
     646        char *dest = malloc(size + 1, 0);
     647        ASSERT(dest);
     648       
     649        str_ncpy(dest, size + 1, src, size);
     650        return dest;
    596651}
    597652
  • uspace/lib/c/generic/str.c

    rd7f8796c rabf09311  
    471471 * null-terminated and containing only complete characters.
    472472 *
    473  * @param dest   Destination buffer.
     473 * @param dest  Destination buffer.
    474474 * @param count Size of the destination buffer (must be > 0).
    475475 * @param src   Source string.
     
    477477void str_cpy(char *dest, size_t size, const char *src)
    478478{
    479         wchar_t ch;
    480         size_t src_off;
    481         size_t dest_off;
    482 
    483479        /* There must be space for a null terminator in the buffer. */
    484480        assert(size > 0);
    485481       
    486         src_off = 0;
    487         dest_off = 0;
    488 
     482        size_t src_off = 0;
     483        size_t dest_off = 0;
     484       
     485        wchar_t ch;
    489486        while ((ch = str_decode(src, &src_off, STR_NO_LIMIT)) != 0) {
    490487                if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
    491488                        break;
    492489        }
    493 
     490       
    494491        dest[dest_off] = '\0';
    495492}
     
    505502 * have to be null-terminated.
    506503 *
    507  * @param dest   Destination buffer.
     504 * @param dest  Destination buffer.
    508505 * @param count Size of the destination buffer (must be > 0).
    509506 * @param src   Source string.
    510  * @param n     Maximum number of bytes to read from @a src.
     507 * @param n     Maximum number of bytes to read from @a src.
    511508 */
    512509void str_ncpy(char *dest, size_t size, const char *src, size_t n)
    513510{
    514         wchar_t ch;
    515         size_t src_off;
    516         size_t dest_off;
    517 
    518511        /* There must be space for a null terminator in the buffer. */
    519512        assert(size > 0);
    520513       
    521         src_off = 0;
    522         dest_off = 0;
    523 
     514        size_t src_off = 0;
     515        size_t dest_off = 0;
     516       
     517        wchar_t ch;
    524518        while ((ch = str_decode(src, &src_off, n)) != 0) {
    525519                if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
    526520                        break;
    527521        }
    528 
     522       
    529523        dest[dest_off] = '\0';
    530524}
     
    896890}
    897891
     892/** Duplicate string.
     893 *
     894 * Allocate a new string and copy characters from the source
     895 * string into it. The duplicate string is allocated via sleeping
     896 * malloc(), thus this function can sleep in no memory conditions.
     897 *
     898 * The allocation cannot fail and the return value is always
     899 * a valid pointer. The duplicate string is always a well-formed
     900 * null-terminated UTF-8 string, but it can differ from the source
     901 * string on the byte level.
     902 *
     903 * @param src Source string.
     904 *
     905 * @return Duplicate string.
     906 *
     907 */
    898908char *str_dup(const char *src)
    899909{
    900         size_t size = str_size(src);
    901         void *dest = malloc(size + 1);
    902        
     910        size_t size = str_size(src) + 1;
     911        char *dest = (char *) malloc(size);
    903912        if (dest == NULL)
    904913                return (char *) NULL;
    905914       
    906         return (char *) memcpy(dest, src, size + 1);
    907 }
    908 
    909 char *str_ndup(const char *src, size_t max_size)
     915        str_cpy(dest, size, src);
     916        return dest;
     917}
     918
     919/** Duplicate string with size limit.
     920 *
     921 * Allocate a new string and copy up to @max_size bytes from the source
     922 * string into it. The duplicate string is allocated via sleeping
     923 * malloc(), thus this function can sleep in no memory conditions.
     924 * No more than @max_size + 1 bytes is allocated, but if the size
     925 * occupied by the source string is smaller than @max_size + 1,
     926 * less is allocated.
     927 *
     928 * The allocation cannot fail and the return value is always
     929 * a valid pointer. The duplicate string is always a well-formed
     930 * null-terminated UTF-8 string, but it can differ from the source
     931 * string on the byte level.
     932 *
     933 * @param src Source string.
     934 * @param n   Maximum number of bytes to duplicate.
     935 *
     936 * @return Duplicate string.
     937 *
     938 */
     939char *str_ndup(const char *src, size_t n)
    910940{
    911941        size_t size = str_size(src);
    912         if (size > max_size)
    913                 size = max_size;
     942        if (size > n)
     943                size = n;
    914944       
    915945        char *dest = (char *) malloc(size + 1);
    916        
    917946        if (dest == NULL)
    918947                return (char *) NULL;
    919948       
    920         memcpy(dest, src, size);
    921         dest[size] = 0;
     949        str_ncpy(dest, size + 1, src, size);
    922950        return dest;
    923951}
Note: See TracChangeset for help on using the changeset viewer.