shared_ptr< vector<char> > GetNameChars(LPCSTR lpszName) { int nLen = lstrlenA(lpszName) + 1; // 结束符 if (nLen <= 0x60 - 1 ) { shared_ptr< vector<char> > spBuffer; char type; if (nLen < 0x40 - 1) { type = nLen / 0x10; spBuffer.reset(new vector<char>( (type + 1) * 0x10)); } else { type = 4; spBuffer.reset(new vector<char>(0x60)); } *spBuffer->begin() = type; lstrcpyA( &(*spBuffer->begin()) + 1, lpszName ); return spBuffer; } else { DWORD dwLen = (nLen + 5) % 0x10; if (dwLen == 0) { dwLen = nLen; } else { dwLen = (nLen - dwLen + 0x10); } shared_ptr< vector<char> > spBuffer(new vector<char>( 5 + dwLen)); memset( &(*spBuffer->begin()), 0, dwLen + 5); *spBuffer->begin() = 5; memcpy( &(*spBuffer->begin()) + 1, &dwLen, 2); lstrcpyA( &(*spBuffer->begin()) + 5, lpszName); return spBuffer; } }
HRESULT GetMappedAddress(uint8_t* pData, size_t size, CSocketAddress* pAddr) { uint16_t port; HRESULT hr = S_OK; uint8_t attributeid; uint8_t ip6[STUN_IPV6_LENGTH]; uint32_t ip4; CRefCountedBuffer spBuffer(new CBuffer(pData, size, false)); CDataStream stream(spBuffer); ChkIfA(pAddr==NULL, E_INVALIDARG); Chk(stream.SeekDirect(1)); // skip over the zero byte Chk(stream.ReadUint8(&attributeid)); Chk(stream.ReadUint16(&port)); port = ntohs(port); if (attributeid == STUN_ATTRIBUTE_FIELD_IPV4) { Chk(stream.ReadUint32(&ip4)); ip4 = ntohl(ip4); *pAddr = CSocketAddress(ip4, port); } else { sockaddr_in6 addr6={}; Chk(stream.Read(ip6, STUN_IPV6_LENGTH)); addr6.sin6_family = AF_INET6; addr6.sin6_port = htons(port); memcpy(&addr6.sin6_addr, ip6, STUN_IPV6_LENGTH); *pAddr = CSocketAddress(addr6); } Cleanup: return hr; }
CAPELink::CAPELink(const str_utf16 * pFilename) { // empty m_bIsLinkFile = FALSE; m_nStartBlock = 0; m_nFinishBlock = 0; m_cImageFilename[0] = 0; // open the file IO_CLASS_NAME ioLinkFile; if (ioLinkFile.Open(pFilename) == ERROR_SUCCESS) { // create a buffer CSmartPtr<char> spBuffer(new char [1024], TRUE); // fill the buffer from the file and null terminate it unsigned int nBytesRead = 0; ioLinkFile.Read(spBuffer.GetPtr(), 1023, &nBytesRead); spBuffer[nBytesRead] = 0; // call the other constructor (uses a buffer instead of opening the file) ParseData(spBuffer, pFilename); } }
void writeTIFF(const char *pszFile, HDC hdc) { precondition_throw(pszFile != NULL, "Path to TIFF file cannot be null."); precondition_throw(hdc != NULL, "Source device context cannot be null."); HBITMAP hbm = (HBITMAP) ::GetCurrentObject(hdc, OBJ_BITMAP); DIBSECTION ds = {255}; memset(&ds,255,sizeof(ds)); ::GetObject(hbm, sizeof(DIBSECTION), &ds); LONG buffWidth = ds.dsBmih.biWidth; LONG buffHeight = (ds.dsBmih.biHeight < 0) ? ds.dsBmih.biHeight * -1 : ds.dsBmih.biHeight; WORD planes = ds.dsBmih.biPlanes; WORD bitsPerPixel = ds.dsBmih.biBitCount; if (planes != 1) throw std::runtime_error("Unsupported image format."); long padding = 0; while ((buffWidth * 3 + padding) % sizeof(DWORD) != 0) ++padding; long pitchBytes = buffWidth * 3 + padding; long neededBytes = pitchBytes * buffHeight; ScopedArray<BYTE> spBuffer(new BYTE[neededBytes]); memset(spBuffer.get(), 0, neededBytes); BITMAPINFO bmi = {0}; bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader); bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = 24; bmi.bmiHeader.biCompression = BI_RGB; bmi.bmiHeader.biWidth = buffWidth; bmi.bmiHeader.biHeight = buffHeight * -1; ::GetDIBits(hdc, hbm, 0, buffHeight, spBuffer.get(), &bmi, DIB_RGB_COLORS); TIFF *pTIFF = TIFFOpen(pszFile, "w"); if (!pTIFF) throw std::runtime_error("Couldn't create specified TIFF file."); ScopeGuard guardTIFF = MakeGuard(TIFFClose, pTIFF); TIFFSetField(pTIFF, TIFFTAG_IMAGEWIDTH, buffWidth); TIFFSetField(pTIFF, TIFFTAG_IMAGELENGTH, buffHeight); TIFFSetField(pTIFF, TIFFTAG_SAMPLESPERPIXEL, 3); TIFFSetField(pTIFF, TIFFTAG_BITSPERSAMPLE, 8); TIFFSetField(pTIFF, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(pTIFF, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(pTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); long packedSize = buffWidth * buffHeight * 3; ScopedArray<BYTE> spPackedBuffer(new BYTE[packedSize]); BYTE *pDest = spPackedBuffer.get(); BYTE *pSrc = spBuffer.get(); for (long y = 0; y < buffHeight; ++y) { for (long x = 0; x < buffWidth; ++x) { // Swapping bytes so the byte order is as we expect pDest[0] = pSrc[2]; pDest[1] = pSrc[1]; pDest[2] = pSrc[0]; pDest += 3; pSrc += 3; } pSrc += padding; } if (TIFFWriteEncodedStrip(pTIFF, 0, spPackedBuffer.get(), buffWidth * buffHeight * 3) == 0) throw std::runtime_error("Couldn't write image."); }