int CAPETag::SetFieldString(const str_utf16 * pFieldName, const char * pFieldValue, BOOL bAlreadyUTF8Encoded) { // remove if empty if ((pFieldValue == NULL) || (strlen(pFieldValue) <= 0)) return RemoveField(pFieldName); // get the length and call the binary SetField(...) if (bAlreadyUTF8Encoded == FALSE) { CSmartPtr<char> spUTF8((char *) GetUTF8FromANSI(pFieldValue), TRUE); int nFieldBytes = strlen(spUTF8.GetPtr()); return SetFieldBinary(pFieldName, spUTF8.GetPtr(), nFieldBytes, TAG_FIELD_FLAG_DATA_TYPE_TEXT_UTF8); } else { int nFieldBytes = strlen(pFieldValue); return SetFieldBinary(pFieldName, pFieldValue, nFieldBytes, TAG_FIELD_FLAG_DATA_TYPE_TEXT_UTF8); } }
int CAPETag::LoadField(const char * pBuffer, int nMaximumBytes, int * pBytes) { // set bytes to 0 if (pBytes) *pBytes = 0; // size and flags int nLocation = 0; char b = pBuffer[nLocation]; //int nFieldValueSize = *((int *) &pBuffer[nLocation]); //Data type misalignment on Wince!!! int nFieldValueSize = (int)b; nLocation += 4; b = pBuffer[nLocation]; int nFieldFlags = (int)b; nLocation += 4; // safety check (so we can't get buffer overflow attacked) BOOL bSafe = FALSE; int nMaximumRead = nMaximumBytes - 8 - nFieldValueSize; if (nMaximumRead > 0) { bSafe = TRUE; for (int z = 0; (z < nMaximumRead) && (bSafe == TRUE); z++) { int nCharacter = pBuffer[nLocation + z]; if (nCharacter == 0) break; if ((nCharacter < 0x20) || (nCharacter > 0x7E)) bSafe = FALSE; } } if (bSafe == FALSE) return -1; // name int nNameCharacters = strlen(&pBuffer[nLocation]); CSmartPtr<str_utf8> spNameUTF8(new str_utf8 [nNameCharacters + 1], TRUE); memcpy(spNameUTF8, &pBuffer[nLocation], (nNameCharacters + 1) * sizeof(str_utf8)); nLocation += nNameCharacters + 1; CSmartPtr<str_utf16> spNameUTF16(CAPECharacterHelper::GetUTF16FromUTF8(spNameUTF8.GetPtr()), TRUE); // value CSmartPtr<char> spFieldBuffer(new char [nFieldValueSize], TRUE); memcpy(spFieldBuffer, &pBuffer[nLocation], nFieldValueSize); nLocation += nFieldValueSize; // update the bytes if (pBytes) *pBytes = nLocation; // set return SetFieldBinary(spNameUTF16.GetPtr(), spFieldBuffer, nFieldValueSize, nFieldFlags); }