/* 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; } const char * NSSUTIL_ArgStrip(const 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. */ const char * NSSUTIL_ArgFindEnd(const 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(const char *string, int *pcount) { const 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 */ const char * NSSUTIL_ArgSkipParameter(const char *string) { const 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(const char *paramName, const char *parameters) { char searchValue[256]; size_t 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 */ const char * NSSUTIL_ArgNextFlag(const 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(const char *label, const char *flag, const char *parameters) { char *flags; const char *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(const 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(const char *inString, int *next) { char *name = NULL; const char *string; int len; /* look for the end of the