// Decode the object void DecodeSelf(const NEncoder &theEncoder) { NArray theArray; NDictionary theDict; NData theData; NN_ASSERT(theEncoder.DecodeBoolean(kKeyBoolean1) == kValueBoolean1); NN_ASSERT(theEncoder.DecodeBoolean(kKeyBoolean2) == kValueBoolean2); NN_ASSERT(theEncoder.DecodeNumber( kKeyNumber1) == kValueNumber1); NN_ASSERT(theEncoder.DecodeNumber( kKeyNumber2) == kValueNumber2); NN_ASSERT(theEncoder.DecodeNumber( kKeyNumber3) == kValueNumber3); NN_ASSERT(theEncoder.DecodeNumber( kKeyNumber4) == kValueNumber4); NN_ASSERT(theEncoder.DecodeString( kKeyString) == kValueString); theData = theEncoder.DecodeData(kKeyData); NN_ASSERT(theData.GetSize() == NN_ARRAY_SIZE(kValueData)); NN_ASSERT(memcmp(theData.GetData(), kValueData, (size_t) theData.GetSize()) == 0); NN_ASSERT(theEncoder.DecodeObject(kKeyArray).GetValue(theArray)); NN_ASSERT(theArray.GetSize() == 3); NN_ASSERT(theArray.GetValueBoolean(0) == kValueBoolean1); NN_ASSERT(theArray.GetValue (1) == kValueNumber1); NN_ASSERT(theArray.GetValueString (2) == kValueString); NN_ASSERT(theEncoder.DecodeObject(kKeyDictionary).GetValue(theDict)); NN_ASSERT(theDict.GetSize() == 6); NN_ASSERT(theDict.GetValueBoolean (kKeyBoolean1) == kValueBoolean1); NN_ASSERT(theDict.GetValue (kKeyNumber1) == kValueNumber1); NN_ASSERT(theDict.GetValueString (kKeyString) == kValueString); NN_ASSERT(theDict.GetValuePoint (kKeyPoint) == kValuePoint); NN_ASSERT(theDict.GetValueSize (kKeySize) == kValueSize); NN_ASSERT(theDict.GetValueRectangle(kKeyRectangle) == kValueRectangle); }
//============================================================================ // NDataEncoder::B64_Encode : Encode to Base64. //---------------------------------------------------------------------------- NString NDataEncoder::B64_Encode(const NData &theValue) { NData theBuffer; NString theString; base64_encodestate theState; char *dataPtr; NIndex dataSize; // Get the state we need base64_init_encodestate(&theState); if (theValue.IsEmpty() || !theBuffer.SetSize(theValue.GetSize() * 2)) return(theString); // Encode the value dataPtr = (char *) theBuffer.GetData(); dataSize = base64_encode_block((const char *) theValue.GetData(), theValue.GetSize(), dataPtr, &theState); theString = NString(dataPtr, dataSize); dataSize = base64_encode_blockend(dataPtr, &theState); if (dataSize != 0) theString += NString(dataPtr, dataSize); // Remove the trailing newline NN_ASSERT(theString.GetRight(1) == "\n"); theString.TrimRight(1); return(theString); }
//============================================================================ // NFileUtilities::SetFileData : Set a file to data. //---------------------------------------------------------------------------- NStatus NFileUtilities::SetFileData(const NFile &theFile, const NData &theData) { uint64_t theSize, numWritten; NFile mutableFile; NStatus theErr; // Get the state we need mutableFile = theFile; theSize = theData.GetSize(); // Open the file theErr = mutableFile.Open(kNPermissionUpdate, true); if (theErr != kNoErr) return(theErr); // Write the data theErr = mutableFile.SetSize(0); theErr |= mutableFile.Write(theSize, theData.GetData(), numWritten); NN_ASSERT_NOERR(theErr); NN_ASSERT(numWritten == theSize); // Clean up mutableFile.Close(); return(theErr); }
//============================================================================ // NDataEncoder::Hex_Encode : Encode to hex. //---------------------------------------------------------------------------- NString NDataEncoder::Hex_Encode(const NData &theValue) { NIndex n, theSize; NData tmpBuffer; NString theResult; const uint8_t *dataPtr; char *textPtr; // Get the state we need theSize = theValue.GetSize(); dataPtr = theValue.GetData(); textPtr = (char *) tmpBuffer.AppendData((theSize * 2) + 1); if (theSize == 0 || dataPtr == NULL || textPtr == NULL) return(theResult); // Convert to a string for (n = 0; n < theSize; n++) { sprintf(textPtr, "%.2X", dataPtr[n]); textPtr += 2; } theResult = NString(tmpBuffer); return(theResult); }
//============================================================================ // NUnicodeParser::GetBOM : Get the BOM. //---------------------------------------------------------------------------- NStringEncoding NUnicodeParser::GetBOM(const NData &theData, NRange &theRange) const { NStringEncoding theEncoding; const uint8_t *dataPtr; NIndex dataSize; // Get the state we need theEncoding = kNStringEncodingInvalid; dataSize = theData.GetSize(); dataPtr = theData.GetData(); theRange = kNRangeNone; // Identify the BOM #define MATCH_BOM(_bom, _encoding) \ do \ { \ if (theRange.IsEmpty() && dataSize >= NN_ARRAY_SIZE(_bom)) \ { \ if (memcmp(dataPtr, _bom, NN_ARRAY_SIZE(_bom)) == 0) \ { \ theEncoding = _encoding; \ theRange = NRange(0, NN_ARRAY_SIZE(_bom)); \ } \ } \ } \ while (0) MATCH_BOM(kUTF8BOM, kNStringEncodingUTF8); MATCH_BOM(kUTF16BOMBE, kNStringEncodingUTF16BE); MATCH_BOM(kUTF16BOMLE, kNStringEncodingUTF16LE); MATCH_BOM(kUTF32BOMBE, kNStringEncodingUTF32BE); MATCH_BOM(kUTF32BOMLE, kNStringEncodingUTF32LE); #undef MATCH_BOM return(theEncoding); }