bool NoCachePdbReader::ReadRecord(word nRec, word n, word cb, void *pv) { // See if the data is cached Assert(nRec < m_cRecs); CompressionHeader coh; dword offBytes = GetCompressionHeader(nRec, &coh); if (offBytes == 0) { return false; } if (n + cb > BigWord(coh.cbUncompressed)) { return false; } // If not compressed, read data straight from file if (!BigWord(coh.fCompressed)) { memcpy(pv, &m_pb[offBytes + n], cb); } else { byte *pbT = new byte[BigWord(coh.cbUncompressed)]; if (pbT == NULL) { return false; } DecompressToMemory(&m_pb[offBytes], pbT, &coh); memcpy(pv, &pbT[n], cb); delete[] pbT; } return true; }
byte *NoCachePdbReader::MapRecord(word nRec, void **ppvCookie, word *pcb) { // First see if the record is cached Assert(m_pb != NULL); Assert(nRec < m_cRecs); CompressionHeader coh; dword offBytes = GetCompressionHeader(nRec, &coh); if (offBytes == 0) return NULL; word cbT = BigWord(coh.cbUncompressed); byte *pbT = new byte[cbT]; if (pbT == NULL) { return NULL; } if (BigWord(coh.fCompressed)) { DecompressToMemory(&m_pb[offBytes], pbT, &coh); } else { memcpy(pbT, &m_pb[offBytes], cbT); } m_cMapped++; *ppvCookie = pbT; *pcb = cbT; return pbT; }
byte *MemPdbReader::MapRecord(word nRec, void **ppvCookie, word *pcb) { // First see if the record is cached Assert(m_pb != NULL); Assert(nRec < m_cRecs); CacheHandle hc = m_aphcRecordData[nRec]; if (!gcam.IsValid(hc)) { // Not cached, get the compression header CompressionHeader coh; dword offBytes = GetCompressionHeader(nRec, &coh); if (offBytes == 0) return NULL; // If it is not compressed, we can't just point to it; it may // be unaligned if (!BigWord(coh.fCompressed)) { word cbT = BigWord(coh.cbUncompressed); byte *pbT = new byte[cbT]; if (pbT == NULL) return NULL; memcpy(pbT, &m_pb[offBytes], cbT); m_cMapped++; *ppvCookie = pbT; *pcb = cbT; return pbT; } // It is compressed, decompress it and lock it hc = gcam.NewObject(NULL, BigWord(coh.cbUncompressed), kfHintWillLock); if (hc == 0) return NULL; m_aphcRecordData[nRec] = hc; DecompressToCache(&m_pb[offBytes], hc, &coh); } // We have cache access to this record and it is locked. Return a pointer. byte *pbUncompressed = (byte *)gcam.Lock(hc); if (pcb != NULL) *pcb = gcam.GetSize(hc); *ppvCookie = 0; m_cMapped++; return pbUncompressed; }
bool NoCachePdbReader::Open(const char *pszFn) { // Attempt to open Assert(m_pb == NULL); FILE *pfil = fopen(pszFn, "rb"); if (pfil == NULL) { RLOG() << "fopen failed: " << pszFn; return false; } // Read in the entire thing fseek(pfil, 0, SEEK_END); m_cb = ftell(pfil); fseek(pfil, 0, SEEK_SET); m_pb = new byte[m_cb]; if (m_pb == NULL) { fclose(pfil); return false; } if (fread(m_pb, m_cb, 1, pfil) != 1) { fclose(pfil); return false; } fclose(pfil); DatabaseHdrType *phdr = (DatabaseHdrType *)m_pb; m_cRecs = BigWord(phdr->recordList.numRecords); return true; }
bool NoCachePdbReader::GetRecordSize(word nRec, word *pcb) { CompressionHeader coh; if (GetCompressionHeader(nRec, &coh) == 0) return false; *pcb = BigWord(coh.cbUncompressed); return true; }
bool MemPdbReader::ReadRecord(word nRec, word n, word cb, void *pv) { // See if the data is cached Assert(nRec < m_cRecs); CacheHandle hc = m_aphcRecordData[nRec]; if (!gcam.IsValid(hc)) { // Not cached, get the compression header CompressionHeader coh; dword offBytes = GetCompressionHeader(nRec, &coh); if (offBytes == 0) return false; // If not compressed, read data straight from file if (!BigWord(coh.fCompressed)) { if (n + cb > BigWord(coh.cbUncompressed)) return false; memcpy(pv, &m_pb[offBytes + n], cb); return true; } // It is compressed, but not cached. hc = gcam.NewObject(NULL, BigWord(coh.cbUncompressed)); if (hc == 0) return false; m_aphcRecordData[nRec] = hc; DecompressToCache(&m_pb[offBytes], hc, &coh); } // Read from cached data word cbUncompressed = gcam.GetSize(hc); byte *pbUncompressed = (byte *)gcam.GetPtr(hc); if (n + cb > cbUncompressed) return false; memcpy(pv, pbUncompressed + n, cb); return true; }
int Font::CalcMultilineHeight(char *psz, int cxMultiline) { int cy = 0; char *pszNext = psz; while (pszNext != NULL) { char *pszStart = pszNext; CalcBreak(cxMultiline, &pszNext); cy += BigWord(m_pfnth->cy) - m_nLineOverlap; } return cy; }
void Font::DrawText(DibBitmap *pbm, char *psz, int x, int y, int cx, int cy, bool fEllipsis) { int cyFont = BigWord(m_pfnth->cy) - m_nLineOverlap; int cyT = cyFont; char *pszNext = psz; while (pszNext != NULL) { if (cy != -1 && cyT > cy) return; char *pszStart = pszNext; int cch = CalcBreak(cx, &pszNext); if (fEllipsis && pszNext != NULL && cy != -1 && cyT + cyFont > cy) { DrawTextWithEllipsis(pbm, pszStart, cch, x, y, cx, true); } else if (fEllipsis && pszNext == NULL) { DrawTextWithEllipsis(pbm, pszStart, cch, x, y, cx, false); } else { DrawText(pbm, pszStart, x, y, cch); } y += cyFont; cyT += cyFont; } }
bool MemPdbReader::Open(char *pszFn) { // Attempt to open Assert(m_pb == NULL); FILE *pfil = fopen(pszFn, "rb"); if (pfil == NULL) { Trace("fopen(\"%s\", \"rb\"); failed", pszFn); return false; } // Read in the entire thing fseek(pfil, 0, SEEK_END); m_cb = (dword)ftell(pfil); fseek(pfil, 0, SEEK_SET); m_pb = new byte[m_cb]; if (m_pb == NULL) { fclose(pfil); return false; } if (fread(m_pb, m_cb, 1, pfil) != 1) { fclose(pfil); return false; } fclose(pfil); // Alloc cache handle array DatabaseHdrType *phdr = (DatabaseHdrType *)m_pb; m_cRecs = BigWord(phdr->recordList.numRecords); m_aphcRecordData = new CacheHandle[m_cRecs]; if (m_aphcRecordData == NULL) return false; memset(m_aphcRecordData, 0, sizeof(CacheHandle) * m_cRecs); return true; }
int Font::DrawText(DibBitmap *pbm, char *psz, int x, int y, int cch, dword *mpscaiclr) { #ifdef DEBUG for (int ichT = 0; ichT < cch; ichT++) Assert((word)psz[ichT] < (word)kcchFont); #endif if (cch == -1) cch = (int)strlen(psz); // Clip entire line of text first. Size siz; pbm->GetSize(&siz); // Top clip if (y < 0) return 0; if (y + BigWord(m_pfnth->cy) > siz.cy) return 0; // Don't include x clipped chars // Left clip int ich = 0; int xT = x; char *pszT; for (pszT = psz; pszT - psz < cch; pszT++) { if (xT < 0) { ich++; xT += m_pfnth->acxChar[(byte)*pszT] - m_nGlyphOverlap; continue; } break; } int xDst = xT; // Right clip int cchT = 0; for (; pszT - psz < cch; pszT++) { int cx = m_pfnth->acxChar[(byte)*pszT] - m_nGlyphOverlap; if (xT + cx <= siz.cx) { cchT++; xT += cx; continue; } break; } if (cchT == 0) return 0; cch = cchT; // Draw xT = xDst; byte *pbDst = pbm->GetBits() + (long)y * siz.cx + xT; char *pszMax = &psz[ich + cch]; for (pszT = &psz[ich]; pszT < pszMax; pszT++) { char ch = *pszT; int cxChar = m_pfnth->acxChar[(byte)ch]; if (cxChar == 0) continue; byte *pbDraw; if (xT & 1) { pbDraw = m_mpchpbCodeOdd[(byte)ch]; } else { pbDraw = m_mpchpbCodeEven[(byte)ch]; } if (pbDraw == NULL) { ScanData *psd = (ScanData *)(((byte *)m_pfnth) + BigWord(m_pfnth->mpchibsd[(byte)*pszT])); word cb = Compile8Thunk(gpbScratch, psd, xT & 1); byte *pbT = (byte *)gmmgr.AllocPtr(cb); if (pbT != NULL) { gmmgr.WritePtr(pbT, 0, gpbScratch, cb); if (xT & 1) { gmmgr.WritePtr(m_mpchpbCodeOdd, (byte)ch * sizeof(byte *), &pbT, sizeof(byte *)); } else { gmmgr.WritePtr(m_mpchpbCodeEven, (byte)ch * sizeof(byte *), &pbT, sizeof(byte *)); } } pbDraw = gpbScratch; } DrawDispatchThunk(pbDraw, NULL, pbDst, siz.cx - cxChar, mpscaiclr, gmpiclriclrShadow); pbDst += cxChar - m_nGlyphOverlap; xT += cxChar - m_nGlyphOverlap; } return xT - xDst; }
bool SocConnection::Poll() { if (m_fDisconnecting) { m_fDisconnecting = false; if (m_pccb != NULL) m_pccb->OnDisconnect(this); return false; } if (m_soc == INVALID_SOCKET) return false; // Test if any incoming data is pending fd_set fds; FD_ZERO(&fds); FD_SET(m_soc, &fds); TIMEVAL tvTimeout; tvTimeout.tv_sec = 0; tvTimeout.tv_usec = 0; fd_set fdsDummy; FD_ZERO(&fdsDummy); // MpTrace("m_soc = %d, fds = %ld", (UInt16)m_soc, (UInt32)fds); int nSelected = select(((int)m_soc) + 1, &fds, &fdsDummy, &fdsDummy, &tvTimeout); if (nSelected == SOCKET_ERROR) { #ifdef DEBUG HostMessageBox(TEXT("select err: %s"), PszFromSocError()); #endif return false; } if (nSelected == 1) { NetMessage nm; int cb = recv(m_soc, (char *)&nm, sizeof(NetMessage), 0); // MpTrace("recv: cb %d", cb); if (cb != sizeof(NetMessage)) { HandleRecvError(); return false; } int cbT = BigWord(nm.cb); NetMessage *pnm = (NetMessage *)new byte[cbT]; if (pnm == NULL) { // Data is still pending but we can't do anything about it. // Follow the usual course of action (HandleRecvError will close the Connection) HandleRecvError(); return false; } memcpy(pnm, &nm, sizeof(NetMessage)); int cbRemaining = cbT - sizeof(NetMessage); // UNDONE: this now is a blocking operation until all of the message is // received. Better would be to accumulate the pieces of the message in // a temp buffer (SocConnection member) until it is complete, in a non- // blocking fashion. byte *pbT = (byte *)(pnm + 1); while (cbRemaining != 0) { cbT = recv(m_soc, (char *)pbT, cbRemaining, 0); if (cbT == SOCKET_ERROR) { int err = WSAGetLastError(); if (err != WSAEWOULDBLOCK) { delete[] pnm; HandleRecvError(); return false; } } pbT += cbT; cbRemaining -= cbT; } if (m_pccb != NULL) { // Before calling OnReceive, order in native byte order NetMessageByteOrderSwap(pnm, false); MpTrace("< %s", PszFromNetMessage(pnm)); m_pccb->OnReceive(this, pnm); } delete[] pnm; } return true; }