TEST_F(InputFromBuffer, ReadLargeBuffer) { OpcUa::InputFromBuffer input(&SourceBuffer[0], SourceBuffer.size()); const std::vector<char> expectedBuf{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11}; std::vector<char> targetBuf(11, 11); size_t revceivedSize = 0; ASSERT_NO_THROW(revceivedSize = input.Receive(&targetBuf[0], targetBuf.size())); ASSERT_EQ(revceivedSize, SourceBuffer.size()); ASSERT_EQ(targetBuf, expectedBuf); }
void UvTcpServerStream::HandleRead(ssize_t nread, const uv_buf_t* buf) { if (nread > 0) { std::vector<uint8_t> targetBuf(nread); memcpy(&targetBuf[0], buf->base, targetBuf.size()); if (GetReadCallback()) { GetReadCallback()(targetBuf); } } else if (nread < 0) { // hold a reference to ourselves while in this scope fwRefContainer<UvTcpServerStream> tempContainer = this; trace("read error: %s\n", uv_strerror(nread)); Close(); } }
void ArrayBuffer_selfTest (void) { // default constructor { de::ArrayBuffer<int> buf; DE_TEST_ASSERT(buf.size() == 0); DE_TEST_ASSERT(buf.getPtr() == DE_NULL); } // sized constructor { de::ArrayBuffer<int> buf(4); DE_TEST_ASSERT(buf.size() == 4); DE_TEST_ASSERT(buf.getPtr() != DE_NULL); } // copy constructor { de::ArrayBuffer<int> originalBuf(4); *originalBuf.getElementPtr(0) = 1; *originalBuf.getElementPtr(1) = 2; *originalBuf.getElementPtr(2) = 3; *originalBuf.getElementPtr(3) = 4; de::ArrayBuffer<int> targetBuf(originalBuf); DE_TEST_ASSERT(*originalBuf.getElementPtr(0) == 1); DE_TEST_ASSERT(*originalBuf.getElementPtr(1) == 2); DE_TEST_ASSERT(*originalBuf.getElementPtr(2) == 3); DE_TEST_ASSERT(*originalBuf.getElementPtr(3) == 4); DE_TEST_ASSERT(*targetBuf.getElementPtr(0) == 1); DE_TEST_ASSERT(*targetBuf.getElementPtr(1) == 2); DE_TEST_ASSERT(*targetBuf.getElementPtr(2) == 3); DE_TEST_ASSERT(*targetBuf.getElementPtr(3) == 4); } // assignment { de::ArrayBuffer<int> originalBuf(4); *originalBuf.getElementPtr(0) = 1; *originalBuf.getElementPtr(1) = 2; *originalBuf.getElementPtr(2) = 3; *originalBuf.getElementPtr(3) = 4; de::ArrayBuffer<int> targetBuf(1); targetBuf = originalBuf; DE_TEST_ASSERT(*originalBuf.getElementPtr(0) == 1); DE_TEST_ASSERT(*originalBuf.getElementPtr(1) == 2); DE_TEST_ASSERT(*originalBuf.getElementPtr(2) == 3); DE_TEST_ASSERT(*originalBuf.getElementPtr(3) == 4); DE_TEST_ASSERT(*targetBuf.getElementPtr(0) == 1); DE_TEST_ASSERT(*targetBuf.getElementPtr(1) == 2); DE_TEST_ASSERT(*targetBuf.getElementPtr(2) == 3); DE_TEST_ASSERT(*targetBuf.getElementPtr(3) == 4); } // clear { de::ArrayBuffer<int> buf(4); buf.clear(); DE_TEST_ASSERT(buf.size() == 0); DE_TEST_ASSERT(buf.getPtr() == DE_NULL); } // setStorage { de::ArrayBuffer<int> buf(4); buf.setStorage(12); DE_TEST_ASSERT(buf.size() == 12); DE_TEST_ASSERT(buf.getPtr() != DE_NULL); } // setStorage, too large { de::ArrayBuffer<int> buf(4); *buf.getElementPtr(0) = 1; *buf.getElementPtr(1) = 2; *buf.getElementPtr(2) = 3; *buf.getElementPtr(3) = 4; try { buf.setStorage((size_t)-1); // setStorage succeeded, all ok } catch (std::bad_alloc&) { // alloc failed, check storage not changed DE_TEST_ASSERT(buf.size() == 4); DE_TEST_ASSERT(*buf.getElementPtr(0) == 1); DE_TEST_ASSERT(*buf.getElementPtr(1) == 2); DE_TEST_ASSERT(*buf.getElementPtr(2) == 3); DE_TEST_ASSERT(*buf.getElementPtr(3) == 4); } } // swap { de::ArrayBuffer<int> buf; de::ArrayBuffer<int> source(4); *source.getElementPtr(0) = 1; *source.getElementPtr(1) = 2; *source.getElementPtr(2) = 3; *source.getElementPtr(3) = 4; buf.swap(source); DE_TEST_ASSERT(source.size() == 0); DE_TEST_ASSERT(buf.size() == 4); DE_TEST_ASSERT(*buf.getElementPtr(0) == 1); DE_TEST_ASSERT(*buf.getElementPtr(1) == 2); DE_TEST_ASSERT(*buf.getElementPtr(2) == 3); DE_TEST_ASSERT(*buf.getElementPtr(3) == 4); } // default { de::ArrayBuffer<int> source(4); int dst; *source.getElementPtr(1) = 2; deMemcpy(&dst, (int*)source.getPtr() + 1, sizeof(int)); DE_TEST_ASSERT(dst == 2); } // Aligned { de::ArrayBuffer<int, 64, sizeof(int)> source(4); int dst; *source.getElementPtr(1) = 2; deMemcpy(&dst, (int*)source.getPtr() + 1, sizeof(int)); DE_TEST_ASSERT(dst == 2); } // Strided { de::ArrayBuffer<int, 4, 64> source(4); int dst; *source.getElementPtr(1) = 2; deMemcpy(&dst, (deUint8*)source.getPtr() + 64, sizeof(int)); DE_TEST_ASSERT(dst == 2); } // Aligned, Strided { de::ArrayBuffer<int, 32, 64> source(4); int dst; *source.getElementPtr(1) = 2; deMemcpy(&dst, (deUint8*)source.getPtr() + 64, sizeof(int)); DE_TEST_ASSERT(dst == 2); } }
//---------------------------------------------------------------------- // //---------------------------------------------------------------------- ReferenceBuffer* Encoding::Convert( const void* src, size_t srcByteCount, const Encoding* srcEncoding, const Encoding* targetEncoding, size_t* bytesUsed, size_t* charsUsed, bool* usedDefaultChar) { LN_THROW(src != NULL, ArgumentException); LN_THROW(srcEncoding != NULL, ArgumentException); LN_THROW(targetEncoding != NULL, ArgumentException); LN_THROW(srcByteCount >= srcEncoding->GetMinByteCount(), ArgumentException); // バッファのバイト数は、そのバッファのエンコーディングの最低バイト数以上でなければならない /* 変換後に必要な最大のバイト数を求める 例) pEncoding = UTF32 だったら 変換後最大文字数 = uByteCount / 1文字最低バイト数(4) pEncoding = UTF16 だったら 変換後最大文字数 = uByteCount / 1文字最低バイト数(2) pEncoding = UTF8 だったら 変換後最大文字数 = uByteCount / 1文字最大バイト数(1) これに、pThisEncoding の1文字の最大バイト数を掛ける。 */ int nConvertedMaxCharCount = srcByteCount / srcEncoding->GetMinByteCount(); int nConvertedMaxBytes = nConvertedMaxCharCount * targetEncoding->GetMaxByteCount(); nConvertedMaxBytes += targetEncoding->GetMinByteCount(); // 終端 \0 の分 // 変換の中間フォーマットとして wchar_t を使用する。そのための一時バッファ確保 int nWideMaxBytes = nConvertedMaxCharCount * Encoding::GetWideCharEncoding()->GetMaxByteCount(); RefPtr<ReferenceBuffer> tmpBuf(LN_NEW ReferenceBuffer()); tmpBuf->reserve(nWideMaxBytes + sizeof(wchar_t)); // 終端 \0 考慮 (mbstowcs_s は \0 を書き込もうとする) // 変換先バッファを、最大要素数で確保 RefPtr<ReferenceBuffer> targetBuf(LN_NEW ReferenceBuffer()); targetBuf->reserve(nConvertedMaxBytes/* / targetEncoding->GetMinByteCount()*/); // 変換実行 bool bRes; size_t uBytesUsed; size_t uCharsUsed; bool bUsedDefaultChar; #if defined(LNOTE_WCHAR_16) // ソースフォーマットから中間フォーマットへ bRes = srcEncoding->ConvertToUTF16( (const byte_t*)src, srcByteCount, tmpBuf->getPointer(), nWideMaxBytes, // \0 強制格納に備え、1文字分余裕のあるサイズを指定する &uBytesUsed, &uCharsUsed); LN_THROW(bRes, InvalidFormatException); // 中間フォーマットからターゲットフォーマットへ bRes = targetEncoding->ConvertFromUTF16( (const byte_t*)tmpBuf->getPointer(), uBytesUsed, (byte_t*)targetBuf->getPointer(), targetBuf->getSize(), // \0 強制格納に備え、1文字分余裕のあるサイズを指定する &uBytesUsed, &uCharsUsed, &bUsedDefaultChar); LN_THROW(bRes, InvalidFormatException); #elif defined(LNOTE_WCHAR_32) #else #error "Invalid wchar_t size." #endif *bytesUsed = uBytesUsed; *charsUsed = uCharsUsed; *usedDefaultChar = bUsedDefaultChar; targetBuf->resize(uBytesUsed); targetBuf.safeAddRef(); return targetBuf; }
//----------------------------------------------------------------------------- // The DX Support file is a very fat expensive KV file, causes a run-time startup slowdown. // Becauase it normally lives in the game\bin directory, it can't be in the zip or preloaded. // Thus, it gets reprocessed into just the trivial 360 portion and placed into the platform.zip // Yes, it's evil. //----------------------------------------------------------------------------- bool ProcessDXSupportConfig( bool bWriteToZip ) { if ( !g_bIsPlatformZip ) { // only relevant when building platform zip, otherwise no-op return false; } const char *pConfigName = "dxsupport.cfg"; char szTempPath[MAX_PATH]; char szSourcePath[MAX_PATH]; V_ComposeFileName( g_szModPath, "../bin", szTempPath, sizeof( szTempPath ) ); V_ComposeFileName( szTempPath, pConfigName, szSourcePath, sizeof( szSourcePath ) ); CUtlBuffer sourceBuf( 0, 0, CUtlBuffer::TEXT_BUFFER ); if ( !g_pFullFileSystem->ReadFile( szSourcePath, NULL, sourceBuf ) ) { Msg( "Error! Couldn't open file '%s'!\n", pConfigName ); return false; } KeyValues *pKV = new KeyValues( "" ); if ( !pKV->LoadFromBuffer( "dxsupport.cfg", sourceBuf ) ) { Msg( "Error! Couldn't parse config file '%s'!\n", pConfigName ); pKV->deleteThis(); return false; } // only care about the xbox specific dxlevel 98 block KeyValues *pXboxSubKey = NULL; for ( KeyValues *pSubKey = pKV->GetFirstSubKey(); pSubKey != NULL && pXboxSubKey == NULL; pSubKey = pSubKey->GetNextKey() ) { // descend each sub block for ( KeyValues *pKey = pSubKey->GetFirstSubKey(); pKey != NULL && pXboxSubKey == NULL; pKey = pKey->GetNextKey() ) { if ( !V_stricmp( pKey->GetName(), "name" ) && pKey->GetInt( (const char *)NULL ) == 98 ) { pXboxSubKey = pSubKey; } } } if ( !pXboxSubKey ) { Msg( "Error! Couldn't find expected dxlevel 98 in config file '%s'!\n", pConfigName ); pKV->deleteThis(); return false; } CUtlBuffer kvBuffer( 0, 0, CUtlBuffer::TEXT_BUFFER ); kvBuffer.Printf( "\"dxsupport\"\n" ); kvBuffer.Printf( "{\n" ); kvBuffer.Printf( "\t\"0\"\n" ); kvBuffer.Printf( "\t{\n" ); for ( KeyValues *pKey = pXboxSubKey->GetFirstSubKey(); pKey != NULL; pKey = pKey->GetNextKey() ) { kvBuffer.Printf( "\t\t\"%s\" \"%s\"\n", pKey->GetName(), pKey->GetString( (const char *)NULL ) ); } kvBuffer.Printf( "\t}\n" ); kvBuffer.Printf( "}\n" ); CUtlBuffer targetBuf( 0, 0, CUtlBuffer::TEXT_BUFFER | CUtlBuffer::CONTAINS_CRLF ); kvBuffer.ConvertCRLF( targetBuf ); // only appears in zip file bool bSuccess = WriteBufferToFile( pConfigName, targetBuf, bWriteToZip, WRITE_TO_DISK_NEVER ); pKV->deleteThis(); return bSuccess; }