コード例 #1
0
ファイル: NDataEncoder.cpp プロジェクト: refnum/nano
//============================================================================
//		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);
}
コード例 #2
0
ファイル: TEncoder.cpp プロジェクト: x414e54/nano
	// 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);
	}
コード例 #3
0
ファイル: NFileUtilities.cpp プロジェクト: refnum/nano
//============================================================================
//		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);
}
コード例 #4
0
ファイル: NDataEncoder.cpp プロジェクト: refnum/nano
//============================================================================
//		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);
}
コード例 #5
0
ファイル: NUnicodeParser.cpp プロジェクト: refnum/nano
//============================================================================
//		NUnicodeParser::AddBOMToUTF8 : Add a UTF8 BOM.
//----------------------------------------------------------------------------
void NUnicodeParser::AddBOMToUTF8(NData &theData) const
{   const uint8_t	*theBOM;



    // Insert the BOM
    theBOM = theData.InsertData(0, NN_ARRAY_SIZE(kUTF8BOM), kUTF8BOM);
    NN_ASSERT(theBOM == theData.GetData());
}
コード例 #6
0
ファイル: NUnicodeParser.cpp プロジェクト: refnum/nano
//============================================================================
//		NUnicodeParser::AddBOMToUTF32 : Add a UTF32 BOM.
//----------------------------------------------------------------------------
void NUnicodeParser::AddBOMToUTF32(NData &theData, NEndianFormat theFormat) const
{   const uint8_t	*theBOM;



    // Insert the BOM
    if (theFormat == kNEndianBig)
        theBOM = theData.InsertData(0, NN_ARRAY_SIZE(kUTF32BOMBE), kUTF32BOMBE);
    else
        theBOM = theData.InsertData(0, NN_ARRAY_SIZE(kUTF32BOMLE), kUTF32BOMLE);

    NN_ASSERT(theBOM == theData.GetData());
}
コード例 #7
0
ファイル: NFileUtilities.cpp プロジェクト: refnum/nano
//============================================================================
//		NFileUtilities::GetFileData : Get a file as data.
//----------------------------------------------------------------------------
NData NFileUtilities::GetFileData(const NFile &theFile)
{	uint64_t		theSize, numRead;
	NFile			mutableFile;
	NData			theData;
	NStatus			theErr;



	// Get the state we need
	mutableFile = theFile;
	theSize     = mutableFile.GetSize();
	NN_ASSERT( ((int64_t) theSize) <= kInt32Max);



	// Resize the buffer
	if (!theData.SetSize((NIndex) theSize))
		return(theData);



	// Open the file
	theErr = mutableFile.Open();
	if (theErr != kNoErr)
		{
		theData.Clear();
		return(theData);
		}



	// Read the data
	theErr = mutableFile.Read(theSize, theData.GetData(), numRead);
	theData.SetSize((NIndex) numRead);

	NN_ASSERT_NOERR(theErr);
	NN_ASSERT(numRead == theSize);
	NN_ASSERT(((int64_t) numRead) <= kInt32Max);



	// Clean up
	mutableFile.Close();

	return(theData);
}
コード例 #8
0
ファイル: NUnicodeParser.cpp プロジェクト: refnum/nano
//============================================================================
//		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);
}