Beispiel #1
0
//-------------------------------------------------------------------
// SimpleString replacedata contains data to replace with.
bool ReplaceDlg::replace_selected_data(HWindow *pDlg)
{
    if (!bSelected)
    {
        MessageBox(pDlg, GetLangString(IDS_REPL_NO_DATA), MB_ICONERROR);
        return false;
    }
    int i = iGetStartOfSelection();
    int n = iGetEndOfSelection() - i + 1;
    SimpleArray<BYTE> olddata(n, &m_dataArray[i]);
    if (strReplaceWithData.IsEmpty())
    {
        // Selected data is to be deleted, since replace-with data is empty string.
        if (!m_dataArray.Replace(i, n, 0, 0))
        {
            MessageBox(pDlg, GetLangString(IDS_REPL_CANT_DELETE), MB_ICONERROR);
            return FALSE;
        }
        push_undorecord(i, olddata, olddata.GetLength(), NULL, 0);
        bSelected = false;
        iCurByte = iStartOfSelection;
    }
    else if (bPasteAsText)
    {
        // Replace with non-zero-length data.
        if (!m_dataArray.Replace(i, n, (const BYTE *)(const char *)strReplaceWithData, strReplaceWithData.StrLen()))
        {
            MessageBox(pDlg, GetLangString(IDS_REPL_FAILED), MB_ICONERROR);
            return false;
        }
        push_undorecord(i, olddata, olddata.GetLength(), (const BYTE *)(const char *)strReplaceWithData, strReplaceWithData.StrLen());
        iEndOfSelection = iStartOfSelection + strReplaceWithData.StrLen() - 1;
    }
    else
    {
        // Input string contains special-syntax-coded binary data.
        SimpleArray<BYTE> out;
        if (!transl_text_to_binary(out))
        {
            MessageBox(pDlg, GetLangString(IDS_REPL_CANNOT_CONVERT), MB_ICONERROR);
            return false;
        }
        if (!m_dataArray.Replace(i, n, out, out.GetLength()))
        {
            MessageBox(pDlg, GetLangString(IDS_REPL_FAILED), MB_ICONERROR);
            return false;
        }
        push_undorecord(i, olddata, olddata.GetLength(), out, out.GetLength());
        iEndOfSelection = iStartOfSelection + out.GetLength() - 1;
    }
    bFilestatusChanged = true;
    return true;
}
Beispiel #2
0
/**
 * @brief Apply the manipulation.
 * @param [in] hDlg Handle to the dialog.
 * @param [in] wParam Command user selected.
 * @return TRUE if bytes were copied, FALSE otherwise.
 */
BOOL BitManipDlg::Apply(HWindow *pDlg, WPARAM wParam)
{
	BYTE cBitValue = 0;
	if (pDlg->IsDlgButtonChecked(IDC_MANIPBITS_BIT8))
		cBitValue |= 128;
	if (pDlg->IsDlgButtonChecked(IDC_MANIPBITS_BIT7))
		cBitValue |= 64;
	if (pDlg->IsDlgButtonChecked(IDC_MANIPBITS_BIT6))
		cBitValue |= 32;
	if (pDlg->IsDlgButtonChecked(IDC_MANIPBITS_BIT5))
		cBitValue |= 16;
	if (pDlg->IsDlgButtonChecked(IDC_MANIPBITS_BIT4))
		cBitValue |= 8;
	if (pDlg->IsDlgButtonChecked(IDC_MANIPBITS_BIT3))
		cBitValue |= 4;
	if (pDlg->IsDlgButtonChecked(IDC_MANIPBITS_BIT2))
		cBitValue |= 2;
	if (pDlg->IsDlgButtonChecked(IDC_MANIPBITS_BIT1))
		cBitValue |= 1;
	if (wParam == IDOK)
	{
		BYTE ch = m_dataArray[iCurByte];
		m_dataArray[iCurByte] = cBitValue;
		push_undorecord(iCurByte, &ch, 1, &m_dataArray[iCurByte], 1);
		bFilestatusChanged = true;
		repaint();
		return TRUE;
	}
	TCHAR buf[64];
	_sntprintf(buf, RTL_NUMBER_OF(buf), GetLangString(IDS_BITMANIP_VALUE),
		(BYTE)cBitValue, (signed char)cBitValue, (BYTE)cBitValue);
	pDlg->SetDlgItemText(IDC_MANIPBITS_VALUE, buf);
	return FALSE;
}
Beispiel #3
0
/**
 * @brief Paste the bytes.
 * @param [in] hDlg Handle to dialog.
 * @return TRUE if paste succeeded, FALSE if failed.
 */
