void CHexEdit::OnEditCopy() { COleDataSource* pSource = new COleDataSource(); EmptyClipboard(); int dwLen = GetSelLength(); HGLOBAL hMemb = ::GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE|GMEM_ZEROINIT, dwLen); HGLOBAL hMema = ::GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE|GMEM_ZEROINIT, (dwLen) * 3); if (!hMemb || !hMema) return; LPBYTE p = (BYTE*)::GlobalLock(hMemb); // copy binary memcpy(p, m_pData+m_selStart, dwLen); ::GlobalUnlock(hMemb); p = (BYTE*)::GlobalLock(hMema); // copy ascii memcpy(p, m_pData+m_selStart, dwLen); ::GlobalUnlock(hMema); for(int i = 0; i < dwLen;i++) { if(m_currentMode != EDIT_ASCII) { TOHEX(m_pData[m_selStart+i], p); *p++ = ' '; } else if(!isprint(*p)) *p++ = '.'; } pSource->CacheGlobalData(RegisterClipboardFormat("BinaryData"), hMemb); pSource->CacheGlobalData(CF_TEXT, hMema); pSource->SetClipboard(); }
static size_t emv_pk_write_bin(unsigned char *out, size_t outlen, const unsigned char *buf, size_t len) { int i; size_t pos = 0; if (len == 0) return 0; if (outlen < len * 3) return 0; out[pos++] = TOHEX(buf[0] >> 4); out[pos++] = TOHEX(buf[0] & 0xf); for (i = 1; i < len; i++) { out[pos++] = ':'; out[pos++] = TOHEX(buf[i] >> 4); out[pos++] = TOHEX(buf[i] & 0xf); } out[pos++] = ' '; return pos; }
void MsgCommonText::writeBitMap(bool*bitmap,unsigned bitmaplen, const char*name) { char txtbits[bitmaplen+6], *tp = txtbits; unsigned i, accum = 0; for (i=0; i<bitmaplen; i++) { accum = (accum<<1) + (bitmap[i] ? 1 : 0); if (((i+1) & 3) == 0) { *tp++ = TOHEX(accum); accum = 0; } } //if (i & 3) { *tp++ = TOHEX(accum); } Our bitmap is always evenly % 4, so dont bother. *tp = 0; //truncateredundant(txtbits,bitmaplen); mos << " " << name << "=(" << txtbits << ")"; }
void _assuan_debug_buffer (assuan_context_t ctx, unsigned int cat, const char *const fmt, const char *const func, const char *const tagname, void *tag, const char *const buffer, size_t len) { int idx = 0; int j; /* Probe if this wants to be logged based on category. */ if (!ctx || ! ctx->log_cb || ! (*ctx->log_cb) (ctx, ctx->log_cb_data, cat, NULL)) return; while (idx < len) { char str[51]; char *strp = str; char *strp2 = &str[34]; for (j = 0; j < 16; j++) { unsigned char val; if (idx < len) { val = buffer[idx++]; *(strp++) = TOHEX (val >> 4); *(strp++) = TOHEX (val % 16); *(strp2++) = isprint (val) ? val : '.'; } else { *(strp++) = ' '; *(strp++) = ' '; } if (j == 7) *(strp++) = ' '; }
//URL编码 void URLEncode(const char* szIn, char** pOut) { int nInLenth = (int) strlen( szIn ); int nFlag = 0; BYTE byte; *pOut = (char*)malloc(nInLenth*3); char *szOut = *pOut; for ( int i = 0; i < nInLenth; i++ ) { byte = szIn[i]; if ( isalnum( byte ) ) { szOut[nFlag++] = byte; } else { szOut[nFlag++] = '%'; szOut[nFlag++] = TOHEX( byte >> 4 ); szOut[nFlag++] = TOHEX( byte % 16 ); } } szOut[nFlag] = '\0'; }
unsigned char *emv_pk_dump_pk(const struct emv_pk *pk) { size_t outsize = 1024; /* should be enough */ unsigned char *out = malloc(outsize); /* should be enough */ size_t outpos = 0; size_t rc; if (!out) return NULL; rc = emv_pk_write_bin(out + outpos, outsize - outpos, pk->rid, 5); if (rc == 0) goto err; outpos += rc; rc = emv_pk_write_bin(out + outpos, outsize - outpos, &pk->index, 1); if (rc == 0) goto err; outpos += rc; if (outpos + 7 > outsize) goto err; out[outpos++] = TOHEX((pk->expire >> 20) & 0xf); out[outpos++] = TOHEX((pk->expire >> 16) & 0xf); out[outpos++] = TOHEX((pk->expire >> 12) & 0xf); out[outpos++] = TOHEX((pk->expire >> 8 ) & 0xf); out[outpos++] = TOHEX((pk->expire >> 4 ) & 0xf); out[outpos++] = TOHEX((pk->expire >> 0 ) & 0xf); out[outpos++] = ' '; if (pk->pk_algo == PK_RSA) { rc = emv_pk_write_str(out + outpos, outsize - outpos, "rsa"); if (rc == 0) goto err; outpos += rc; out[outpos++] = ' '; } else { if (outpos + 4 > outsize) goto err; out[outpos++] = '?'; out[outpos++] = '?'; out[outpos++] = TOHEX(pk->pk_algo >> 4); out[outpos++] = TOHEX(pk->pk_algo & 0xf); } rc = emv_pk_write_bin(out + outpos, outsize - outpos, pk->exp, pk->elen); if (rc == 0) goto err; outpos += rc; rc = emv_pk_write_bin(out + outpos, outsize - outpos, pk->modulus, pk->mlen); if (rc == 0) goto err; outpos += rc; if (pk->hash_algo == HASH_SHA_1) { rc = emv_pk_write_str(out + outpos, outsize - outpos, "sha1"); if (rc == 0) goto err; outpos += rc; out[outpos++] = ' '; } else { if (outpos + 4 > outsize) goto err; out[outpos++] = '?'; out[outpos++] = '?'; out[outpos++] = TOHEX(pk->pk_algo >> 4); out[outpos++] = TOHEX(pk->pk_algo & 0xf); } rc = emv_pk_write_bin(out + outpos, outsize - outpos, pk->hash, 20); if (rc == 0) goto err; outpos += rc; out[outpos-1] = '\0'; return out; err: free(out); return NULL; }
void CHexEdit::OnPaint() { CPaintDC pdc(this); // device context for painting CRect rc; GetClientRect(rc); CDC dc; dc.CreateCompatibleDC(CDC::FromHandle(pdc.m_ps.hdc)); CBitmap bm; bm.CreateCompatibleBitmap(CDC::FromHandle(pdc.m_ps.hdc), rc.Width(), rc.Height()); dc.SelectObject(bm); CBrush b; b.CreateSolidBrush(RGB(0xff,0xff,0xff)); dc.FillRect(rc, &b); ASSERT(m_currentAddress >= 0); ASSERT(m_topindex >= 0); dc.SelectObject(m_Font); int height = 0; int x,y; char buf[256]; WCHAR wbuf[256]; int n=0; x = rc.TopLeft().x; y = rc.TopLeft().y; dc.SetBoundsRect(&rc, DCB_DISABLE); if(m_pData) { // // get char dimensions // if(m_bUpdate) { dc.GetCharWidth('0', '0', &m_nullWidth); CSize sz = dc.GetTextExtent(L"0", 1); m_lineHeight = sz.cy; m_offHex = m_bShowAddress ? (m_bAddressIsWide ? m_nullWidth * 9 : m_nullWidth * 5) : 0; m_offAscii = m_bShowAddress ? (m_bAddressIsWide ? m_nullWidth * 9 : m_nullWidth * 5) : 0; m_offAscii += m_bShowHex ? (m_bpr * 3 * m_nullWidth) : 0; m_lpp = rc.Height() / m_lineHeight; m_bHalfPage = FALSE; if(m_lpp * m_bpr > m_length) { m_lpp = (m_length + (m_bpr/2)) / m_bpr ; if(m_length % m_bpr != 0) { m_bHalfPage = TRUE; m_lpp++; } } m_bUpdate = FALSE; UpdateScrollbars(); } //TRACE("%i %i\n", m_topindex, m_selStart); height = rc.Height() / m_lineHeight; height *= m_lineHeight; if(m_bShowAddress) { char fmt[8] = {'%','0','8','l','X'}; fmt[2] = m_bAddressIsWide ? '8' : '4'; int w = m_bAddressIsWide ? 8 : 4; y = 0; CRect rcd = rc; rcd.TopLeft().x = m_offAddress; for(int i = m_topindex + m_AddOff; (i < m_length+ m_AddOff) && (rcd.TopLeft().y < height); i+= m_bpr) { sprintf(buf, fmt, i); CMySystem::CharToWCHAR(wbuf,buf); dc.DrawText(wbuf, w, rcd, DT_LEFT|DT_TOP|DT_SINGLELINE|DT_NOPREFIX); rcd.TopLeft().y += m_lineHeight; } } if(m_bShowHex) { y = 0; CRect rcd = rc; rcd.TopLeft().x = x = m_offHex; if(m_selStart != 0xffffffff && (m_currentMode == EDIT_HIGH || m_currentMode == EDIT_LOW)) { int i; int n = 0; int selStart = m_selStart, selEnd = m_selEnd; if(selStart > selEnd) selStart ^= selEnd ^= selStart ^= selEnd; for(i = m_topindex ; (i < selStart) && (y < height); i++) { char* p = &buf[0]; TOHEX(m_pData[i], p); *p++ = ' '; CMySystem::CharToWCHAR(wbuf,buf); dc.TextOut(x, y, wbuf, 3); x += m_nullWidth * 3; n++; if(n == m_bpr) { n = 0; x = m_offHex; y += m_lineHeight; } } dc.SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT)); dc.SetBkColor(GetSysColor(COLOR_HIGHLIGHT)); for(; (i < selEnd) && (i < m_length) && (y < height); i++) { char* p = &buf[0]; TOHEX(m_pData[i], p); *p++ = ' '; CMySystem::CharToWCHAR(wbuf,buf); dc.TextOut(x, y, wbuf, 3); x += m_nullWidth * 3; n++; if(n == m_bpr) { n = 0; x = m_offHex; y += m_lineHeight; } } dc.SetTextColor(GetSysColor(COLOR_WINDOWTEXT)); dc.SetBkColor(GetSysColor(COLOR_WINDOW)); for(; (i < m_length) && (y < height); i++) { char* p = &buf[0]; TOHEX(m_pData[i], p); *p++ = ' '; CMySystem::CharToWCHAR(wbuf,buf); dc.TextOut(x, y, wbuf, 3); x += m_nullWidth * 3; n++; if(n == m_bpr) { n = 0; x = m_offHex; y += m_lineHeight; } } } else { for(int i = m_topindex ; (i < m_length) && (rcd.TopLeft().y < height);) { char* p = &buf[0]; for( n = 0; (n < m_bpr) && (i < m_length); n++) { TOHEX(m_pData[i ], p); *p++ = ' '; i++; } while(n < m_bpr) { *p++ = ' '; *p++ = ' '; *p++ = ' '; n++; } CMySystem::CharToWCHAR(wbuf,buf); dc.DrawText(wbuf, m_bpr*3, rcd, DT_LEFT|DT_TOP|DT_SINGLELINE|DT_NOPREFIX); rcd.TopLeft().y += m_lineHeight; } } } if(m_bShowAscii) { y = 0; CRect rcd = rc; rcd.TopLeft().x = x = m_offAscii; if(m_selStart != 0xffffffff && m_currentMode == EDIT_ASCII) { int i; int n = 0; int selStart = m_selStart, selEnd = m_selEnd; if(selStart > selEnd) selStart ^= selEnd ^= selStart ^= selEnd; for(i = m_topindex ; (i < selStart) && (y < height); i++) { buf[0] = isprint(m_pData[i]) ? m_pData[i] : '.'; CMySystem::CharToWCHAR(wbuf,buf); dc.TextOut(x, y, wbuf, 1); x += m_nullWidth; n++; if(n == m_bpr) { n = 0; x = m_offAscii; y += m_lineHeight; } } dc.SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT)); dc.SetBkColor(GetSysColor(COLOR_HIGHLIGHT)); for(; (i < selEnd) && (y < height); i++) { buf[0] = isprint(m_pData[i]) ? m_pData[i] : '.'; CMySystem::CharToWCHAR(wbuf,buf); dc.TextOut(x, y, wbuf, 1); x += m_nullWidth; n++; if(n == m_bpr) { n = 0; x = m_offAscii; y += m_lineHeight; } } dc.SetTextColor(GetSysColor(COLOR_WINDOWTEXT)); dc.SetBkColor(GetSysColor(COLOR_WINDOW)); for(; (i < m_length) && y < height; i++) { buf[0] = isprint(m_pData[i]) ? m_pData[i] : '.'; CMySystem::CharToWCHAR(wbuf,buf); dc.TextOut(x, y, wbuf, 1); x += m_nullWidth; n++; if(n == m_bpr) { n = 0; x = m_offAscii; y += m_lineHeight; } } } else { for(int i = m_topindex ; (i < m_length) && (rcd.TopLeft().y < height);) { char* p = &buf[0]; for( n = 0; (n < m_bpr) && (i < m_length); n++) { *p++ = isprint(m_pData[i]) ? m_pData[i] : '.'; i++; } CMySystem::CharToWCHAR(wbuf,buf); dc.DrawText(wbuf, n, rcd, DT_LEFT|DT_TOP|DT_SINGLELINE|DT_NOPREFIX); rcd.TopLeft().y += m_lineHeight; } } } } pdc.BitBlt(0, 0, rc.Width(), rc.Height(), &dc, 0, 0, SRCCOPY); }
// 重载OnPaint,处理每一行 void CHexEdit::OnPaint() { CPaintDC pdc(this); // device context for painting CRect rc; GetClientRect(rc); CDC dc; dc.CreateCompatibleDC(CDC::FromHandle(pdc.m_ps.hdc)); CBitmap bm; bm.CreateCompatibleBitmap(CDC::FromHandle(pdc.m_ps.hdc), rc.Width(), rc.Height()); dc.SelectObject(bm); CBrush b; b.CreateSolidBrush(RGB(0xff,0xff,0xff)); dc.FillRect(rc, &b); ASSERT(m_currentAddress >= 0); ASSERT(m_topindex >= 0); dc.SelectObject(m_Font); int height = 0; int x,y; char buf[256]; x = rc.TopLeft().x; y = rc.TopLeft().y; dc.SetBoundsRect(&rc, DCB_DISABLE); if(m_pData) { // // get char dimensions // if(m_bUpdate) { dc.GetCharWidth('0', '0', &m_nullWidth); CSize sz = dc.GetTextExtent("0", 1); m_lineHeight = sz.cy; // 每一种类型的间隔 m_offHex = m_bShowAddress ? (m_bAddressIsWide ? m_nullWidth * 9 : m_nullWidth * 5) : 0; m_offAscii = m_bShowAddress ? (m_bAddressIsWide ? m_nullWidth * 10 : m_nullWidth * 5) : 0; m_offAscii += m_bShowHex ? (m_bpr * 3 * m_nullWidth) : 0; m_lpp = rc.Height() / m_lineHeight; m_bHalfPage = FALSE; if(m_lpp * m_bpr > m_length) { m_lpp = (m_length + (m_bpr/2)) / m_bpr ; if(m_length % m_bpr != 0) { m_bHalfPage = TRUE; m_lpp++; } } m_bUpdate = FALSE; UpdateScrollbars(); } TRACE("%i %i\n", m_topindex, m_selStart); height = rc.Height() / m_lineHeight; height *= m_lineHeight; if(m_bShowAddress) { char fmt[8] = {'%','0','8','l','X'}; fmt[2] = m_bAddressIsWide ? '8' : '4'; int w = m_bAddressIsWide ? 8 : 4; y = 0; CRect rcd = rc; rcd.TopLeft().x = m_offAddress; // 使用用户自定义偏移--在分析定位文件内容时有作用 // todo:存在bug,滚动时,偏移量不会变化。-->临时不使用 int tmp = m_myoffset; for(int i = m_topindex; (i < m_length) && (rcd.TopLeft().y < height); i+= m_bpr) { if (m_myoffset) { sprintf(buf, fmt, tmp); tmp += m_bpr; } else { sprintf(buf, fmt, i); } dc.DrawText(buf, w, rcd, DT_LEFT|DT_TOP|DT_SINGLELINE|DT_NOPREFIX); rcd.TopLeft().y += m_lineHeight; } } if(m_bShowHex) { y = 0; CRect rcd = rc; rcd.TopLeft().x = x = m_offHex; if(m_selStart != 0xffffffff) { int i; int n = 0; int selStart = m_selStart, selEnd = m_selEnd; if(selStart > selEnd) selStart ^= selEnd ^= selStart ^= selEnd; char* p; for(i = m_topindex; (i < selStart) && (y < height); i++) { p = &buf[0]; TOHEX(m_pData[i], p); *p++ = ' '; dc.TextOut(x, y, buf, 3); x += m_nullWidth * 3; n++; if(n == m_bpr) { n = 0; x = m_offHex; y += m_lineHeight; } } dc.SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT)); dc.SetBkColor(GetSysColor(COLOR_HIGHLIGHT)); for(; i <= selEnd && i < m_length && y < height ; i++) { p = &buf[0]; TOHEX(m_pData[i], p); *p++ = ' '; dc.TextOut(x, y, buf, (i==selEnd || (i+1)%m_bpr==0 || i==m_length-1) ? 2:3); x += m_nullWidth * 3; n++; if(n == m_bpr) { n = 0; x = m_offHex; y += m_lineHeight; } } dc.SetTextColor(GetSysColor(COLOR_WINDOWTEXT)); dc.SetBkColor(GetSysColor(COLOR_WINDOW)); for(; (i < m_length) && (y < height); i++) { char* p = &buf[0]; TOHEX(m_pData[i], p); *p++ = ' '; dc.TextOut(x, y, buf, 3); x += m_nullWidth * 3; n++; if(n == m_bpr) { n = 0; x = m_offHex; y += m_lineHeight; } } } else { for(int i = m_topindex; (i < m_length) && (rcd.TopLeft().y < height);) { int n = 0; char* p = &buf[0]; for(n = 0; (n < m_bpr) && (i < m_length); n++) { TOHEX(m_pData[i], p); *p++ = ' '; i++; } while(n < m_bpr) { *p++ = ' '; *p++ = ' '; *p++ = ' '; n++; } dc.DrawText(buf, m_bpr*3, rcd, DT_LEFT|DT_TOP|DT_SINGLELINE|DT_NOPREFIX); rcd.TopLeft().y += m_lineHeight; } } } if(m_bShowAscii) { y = 0; CRect rcd = rc; rcd.TopLeft().x = x = m_offAscii; if(m_selStart != 0xffffffff) { int i; int n = 0; int selStart = m_selStart, selEnd = m_selEnd; if(selStart > selEnd) selStart ^= selEnd ^= selStart ^= selEnd; for(i = m_topindex; (i < selStart) && (y < height); i++) { buf[0] = isprint(m_pData[i]) ? m_pData[i] : '.'; dc.TextOut(x, y, buf, 1); x += m_nullWidth; n++; if(n == m_bpr) { n = 0; x = m_offAscii; y += m_lineHeight; } } dc.SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT)); dc.SetBkColor(GetSysColor(COLOR_HIGHLIGHT)); for(; (i <=selEnd) && i<m_length && (y < height); i++) { buf[0] = isprint(m_pData[i]) ? m_pData[i] : '.'; dc.TextOut(x, y, buf, 1); x += m_nullWidth; n++; if(n == m_bpr) { n = 0; x = m_offAscii; y += m_lineHeight; } } dc.SetTextColor(GetSysColor(COLOR_WINDOWTEXT)); dc.SetBkColor(GetSysColor(COLOR_WINDOW)); for(; (i < m_length) && y < height; i++) { buf[0] = isprint(m_pData[i]) ? m_pData[i] : '.'; dc.TextOut(x, y, buf, 1); x += m_nullWidth; n++; if(n == m_bpr) { n = 0; x = m_offAscii; y += m_lineHeight; } } } else { for(int i = m_topindex; (i < m_length) && (rcd.TopLeft().y < height);) { int n = 0; char* p = &buf[0]; for(n = 0; (n < m_bpr) && (i < m_length); n++) { *p++ = isprint(m_pData[i]) ? m_pData[i] : '.'; i++; } dc.DrawText(buf, n, rcd, DT_LEFT|DT_TOP|DT_SINGLELINE|DT_NOPREFIX); rcd.TopLeft().y += m_lineHeight; } } } } pdc.BitBlt(0, 0, rc.Width(), rc.Height(), &dc, 0, 0, SRCCOPY); //char ss[40]; //sprintf(ss,"m_length:%d m_currentAddress:%d",m_length,m_currentAddress); //GetParent()->SetWindowText(ss); // no need to do this }