keyFailure = PR_TRUE;
Wallet_SetKey(PRBool isNewkey) {  if (keySet && !isNewkey) {    return PR_TRUE;  }  Wallet_RestartKey();  /* ask the user for his key */  char * password;  if (wallet_KeySize() < 0) { /* no password has yet been established */    password = Wallet_Localize("firstPassword");  } else if (isNewkey) {    password = Wallet_Localize("newPassword");  } else {    password = Wallet_Localize("password");  }  char * newkey;  if ((wallet_KeySize() == 0) && !isNewkey) {    newkey = PL_strdup("");  } else {    newkey = wallet_GetString(password);  }  if (newkey == NULL) { /* user hit cancel button */    if (wallet_KeySize() < 0) { /* no password file existed before */      newkey  = PL_strdup(""); /* use zero-length password */    } else {      return PR_FALSE; /* user could not supply the correct password */    }  }  PR_FREEIF(password);  for (; (keyPosition < PL_strlen(newkey) && keyPosition < maxKeySize); keyPosition++) {    key[keyPosition] = newkey[keyPosition];  }  key[keyPosition] = '\0';  PR_FREEIF(newkey);  Wallet_RestartKey();  /* verify this with the saved key */  if (isNewkey || (wallet_KeySize() < 0)) {    /*     * Either key is to be changed or the file containing the saved key doesn' exist.     * In either case we need to (re)create and re(write) the file.     */    nsFileSpec dirSpec;    nsresult rval = Wallet_ProfileDirectory(dirSpec);    if (NS_FAILED(rval)) {      keyFailure = PR_TRUE;      return PR_FALSE;    }    nsOutputFileStream strm2(dirSpec + "key");    if (!strm2.is_open()) {      keyFailure = PR_TRUE;      *key = '\0';      return PR_FALSE;    }    /* If we store the key obscured by the key itself, then the result will be zero     * for all keys (since we are using XOR to obscure).  So instead we store     * key[1..n],key[0] obscured by the actual key.     */    if (PL_strlen(key) != 0) {      char* p = key+1;      while (*p) {        strm2.put(*(p++)^Wallet_GetKey());      }      strm2.put((*key)^Wallet_GetKey());    }    strm2.flush();    strm2.close();    Wallet_RestartKey();    keySet = PR_TRUE;    keyFailure = PR_FALSE;    return PR_TRUE;  } else {    /* file of saved key existed so see if it matches the key the user typed in */    /*     * Note that eof() is not set until after we read past the end of the file.  That     * is why the following code reads a character and immediately after the read     * checks for eof()     */    /* test for a null key */    if ((PL_strlen(key) == 0) && (wallet_KeySize() == 0) ) {      Wallet_RestartKey();      keySet = PR_TRUE;      keyFailure = PR_FALSE;      return PR_TRUE;    }    nsFileSpec dirSpec;    nsresult rval = Wallet_ProfileDirectory(dirSpec);    if (NS_FAILED(rval)) {      keyFailure = PR_TRUE;      return PR_FALSE;    }    nsInputFileStream strm(dirSpec + "key");    Wallet_RestartKey();    char* p = key+1;    while (*p) {      if (strm.get() != (*(p++)^Wallet_GetKey()) || strm.eof()) {        strm.close();        keyFailure = PR_TRUE;        *key = '\0';        return PR_FALSE;      }    }    if (strm.get() != ((*key)^Wallet_GetKey()) || strm.eof()) {      strm.close();      keyFailure = PR_TRUE;      *key = '\0';      return PR_FALSE;    }    strm.get(); /* to get past the end of the file so eof() will get set */    PRBool rv = strm.eof();    strm.close();    if (rv) {      Wallet_RestartKey();      keySet = PR_TRUE;      keyFailure = PR_FALSE;      return PR_TRUE;    } else {      keyFailure = PR_TRUE;      *key = '\0';      return PR_FALSE;    }  }}