Exemple #1
0
	// 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);
	}
Exemple #2
0
//============================================================================
//		NFileUtilities::GetFileText : Get a file as text.
//----------------------------------------------------------------------------
NString NFileUtilities::GetFileText(const NFile &theFile, NStringEncoding theEncoding)
{	NStringEncoder		theEncoder;
	NData				theData;
	NString				theText;



	// Get the state we need
	theData = GetFileData(theFile);
	if (theData.IsEmpty())
		return(theText);
	
	
	
	// Get the text
	if (theEncoding == kNStringEncodingInvalid)
		{
		theEncoding = theEncoder.GetEncoding(theData);
		NN_ASSERT(theEncoding != kNStringEncodingInvalid);
		}

	if (theEncoding != kNStringEncodingInvalid)
		theText = NString(theData, theEncoding);
		
	return(theText);
}
Exemple #3
0
//============================================================================
//		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);
}
Exemple #4
0
//============================================================================
//		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);
}
Exemple #5
0
//============================================================================
//		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());
}
Exemple #6
0
//============================================================================
//		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());
}
Exemple #7
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);
}
Exemple #8
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);
}
Exemple #9
0
//============================================================================
//		NDataEncoder::Hex_Decode : Decode from hex.
//----------------------------------------------------------------------------
NData NDataEncoder::Hex_Decode(const NString &theValue)
{	NIndex			n, theSize;
	NData			theResult;
	uint8_t			*dataPtr;
	const char		*textPtr;
	unsigned int	byteVal;



	// Validate our parameters
	NN_ASSERT(theValue.IsEmpty() || (theValue.GetSize() % 2) == 0);



	// Get the state we need
	theSize = theValue.GetSize() / 2;
	dataPtr = theResult.AppendData(theSize);
	textPtr = theValue.GetUTF8();

	if (theSize == 0 || dataPtr == NULL || textPtr == NULL)
		return(theResult);



	// Convert the string
	//
	// Visual Studio does not support 'hh' correctly, so we need to read byte
	// values into a temporary variable:
	//
	// https://connect.microsoft.com/VisualStudio/feedback/details/416843/sscanf-cannot-not-handle-hhd-format
	for (n = 0; n < theSize; n++)
		{
		sscanf(textPtr, "%2x", &byteVal);
		NN_ASSERT((byteVal & 0xFFFFFF00) == 0);

		dataPtr[n] = (uint8_t) byteVal;
		textPtr   += 2;
		}
	
	return(theResult);
}
Exemple #10
0
//============================================================================
//		NDBResult::GetValueData : Get a data value.
//----------------------------------------------------------------------------
NData NDBResult::GetValueData(NIndex theIndex) const
{	NData			theResult;
	const void		*thePtr;
	NIndex			theSize;



	// Validate our parameters
	NN_ASSERT(theIndex < GetSize());



	// Get the value
	thePtr  = (const void *) sqlite3_column_blob( (sqlite3_stmt *) mResult, theIndex);
	theSize = (NIndex      ) sqlite3_column_bytes((sqlite3_stmt *) mResult, theIndex);
	
	if (thePtr != NULL && theSize != 0)
		theResult.AppendData(theSize, thePtr);
	
	return(theResult);
}
Exemple #11
0
//============================================================================
//		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);
}
Exemple #12
0
//============================================================================
//		NUnicodeParser::RemoveBOM : Remove a BOM prefix.
//----------------------------------------------------------------------------
void NUnicodeParser::RemoveBOM(NData &theData, NStringEncoding theEncoding) const
{   NStringEncoding		bomEncoding;
    NRange				theBOM;



    // Validate our parameters
    NN_ASSERT(NStringEncoder::IsEncodingUTF(theEncoding));
    NN_UNUSED(theEncoding);



    // Get the state we need
    bomEncoding = GetBOM(theData, theBOM);
    if (!theBOM.IsEmpty())
        theData.RemoveData(theBOM);



    // Validate the encoding
    //
    // Endian-specific BOMs should match the format we expected.
    switch (bomEncoding) {
    case kNStringEncodingInvalid:
        // No BOM
        break;

    case kNStringEncodingUTF8:
        NN_ASSERT(theEncoding == kNStringEncodingUTF8);
        break;

    case kNStringEncodingUTF16BE:
        NN_ASSERT(theEncoding == kNStringEncodingUTF16 || theEncoding == kNStringEncodingUTF16BE);
        break;

    case kNStringEncodingUTF16LE:
        NN_ASSERT(theEncoding == kNStringEncodingUTF16 || theEncoding == kNStringEncodingUTF16LE);
        break;

    case kNStringEncodingUTF32BE:
        NN_ASSERT(theEncoding == kNStringEncodingUTF32 || theEncoding == kNStringEncodingUTF32BE);
        break;

    case kNStringEncodingUTF32LE:
        NN_ASSERT(theEncoding == kNStringEncodingUTF32 || theEncoding == kNStringEncodingUTF32LE);
        break;

    default:
        NN_LOG("Invalid encoding: %d", theEncoding);
        break;
    }
}
Exemple #13
0
//============================================================================
//		NSocketRequest::NSocketRequest : Constructor.
//----------------------------------------------------------------------------
NSocketRequest::NSocketRequest(const NData &theData)
{


	// Validate our parameters
	NN_ASSERT(!theData.IsEmpty());



	// Initialise ourselves
	mData      = theData;
	mProcessed = 0;

	mStatus    = kNoErr;
	mSemaphore = NULL;
}