BOOL PasteDlg::Apply(HWindow *pDlg)
{
	bPasteAsText = pDlg->IsDlgButtonChecked(IDC_PASTE_BINARY) == BST_CHECKED;
	iPasteTimes = pDlg->GetDlgItemInt(IDC_PASTE_TIMES);
	if (iPasteTimes <= 0)
	{
		MessageBox(pDlg, GetLangString(IDS_PASTE_ATLEAST_ONCE), MB_ICONERROR);
		return FALSE;
	}
	iPasteSkip = pDlg->GetDlgItemInt(IDC_PASTE_SKIPBYTES);
	HEdit *pwndEdit1 = static_cast<HEdit *>(pDlg->GetDlgItem(IDC_PASTE_CLIPBOARD));
	int destlen = pwndEdit1->GetWindowTextLength() + 1;
	char *pcPastestring = new char[destlen];
	destlen = pwndEdit1->GetWindowTextA(pcPastestring, destlen);
	if (!bPasteAsText)
	{
		char *pc = 0;
		destlen = create_bc_translation((BYTE **)&pc, pcPastestring,
			static_cast<int>(strlen(pcPastestring)), iCharacterSet, iBinaryMode);
		delete [] pcPastestring;
		pcPastestring = pc;
	}
	if (destlen == 0)
	{
		MessageBox(pDlg, GetLangString(IDS_PASTE_WAS_EMPTY), MB_ICONERROR);
		delete [] pcPastestring;
		return FALSE;
	}
	WaitCursor wc1;
	SimpleArray<BYTE> olddata;
	if (bSelected || pDlg->IsDlgButtonChecked(IDC_PASTE_INSERT))
	{
		// Insert at iCurByte. Bytes there will be pushed up.
		if (bSelected)
		{
			iCurByte = iGetStartOfSelection();
			int iEndByte = iGetEndOfSelection();
			olddata.AppendArray(&m_dataArray[iCurByte], iEndByte - iCurByte + 1 + (iPasteTimes - 1) * iPasteSkip);
			m_dataArray.RemoveAt(iCurByte, iEndByte - iCurByte + 1);//Remove extraneous data
			bSelected = false; // Deselect
		}
		else
		{
			olddata.AppendArray(&m_dataArray[iCurByte], (iPasteTimes - 1) * iPasteSkip);
		}
		int i = iCurByte;
		for (int k = 0 ; k < iPasteTimes ; k++)
		{
			if (!m_dataArray.InsertAtGrow(i, (BYTE*)pcPastestring, 0, destlen))
			{
				MessageBox(pDlg, GetLangString(IDS_PASTE_NO_MEM), MB_ICONERROR);
				break;
			}
			i += destlen + iPasteSkip;
		}
		bFilestatusChanged = true;
		resize_window();
	}
	else
	{
		// Overwrite.
		// Enough space for writing?
		// m_dataArray.GetLength()-iCurByte = number of bytes from including curbyte to end.
		if (m_dataArray.GetLength() - iCurByte < (iPasteSkip + destlen) * iPasteTimes)
		{
			MessageBox(pDlg, GetLangString(IDS_PASTE_NO_SPACE), MB_ICONERROR);
			delete [] pcPastestring;
			return TRUE;
		}
		olddata.AppendArray(&m_dataArray[iCurByte], (iPasteTimes - 1) * (iPasteSkip + destlen) + destlen);
		// Overwrite data.
		for (int k = 0 ; k < iPasteTimes ; k++)
		{
			for (int i = 0 ; i < destlen ; i++)
			{
				m_dataArray[iCurByte + k * (iPasteSkip + destlen) + i] = pcPastestring[i];
			}
		}
		bFilestatusChanged = true;
		repaint();
	}
	push_undorecord(iCurByte, olddata, olddata.GetLength(), &m_dataArray[iCurByte], (iPasteTimes - 1) * (iPasteSkip + destlen) + destlen);
	delete [] pcPastestring;
	return TRUE;
}
Beispiel #4
0
/**
 * @brief Handle dialog messages.
 * @param [in] hDlg Handle to the dialog.
 * @param [in] iMsg The message.
 * @param [in] wParam The command in the message.
 * @param [in] lParam The optional parameter for the command.
 * @return TRUE if the message was handled, FALSE otherwise.
 */
