Annotation of libelwix/src/str.c, revision 1.9

1.1       misho       1: /*************************************************************************
                      2: * (C) 2013 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
                      3: *  by Michael Pounov <misho@elwix.org>
                      4: *
                      5: * $Author: misho $
1.9     ! misho       6: * $Id: str.c,v 1.8.10.1 2019/01/23 13:24:41 misho Exp $
1.1       misho       7: *
                      8: **************************************************************************
                      9: The ELWIX and AITNET software is distributed under the following
                     10: terms:
                     11: 
                     12: All of the documentation and software included in the ELWIX and AITNET
                     13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
                     14: 
1.9     ! misho      15: Copyright 2004 - 2019
1.1       misho      16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
                     17: 
                     18: Redistribution and use in source and binary forms, with or without
                     19: modification, are permitted provided that the following conditions
                     20: are met:
                     21: 1. Redistributions of source code must retain the above copyright
                     22:    notice, this list of conditions and the following disclaimer.
                     23: 2. Redistributions in binary form must reproduce the above copyright
                     24:    notice, this list of conditions and the following disclaimer in the
                     25:    documentation and/or other materials provided with the distribution.
                     26: 3. All advertising materials mentioning features or use of this software
                     27:    must display the following acknowledgement:
                     28: This product includes software developed by Michael Pounov <misho@elwix.org>
                     29: ELWIX - Embedded LightWeight unIX and its contributors.
                     30: 4. Neither the name of AITNET nor the names of its contributors
                     31:    may be used to endorse or promote products derived from this software
                     32:    without specific prior written permission.
                     33: 
                     34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
                     35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     44: SUCH DAMAGE.
                     45: */
                     46: #include "global.h"
                     47: 
                     48: 
                     49: /*
                     50:  * str_FreeNullTerm() - Free dynamic allocated null terminated array with strings
                     51:  *
                     52:  * @arr = Pointer to array for free
                     53:  * return: none
                     54:  */
1.3       misho      55: void
1.1       misho      56: str_FreeNullTerm(char *** __restrict arr)
                     57: {
                     58:        char **a;
                     59: 
                     60:        if (arr && *arr) {
                     61:                a = *arr;
                     62:                while (a && *a)
                     63:                        e_free(*a++);
                     64:                e_free(*arr);
                     65:                *arr = NULL;
                     66:        }
                     67: }
                     68: 
                     69: /*
                     70:  * str_ArgsNum() Parse and calculate number of arguments
                     71:  *
                     72:  * @csArgs = Input arguments line
                     73:  * @csDelim = Delimiter(s) for separate
                     74:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of items
                     75:  */
