DWORD GetResourceInformation(const CResourceW &resource, CResourceW &destResource, UString &systemPathPart) { if (g_IsNT) { const DWORD kBufferSize = 16384; CByteArr byteBuffer(kBufferSize); LPNETRESOURCEW lpnrLocal = (LPNETRESOURCEW) (BYTE *)(byteBuffer); ZeroMemory(lpnrLocal, kBufferSize); DWORD bufferSize = kBufferSize; NETRESOURCEW netResource; ConvertCResourceToNETRESOURCE(resource, netResource); LPWSTR lplpSystem; DWORD result = ::WNetGetResourceInformationW(&netResource, lpnrLocal, &bufferSize, &lplpSystem); if (result != NO_ERROR) return result; if (lplpSystem != 0) systemPathPart = lplpSystem; ConvertNETRESOURCEToCResource(lpnrLocal[0], destResource); return result; } CResource resourceA, destResourceA; ConvertResourceWToResource(resource, resourceA); AString systemPathPartA; DWORD result = GetResourceInformation(resourceA, destResourceA, systemPathPartA); ConvertResourceToResourceW(destResourceA, destResource); systemPathPart = GetUnicodeString(systemPathPartA); return result; }
HRESULT FindSignatureInStream(ISequentialInStream *stream, const Byte *signature, unsigned signatureSize, const UInt64 *limit, UInt64 &resPos) { resPos = 0; CByteBuffer byteBuffer2(signatureSize); RINOK(ReadStream_FALSE(stream, byteBuffer2, signatureSize)); if (memcmp(byteBuffer2, signature, signatureSize) == 0) return S_OK; const UInt32 kBufferSize = (1 << 16); CByteBuffer byteBuffer(kBufferSize); Byte *buffer = byteBuffer; UInt32 numPrevBytes = signatureSize - 1; memcpy(buffer, (const Byte *)byteBuffer2 + 1, numPrevBytes); resPos = 1; for (;;) { if (limit != NULL) if (resPos > *limit) return S_FALSE; do { UInt32 numReadBytes = kBufferSize - numPrevBytes; UInt32 processedSize; RINOK(stream->Read(buffer + numPrevBytes, numReadBytes, &processedSize)); numPrevBytes += processedSize; if (processedSize == 0) return S_FALSE; } while (numPrevBytes < signatureSize); UInt32 numTests = numPrevBytes - signatureSize + 1; for (UInt32 pos = 0; pos < numTests; pos++) { Byte b = signature[0]; for (; buffer[pos] != b && pos < numTests; pos++); if (pos == numTests) break; if (memcmp(buffer + pos, signature, signatureSize) == 0) { resPos += pos; return S_OK; } } resPos += numTests; numPrevBytes -= numTests; memmove(buffer, buffer + numTests, numPrevBytes); } }
DWORD GetResourceParent(const CResource &resource, CResource &parentResource) { const DWORD kBufferSize = 16384; CByteArr byteBuffer(kBufferSize); LPNETRESOURCE lpnrLocal = (LPNETRESOURCE) (BYTE *)(byteBuffer); ZeroMemory(lpnrLocal, kBufferSize); DWORD bufferSize = kBufferSize; NETRESOURCE netResource; ConvertCResourceToNETRESOURCE(resource, netResource); DWORD result = ::WNetGetResourceParent(&netResource, lpnrLocal, &bufferSize); if (result != NO_ERROR) return result; ConvertNETRESOURCEToCResource(lpnrLocal[0], parentResource); return result; }
DWORD CEnum::Next(CResource &resource) { const DWORD kBufferSize = 16384; CByteArr byteBuffer(kBufferSize); LPNETRESOURCE lpnrLocal = (LPNETRESOURCE) (BYTE *)(byteBuffer); ZeroMemory(lpnrLocal, kBufferSize); DWORD bufferSize = kBufferSize; DWORD numEntries = 1; DWORD result = Next(&numEntries, lpnrLocal, &bufferSize); if (result != NO_ERROR) return result; if (numEntries != 1) return (DWORD)E_FAIL; ConvertNETRESOURCEToCResource(lpnrLocal[0], resource); return result; }
void OStream::writeVersionStamp() { if (mRuntimeVersion >= 6) { const std::size_t BufferLen = 1+5+5; char buffer[BufferLen] = {0}; buffer[0] = BeginVersionStamp; RCF::ByteBuffer byteBuffer(&buffer[0], BufferLen); std::size_t pos = 1; encodeInt(mRuntimeVersion, byteBuffer, pos); encodeInt(mArchiveVersion, byteBuffer, pos); writeRaw(&buffer[0], static_cast<UInt32>(pos)); } }
int main(int argc, const char * argv[]) { const char defaultBytes[] = {0x61,0x62,0x63}; std::unique_ptr<MyClass> byteBuffer(new MyClass( defaultBytes, sizeof(defaultBytes) )); // std::vector<char> current = byteBuffer->getBytes();// この時点でコピーになる。const std::vector<char>& で受ければ実際にリファレンスになると思われる。 const std::vector<char>& current = byteBuffer->getBytes(); byteBuffer->appendByte(0x64); for(auto ite=current.begin();ite<current.end();ite++) { std::cout << std::hex << (unsigned short)*ite << " "; } std::cout << std::endl; return 0; }
DWORD GetResourceInformation(const CResource &resource, CResource &destResource, CSysString &systemPathPart) { const DWORD kBufferSize = 16384; CByteArr byteBuffer(kBufferSize); LPNETRESOURCE lpnrLocal = (LPNETRESOURCE) (BYTE *)(byteBuffer); ZeroMemory(lpnrLocal, kBufferSize); DWORD bufferSize = kBufferSize; NETRESOURCE netResource; ConvertCResourceToNETRESOURCE(resource, netResource); LPTSTR lplpSystem; DWORD result = ::WNetGetResourceInformation(&netResource, lpnrLocal, &bufferSize, &lplpSystem); if (result != NO_ERROR) return result; if (lplpSystem != 0) systemPathPart = lplpSystem; ConvertNETRESOURCEToCResource(lpnrLocal[0], destResource); return result; }
void OStream::writeArchiveMetadata() { if (mRuntimeVersion >= 6) { // Max possible size. const std::size_t BufferLen = 1+5+5+1; char buffer[BufferLen] = {0}; buffer[0] = BeginArchiveMetadata; RCF::ByteBuffer byteBuffer(&buffer[0], BufferLen); std::size_t pos = 1; encodeInt(mRuntimeVersion, byteBuffer, pos); encodeInt(mArchiveVersion, byteBuffer, pos); if (mRuntimeVersion >= 10) { encodeBool(getTrackingContext().getEnabled(), byteBuffer, pos); } writeRaw(&buffer[0], static_cast<UInt32>(pos)); } }
DWORD GetResourceParent(const CResourceW &resource, CResourceW &parentResource) { if (g_IsNT) { const DWORD kBufferSize = 16384; CByteArr byteBuffer(kBufferSize); LPNETRESOURCEW lpnrLocal = (LPNETRESOURCEW) (BYTE *)(byteBuffer); ZeroMemory(lpnrLocal, kBufferSize); DWORD bufferSize = kBufferSize; NETRESOURCEW netResource; ConvertCResourceToNETRESOURCE(resource, netResource); DWORD result = ::WNetGetResourceParentW(&netResource, lpnrLocal, &bufferSize); if (result != NO_ERROR) return result; ConvertNETRESOURCEToCResource(lpnrLocal[0], parentResource); return result; } CResource resourceA, parentResourceA; ConvertResourceWToResource(resource, resourceA); DWORD result = GetResourceParent(resourceA, parentResourceA); ConvertResourceToResourceW(parentResourceA, parentResource); return result; }
ByteBuffer ByteBuffer::release() { ByteBuffer byteBuffer(*this); *this = ByteBuffer(); return byteBuffer; }
bool IStream::begin(Node &node) { while (true) { Byte8 byte = 0; read_byte(byte); switch (byte) { case Blank: { Byte8 count = 0; read_byte(count); std::vector<Byte8> buffer(count); UInt32 bytesRead = read( &(buffer[0]), count); if (bytesRead != static_cast<UInt32>(count)) { RCF::Exception e(RCF::_SfError_DataFormat()); RCF_THROW(e)(bytesRead)(count); } continue; } case BeginArchiveMetadata: { int runtimeVersion = 0; int archiveVersion = 0; bool pointerTrackingEnabled = false; bool * pPointerTrackingEnabled = NULL; const size_t BufferLen = 11; char buffer[BufferLen] = {0}; RCF::ByteBuffer byteBuffer( &buffer[0], BufferLen); std::size_t pos0 = static_cast<std::size_t>(mpIs->tellg()); #ifdef _MSC_VER #pragma warning( push ) #pragma warning( disable : 4996 ) // warning C4996: 'std::basic_istream<_Elem,_Traits>::readsome': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators' #endif std::size_t bytesRead = static_cast<std::size_t>(mpIs->readsome(buffer, BufferLen)); #ifdef _MSC_VER #pragma warning( pop ) #endif byteBuffer = RCF::ByteBuffer(byteBuffer, 0, bytesRead); std::size_t pos1 = 0; decodeInt(runtimeVersion, byteBuffer, pos1); decodeInt(archiveVersion, byteBuffer, pos1); if (runtimeVersion >= 10) { decodeBool(pointerTrackingEnabled, byteBuffer, pos1); pPointerTrackingEnabled = &pointerTrackingEnabled; } mpIs->seekg( static_cast<std::istream::off_type>(pos0 + pos1), std::ios_base::beg); if (!mIgnoreVersionStamp) { if (runtimeVersion) { mRuntimeVersion = runtimeVersion; } if (archiveVersion) { mArchiveVersion = archiveVersion; } } if (pPointerTrackingEnabled && !*pPointerTrackingEnabled) { getTrackingContext().setEnabled(false); } continue; } case Begin: { read_byte( byte ); Byte8 attrSpec = byte; // id if (attrSpec & 1) { read_int(node.id); } // ref attrSpec = attrSpec >> 1; if (attrSpec & 1) { node.ref = 1; } // type attrSpec = attrSpec >> 1; if (attrSpec & 1) { UInt32 length = 0; read_int(length); node.type.allocate(length); read(node.type.get(), length ); } // label attrSpec = attrSpec >> 1; if (attrSpec & 1) { UInt32 length = 0; read_int(length); node.label.allocate(length); read(node.label.get(), length); } return true; } default: { RCF::Exception e(RCF::_SfError_DataFormat()); RCF_THROW(e)(byte); } } } }
HRESULT CInArchive::FindCd(CCdInfo &cdInfo) { UInt64 endPosition; RINOK(Stream->Seek(0, STREAM_SEEK_END, &endPosition)); const UInt32 kBufSizeMax = ((UInt32)1 << 16) + kEcdSize + kEcd64Locator_Size + kEcd64_FullSize; UInt32 bufSize = (endPosition < kBufSizeMax) ? (UInt32)endPosition : kBufSizeMax; if (bufSize < kEcdSize) return S_FALSE; CByteArr byteBuffer(bufSize); UInt64 startPosition = endPosition - bufSize; RINOK(Stream->Seek(startPosition, STREAM_SEEK_SET, &m_Position)); if (m_Position != startPosition) return S_FALSE; RINOK(ReadStream_FALSE(Stream, byteBuffer, bufSize)); const Byte *buf = byteBuffer; for (UInt32 i = bufSize - kEcdSize;; i--) { if (buf[i] != 0x50) { if (i == 0) return S_FALSE; i--; if (buf[i] != 0x50) { if (i == 0) return S_FALSE; continue; } } if (Get32(buf + i) == NSignature::kEcd) { if (i >= kEcd64_FullSize + kEcd64Locator_Size) { const Byte *locator = buf + i - kEcd64Locator_Size; if (Get32(locator) == NSignature::kEcd64Locator && Get32(locator + 4) == 0) // number of the disk with the start of the zip64 ECD { // Most of the zip64 use fixed size Zip64 ECD UInt64 ecd64Offset = Get64(locator + 8); UInt64 absEcd64 = endPosition - bufSize + i - (kEcd64Locator_Size + kEcd64_FullSize); { const Byte *ecd64 = locator - kEcd64_FullSize; if (Get32(ecd64) == NSignature::kEcd64 && Get64(ecd64 + 4) == kEcd64_MainSize) { cdInfo.ParseEcd64(ecd64); ArcInfo.Base = absEcd64 - ecd64Offset; return S_OK; } } // some zip64 use variable size Zip64 ECD. // we try to find it if (absEcd64 != ecd64Offset) { if (TryEcd64(ecd64Offset, cdInfo) == S_OK) { ArcInfo.Base = 0; return S_OK; } } if (ArcInfo.MarkerPos != 0 && ArcInfo.MarkerPos + ecd64Offset != absEcd64) { if (TryEcd64(ArcInfo.MarkerPos + ecd64Offset, cdInfo) == S_OK) { ArcInfo.Base = ArcInfo.MarkerPos; return S_OK; } } } } if (Get32(buf + i + 4) == 0) // ThisDiskNumber, StartCentralDirectoryDiskNumber; { cdInfo.ParseEcd(buf + i); UInt64 absEcdPos = endPosition - bufSize + i; UInt64 cdEnd = cdInfo.Size + cdInfo.Offset; ArcInfo.Base = 0; if (absEcdPos != cdEnd) { /* if (cdInfo.Offset <= 16 && cdInfo.Size != 0) { // here we support some rare ZIP files with Central directory at the start ArcInfo.Base = 0; } else */ ArcInfo.Base = absEcdPos - cdEnd; } return S_OK; } } if (i == 0) return S_FALSE; } }
int test_main(int, char**) { RCF::RcfInitDeinit rcfinitDeinit; #ifdef BOOST_WINDOWS RCF::tstring pipeName = RCF_T("TestPipe"); #else RCF::tstring pipeName = RCF_TEMP_DIR "TestPipe"; #endif RCF::NamedPipeEndpoint ep(pipeName); Echo echo; RCF::RcfServer server(ep); // 10 server threads server.setThreadPool( RCF::ThreadPoolPtr( new RCF::ThreadPool(10)) ); server.bind( (I_Echo *) 0, echo); server.start(); { // Make a single call, no waiting std::string s1 = "test"; RCF::NamedPipeEndpoint ep(pipeName); RcfClient<I_Echo> client(ep); std::string s2 = client.echo(s1); RCF_CHECK_EQ(s1 , s2 ); } { // Make 5 calls, in parallel, each one waiting 2 seconds boost::uint32_t t0 = RCF::getCurrentTimeMs(); boost::uint32_t waitMs = 2000; ThreadGroup clients; for (std::size_t i=0; i<5; ++i) { clients.push_back( RCF::ThreadPtr( new RCF::Thread( boost::bind(clientTask, pipeName, waitMs)))); } joinThreadGroup(clients); // Total time should be little more than 2 seconds boost::uint32_t t1 = RCF::getCurrentTimeMs(); std::cout << "Total time: " << t1-t0 << " ms..." << std::endl; RCF_CHECK_LT(t1-t0 , 2*waitMs); } #ifdef BOOST_WINDOWS { // Named pipe impersonation - use our own credentials RCF::tstring tUserName = RCF::getMyUserName(); std::string userName(util::toString(tUserName)); RCF::NamedPipeEndpoint ep(pipeName); RcfClient<I_Echo> client(ep); std::string s = client.whatsMyWin32UserName(); RCF_CHECK_EQ(s , userName); } { // SSPI impersonation - use our own credentials server.stop(); RCF::FilterServicePtr filterServicePtr(new RCF::FilterService()); filterServicePtr->addFilterFactory( RCF::FilterFactoryPtr( new RCF::NtlmFilterFactory())); server.addService(filterServicePtr); server.start(); RCF::tstring tUserName = RCF::getMyUserName(); std::string userName(util::toString(tUserName)); RCF::NamedPipeEndpoint ep(pipeName); RcfClient<I_Echo> client(ep); RCF::FilterPtr ntlmFilterPtr( new RCF::NtlmFilter() ); client.getClientStub().requestTransportFilters(ntlmFilterPtr); client.getClientStub().setRemoteCallTimeoutMs(1000*3600); std::string s = client.whatsMyWin32UserName(); RCF_CHECK_EQ(s , userName); server.stop(); server.removeService(filterServicePtr); server.start(); } #endif { // Test error reporting, by exceeding the max message length. int maxLengthOrig = server.getServerTransport().getMaxMessageLength(); server.getServerTransport().setMaxMessageLength(10*1024); RCF::ByteBuffer byteBuffer(50*1024); for (std::size_t i=0; i<byteBuffer.getLength(); ++i) { byteBuffer.getPtr()[i] = i%256 ; } RCF::NamedPipeEndpoint ep(pipeName); RcfClient<I_Echo> client(ep); try { RCF_CHECK(byteBuffer == client.echo(byteBuffer) ); RCF_CHECK_FAIL(); } catch(RCF::Exception &e) { RCF_CHECK_OK(); #ifdef BOOST_WINDOWS RCF_CHECK_EQ(e.getErrorId() , RCF::RcfError_ClientWriteFail )(e.getError()); #else RCF_CHECK_EQ(e.getErrorId() , RCF::RcfError_ServerMessageLength )(e.getError()); #endif } server.getServerTransport().setMaxMessageLength(maxLengthOrig); // This time, turn up the message length and check that it goes through. server.getServerTransport().setMaxMessageLength(-1); client.getClientStub().getTransport().setMaxMessageLength(-1); RCF_CHECK(byteBuffer == client.echo(byteBuffer) ); } /* { // Some performance testing. RCF Win32 named pipe server sessions have a // reuse-instead-of-delete feature that needs some exercise. // Several threads, concurrently connecting and disconnecting, especially // disconnecting while a call is in progress. void clientTask2(const std::string & pipeName); std::vector<RCF::ThreadPtr> threads; for (std::size_t i=0; i<10; ++i) { threads.push_back( RCF::ThreadPtr( new RCF::Thread( boost::bind(clientTask2, pipeName)))); } joinThreadGroup(threads); } */ return 0; }
/** * 指定したバイト数だけpcmデータを読み込みます。 * エラーが発生するか、終端に達するまでsizeで指定したバイト数だけ読み込めます。 */ bool PitchShiftWaveReader::read(void *dst, std::size_t size, std::size_t *actualSize) { if(!isOpen()){ return false; } if(size == 0){ if(actualSize){ *actualSize = 0; } return true; } // 出力に必要な入力サンプル数を求める。 const std::size_t reqOutSamples = size / format_.blockAlign; //floor const WaveSize reqSrcSamples0 = resampler_ ? resampler_->calcRequiredInputSize(reqOutSamples) : reqOutSamples; if(reqSrcSamples0 > std::numeric_limits<std::size_t>::max() / format_.blockAlign){ return false; } const std::size_t reqSrcSamples = static_cast<std::size_t>(reqSrcSamples0); const std::size_t reqSrcBytes = reqSrcSamples * format_.blockAlign; // read source bytes. std::size_t actualSizeSrc = 0; HeapArray<uint8_t> byteBuffer(reqSrcBytes); if(!source_->read(byteBuffer.get(), reqSrcBytes, &actualSizeSrc)){ return false; } // pitch shift. const std::size_t actualSrcSamples = actualSizeSrc / format_.blockAlign; //floor if(shiftRatio_ != 1.0f){ HeapArray<float> floatBuffer(actualSrcSamples); for(unsigned int ch = 0; ch < format_.channels; ++ch){ demultiplex(floatBuffer.get(), byteBuffer.get(), ch, actualSrcSamples, format_.blockAlign, format_.bytesPerSample); shifters_[ch]->smbPitchShift( shiftRatio_, actualSrcSamples, fftOverlapFactor_, static_cast<float>(format_.samplesPerSec), floatBuffer.get(), floatBuffer.get()); multiplex(byteBuffer.get(), floatBuffer.get(), ch, actualSrcSamples, format_.blockAlign, format_.bytesPerSample); } } // resample. if(resampler_){ assert(actualSrcSamples == reqSrcSamples); std::size_t actualOutSamples = reqOutSamples; if(actualSrcSamples != reqSrcSamples){ const std::size_t maxOutSamples = static_cast<std::size_t>(std::min(resampler_->calcMaxOutputSize(actualSrcSamples), static_cast<WaveSize>(reqOutSamples))); if(actualOutSamples > maxOutSamples){ actualOutSamples = maxOutSamples; } } switch(format_.blockAlign){ case 1: resampler_->resample( reinterpret_cast<uint8_t *>(dst), reinterpret_cast<uint8_t *>(dst) + actualOutSamples, byteBuffer.get(), byteBuffer.get() + actualSrcSamples); break; case 2: resampler_->resample( reinterpret_cast<uint16_t *>(dst), reinterpret_cast<uint16_t *>(dst) + actualOutSamples, reinterpret_cast<const uint16_t *>(byteBuffer.get()), reinterpret_cast<const uint16_t *>(byteBuffer.get()) + actualSrcSamples); break; case 4: resampler_->resample( reinterpret_cast<uint32_t *>(dst), reinterpret_cast<uint32_t *>(dst) + actualOutSamples, reinterpret_cast<const uint32_t *>(byteBuffer.get()), reinterpret_cast<const uint32_t *>(byteBuffer.get()) + actualSrcSamples); break; } if(actualSize){ *actualSize = actualOutSamples * format_.blockAlign; } outputPos_ += actualOutSamples; } else{ std::memcpy(dst, byteBuffer.get(), actualSrcSamples * format_.blockAlign); if(actualSize){ *actualSize = actualSrcSamples * format_.blockAlign; } outputPos_ += actualSrcSamples; } return true; }