Skip to content

Commit

Permalink
1. Падение при копирование выделенного UTF-8 текста из вьювера.
Browse files Browse the repository at this point in the history
2. Некорректное преобразование text/hex в диалоге поиска.
3. Падение при нажатии End на radiobutton в диалоге.
4. Не обновлялся экран редактора при прокрутке по Ctrl-Up/Down из меню "Find all".
  • Loading branch information
alabuzhev committed Sep 13, 2016
1 parent 8f27f86 commit 1380be3
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 109 deletions.
10 changes: 10 additions & 0 deletions far/changelog
@@ -1,3 +1,13 @@
drkns 13.09.2016 18:28:08 +0200 - build 4791

1. Падение при копирование выделенного UTF-8 текста из вьювера.

2. Некорректное преобразование text/hex в диалоге поиска.

3. Падение при нажатии End на radiobutton в диалоге.

4. Не обновлялся экран редактора при прокрутке по Ctrl-Up/Down из меню "Find all".

svs 13.09.2016 15:13:03 +0300 - build 4790

1. SQLite 3.14.2
Expand Down
33 changes: 9 additions & 24 deletions far/dialog.cpp
Expand Up @@ -2846,6 +2846,15 @@ int Dialog::ProcessKey(const Manager::Key& Key)
case KEY_CTRLDOWN: case KEY_CTRLNUMPAD2:
case KEY_RCTRLDOWN: case KEY_RCTRLNUMPAD2:
return ProcessOpenComboBox(Items[m_FocusPos].Type, &Items[m_FocusPos], m_FocusPos);

case KEY_F11:
if (!Global->IsProcessAssignMacroKey)
{
if (!CheckDialogMode(DMODE_NOPLUGINS))
doGlobalProcessKey = true;
}
break;

// ЭТО перед default предпоследний!!!
case KEY_END: case KEY_NUMPAD1:

Expand All @@ -2858,14 +2867,6 @@ int Dialog::ProcessKey(const Manager::Key& Key)
return TRUE;
}

case KEY_F11:
if (!Global->IsProcessAssignMacroKey)
{
if (!CheckDialogMode(DMODE_NOPLUGINS))
doGlobalProcessKey = true;
}
break;

// ???
// ЭТО перед default последний!!!
case KEY_PGDN: case KEY_NUMPAD3:
Expand Down Expand Up @@ -6229,19 +6230,3 @@ void Dialog::ShowConsoleTitle()
{
ConsoleTitle::SetFarTitle(DialogMode.Check(DMODE_KEEPCONSOLETITLE)? m_ConsoleTitle : GetTitle());
}

string Dialog::ExtractHexString(const string& DialogHexString)
{
auto Result{ DialogHexString };
// TODO: Fix these and trailing spaces in Dialog class?
Result.erase(std::remove(ALL_RANGE(Result), L' '), Result.end());
if (Result.size() & 1)
{
// Odd length - hex string is not valid.
// This is an UI helper, so we don't want to throw.
// Fixing it gracefully and in 1.7x compatible way:
// "12 34 5" -> "12 34 05"
Result.insert(Result.end() - 1, L'0');
}
return Result;
}
2 changes: 0 additions & 2 deletions far/dialog.hpp
Expand Up @@ -226,8 +226,6 @@ class Dialog: public Modal
return Items[ListId].ListPtr->GetUserDataPtr<T>(static_cast<int>(ItemId));
}

static string ExtractHexString(const string& DialogHexString);

protected:
size_t InitDialogObjects(size_t ID = (size_t)-1);

Expand Down
6 changes: 6 additions & 0 deletions far/editor.cpp
Expand Up @@ -3881,10 +3881,16 @@ BOOL Editor::Search(int Next)
Show();
}
break;

case KEY_CTRLUP: case KEY_RCTRLUP:
case KEY_CTRLDOWN: case KEY_RCTRLDOWN:
ProcessKeyInternal(Manager::Key(Key), RefreshMe);
if (RefreshMe)
{
Refresh();
}
break;