1.3       misho      76: int
1.1       misho      77: str_ArgsNum(const char *csArgs, const char *csDelim)
                     78: {
                     79:        register int res;
                     80:        char *pos;
                     81: 
                     82:        if (!csArgs || !csDelim)
                     83:                return -1;
                     84: 
                     85:        for (res = 1, pos = (char*) csArgs; (pos = strpbrk(pos, csDelim)); res++, pos++);
                     86: 
                     87:        return res;
                     88: }
                     89: 
                     90: /*
                     91:  * str_ExecArgs() - Build exec arguments from other array
                     92:  *
                     93:  * @psProg = Program name for execute
                     94:  * @oldarg = Arguments array
                     95:  * return: NULL error; !=NULL Allocated execution array(must be e_free)
                     96:  */
                     97: char **
                     98: str_ExecArgs(const char *psProg, const char **oldarg)
                     99: {
                    100:        char **newarg, **el;
                    101:        register int i, num;
                    102: 
                    103:        if (!psProg || !oldarg)
                    104:                return NULL;
                    105:        else
                    106:                newarg = el = NULL;
                    107: 
                    108:        /* count items arguments */
                    109:        for (num = 0; oldarg[num]; num++);
                    110: 
                    111:        /* create and copy new arguments */
                    112:        newarg = e_calloc(num + 2, sizeof(char*));
                    113:        if (!newarg)
                    114:                return NULL;
                    115:        else
                    116:                el = newarg;
                    117: 
                    118:        *el = e_strdup(psProg);
                    119:        el++;
                    120: 
                    121:        for (i = 0; oldarg[i]; i++, el++)
                    122:                *el = e_strdup(oldarg[i]);
                    123:        *el = NULL;
                    124: 
                    125:        return newarg;
                    126: }
                    127: 
                    128: /*
                    129:  * str_CopyEnv() - Copy environment to new environment array;
                    130:  *
                    131:  * @oldenv = Environment array
                    132:  * return: NULL error; !=NULL Allocated new environment array(must be e_free)
                    133:  */
                    134: char **
                    135: str_CopyEnv(const char **oldenv)
                    136: {
                    137:        char **newenv, **el;
                    138:        register int i, num;
                    139: 
                    140:        if (!oldenv)
                    141:                return NULL;
                    142:        else
                    143:                newenv = el = NULL;
                    144: 
                    145:        /* count items environment */
                    146:        for (i = num = 0; oldenv[i]; i++)
                    147:                if (*strchr(oldenv[i], '='))
                    148:                        num++;
                    149: 
                    150:        /* create and copy new environment */
                    151:        newenv = e_calloc(num + 1, sizeof(char*));
                    152:        if (!newenv)
                    153:                return NULL;
                    154:        else
                    155:                el = newenv;
                    156: 
                    157:        for (i = 0; oldenv[i]; i++)
                    158:                if (*strchr(oldenv[i], '=')) {
                    159:                        *el = e_strdup(oldenv[i]);
                    160:                        el++;
                    161:                }
                    162:        *el = NULL;
                    163: 
                    164:        return newenv;
                    165: }
                    166: 
                    167: /*
                    168:  * str_Ast() - Function for evaluate string like asterisk variable "{text[:[-]#[:#]]}"
                    169:  *
                    170:  * @csString = Input string
                    171:  * return: NULL error, !=NULL Allocated new string evaluated from input string, 
                    172:  *             must be ait_freeVar()
                    173:  */
                    174: ait_val_t *
                    175: str_Ast(const char *csString)
                    176: {
                    177:        char *ext, *str, *eb;
                    178:        int e[2] = { 0, 0 };
                    179:        ait_val_t *out = NULL;
                    180: 
                    181:        if (!csString)
                    182:                return NULL;
                    183: 
                    184:        if (!strchr(csString, '{') || !strrchr(csString, '}')) {
                    185:                elwix_SetErr(EINVAL, "Invalid input string format ... must be like "
                    186:                                "{text[:[-]#[:#]]}");
                    187:                return NULL;
                    188:        } else if (!(out = ait_allocVar()))
                    189:                return NULL;
                    190:        else {
                    191:                AIT_INIT_VAL2(out, string);
                    192: 
                    193:                str = e_strdup(strchr(csString, '{') + 1);
                    194:                *strrchr(str, '}') = 0;
                    195:        }
                    196: 
                    197:        if ((ext = strchr(str, ':'))) {
                    198:                *ext++ = 0;
                    199:                e[0] = strtol(ext, &eb, 0);
                    200:                if ((ext = strchr(eb, ':')))
                    201:                        e[1] = strtol(++ext, NULL, 0);
                    202: 
                    203:                /* make cut prefix */
                    204:                if (e[0] >= 0)
                    205:                        ext = str + MIN(e[0], strlen(str));
                    206:                else
                    207:                        ext = str + MAX(strlen(str) + e[0], 0);
                    208:                /* make cut suffix */
                    209:                if (e[1] > 0)
                    210:                        *(ext + MIN(e[1], strlen(ext))) = 0;
                    211:        } else
                    212:                /* ok, clear show */
                    213:                ext = str;
                    214: 
                    215:        AIT_SET_STR(out, ext);
                    216:        e_free(str);
                    217: 
                    218:        return out;
                    219: }
                    220: 
                    221: /*
                    222:  * str_Hex2Dig() - Convert from Hex string to digit array
                    223:  *
                    224:  * @psLine = Text string
                    225:  * return: NULL nothing to do or error; 
                    226:  *             !=0 Allocated new converted data (must be ait_freeVar())
                    227:  */
                    228: ait_val_t *
                    229: str_Hex2Dig(const char *psLine)
                    230: {
                    231:        register int i, j;
                    232:        char *str, szWork[3] = { 0, 0, 0 };
1.7       misho     233:        ait_val_t *v, s = AIT_VAL_INIT;
1.1       misho     234:        u_char *b;
                    235:        int n;
                    236: 
                    237:        if (!psLine || !*psLine)
                    238:                return NULL;
                    239:        else {
                    240:                v = ait_allocVar();
                    241:                if (!v)
                    242:                        return NULL;
                    243: 
                    244:                /* normalize input string if not even */
                    245:                n = strlen(psLine);
                    246:                if (n % 2)
                    247:                        n++;
                    248:                AIT_SET_STRSIZ(&s, n);
                    249:                for (i = strlen(psLine) - 1, j = n - 1, str = AIT_GET_STR(&s), *str = '0'; 
                    250:                                i > -1; i--, j--)
                    251:                        str[j] = psLine[i];
                    252:        }
                    253: 
                    254:        AIT_SET_BUFSIZ(v, 0, n / 2);
                    255:        for (i = 0, b = AIT_GET_BUF(v); i < n && str[i * 2]; i++) {
                    256:                strncpy(szWork, &str[i * 2], 2);
                    257:                b[i] = (u_char) strtol(szWork, NULL, 16);
                    258:        }
                    259: 
                    260:        AIT_FREE_VAL(&s);
                    261:        return v;
                    262: }
                    263: 
                    264: /*
                    265:  * str_Dig2Hex() - Convert from digit array to Hex string
                    266:  *
                    267:  * @digz = Digits
                    268:  * return: NULL nothing to do or error; 
                    269:  *             !=0 Allocated new converted string (must be e_free())
                    270: */
                    271: char *
                    272: str_Dig2Hex(ait_val_t *digz)
                    273: {
                    274:        register int i;
                    275:        char szWork[3] = { 0, 0, 0 }, *str;
                    276:        u_char *b;
                    277: 
                    278:        if (!digz || AIT_ISEMPTY(digz))
                    279:                return NULL;
                    280: 
                    281:        str = e_malloc(AIT_LEN(digz) * 2 + 1);
                    282:        if (!str)
                    283:                return NULL;
                    284:        else
                    285:                memset(str, 0, AIT_LEN(digz) * 2 + 1);
                    286: 
                    287:        for (i = 0, b = AIT_GET_BUF(digz); i < AIT_LEN(digz); i++) {
1.9     ! misho     288:                snprintf(szWork, sizeof szWork, "%02hhX", b[i]);
        !           289:                strncat(str, szWork, 2);
        !           290:        }
        !           291: 
        !           292:        return str;
        !           293: }
        !           294: 
        !           295: /*
        !           296:  * str_Dig2Hex2() - Convert from digit array to Hex string
        !           297:  *
        !           298:  * @digz = Digits array
        !           299:  * @diglen = Array length
        !           300:  * return: NULL nothing to do or error; 
        !           301:  *             !=0 Allocated new converted string (must be e_free())
        !           302: */
        !           303: char *
        !           304: str_Dig2Hex2(u_char * __restrict digz, int diglen)
        !           305: {
        !           306:        register int i;
        !           307:        char szWork[3] = { 0, 0, 0 }, *str;
        !           308:        u_char *b;
        !           309: 
        !           310:        if (!digz || !diglen)
        !           311:                return NULL;
        !           312: 
        !           313:        str = e_malloc(diglen * 2 + 1);
        !           314:        if (!str)
        !           315:                return NULL;
        !           316:        else
        !           317:                memset(str, 0, diglen * 2 + 1);
        !           318: 
        !           319:        for (i = 0, b = digz; i < diglen; i++) {
1.1       misho     320:                snprintf(szWork, sizeof szWork, "%02hhX", b[i]);
                    321:                strncat(str, szWork, 2);
                    322:        }
                    323: 
                    324:        return str;
                    325: }
                    326: 
                    327: /*
                    328:  * str_LTrim() - Remove left whitespaces from text string
                    329:  *
                    330:  * @psLine = Text string
                    331:  * return: 0 nothing to do; !=0 Removed bytes
                    332:  */
1.3       misho     333: int
1.1       misho     334: str_LTrim(char * __restrict psLine)
                    335: {
                    336:        int pos = 0;
                    337:        char *s;
                    338: 
                    339:        if (!psLine || !*psLine)
                    340:                return 0;
                    341: 
                    342:        for (s = psLine; isspace((u_char) *s); s++);
                    343:        pos = s - psLine;
                    344: 
                    345:        memmove(psLine, s, (strlen(psLine) - pos) + 1);
                    346:        return pos;
                    347: }
                    348: 
                    349: /*
                    350:  * str_RTrim() - Remove right whitespaces from text string
                    351:  *
                    352:  * @psLine = Text string
                    353:  * return: 0 nothing to do; !=0 Removed bytes
                    354:  */
1.3       misho     355: int
1.1       misho     356: str_RTrim(char * __restrict psLine)
                    357: {
                    358:        char *t, *pos;
                    359: 
                    360:        if (!psLine || !*psLine)
                    361:                return 0;
                    362: 
                    363:        pos = psLine + strlen(psLine);
                    364:        for (t = pos - 1; t > psLine && isspace((u_char) *t); t--);
                    365:        *++t = 0;
                    366: 
                    367:        return pos - t;
                    368: }
                    369: 
                    370: /*
                    371:  * str_Trim() - Remove left and right whitespaces from text string
                    372:  *
                    373:  * @psLine = Text string
                    374:  * return: 0 nothing to do; !=0 Removed bytes
                    375:  */
1.3       misho     376: int
1.1       misho     377: str_Trim(char * __restrict psLine)
                    378: {
                    379:        int ret = 0;
                    380: 
                    381:        ret = str_LTrim(psLine);
                    382:        ret += str_RTrim(psLine);
                    383: 
                    384:        return ret;
                    385: }
                    386: 
                    387: /*
                    388:  * str_Unquot() - Remove quots from input text string 
                    389:  *
                    390:  * @psLine = Text string
                    391:  * return: 0 nothing to do; 1 successful unquoted string
                    392:  */
1.3       misho     393: int
1.1       misho     394: str_Unquot(char * __restrict psLine)
                    395: {
                    396:        char *pos, *str = NULL;
                    397:        int flg;
                    398: 
                    399:        if (!psLine || !*psLine)
                    400:                return 0;
                    401: 
                    402:        if (*psLine == '"' || *psLine == '\'') {
                    403:                str = e_strdup(psLine + 1);
                    404:                for (pos = str, flg = 0; *pos; flg = ('\\' == *pos), pos++) {
                    405:                        if (!flg && *pos == *psLine) {
                    406:                                *pos = 0;
                    407:                                strlcpy(psLine, str, strlen(psLine) + 1);
                    408:                                break;
                    409:                        }
                    410:                }
                    411:                e_free(str);
                    412:                return 1;
                    413:        }
                    414: 
                    415:        return 0;
                    416: }
                    417: 
1.2       misho     418: /*
                    419:  * str_Upper() - Convert all lower characters to upper
                    420:  *
                    421:  * @psLine = Text string
                    422:  * return: 0 nothing to do; !=0 converted chars
                    423:  */
1.3       misho     424: int
1.2       misho     425: str_Upper(char * __restrict psLine)
                    426: {
                    427:        char *s;
                    428:        register int cx = 0;
                    429: 
                    430:        if (!psLine || !*psLine)
                    431:                return 0;
                    432: 
                    433:        for (s = psLine; *s; s++)
                    434:                if (islower(*s)) {
                    435:                        *s = toupper(*s);
                    436:                        cx++;
                    437:                }
                    438: 
                    439:        return cx;
                    440: }
                    441: 
                    442: /*
                    443:  * str_Lower() - Convert all upper characters to lower
                    444:  *
                    445:  * @psLine = Text string
                    446:  * return: 0 nothing to do; !=0 converted chars
                    447:  */
1.3       misho     448: int
1.2       misho     449: str_Lower(char * __restrict psLine)
                    450: {
                    451:        char *s;
                    452:        register int cx = 0;
                    453: 
                    454:        if (!psLine || !*psLine)
                    455:                return 0;
                    456: 
                    457:        for (s = psLine; *s; s++)
                    458:                if (isupper(*s)) {
                    459:                        *s = tolower(*s);
                    460:                        cx++;
                    461:                }
                    462: 
                    463:        return cx;
                    464: }
1.5       misho     465: 
                    466: /*
1.8       misho     467:  * str_getString() - Get NULL delimited string from data buffer
1.5       misho     468:  *
1.8       misho     469:  * @data = Const data buffer
1.5       misho     470:  * @dlen = Data length
                    471:  * @next = Return next position after string if !=NULL
                    472:  * return: -1 error or size of string 
                    473:  */
                    474: int
                    475: str_getString(const u_char * __restrict data, int dlen, char ** __restrict next)
                    476: {
                    477:        const u_char *pos;
                    478: 
                    479:        if (!data || !dlen)
                    480:                return -1;
                    481: 
                    482:        for (pos = data; pos < data + dlen; pos++)
                    483:                if (!*pos)
                    484:                        break;
                    485:        if (*pos) {
                    486:                elwix_SetErr(ENOEXEC, "Not found null-terminated string");
                    487:                return -1;
                    488:        }
                    489: 
                    490:        if (next)
                    491:                *next = (char*) pos + 1;
                    492:        return pos - data + 1;
1.8       misho     493: }
                    494: 
                    495: /*
                    496:  * str_getString2() - Get string from data buffer with delimiter
                    497:  *
                    498:  * @data = Data buffer
                    499:  * @dlen = Data length
                    500:  * @delim = Data delimiter
                    501:  * @next = Return next position after delimited string if !=NULL
                    502:  * return: -1 error or size of string 
                    503:  */
                    504: int
                    505: str_getString2(char * __restrict data, int dlen, char delim, char ** __restrict next)
                    506: {
                    507:        char *pos;
                    508: 
                    509:        if (!data || !dlen)
                    510:                return -1;
                    511: 
                    512:        for (pos = data; pos < data + dlen; pos++)
                    513:                if (!*pos || *pos == (u_char) delim) {
                    514:                        *pos = 0;
                    515:                        break;
                    516:                }
                    517:        if (*pos) {
                    518:                elwix_SetErr(ENOEXEC, "Not found null-terminated string");
                    519:                return -1;
                    520:        }
                    521: 
                    522:        if (next)
                    523:                *next = (char*) pos + 1;
                    524:        return pos - data;
1.5       misho     525: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>