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