// do the fallback, reallocate the buffer if necessary // need to pass destination buffer info (size, current position and estimation of rest of the conversion) NS_IMETHODIMP nsSaveAsCharset::HandleFallBack(uint32_t character, char **outString, int32_t *bufferLength, int32_t *currentPos, int32_t estimatedLength) { NS_ENSURE_ARG_POINTER(outString); NS_ENSURE_ARG_POINTER(bufferLength); NS_ENSURE_ARG_POINTER(currentPos); char fallbackStr[256]; nsresult rv = DoConversionFallBack(character, fallbackStr, 256); if (NS_SUCCEEDED(rv)) { int32_t tempLen = (int32_t) strlen(fallbackStr); // reallocate if the buffer is not large enough if ((tempLen + estimatedLength) >= (*bufferLength - *currentPos)) { int32_t addLength = tempLen + RESERVE_FALLBACK_BYTES; // + 1 is for the terminating NUL, don't add that to bufferLength char *temp = (char *) PR_Realloc(*outString, *bufferLength + addLength + 1); if (temp) { // adjust length/pointer after realloc *bufferLength += addLength; *outString = temp; } else { *outString = nullptr; *bufferLength = 0; return NS_ERROR_OUT_OF_MEMORY; } } memcpy((*outString + *currentPos), fallbackStr, tempLen); *currentPos += tempLen; } return rv; }
// do the fallback, reallocate the buffer if necessary // need to pass destination buffer info (size, current position and estimation of rest of the conversion) NS_IMETHODIMP nsSaveAsCharset::HandleFallBack(PRUint32 character, char **outString, PRInt32 *bufferLength, PRInt32 *currentPos, PRInt32 estimatedLength) { if((nullptr == outString ) || (nullptr == bufferLength) ||(nullptr ==currentPos)) return NS_ERROR_NULL_POINTER; char fallbackStr[256]; nsresult rv = DoConversionFallBack(character, fallbackStr, 256); if (NS_SUCCEEDED(rv)) { PRInt32 tempLen = (PRInt32) PL_strlen(fallbackStr); // reallocate if the buffer is not large enough if ((tempLen + estimatedLength) >= (*bufferLength - *currentPos)) { char *temp = (char *) PR_Realloc(*outString, *bufferLength + tempLen); if (NULL != temp) { // adjust length/pointer after realloc *bufferLength += tempLen; *outString = temp; } else { *outString = NULL; *bufferLength =0; return NS_ERROR_OUT_OF_MEMORY; } } memcpy((*outString + *currentPos), fallbackStr, tempLen); *currentPos += tempLen; } return rv; }