diff --git a/mozilla/extensions/wallet/src/wallet.cpp b/mozilla/extensions/wallet/src/wallet.cpp index d901d5fa9c5..627a91e2762 100644 --- a/mozilla/extensions/wallet/src/wallet.cpp +++ b/mozilla/extensions/wallet/src/wallet.cpp @@ -202,6 +202,8 @@ wallet_GetWalletNotificationPref(void) { /* The following declarations define the data base */ /***************************************************/ +#define WALLET_FREE(_ptr) { nsMemory::Free((void*)_ptr); (_ptr) = nsnull; } +#define WALLET_FREEIF(_ptr) if (_ptr) WALLET_FREE(_ptr) enum PlacementType {DUP_IGNORE, DUP_OVERWRITE, DUP_BEFORE, DUP_AFTER, AT_END, BY_LENGTH}; #define LIST_COUNT(list) ((list) ? (list)->Count() : 0) @@ -216,9 +218,10 @@ public: } ~wallet_Sublist() { + WALLET_FREEIF(item); MOZ_COUNT_DTOR(wallet_Sublist); } - nsString item; + const char* item; }; /* @@ -239,6 +242,8 @@ public: } ~wallet_MapElement() { + WALLET_FREEIF(item1); + WALLET_FREEIF(item2); if (itemList) { PRInt32 count = LIST_COUNT(itemList); wallet_Sublist * sublistPtr; @@ -250,8 +255,8 @@ public: } MOZ_COUNT_DTOR(wallet_MapElement); } - nsString item1; - nsString item2; + const char* item1; + const char* item2; nsVoidArray * itemList; }; @@ -267,15 +272,20 @@ MOZ_DECL_CTOR_COUNTER(wallet_HelpMac) class wallet_HelpMac { public: wallet_HelpMac() { + item1 = nsnull; + item2 = nsnull; + item3 = nsnull; MOZ_COUNT_CTOR(wallet_HelpMac); } ~wallet_HelpMac() { + WALLET_FREEIF(item1); + WALLET_FREEIF(item2); + WALLET_FREEIF(item3); MOZ_COUNT_DTOR(wallet_HelpMac); } - nsAutoString item1; - nsAutoString item2; - nsAutoString item3; - nsAutoString dummy; + const char* item1; + const char* item2; + const char* item3; }; wallet_HelpMac * helpMac; @@ -291,8 +301,8 @@ PRIVATE nsVoidArray * wallet_URL_list=0; PRIVATE nsVoidArray * wallet_DistinguishedSchema_list=0; #endif -#define NO_CAPTURE 0 -#define NO_PREVIEW 1 +#define NO_CAPTURE(x) x.First() +#define NO_PREVIEW(x) x.Last() MOZ_DECL_CTOR_COUNTER(wallet_PrefillElement) @@ -300,17 +310,19 @@ class wallet_PrefillElement { public: wallet_PrefillElement() : inputElement(nsnull), selectElement(nsnull) { + schema = nsnull; MOZ_COUNT_CTOR(wallet_PrefillElement); } ~wallet_PrefillElement() { + WALLET_FREEIF(schema); NS_IF_RELEASE(inputElement); NS_IF_RELEASE(selectElement); MOZ_COUNT_DTOR(wallet_PrefillElement); } nsIDOMHTMLInputElement* inputElement; nsIDOMHTMLSelectElement* selectElement; - nsString schema; + char* schema; nsString value; PRInt32 selectIndex; PRUint32 count; @@ -351,15 +363,12 @@ wallet_Dump(nsVoidArray * list) { PRInt32 count = LIST_COUNT(list); for (PRInt32 i=0; iElementAt(i)); - fprintf(stdout, "%s %s \n", - NS_LossyConvertUCS2toASCII(mapElementPtr->item1).get(), - NS_LossyConvertUCS2toASCII(mapElementPtr->item2).get()); + fprintf(stdout, "%s %s \n", (mapElementPtr->item1), (mapElementPtr->item2)); wallet_Sublist * sublistPtr; PRInt32 count2 = LIST_COUNT(mapElementPtr->itemList); for (PRInt32 i2=0; i2itemList->ElementAt(i2)); - fprintf(stdout, " %s \n", - NS_LossyConvertUCS2toASCII(sublistPtr->item).get()); + fprintf(stdout, " %s \n", (sublistPtr->item)); } } wallet_Pause(); @@ -509,6 +518,7 @@ Wallet_Localize(const char* genericString) { return ToNewUnicode(v); } + /**********************/ /* Modal dialog boxes */ /**********************/ @@ -548,7 +558,7 @@ Wallet_ConfirmYN(PRUnichar * szMessage, nsIDOMWindowInternal* window) { (nsIPrompt::BUTTON_TITLE_NO * nsIPrompt::BUTTON_POS_1), nsnull, nsnull, nsnull, nsnull, nsnull, &buttonPressed); - Recycle(confirm_string); + WALLET_FREE(confirm_string); return (buttonPressed == 0); } @@ -572,8 +582,8 @@ Wallet_3ButtonConfirm(PRUnichar * szMessage, nsIDOMWindowInternal* window) (nsIPrompt::BUTTON_TITLE_IS_STRING * nsIPrompt::BUTTON_POS_2), nsnull, nsnull, never_string, nsnull, nsnull, &buttonPressed); - Recycle(never_string); - Recycle(confirm_string); + WALLET_FREE(never_string); + WALLET_FREE(confirm_string); return buttonPressed; } @@ -591,7 +601,7 @@ wallet_Alert(PRUnichar * szMessage, nsIDOMWindowInternal* window) const nsAutoString message( szMessage ); PRUnichar * title = Wallet_Localize("CaveatTitle"); res = dialog->Alert(title, message.get()); - Recycle(title); + WALLET_FREE(title); return; // XXX should return the error } @@ -602,7 +612,7 @@ wallet_Alert(PRUnichar * szMessage, nsIPrompt* dialog) const nsAutoString message( szMessage ); PRUnichar * title = Wallet_Localize("CaveatTitle"); res = dialog->Alert(title, message.get()); - Recycle(title); + WALLET_FREE(title); return; // XXX should return the error } @@ -632,7 +642,7 @@ Wallet_CheckConfirmYN NS_ASSERTION(PR_FALSE, "Bad result from checkbox"); *checkValue = 0; /* this should never happen but it is happening!!! */ } - Recycle(confirm_string); + WALLET_FREE(confirm_string); return (buttonPressed == 0); } @@ -682,7 +692,7 @@ PRIVATE nsresult EncryptString (const char * text, char *& crypt) { } /* otherwise do our own obscuring using Base64 encoding */ - char * crypt0 = PL_Base64Encode((const char *)text, 0, NULL); + char * crypt0 = PL_Base64Encode(text, 0, NULL); if (!crypt0) { return NS_ERROR_FAILURE; } @@ -697,7 +707,7 @@ PRIVATE nsresult EncryptString (const char * text, char *& crypt) { crypt[PREFIX_len+i] = crypt0[i]; } crypt[PREFIX_len + crypt0_len] = '\0'; - Recycle(crypt0); + WALLET_FREE(crypt0); return NS_OK; } @@ -775,76 +785,49 @@ WLLT_ChangePassword(PRBool* status) { *status = NS_SUCCEEDED(rv); } -PUBLIC nsresult -Wallet_Encrypt (const nsString& text, nsString& crypt) { +nsresult +wallet_Encrypt(const nsCString& text, nsCString& crypt) { - /* convert text from unichar to UTF8 */ - nsAutoString UTF8text; - PRUnichar c; - for (PRUint32 i=0; i>6) & 0x1F)); - UTF8text += PRUnichar((0x80) | (c & 0x3F)); - } else { - UTF8text += PRUnichar((0xE0) | ((c>>12) & 0xF)); - UTF8text += PRUnichar((0x80) | ((c>>6) & 0x3F)); - UTF8text += PRUnichar((0x80) | (c & 0x3F)); - } - } - /* encrypt text to crypt */ char * cryptCString = nsnull; - char * UTF8textCString = ToNewCString(UTF8text); - nsresult rv = EncryptString(UTF8textCString, cryptCString); - Recycle (UTF8textCString); + nsresult rv = EncryptString(text.get(), cryptCString); if NS_FAILED(rv) { return rv; } - crypt.AssignWithConversion(cryptCString); - Recycle (cryptCString); + crypt = cryptCString; + WALLET_FREE(cryptCString); + return NS_OK; +} + +nsresult +wallet_Decrypt(const nsCString& crypt, nsCString& text) { + + /* decrypt crypt to text */ + char * textCString = nsnull; + nsresult rv = DecryptString(crypt.get(), textCString); + if NS_FAILED(rv) { + return rv; + } + + text = textCString; + WALLET_FREE(textCString); return NS_OK; } PUBLIC nsresult -Wallet_Decrypt(const nsString& crypt, nsString& text) { +Wallet_Encrypt (const nsString& textUCS2, nsString& cryptUCS2) { + nsCAutoString cryptUTF8; + nsresult rv = wallet_Encrypt(NS_ConvertUCS2toUTF8(textUCS2), cryptUTF8); + cryptUCS2 = NS_ConvertUTF8toUCS2(cryptUTF8); + return rv; +} - /* decrypt crypt to text */ - char * cryptCString = ToNewCString(crypt); - char * UTF8textCString = nsnull; - - nsresult rv = DecryptString(cryptCString, UTF8textCString); - Recycle(cryptCString); - if NS_FAILED(rv) { - return rv; - } - - /* convert text from UTF8 to unichar */ - PRUnichar c1, c2, c3; - text.Truncate(0); - text.SetCapacity(2 * crypt.Length()); - - PRUint32 UTF8textCString_len = PL_strlen(UTF8textCString); - for (PRUint32 i=0; i= kAllocBlockElems) { mapElementTable = new wallet_MapElement[kAllocBlockElems]; if (!mapElementTable) { - return nsnull; + return NS_ERROR_OUT_OF_MEMORY; } if(!wallet_MapElementAllocations_list) { wallet_MapElementAllocations_list = new nsVoidArray(); @@ -908,21 +891,34 @@ wallet_AllocateMapElement() { } wallet_NextAllocSlot = 0; } - return &mapElementTable[wallet_NextAllocSlot++]; + mapElement = &mapElementTable[wallet_NextAllocSlot++]; + return NS_OK; } static void wallet_DeallocateMapElements() { wallet_MapElement * mapElementPtr; PRInt32 count = LIST_COUNT(wallet_MapElementAllocations_list); + + // initialize remainder of last allocated block so we don't crash on []delete + for (PRInt32 j=wallet_NextAllocSlot; jElementAt(count-1)); + mapElementPtr[j].item1 = nsnull; + mapElementPtr[j].item2 = nsnull; + mapElementPtr[j].itemList = nsnull; + } + for (PRInt32 i=count-1; i>=0; i--) { mapElementPtr = NS_STATIC_CAST(wallet_MapElement*, (wallet_MapElementAllocations_list)->ElementAt(i)); delete [] mapElementPtr; } delete wallet_MapElementAllocations_list; - wallet_MapElementAllocations_list = 0; + wallet_MapElementAllocations_list = nsnull; wallet_NextAllocSlot = kAllocBlockElems; + } /* @@ -930,8 +926,8 @@ wallet_DeallocateMapElements() { */ static PRBool wallet_WriteToList( - nsString item1, // not ref. Locally modified - nsString item2, // not ref. Locally modified + const char* item1, + const char* item2, nsVoidArray* itemList, nsVoidArray*& list, PRBool obscure, @@ -940,12 +936,12 @@ wallet_WriteToList( wallet_MapElement * mapElementPtr; PRBool added_to_list = PR_FALSE; - wallet_MapElement * mapElement; + wallet_MapElement * mapElement = nsnull; if (list == wallet_FieldToSchema_list || list == wallet_SchemaStrings_list || list == wallet_PositionalSchema_list || list == wallet_StateSchema_list || list == wallet_SchemaConcat_list || list == wallet_DistinguishedSchema_list || list == wallet_VcardToSchema_list) { - mapElement = wallet_AllocateMapElement(); + wallet_AllocateMapElement(mapElement); } else { mapElement = new wallet_MapElement; } @@ -953,25 +949,28 @@ wallet_WriteToList( return PR_FALSE; } - ToLowerCase(item1); + nsCAutoString item1UTF8(item1); ToLowerCase(item1UTF8); + mapElement->item1 = ToNewCString(item1UTF8); + mapElement->item2 = PL_strdup(item2); + mapElement->itemList = itemList; + if (obscure) { - nsAutoString crypt; - if (NS_FAILED(Wallet_Encrypt(item2, crypt))) { + char * crypt = nsnull; + if (NS_FAILED(EncryptString(mapElement->item2, crypt))) { + delete mapElement; return PR_FALSE; } - item2 = crypt; + WALLET_FREEIF(mapElement->item2); + mapElement->item2 = crypt; } - mapElement->item1 = item1; - mapElement->item2 = item2; - mapElement->itemList = itemList; - /* make sure the list exists */ if(!list) { - list = new nsVoidArray(); - if(!list) { - return PR_FALSE; - } + list = new nsVoidArray(); + if(!list) { + delete mapElement; + return PR_FALSE; + } } /* @@ -997,22 +996,22 @@ wallet_WriteToList( wallet_Sublist * sublistPtr2; sublistPtr = NS_STATIC_CAST(wallet_Sublist*, mapElementPtr->itemList->ElementAt(0)); sublistPtr2 = NS_STATIC_CAST(wallet_Sublist*, itemList->ElementAt(0)); - if(sublistPtr->item.Length() < sublistPtr2->item.Length()) { + if(PL_strlen(sublistPtr->item) < PL_strlen(sublistPtr2->item)) { list->InsertElementAt(mapElement, i); added_to_list = PR_TRUE; break; } - } else if (mapElementPtr->item2.Length() < item2.Length()) { + } else if (PL_strlen(mapElementPtr->item2) < PL_strlen(mapElement->item2)) { list->InsertElementAt(mapElement, i); added_to_list = PR_TRUE; break; } } - } else if(mapElementPtr->item1.Equals(item1)) { + } else if(!PL_strcmp(mapElementPtr->item1, mapElement->item1)) { if (DUP_OVERWRITE==placement) { delete mapElement; - mapElementPtr->item1 = item1; - mapElementPtr->item2 = item2; + mapElementPtr->item1 = mapElement->item1; + mapElementPtr->item2 = mapElement->item2; mapElementPtr->itemList = itemList; } else if (DUP_BEFORE==placement) { list->InsertElementAt(mapElement, i); @@ -1021,7 +1020,7 @@ wallet_WriteToList( added_to_list = PR_TRUE; break; } - } else if(Compare(mapElementPtr->item1,item1)>=0) { + } else if(PL_strcmp(mapElementPtr->item1, mapElement->item1)>=0) { list->InsertElementAt(mapElement, i); added_to_list = PR_TRUE; break; @@ -1038,8 +1037,8 @@ wallet_WriteToList( */ static PRBool wallet_ReadFromList( - nsString item1, - nsString& item2, + const nsACString& item1, + nsACString& item2, nsVoidArray*& itemList, nsVoidArray*& list, PRBool obscure, @@ -1051,17 +1050,18 @@ wallet_ReadFromList( /* find item1 in the list */ wallet_MapElement * mapElementPtr; - ToLowerCase(item1); PRInt32 count = LIST_COUNT(list); for (PRInt32 i=index; iElementAt(i)); - if(mapElementPtr->item1.Equals(item1)) { + if(item1.Equals(mapElementPtr->item1, nsCaseInsensitiveCStringComparator())) { if (obscure) { - if (NS_FAILED(Wallet_Decrypt(mapElementPtr->item2, item2))) { + char * plaintext = nsnull; + if (NS_FAILED(DecryptString(mapElementPtr->item2, plaintext))) { return PR_FALSE; } + item2 = plaintext; } else { - item2 = nsAutoString(mapElementPtr->item2); + item2 = PL_strdup(mapElementPtr->item2); } itemList = mapElementPtr->itemList; index = i+1; @@ -1077,8 +1077,8 @@ wallet_ReadFromList( PRBool wallet_ReadFromList( - nsString item1, - nsString& item2, + const nsACString& item1, + nsACString& item2, nsVoidArray*& itemList, nsVoidArray*& list, PRBool obscure) @@ -1166,7 +1166,12 @@ Wallet_UTF8Put(nsOutputFileStream& strm, PRUnichar c) { } } -static PRUnichar +static void +wallet_Put(nsOutputFileStream& strm, char c) { + strm.put(c); +} + +static char wallet_Get(nsInputFileStream& strm) { const PRUint32 buflen = 1000; static char buf[buflen+1]; @@ -1180,7 +1185,7 @@ wallet_Get(nsInputFileStream& strm) { return 0; } } - return (buf[next++] & 0xFF); + return (buf[next++]); } PUBLIC PRUnichar @@ -1196,8 +1201,9 @@ Wallet_UTF8Get(nsInputFileStream& strm) { } else { return 0; /* this is an error, input was not utf8 */ } -}/* +} +/* * I have an even a simpler set of routines if you are not concerned about UTF-8. The * algorithms for those routines are as follows: * @@ -1331,10 +1337,11 @@ Wallet_RandomName(char* suffix) * strip carriage returns and line feeds from end of line */ -static PRInt32 -wallet_GetLine(nsInputFileStream& strm, nsString& line) +static nsresult +wallet_GetLine(nsInputFileStream& strm, const char** lineCString) { const PRUint32 kInitialStringCapacity = 64; + nsCAutoString line; /* read the line */ line.Truncate(0); @@ -1343,14 +1350,14 @@ wallet_GetLine(nsInputFileStream& strm, nsString& line) PRInt32 stringCap = kInitialStringCapacity; line.SetCapacity(stringCap); - PRUnichar c; - static PRUnichar lastC = '\0'; + char c; + static char lastC = '\0'; for (;;) { - c = Wallet_UTF8Get(strm); + c = wallet_Get(strm); /* check for eof */ if (c == 0) { - return -1; + return NS_ERROR_FAILURE; } /* check for line terminator (mac=CR, unix=LF, win32=CR+LF */ @@ -1371,49 +1378,51 @@ wallet_GetLine(nsInputFileStream& strm, nsString& line) } line += c; } - + WALLET_FREEIF(*lineCString); + *lineCString = ToNewCString(line); return NS_OK; } static PRBool wallet_GetHeader(nsInputFileStream& strm) { - nsAutoString format; - nsAutoString buffer; + const char* format = nsnull; /* format revision number */ - if (NS_FAILED(wallet_GetLine(strm, format))) { + if (NS_FAILED(wallet_GetLine(strm, &format))) { return PR_FALSE; } - if (!format.EqualsWithConversion(HEADER_VERSION)) { - /* something's wrong */ - return PR_FALSE; - } - return PR_TRUE; + PRBool rv = !PL_strcmp(format, HEADER_VERSION); + WALLET_FREEIF(format); + return rv; +} + +/* + * Write a line-feed to a file + */ +static void +wallet_EndLine(nsOutputFileStream& strm) { + strm.put('\n'); } /* * Write a line to a file */ static void -wallet_PutLine(nsOutputFileStream& strm, const nsString& line) { - for (PRUint32 i=0; iElementAt(i)); wallet_PutLine(strm, (*mapElementPtr).item1); - if (!(*mapElementPtr).item2.IsEmpty()) { + if (!WALLET_NULL((*mapElementPtr).item2)) { wallet_PutLine(strm, (*mapElementPtr).item2); } else { wallet_Sublist * sublistPtr; @@ -1459,7 +1468,7 @@ wallet_WriteToFile(const char * filename, nsVoidArray* list) { wallet_PutLine(strm, (*sublistPtr).item); } } - wallet_PutLine(strm, nsAutoString()); + wallet_EndLine(strm); } /* close the stream */ @@ -1491,7 +1500,7 @@ wallet_ReadFromFile } /* read in the header */ - if (filename == schemaValueFileName) { + if (!PL_strcmp(filename, schemaValueFileName)) { if (!wallet_GetHeader(strm)) { /* something's wrong -- ignore the file */ strm.close(); @@ -1500,32 +1509,33 @@ wallet_ReadFromFile } for (;;) { - if (NS_FAILED(wallet_GetLine(strm, helpMac->item1))) { + if (NS_FAILED(wallet_GetLine(strm, &helpMac->item1))) { /* end of file reached */ break; } #ifdef AutoCapture /* Distinguished schema list is a list of single entries, not name/value pairs */ - if (PL_strcmp(filename, distinguishedSchemaFileName) == 0) { + if (!PL_strcmp(filename, distinguishedSchemaFileName)) { nsVoidArray* dummy = NULL; wallet_WriteToList(helpMac->item1, helpMac->item1, dummy, list, PR_FALSE, placement); continue; } #endif - if (NS_FAILED(wallet_GetLine(strm, helpMac->item2))) { + if (NS_FAILED(wallet_GetLine(strm, &helpMac->item2))) { /* unexpected end of file reached */ break; } - if (helpMac->item2.Length()==0) { + + if (WALLET_NULL(helpMac->item2)) { /* the value must have been deleted */ nsVoidArray* dummy = NULL; wallet_WriteToList(helpMac->item1, helpMac->item2, dummy, list, PR_FALSE, placement); continue; } - if (NS_FAILED(wallet_GetLine(strm, helpMac->item3))) { + if (NS_FAILED(wallet_GetLine(strm, &helpMac->item3))) { /* end of file reached */ nsVoidArray* dummy = NULL; wallet_WriteToList(helpMac->item1, helpMac->item2, dummy, list, PR_FALSE, placement); @@ -1533,54 +1543,53 @@ wallet_ReadFromFile return; } - if (helpMac->item3.Length()==0) { + if (WALLET_NULL(helpMac->item3)) { /* just a pair of values, no need for a sublist */ nsVoidArray* dummy = NULL; wallet_WriteToList(helpMac->item1, helpMac->item2, dummy, list, PR_FALSE, placement); } else { /* need to create a sublist and put item2 and item3 onto it */ - // don't we leak itemList here? - nsVoidArray * itemList = new nsVoidArray(); if (!itemList) { break; } - // Don't we leak sublist too? wallet_Sublist * sublist = new wallet_Sublist; if (!sublist) { break; } - sublist->item = helpMac->item2; + sublist->item = PL_strdup(helpMac->item2); itemList->AppendElement(sublist); sublist = new wallet_Sublist; if (!sublist) { + delete itemList; break; } - sublist->item = helpMac->item3; + sublist->item = PL_strdup(helpMac->item3); itemList->AppendElement(sublist); /* add any following items to sublist up to next blank line */ - helpMac->dummy.Truncate(0); for (;;) { /* get next item for sublist */ - if (NS_FAILED(wallet_GetLine(strm, helpMac->item3))) { + if (NS_FAILED(wallet_GetLine(strm, &helpMac->item3))) { /* end of file reached */ - wallet_WriteToList(helpMac->item1, helpMac->dummy, itemList, list, PR_FALSE, placement); + wallet_WriteToList(helpMac->item1, nsnull, itemList, list, PR_FALSE, placement); strm.close(); return; } - if (helpMac->item3.Length()==0) { + + if (WALLET_NULL(helpMac->item3)) { /* blank line reached indicating end of sublist */ - wallet_WriteToList(helpMac->item1, helpMac->dummy, itemList, list, PR_FALSE, placement); + wallet_WriteToList(helpMac->item1, nsnull, itemList, list, PR_FALSE, placement); break; } /* add item to sublist */ sublist = new wallet_Sublist; if (!sublist) { + delete itemList; break; } - sublist->item = helpMac->item3; + sublist->item = PL_strdup(helpMac->item3); itemList->AppendElement(sublist); } } @@ -1604,7 +1613,7 @@ Wallet_GiveCaveat(nsIDOMWindowInternal* window, nsIPrompt* dialog) { } else { wallet_Alert(message, dialog); } - Recycle(message); + WALLET_FREE(message); } } @@ -1613,14 +1622,14 @@ wallet_GetHostFile(nsIURI * url, nsString& outHostFile) { outHostFile.Truncate(0); nsAutoString urlName; - char* host; + char* host = nsnull; nsresult rv = url->GetHost(&host); if (NS_FAILED(rv)) { return; } urlName.AppendWithConversion(host); nsCRT::free(host); - char* file; + char* file = nsnull; rv = url->GetPath(&file); if (NS_FAILED(rv)) { return; @@ -1633,23 +1642,26 @@ wallet_GetHostFile(nsIURI * url, nsString& outHostFile) urlName.Left(outHostFile, stringEnd); } -static nsString& -Strip(const nsString& text, nsString& stripText) { - for (PRUint32 i=0; i'~') { stripText += c; } } - return stripText; } /* * given a displayable text, get the schema */ -static void TextToSchema( +static void +TextToSchema( const nsString& text, - nsString& schema) + nsACString& schema) { /* return if no SchemaStrings list exists */ if (!wallet_SchemaStrings_list) { @@ -1688,7 +1700,7 @@ static void TextToSchema( if (isSubstring) { /* all strings were contained in the displayable text, accept this schema */ - schema = mapElementPtr->item1; + schema.Assign(mapElementPtr->item1); return; } } @@ -1697,70 +1709,78 @@ static void TextToSchema( /* * given a field name, get the value */ -static PRInt32 FieldToValue( +static nsresult +FieldToValue( const nsString& field, - nsString& schema, - nsString& value, + nsACString& schema, + nsString& valueUCS2, nsVoidArray*& itemList, PRInt32& index) { /* return if no SchemaToValue list exists or if all values previous used */ if (!wallet_SchemaToValue_list || index == -1) { - return -1; + return NS_ERROR_FAILURE; } /* if no schema name is given, fetch schema name from field/schema tables */ nsVoidArray* dummy; - nsString stripField; - if ((schema.Length() > 0) || - wallet_ReadFromList(Strip(field, stripField), schema, dummy, wallet_FieldToSchema_list, PR_FALSE)) { + nsCAutoString stripField; + if (schema.IsEmpty()) { + Strip(field, stripField); + } + if (!schema.IsEmpty() || + wallet_ReadFromList(stripField, schema, dummy, wallet_FieldToSchema_list, PR_FALSE)) { /* schema name found, now attempt to fetch value from schema/value table */ + nsCAutoString valueUTF8; PRInt32 index2 = index; if ((index >= 0) && - wallet_ReadFromList(schema, value, itemList, wallet_SchemaToValue_list, PR_TRUE, index2)) { + wallet_ReadFromList + (schema, valueUTF8, itemList, wallet_SchemaToValue_list, PR_TRUE, index2)) { /* value found, prefill it into form and return */ + valueUCS2 = NS_ConvertUTF8toUCS2(valueUTF8); index = index2; - return 0; + return NS_OK; } else { /* value not found, see if concatenation rule exists */ nsVoidArray * itemList2; - nsAutoString dummy2; + nsCAutoString valueUTF8b; if (index > 0) { index = 0; } PRInt32 index0 = index; PRInt32 index00 = index; PRInt32 index4 = 0; - while (wallet_ReadFromList(schema, dummy2, itemList2, wallet_SchemaConcat_list, PR_FALSE, index4)) { + while (wallet_ReadFromList(schema, valueUTF8b, itemList2, wallet_SchemaConcat_list, PR_FALSE, index4)) { /* concatenation rules exist, generate value as a concatenation */ + nsCAutoString concatenatedValueUTF8; wallet_Sublist * sublistPtr; - value.SetLength(0); - nsAutoString value2; + concatenatedValueUTF8.SetLength(0); + nsCAutoString valueUTF8c; PRInt32 index00max = index0; - if (dummy2.Length() > 0) { + if (!valueUTF8b.IsEmpty()) { /* single item on rhs of concatenation rule */ PRInt32 index5 = 0; PRInt32 j; PRBool failed = PR_FALSE; for (j=0; j>index0; j -= 2) { - if (!wallet_ReadFromList(dummy2, value2, dummy, wallet_SchemaToValue_list, PR_TRUE, index5)) { + if (!wallet_ReadFromList(valueUTF8b, valueUTF8c, dummy, wallet_SchemaToValue_list, PR_TRUE, index5)) { failed = PR_TRUE; break; } index00 += 2; } - if (!failed && wallet_ReadFromList(dummy2, value2, dummy, wallet_SchemaToValue_list, PR_TRUE, index5)) { + if (!failed && wallet_ReadFromList(valueUTF8b, valueUTF8c, dummy, wallet_SchemaToValue_list, PR_TRUE, index5)) { /* found an unused value for the single rhs item */ - value += value2; + concatenatedValueUTF8 += valueUTF8c; index00 += 2; } index00max = index00; @@ -1779,8 +1799,9 @@ static PRInt32 FieldToValue( index00 = index0; PRInt32 index3 = 0; PRBool failed = PR_FALSE; + nsCAutoString valueUTF8d; valueUTF8d.Assign(sublistPtr->item); for (PRInt32 j=0; j>index0; j -= 2) { - if (!wallet_ReadFromList(sublistPtr->item, value2, dummy, wallet_SchemaToValue_list, PR_TRUE, index3)) { + if (!wallet_ReadFromList(valueUTF8d, valueUTF8, dummy, wallet_SchemaToValue_list, PR_TRUE, index3)) { /* all values of next multi-rhs item were used previously */ failed = PR_TRUE; @@ -1789,13 +1810,13 @@ static PRInt32 FieldToValue( index00 += 2; } - if (!failed && wallet_ReadFromList(sublistPtr->item, value2, dummy, wallet_SchemaToValue_list, PR_TRUE, index3)) { - if (value.Length()>0) { - value.Append(NS_LITERAL_STRING(" ")); + if (!failed && wallet_ReadFromList(valueUTF8d, valueUTF8, dummy, wallet_SchemaToValue_list, PR_TRUE, index3)) { + if (!concatenatedValueUTF8.IsEmpty()) { + concatenatedValueUTF8 += " "; } /* found an unused value for the multi-rhs item */ - value += value2; + concatenatedValueUTF8 += valueUTF8; index00 += 2; } if (index00 > index00max) { @@ -1804,11 +1825,12 @@ static PRInt32 FieldToValue( } itemList = nsnull; - if (value.Length()>0) { + if (!concatenatedValueUTF8.IsEmpty()) { /* a new value was found */ index -= 2; - return 0; + valueUCS2 = NS_ConvertUTF8toUCS2(concatenatedValueUTF8); + return NS_OK; } /* all values from this concat rule were used, go on to next concat rule */ @@ -1817,29 +1839,33 @@ static PRInt32 FieldToValue( /* no more concat rules, indicate failure */ index = -1; - return -1; + return NS_ERROR_FAILURE; } } else { /* schema name not found, use field name as schema name and fetch value */ PRInt32 index2 = index; - nsAutoString temp; - wallet_GetHostFile(wallet_lastUrl, temp); - temp.Append(NS_LITERAL_STRING(":")); - temp.Append(field); + nsAutoString localSchemaUCS2; + wallet_GetHostFile(wallet_lastUrl, localSchemaUCS2); + localSchemaUCS2.Append(NS_LITERAL_STRING(":")); + localSchemaUCS2.Append(field); + nsCAutoString localSchemaUTF8 = NS_ConvertUCS2toUTF8(localSchemaUCS2); + nsCAutoString valueUTF8; - if (wallet_ReadFromList(temp, value, itemList, wallet_SchemaToValue_list, PR_TRUE, index2)) { + if (wallet_ReadFromList + (localSchemaUTF8, valueUTF8, itemList, wallet_SchemaToValue_list, PR_TRUE, index2)) { /* value found, prefill it into form */ - schema = temp; + schema = localSchemaUTF8; index = index2; - return 0; + valueUCS2 = NS_ConvertUTF8toUCS2(valueUTF8); + return NS_OK; } } index = -1; - return -1; + return NS_ERROR_FAILURE; } -static PRInt32 +static nsresult wallet_GetSelectIndex( nsIDOMHTMLSelectElement* selectElement, const nsString& value, @@ -1871,13 +1897,13 @@ wallet_GetSelectIndex( optionText.Trim(" \n\t\r"); if (valueLC==optionValue || valueLC==optionText) { index = optionX; - return 0; + return NS_OK; } } } } } - return -1; + return NS_ERROR_FAILURE; } void @@ -1954,17 +1980,17 @@ wallet_StepForwardOrBack } /* if we've reached a #text node, append it to accumulated text */ - nsAutoString siblingName; - result = elementNode->GetNodeName(siblingName); - nsCAutoString siblingCName; siblingCName.AssignWithConversion(siblingName); - if (siblingCName.EqualsIgnoreCase("#text")) { + nsAutoString siblingNameUCS2; + result = elementNode->GetNodeName(siblingNameUCS2); + nsCAutoString siblingNameUTF8; siblingNameUTF8.AssignWithConversion(siblingNameUCS2); + if (siblingNameUTF8.EqualsIgnoreCase("#text")) { nsAutoString siblingValue; result = elementNode->GetNodeValue(siblingValue); text.Append(siblingValue); } /* if we've reached a SCRIPT node, don't fetch its siblings */ - if (siblingCName.EqualsIgnoreCase("SCRIPT")) { + if (siblingNameUTF8.EqualsIgnoreCase("SCRIPT")) { return; } @@ -1996,10 +2022,10 @@ wallet_StepForwardOrBack //static nsICaseConversion* gCaseConv = nsnull; static void -wallet_ResolvePositionalSchema(nsIDOMNode* elementNode, nsString& schema) { +wallet_ResolvePositionalSchema(nsIDOMNode* elementNode, nsACString& schema) { static PRInt32 numerator = 0; static PRInt32 denominator = 0; - static nsString lastPositionalSchema; + static nsCString lastPositionalSchema; /* return if no PositionalSchema list exists */ if (!wallet_PositionalSchema_list) { @@ -2007,12 +2033,12 @@ wallet_ResolvePositionalSchema(nsIDOMNode* elementNode, nsString& schema) { return; } - if (schema.Length()) { + if (!schema.IsEmpty()) { numerator = 0; denominator = 0; - lastPositionalSchema = schema; + lastPositionalSchema.Assign(schema); } else if (numerator < denominator) { - schema = lastPositionalSchema; + schema.Assign(lastPositionalSchema); } else { schema.SetLength(0); return; @@ -2023,7 +2049,7 @@ wallet_ResolvePositionalSchema(nsIDOMNode* elementNode, nsString& schema) { PRInt32 count = LIST_COUNT(wallet_PositionalSchema_list); for (PRInt32 i=0; iElementAt(i)); - if (mapElementPtr->item1.EqualsIgnoreCase(schema)) { + if (schema.Equals(mapElementPtr->item1, nsCaseInsensitiveCStringComparator())) { /* found our positional schema in the list */ /* A "position set" is a set of continuous or