示例#1
0
//============================================================================
//		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
//============================================================================
//		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);
}