⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mmstring.c

📁 mmstatd包含一个 C库和服务器
💻 C
📖 第 1 页 / 共 2 页
字号:
/* $Id: mmstring.c,v 1.4 2003/01/08 20:57:46 mmondor Exp $ *//* * Copyright (C) 1989-2003, Matthew Mondor * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software *    must display the following acknowledgement: *      This product includes software written by Matthew Mondor. * 4. The name of Matthew Mondor may not be used to endorse or promote *    products derived from this software without specific prior written *    permission. * * THIS SOFTWARE IS PROVIDED BY MATTHEW MONDOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL MATTHEW MONDOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *//* XXX Implement mm_strstr() *//* A main reason for providing my string library is that as most mmsoftware * is security-aware, and some implementations vary, it is safer. A popular * example is strn*() functions which are not always implemented properly * on some libc's. I even once stumbled on some bogus strlen() which used * some 4-bytes hackery in a wrong way. Moreover, some memmov()/memcpy() * implementations behavior is not right. So let's not worry too much about * each CPU cycle, but rather about preventing buffer overflow opportunities, * and producing consistant code. But let's still code these functions in a * way to remain reasonably fast. */#include <sys/types.h>#include <stdlib.h>#include <mmtypes.h>#include <mmstring.h>MMCOPYRIGHT("@(#) Copyright (c) 2002-2003\n\\tMatthew Mondor. All rights reserved.\n");MMRCSID("$Id: mmstring.c,v 1.4 2003/01/08 20:57:46 mmondor Exp $");static const unsigned char toupper_table[] = {    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,     0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13,     0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,     0x1E, 0x1F, ' ',  '!',  '"',  '#',  '$',  '%',  '&',  0x27,     '(',  ')',  '*',  '+',  ',',  '-',  '.',  '/',  '0',  '1',      '2',  '3',  '4',  '5',  '6',  '7',  '8',  '9',  ':',  ';',      '<',  '=',  '>',  '?',  '@',  'A',  'B',  'C',  'D',  'E',      'F',  'G',  'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',      'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',  'X',  'Y',      'Z',  '[',  0x5C, ']',  '^',  '_',  '`',  'A',  'B',  'C',      'D',  'E',  'F',  'G',  'H',  'I',  'J',  'K',  'L',  'M',      'N',  'O',  'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',      'X',  'Y',  'Z',  '{',  '|',  '}',  '~',  0x7F, 0x80, 0x81,     0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B,     0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95,     0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,     0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9,     0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3,     0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD,     0xBE, 0xBF, 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,     0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1,     0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB,     0xDC, 0xDD, 0xDE, 0xDF, 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5,     0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,     0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9,     0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF};static const unsigned char tolower_table[] = {    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,     0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13,     0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,     0x1E, 0x1F, ' ',  '!',  '"',  '#',  '$',  '%',  '&',  0x27,     '(',  ')',  '*',  '+',  ',',  '-',  '.',  '/',  '0',  '1',      '2',  '3',  '4',  '5',  '6',  '7',  '8',  '9',  ':',  ';',      '<',  '=',  '>',  '?',  '@',  'a',  'b',  'c',  'd',  'e',      'f',  'g',  'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o',      'p',  'q',  'r',  's',  't',  'u',  'v',  'w',  'x',  'y',      'z',  '[',  0x5C, ']',  '^',  '_',  '`',  'a',  'b',  'c',      'd',  'e',  'f',  'g',  'h',  'i',  'j',  'k',  'l',  'm',      'n',  'o',  'p',  'q',  'r',  's',  't',  'u',  'v',  'w',      'x',  'y',  'z',  '{',  '|',  '}',  '~',  0x7F, 0x80, 0x81,     0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B,     0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95,     0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,     0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9,     0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3,     0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD,     0xBE, 0xBF, 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,     0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1,     0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB,     0xDC, 0xDD, 0xDE, 0xDF, 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5,     0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,     0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9,     0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF};/* Splits columns of a string delimited by spaces and/or tabs, and fills * char **argv with pointers to each column. Returns the number of columns, * or 0 on error or overflow (more columns than maxcols). The supplied string * IS modified. */intmm_straspl(char **argv, char *str, int maxcols){    char *ptr = str, *ptr2;    int col = 0;    while (*ptr && col < maxcols) {	while (*ptr == ' ' || *ptr == '\t')	    ptr++;	if (*ptr) {	    ptr2 = ptr;	    while (*ptr && *ptr != ' ' && *ptr != '\t')		ptr++;	    if (ptr != ptr2) {		if (*ptr)		    *ptr++ = '\0';		argv[col++] = ptr2;	    } else		break;	} else	    break;    }    if (col <= maxcols)	return (col);    return (0);}char *mm_strcat(char *dest, const char *src){    while (*dest)	dest++;    while (*src)	*dest++ = *src++;    *dest = '\0';    return (dest);}char *mm_strchr(const char *str, int c){    while (*str && *str != c)	str++;    if (*str == c)	return ((char *)str);    return (NULL);}intmm_strcmp(const char *s1, const char *s2){    int ret = 0;    while (*s1 && *s2 && *s1 == *s2) {	if (!*s1 || !*s2)	    break;	s1++;	s2++;    }    if (*s1 != *s2)	ret = *s1 - *s2;    return (ret);}char *mm_strcpy(char *dest, const char *src){    while (*src)	*dest++ = *src++;    *dest = '\0';    return (dest);}char *mm_strdup(const char *str){    char *new = NULL;    const char *ptr = str;    size_t len;    while (*ptr)	ptr++;    if ((len = ptr - str)) {	if ((new = malloc(len + 1)) != NULL) {	    mm_memcpy(new, str, len);	    new[len] = '\0';	}    }    return (new);}intmm_stricmp(const char *s1, const char *s2){    int ret = 0;    register char cs1 = 0, cs2 = 0;    while (*s1 && *s2 && (cs1 = tolower_table[(int)*s1]) ==	   (cs2 = tolower_table[(int)*s2])) {	if (!cs1 || !cs2)	    break;	s1++;	s2++;    }    if (!*s1 || !*s2)	ret = *s1 - *s2;    else if (cs1 != cs2)	ret = cs1 - cs2;    return (ret);}size_tmm_strlen(const char *str){    const char *ptr = str;    while (*ptr)	ptr++;    return (ptr - str);}/* Splits columns of a string delimited by spaces and/or tabs, and fills * char **argv with pointers to each column. Returns the number of columns, * or 0 on error or overflow (more columns than maxcols). The supplied string * IS modified. The advantage is that this performs one pass, and needs * no additionnal memory. */intmm_strnaspl(char **argv, char *str, int maxcols, size_t max){    char *ptr2, *toptr;    int col;    col = 0;    toptr = str;    toptr += max;    while (str < toptr && *str && col < maxcols) {	while (str < toptr && (*str == ' ' || *str == '\t'))	    str++;	if (*str) {	    ptr2 = str;	    while (str < toptr && *str && *str != ' ' && *str != '\t')		str++;	    if (str != ptr2) {		if (*str)		    *str++ = '\0';		argv[col++] = ptr2;	    } else		break;	} else	    break;    }    if (col <= maxcols)	return (col);    return (0);}char *mm_strncat(char *dest, const char *src, size_t max){    char *toptr;    toptr = dest;    toptr += max;    while (*dest && dest < toptr)	dest++;    while (*src && dest < toptr)	*dest++ = *src++;    *dest = '\0';    return (dest);}/* XXX not tested */char *mm_strnchr(const char *str, int c, size_t max){    const char *toptr;    toptr = str;    toptr += max;    while (*str && *str != c && str < toptr)	str++;    if (*str == c)	return ((char *)str);    return (NULL);}intmm_strncmp(const char *s1, const char *s2, size_t max){    int ret = 0;    const char *toptr;    toptr = s1;    toptr += max;    while (s1 < toptr && *s1 && *s2 && *s1 == *s2)	s1++;    s2++;    if (s1 < toptr && *s1 != *s2)	ret = *s1 - *s2;    return (ret);}/* This function, unlike the useless return code of the standard ANSI one, * this function returns the number of bytes successfully copied. */size_tmm_strncpy(char *dest, const char *src, size_t max){    char *toptr;    toptr = dest;    toptr += max;    while (*src && dest < toptr)	*dest++ = *src++;    *dest = '\0';    return (toptr - dest);}char *mm_strndup(const char *str, size_t max){    char *new = NULL;    const char *toptr;    size_t len = 0;    toptr = str;    toptr += max;    while (*str && str < toptr)	str++;    if ((len = toptr - str)) {	if ((new = malloc(len + 1)) != NULL) {	    mm_memcpy(new, str, len);	    new[len] = '\0';	}    }    return (new);}intmm_strnicmp(const char *s1, const char *s2, size_t max){    int ret = 0;    char cs1 = 0, cs2 = 0;    const char *toptr;    toptr = s1;    toptr += max;    while (s1 < toptr && *s1 && *s2) {	if ((cs1 = tolower_table[(int)*s1]) !=	       	(cs2 = tolower_table[(int)*s2]))	    break;	s1++;	s2++;    }    if (s1 < toptr) {	if (!*s1 || !*s2)	    ret = *s1 - *s2;	else if (cs1 != cs2)	    ret = cs1 - cs2;    }    return (ret);}size_tmm_strnlen(const char *str, size_t max){    const char *toptr;    toptr = str;    toptr += max;    while (*str && str < toptr)	str++;    return (toptr - str);}/* XXX not tested */char *mm_strnrchr(const char *str, int c, size_t max){    const char *ptr, *toptr;    ptr = toptr = str;    toptr += max;    while (*ptr && ptr < toptr)	ptr++;    while (ptr >= str && *ptr != c)	ptr--;    if (*ptr == c)	return ((char *)ptr);    return (NULL);}/* Splits columns of a string delimited by sep, and fills * char **argv with pointers to each column. Returns the number of columns, * or 0 on error or overflow (more columns than maxcols). The supplied string * IS modified. *//* XXX Not tested */intmm_strnspl(char **argv, char *str, int maxcols, char sep, size_t max){    char *ptr2, *toptr;    int col;    col = 0;    toptr = str;    toptr += max;    while (str < toptr && *str && col < maxcols) {	ptr2 = str;	while (str < toptr && *str && *str != sep)	    str++;	if (str != ptr2) {	    if (*str)		*str++ = '\0';	    argv[col++] = ptr2;	} else	    break;    }    if (col <= maxcols)	return (col);    return (0);}/* XXX not tested */char *mm_strrchr(const char *str, int c){    const char *to = str;    while (*to)	to++;    while (to > str && *to != c)	to--;    if (*to == c)	return ((char *)to);    return (NULL);}/* Splits columns of a string delimited by sep, and fills * char **argv with pointers to each column. Returns the number of columns, * or 0 on error or overflow (more columns than maxcols). The supplied string * IS modified. */intmm_strspl(char **argv, char *str, int maxcols, char sep){    char *ptr, *ptr2;    int col;    col = 0;    ptr = str;    while (*ptr && col < maxcols) {	ptr2 = ptr;	while (*ptr && *ptr != sep)	    ptr++;	if (*ptr)	    *ptr++ = '\0';	argv[col++] = ptr2;    }    if (col <= maxcols)	return (col);    return (0);}voidmm_strlower(char *str){    while (*str) {	*str = tolower_table[(int)*str];	str++;    }}voidmm_strupper(char *str){    while (*str) {	*str = toupper_table[(int)*str];	str++;    }}/* This function generates a 32-bit hash using the supplied string which * is suitable for fast lookup for command comparision. It simply converts * characters to uppercase and stores them in the value. It of course can * only perform this for 4 bytes. It will stop at either end of string '\0' * or space ' '. If the string has more than 4 characters -1 is returned. */int32_t

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -