diff -r 000000000000 -r 6474c204b198 security/nss/lib/util/utilpars.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/security/nss/lib/util/utilpars.c Wed Dec 31 06:09:35 2014 +0100 @@ -0,0 +1,1117 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +/* + * The following code handles the storage of PKCS 11 modules used by the + * NSS. This file is written to abstract away how the modules are + * stored so we can decide that later. + */ +#include "secport.h" +#include "prprf.h" +#include "prenv.h" +#include "utilpars.h" +#include "utilmodt.h" + +/* + * return the expected matching quote value for the one specified + */ +PRBool NSSUTIL_ArgGetPair(char c) { + switch (c) { + case '\'': return c; + case '\"': return c; + case '<': return '>'; + case '{': return '}'; + case '[': return ']'; + case '(': return ')'; + default: break; + } + return ' '; +} + +PRBool NSSUTIL_ArgIsBlank(char c) { + return isspace((unsigned char )c); +} + +PRBool NSSUTIL_ArgIsEscape(char c) { + return c == '\\'; +} + +PRBool NSSUTIL_ArgIsQuote(char c) { + switch (c) { + case '\'': + case '\"': + case '<': + case '{': /* } end curly to keep vi bracket matching working */ + case '(': /* ) */ + case '[': /* ] */ return PR_TRUE; + default: break; + } + return PR_FALSE; +} + +char *NSSUTIL_ArgStrip(char *c) { + while (*c && NSSUTIL_ArgIsBlank(*c)) c++; + return c; +} + +/* + * find the end of the current tag/value pair. string should be pointing just + * after the equal sign. Handles quoted characters. + */ +char * +NSSUTIL_ArgFindEnd(char *string) { + char endChar = ' '; + PRBool lastEscape = PR_FALSE; + + if (NSSUTIL_ArgIsQuote(*string)) { + endChar = NSSUTIL_ArgGetPair(*string); + string++; + } + + for (;*string; string++) { + if (lastEscape) { + lastEscape = PR_FALSE; + continue; + } + if (NSSUTIL_ArgIsEscape(*string) && !lastEscape) { + lastEscape = PR_TRUE; + continue; + } + if ((endChar == ' ') && NSSUTIL_ArgIsBlank(*string)) break; + if (*string == endChar) { + break; + } + } + + return string; +} + +/* + * get the value pointed to by string. string should be pointing just beyond + * the equal sign. + */ +char * +NSSUTIL_ArgFetchValue(char *string, int *pcount) +{ + char *end = NSSUTIL_ArgFindEnd(string); + char *retString, *copyString; + PRBool lastEscape = PR_FALSE; + int len; + + len = end - string; + if (len == 0) { + *pcount = 0; + return NULL; + } + + copyString = retString = (char *)PORT_Alloc(len+1); + + if (*end) len++; + *pcount = len; + if (retString == NULL) return NULL; + + + if (NSSUTIL_ArgIsQuote(*string)) string++; + for (; string < end; string++) { + if (NSSUTIL_ArgIsEscape(*string) && !lastEscape) { + lastEscape = PR_TRUE; + continue; + } + lastEscape = PR_FALSE; + *copyString++ = *string; + } + *copyString = 0; + return retString; +} + +/* + * point to the next parameter in string + */ +char * +NSSUTIL_ArgSkipParameter(char *string) +{ + char *end; + /* look for the end of the = */ + for (;*string; string++) { + if (*string == '=') { string++; break; } + if (NSSUTIL_ArgIsBlank(*string)) return(string); + } + + end = NSSUTIL_ArgFindEnd(string); + if (*end) end++; + return end; +} + +/* + * get the value from that tag value pair. + */ +char * +NSSUTIL_ArgGetParamValue(char *paramName,char *parameters) +{ + char searchValue[256]; + int paramLen = strlen(paramName); + char *returnValue = NULL; + int next; + + if ((parameters == NULL) || (*parameters == 0)) return NULL; + + PORT_Assert(paramLen+2 < sizeof(searchValue)); + + PORT_Strcpy(searchValue,paramName); + PORT_Strcat(searchValue,"="); + while (*parameters) { + if (PORT_Strncasecmp(parameters,searchValue,paramLen+1) == 0) { + parameters += paramLen+1; + returnValue = NSSUTIL_ArgFetchValue(parameters,&next); + break; + } else { + parameters = NSSUTIL_ArgSkipParameter(parameters); + } + parameters = NSSUTIL_ArgStrip(parameters); + } + return returnValue; +} + +/* + * find the next flag in the parameter list + */ +char * +NSSUTIL_ArgNextFlag(char *flags) +{ + for (; *flags ; flags++) { + if (*flags == ',') { + flags++; + break; + } + } + return flags; +} + +/* + * return true if the flag is set in the label parameter. + */ +PRBool +NSSUTIL_ArgHasFlag(char *label, char *flag, char *parameters) +{ + char *flags,*index; + int len = strlen(flag); + PRBool found = PR_FALSE; + + flags = NSSUTIL_ArgGetParamValue(label,parameters); + if (flags == NULL) return PR_FALSE; + + for (index=flags; *index; index=NSSUTIL_ArgNextFlag(index)) { + if (PORT_Strncasecmp(index,flag,len) == 0) { + found=PR_TRUE; + break; + } + } + PORT_Free(flags); + return found; +} + +/* + * decode a number. handle octal (leading '0'), hex (leading '0x') or decimal + */ +long +NSSUTIL_ArgDecodeNumber(char *num) +{ + int radix = 10; + unsigned long value = 0; + long retValue = 0; + int sign = 1; + int digit; + + if (num == NULL) return retValue; + + num = NSSUTIL_ArgStrip(num); + + if (*num == '-') { + sign = -1; + num++; + } + + if (*num == '0') { + radix = 8; + num++; + if ((*num == 'x') || (*num == 'X')) { + radix = 16; + num++; + } + } + + + for ( ;*num; num++ ) { + if (isdigit(*num)) { + digit = *num - '0'; + } else if ((*num >= 'a') && (*num <= 'f')) { + digit = *num - 'a' + 10; + } else if ((*num >= 'A') && (*num <= 'F')) { + digit = *num - 'A' + 10; + } else { + break; + } + if (digit >= radix) break; + value = value*radix + digit; + } + + retValue = ((int) value) * sign; + return retValue; +} + +/* + * parameters are tag value pairs. This function returns the tag or label (the + * value before the equal size. + */ +char * +NSSUTIL_ArgGetLabel(char *inString, int *next) +{ + char *name=NULL; + char *string; + int len; + + /* look for the end of the