case KEY_F5:
MenuZoomed=!MenuZoomed;
if(MenuZoomed)
Expand Down
12 changes: 10 additions & 2 deletions far/encoding.cpp
Expand Up @@ -236,9 +236,13 @@ size_t encoding::get_bytes(uintptr_t const Codepage, const wchar_t* const Data,
case CP_UTF8:
return Utf8::get_bytes(Data, DataSize, Buffer, BufferSize);

case CP_UNICODE:
memcpy(Buffer, Data, std::min(DataSize * sizeof(wchar_t), BufferSize));
return DataSize * sizeof(wchar_t);

case CP_REVERSEBOM:
swap_bytes(Data, Buffer, std::min(DataSize * sizeof(wchar_t), BufferSize));
return DataSize * sizeof(wchar_t);
swap_bytes(Data, Buffer, std::min(DataSize * sizeof(wchar_t), BufferSize));
return DataSize * sizeof(wchar_t);

default:
{
Expand Down Expand Up @@ -277,6 +281,10 @@ size_t encoding::get_chars(uintptr_t const Codepage, const char* const Data, siz
case CP_UTF7:
return Utf7::get_chars(Data, DataSize, Buffer, BufferSize, nullptr);

case CP_UNICODE:
memcpy(Buffer, Data, std::min(DataSize, BufferSize * sizeof(wchar_t)));
return DataSize / sizeof(wchar_t);

case CP_REVERSEBOM:
swap_bytes(Data, Buffer, std::min(DataSize, BufferSize * sizeof(wchar_t)));
return DataSize / sizeof(wchar_t);
Expand Down
2 changes: 1 addition & 1 deletion far/encoding.hpp
Expand Up @@ -156,6 +156,7 @@ namespace encoding

void swap_bytes(const void* Src, void* Dst, size_t SizeInBytes);

inline bool IsVirtualCodePage(uintptr_t cp) { return cp == CP_DEFAULT || cp == CP_REDETECT || cp == CP_SET; }
inline bool IsUnicodeCodePage(uintptr_t cp) { return cp == CP_UNICODE || cp == CP_REVERSEBOM; }
inline bool IsStandardCodePage(uintptr_t cp) { return IsUnicodeCodePage(cp) || cp == CP_UTF8 || cp == GetOEMCP() || cp == GetACP(); }
inline bool IsUnicodeOrUtfCodePage(uintptr_t cp) { return IsUnicodeCodePage(cp) || cp==CP_UTF8 || cp==CP_UTF7; }
Expand Down Expand Up @@ -256,5 +257,4 @@ using cp_map = std::unordered_map<UINT, std::pair<UINT, string>>;
const cp_map& InstalledCodepages();
cp_map::value_type::second_type GetCodePageInfo(UINT cp);


#endif // ENCODING_HPP_44AE7032_AF79_4A6F_A2ED_529BC1A38758
16 changes: 2 additions & 14 deletions far/findfile.cpp
Expand Up @@ -766,19 +766,7 @@ intptr_t FindFiles::MainDlgProc(Dialog* Dlg, intptr_t Msg, intptr_t Param1, void
{
Dlg->SendMessage(DM_ENABLEREDRAW, FALSE, nullptr);
const auto Src = reinterpret_cast<const wchar_t*>(Dlg->SendMessage(DM_GETCONSTTEXTPTR, Param2 ? FAD_EDIT_TEXT : FAD_EDIT_HEX, nullptr));
string strDataStr;
if (Param2)
{
// BUGBUG, it's unclear how to represent unicode in hex
const auto AnsiStr = encoding::get_bytes(CodePage, Src);
strDataStr = BlobToHexWString(AnsiStr.data(), AnsiStr.size(), 0);
}
else
{
const auto Blob = HexStringToBlob(Dialog::ExtractHexString(Src).data(), 0);
strDataStr.assign(ALL_CONST_RANGE(Blob));
}

const auto strDataStr = ConvertHexString(Src, CodePage, !Param2);
Dlg->SendMessage(DM_SETTEXTPTR,Param2?FAD_EDIT_HEX:FAD_EDIT_TEXT, UNSAFE_CSTR(strDataStr));
const auto iParam = reinterpret_cast<intptr_t>(Param2);
Dlg->SendMessage(DM_SHOWITEM,FAD_EDIT_TEXT,ToPtr(!iParam));
Expand Down Expand Up @@ -3125,7 +3113,7 @@ FindFiles::FindFiles():

if (SearchHex)
{
strFindStr = Dialog::ExtractHexString(FindAskDlg[FAD_EDIT_HEX].strData);
strFindStr = ExtractHexString(FindAskDlg[FAD_EDIT_HEX].strData);
}
else
strFindStr = FindAskDlg[FAD_EDIT_TEXT].strData;
Expand Down
30 changes: 30 additions & 0 deletions far/strmix.cpp
Expand Up @@ -1534,5 +1534,35 @@ std::vector<char> HexStringToBlob(const wchar_t* Hex, wchar_t Separator)
return HexStringToBlobT(Hex, wcslen(Hex), Separator);
}

string ExtractHexString(const string& HexString)
{
auto Result{ HexString };
// TODO: Fix these and trailing spaces in Dialog class?
Result.erase(std::remove(ALL_RANGE(Result), L' '), Result.end());
if (Result.size() & 1)
{
// Odd length - hex string is not valid.
// This is an UI helper, so we don't want to throw.
// Fixing it gracefully and in 1.7x compatible way:
// "12 34 5" -> "12 34 05"
Result.insert(Result.end() - 1, L'0');
}
return Result;
}

string ConvertHexString(const string& From, uintptr_t Codepage, bool FromHex)
{
const auto CompatibleCp = IsVirtualCodePage(Codepage)? CP_ACP : Codepage;
if (FromHex)
{
const auto Blob = HexStringToBlob(ExtractHexString(From).data(), 0);
return encoding::get_chars(CompatibleCp, Blob.data(), Blob.size());
}
else
{
const auto Blob = encoding::get_bytes(CompatibleCp, From);
return BlobToHexWString(Blob.data(), Blob.size(), 0);
}
}

}
3 changes: 3 additions & 0 deletions far/strmix.hpp
Expand Up @@ -222,6 +222,9 @@ auto to_hex_string(T Value) { return to_hex_string_t<std::string>(Value); }
template<class T>
auto to_hex_wstring(T Value) { return to_hex_string_t<string>(Value); }

string ExtractHexString(const string& HexString);
string ConvertHexString(const string& From, uintptr_t Codepage, bool FromHex);

struct string_i_less
{
bool operator()(const string& a, const string& b) const
Expand Down
76 changes: 11 additions & 65 deletions far/viewer.cpp
Expand Up @@ -2589,9 +2589,8 @@ intptr_t Viewer::ViewerSearchDlgProc(Dialog* Dlg,intptr_t Msg,intptr_t Param1,vo
Dlg->SendMessage(DM_GETEDITPOSITION, sd_src, &esp);
FarDialogItemData item = {sizeof(FarDialogItemData)};
Dlg->SendMessage(DM_GETTEXT, sd_src, &item);
const auto HexString = Dialog::ExtractHexString({ reinterpret_cast<const wchar_t*>(Dlg->SendMessage(DM_GETCONSTTEXTPTR, sd_src, nullptr)), item.PtrLength });
string strTo;
Data->viewer->SearchTextTransform(strTo, HexString, !new_hex, esp.CurPos);
const string Src(reinterpret_cast<const wchar_t*>(Dlg->SendMessage(DM_GETCONSTTEXTPTR, sd_src, nullptr)), item.PtrLength);
const auto strTo = ConvertHexString(Src, m_Codepage, !new_hex);
item.PtrLength = strTo.size();
item.PtrData = UNSAFE_CSTR(strTo);
Dlg->SendMessage(DM_SETTEXT, sd_dst, &item);
Expand Down Expand Up @@ -2721,66 +2720,6 @@ static auto hex2ss(const string& from, intptr_t *pos = nullptr)
return HexStringToBlob(strFrom.data(), 0);
}

void Viewer::SearchTextTransform(string &to, const string& from, bool hex2text, intptr_t &pos)
{
if (hex2text)
{
auto Bytes = hex2ss(from, &pos);
if (IsUnicodeCodePage(m_Codepage))
{
int v = CP_REVERSEBOM == m_Codepage ? 1 : 0;
if (Bytes.size() & 1)
Bytes.emplace_back('\0');

for (size_t i = 0; i < Bytes.size(); i += 2)
{
wchar_t ch = MAKEWORD(Bytes[i+v], Bytes[i+1-v]);
to += ch;
}
if ( pos >= 0 )
pos /= 2;
}
else
{
to = encoding::get_chars(m_Codepage, Bytes);
if ( pos >= 0 )
{
pos = encoding::get_chars_count(m_Codepage, Bytes.data(), pos);
}
}
}
else // text2hex
{
char Buffer[128];
size_t Size;
int ps = 0, pd = 0, p0 = pos, p1 = -1;
for (size_t i = 0, FromSize = from.size(); i != FromSize; ++i)
{
if (ps == p0)
p1 = pd;
++ps;
wchar_t ch = from[i];

switch (m_Codepage)
{
case CP_UNICODE:
Size = 2; Buffer[0] = (char)LOBYTE(ch); Buffer[1] = (char)HIBYTE(ch);
break;
case CP_REVERSEBOM:
Size = 2; Buffer[0] = (char)HIBYTE(ch); Buffer[1] = (char)LOBYTE(ch);
break;
default:
Size = encoding::get_bytes(m_Codepage, &ch, 1, Buffer, 4);
break;
}

to += BlobToHexWString(Buffer, Size, 0);
pd += static_cast<int>(Size) * 3;
}
pos = p1;
}
}

struct Viewer::search_data
{
INT64 CurPos;
Expand Down Expand Up @@ -3423,7 +3362,7 @@ void Viewer::Search(int Next,int FirstChar)

if (SearchHex)
{
strSearchStr = Dialog::ExtractHexString(SearchDlg[SD_EDIT_HEX].strData);
strSearchStr = ExtractHexString(SearchDlg[SD_EDIT_HEX].strData);
}
else
{
Expand Down Expand Up @@ -3731,9 +3670,16 @@ int Viewer::vread(wchar_t *Buf, int Count, wchar_t *Buf2)
{
int tail;
ReadSize = Utf8::get_chars(TmpBuf, ConvertSize, Buf, Count, tail);
std::copy_n(Buf, ReadSize, Buf2);

if (Buf2)
{
std::copy_n(Buf, ReadSize, Buf2);
}

if (tail)
{
Reader.Unread(tail);
}
}
else if (m_Codepage == MB.GetCP())
{
Expand Down
1 change: 0 additions & 1 deletion far/viewer.hpp
Expand Up @@ -91,7 +91,6 @@ class Viewer:public SimpleScreenObject
bool ProcessDisplayMode(VIEWER_MODE_TYPE newMode, bool isRedraw = true);
int ProcessWrapMode(int newMode, bool isRedraw = true);
int ProcessTypeWrapMode(int newMode, bool isRedraw = true);
void SearchTextTransform(string& to, const string& from, bool hex2text, intptr_t &pos);
int GetId(void) const { return ViewerID; }
void OnDestroy(void);

Expand Down

0 comments on commit 1380be3

Please sign in to comment.