INT_PTR FillWithDialog::DlgProc(HWindow* pDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch (iMsg)
	{
	case WM_INITDIALOG:
		{
			HEdit* pEditt = static_cast<HEdit *>(pDlg->GetDlgItem(IDC_HEX));//get the handle to the hex edit box
			pEditt->LimitText(FW_MAX);//limit the amount of text the user can enter
			pEditt->SetWindowText(pcFWText);//init hex text
			pEditt->SetFocus();//give the hex box focus
			pEditt->EnableWindow(!curtyp);
			oldproc = static_cast<LONG_PTR>(pEditt->SetWindowLongPtr(GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(HexProc)));//override the old proc to be HexProc
			EnableDlgItem(pDlg, IDC_HEXSTAT, !curtyp);

			HComboBox* typ = static_cast<HComboBox *>(pDlg->GetDlgItem(IDC_TYPE));
			typ->AddString(_T("Input"));
			typ->AddString(_T("File"));
			typ->SetCurSel(curtyp);//set cursel to previous

			//en/disable filename box and browse button
			HWindow* fn = pDlg->GetDlgItem(IDC_FN);
			fn->SetWindowText(szFWFileName);
			fn->EnableWindow(curtyp);
			EnableDlgItem(pDlg, IDC_BROWSE, curtyp);
			EnableDlgItem(pDlg, IDC_FILESTAT, curtyp);

			hfon = CreateFont(16, 0, 0, 0, FW_NORMAL, 0, 0, 0,
			                  DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
			                  DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, _T("Symbol"));
			inittxt(pDlg);
			switch (asstyp)
			{
			case 0:
				pDlg->CheckDlgButton(IDC_EQ, BST_CHECKED);
				break;
			case 1:
				pDlg->CheckDlgButton(IDC_OR, BST_CHECKED);
				break;
			case 2:
				pDlg->CheckDlgButton(IDC_AND, BST_CHECKED);
				break;
			case 3:
				pDlg->CheckDlgButton(IDC_XOR, BST_CHECKED);
				break;
			}
			return 0;//stop the system from setting focus to the control handle in (HWND) wParam because we already set focus above
		}
	case WM_COMMAND:
		switch (wParam)
		{
		case IDOK: //ok pressed
			{
				if (curtyp)
				{//1-file
					pDlg->GetDlgItemText(IDC_FN, szFWFileName, _MAX_PATH);//get file name
					FWFile = _topen(szFWFileName, _O_RDONLY | _O_BINARY);
					if (FWFile == -1)
					{//if there is error opening
						MessageBox(pDlg, GetLangString(IDS_ERR_OPENING_FILE), MB_ICONERROR);//tell user but don't close dlgbox
						return 1;//didn't process this message
					}//if
					FWFilelen = _filelength(FWFile);
					if (FWFilelen == 0)
					{//if filelen is zero
						MessageBox(pDlg, GetLangString(IDS_FILL_ZERO_SIZE_FILE), MB_ICONERROR);//tell user but don't close dlgbox
						_close(FWFile);//close file
						return 1;//didn't process this message
					}//if
					else if (FWFilelen == -1)
					{//error returned by _filelength
						MessageBox(pDlg, GetLangString(IDS_ERR_OPENING_FILE), MB_ICONERROR);//tell user but don't close dlgbox
						_close(FWFile);//close file
						return 1;//didn't process this message
					}//elseif
				}
				else
				{//0-input
					if (!buflen)
					{//no hex input
						MessageBox(pDlg, GetLangString(IDS_FILL_ZERO_SIZE_STR), MB_ICONERROR);//tell user but don't close dlgbox
						return 1;//didn't process this message
					}//if
					int i = pDlg->GetDlgItemText(IDC_HEX, pcFWText, FW_MAX);
					if (i == 0 || i == FW_MAX - 1)
					{//error
						MessageBox(pDlg, GetLangString(IDS_FILL_TOO_MANY_BYTES), MB_ICONERROR);//tell user but don't close dlgbox
						return 1;//didn't process this message
					}//if
					hexstring2charstring();//just in case
					//pcFWText[(aa?buflen:buflen*2)]='\0';//access violation if i do it in the above function
				}
				if (pDlg->IsDlgButtonChecked(IDC_EQ))
					asstyp = 0;
				else if (pDlg->IsDlgButtonChecked(IDC_OR))
					asstyp = 1;
				else if (pDlg->IsDlgButtonChecked(IDC_AND))
					asstyp = 2;
				else if (pDlg->IsDlgButtonChecked(IDC_XOR))
					asstyp = 3;

				// go ahead
				SetCursor(LoadCursor(nullptr, IDC_WAIT));
				BYTE (*fnc)(int);
				int iStartOfSelSetting;
				int iEndOfSelSetting;
				int iimax;
				if (curtyp)
				{//1-file
					fnc = file;
					iimax = FWFilelen;
				}//if
				else
				{//0-input
					fnc = input;
					iimax = buflen;
				}//else

				if (bSelected)
				{
					iStartOfSelSetting = iGetStartOfSelection();
					iEndOfSelSetting = iGetEndOfSelection();
				}
				else
				{
					iStartOfSelSetting = 0;
					iEndOfSelSetting = m_dataArray.GetUpperBound();
				}

				SimpleArray<BYTE> olddata(iEndOfSelSetting - iStartOfSelSetting + 1, &m_dataArray[iStartOfSelSetting]);
				int i = iStartOfSelSetting;
				int ii = 0;
				switch (asstyp)
				{// use switch instead of pointers to funcs that just call an operator as its faster
				case 0:
					while (i <= iEndOfSelSetting)
					{
						m_dataArray[i++] = fnc(ii++);
						ii %= iimax;
					}
					break;
				case 1:
					while (i <= iEndOfSelSetting)
					{
						m_dataArray[i++] |= fnc(ii++);
						ii %= iimax;
					}
					break;
				case 2:
					while (i <= iEndOfSelSetting)
					{
						m_dataArray[i++] &= fnc(ii++);
						ii %= iimax;
					}
					break;
				case 3:
					while (i <= iEndOfSelSetting)
					{
						m_dataArray[i++] ^= fnc(ii++);
						ii %= iimax;
					}
					break;
				}
				push_undorecord(iStartOfSelSetting, olddata, olddata.GetLength(), &m_dataArray[iStartOfSelSetting], olddata.GetLength());
				if (curtyp)
					_close(FWFile);//close file
				SetCursor(LoadCursor(nullptr, IDC_ARROW));
				bFilestatusChanged = true;
				repaint();//you tell me
			}
			// fall through
		case IDCANCEL: //cancel pressed
			DeleteObject(hfon);// won't need till next time
			pDlg->EndDialog(wParam);//tell CMD_fw not to carry out the fill with operation
			return 1;//did process this message
		case MAKEWPARAM(IDC_TYPE, CBN_SELCHANGE):
			//thing to fill selection with changes
			curtyp = static_cast<char>(pDlg->SendDlgItemMessage(IDC_TYPE, CB_GETCURSEL, 0, 0));//get cursel
			EnableDlgItem(pDlg, IDC_FN, curtyp);//en/disable fnamebox and browse button
			EnableDlgItem(pDlg, IDC_BROWSE, curtyp);
			EnableDlgItem(pDlg, IDC_FILESTAT, curtyp);
			curtyp = !curtyp;//flip it for the others
			EnableDlgItem(pDlg, IDC_HEX, curtyp);//en/disable hexboxand relateds
			EnableDlgItem(pDlg, IDC_HEXSTAT, curtyp);
			curtyp = !curtyp;//restore original value -not for below -accurate value needed elsewhere
			//set text in boxes down below
			inittxt(pDlg);
			break;
		case IDC_BROWSE:
			{
				//prepare OPENFILENAME for the file open common dlg box
				szFWFileName[0] = '\0';
				OPENFILENAME ofn;
				ZeroMemory(&ofn, sizeof ofn);
				ofn.lStructSize = sizeof ofn;
				ofn.hwndOwner = pDlg->m_hWnd;
				ofn.lpstrFilter = GetLangString(IDS_OPEN_ALL_FILES);
				ofn.lpstrFile = szFWFileName;
				ofn.nMaxFile = _MAX_PATH ;
				ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_FILEMUSTEXIST;
				//show open dlgbox and if file good save name & path in edit box
				if (GetOpenFileName(&ofn))
					pDlg->SetDlgItemText(IDC_FN, ofn.lpstrFile);
			}
			return TRUE;
		case MAKEWPARAM(IDC_HEX, EN_UPDATE): //hexedit updated
			pDlg->GetDlgItemText(IDC_HEX, pcFWText, FW_MAX);//gettext
			hexstring2charstring();//convert to char string
			//set text in boxes down below
			inittxt(pDlg);
			return TRUE;
		}
		break;

	case WM_HELP:
		OnHelp(pDlg);
		break;
	}
	return FALSE;
}