1 /*
2   KeePass Password Safe - The Open-Source Password Manager
3   Copyright (C) 2003-2021 Dominik Reichl <dominik.reichl@t-online.de>
4 
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 2 of the License, or
8   (at your option) any later version.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19 
20 using System;
21 using System.Collections.Generic;
22 using System.ComponentModel;
23 using System.Diagnostics;
24 using System.Drawing;
25 using System.Text;
26 using System.Windows.Forms;
27 
28 using KeePass.App;
29 using KeePass.Resources;
30 using KeePass.UI;
31 using KeePass.Util;
32 
33 using KeePassLib.Utility;
34 
35 namespace KeePass.Forms
36 {
37 	public partial class TextEncodingForm : Form
38 	{
39 		private string m_strContext = string.Empty;
40 		private byte[] m_pbData = null;
41 		private bool m_bInitializing = false;
42 		private Encoding m_encSel = null;
43 		private uint m_uStartOffset = 0;
44 
45 		public Encoding SelectedEncoding
46 		{
47 			get { return m_encSel; }
48 		}
49 
50 		public uint DataStartOffset
51 		{
52 			get { return m_uStartOffset; }
53 		}
54 
InitEx(string strContext, byte[] pbData)55 		public void InitEx(string strContext, byte[] pbData)
56 		{
57 			m_strContext = (strContext ?? string.Empty);
58 			m_pbData = pbData;
59 		}
60 
TextEncodingForm()61 		public TextEncodingForm()
62 		{
63 			InitializeComponent();
64 			GlobalWindowManager.InitializeForm(this);
65 		}
66 
OnFormLoad(object sender, EventArgs e)67 		private void OnFormLoad(object sender, EventArgs e)
68 		{
69 			GlobalWindowManager.AddWindow(this);
70 
71 			m_bInitializing = true;
72 			this.Icon = AppIcons.Default;
73 
74 			FontUtil.AssignDefaultBold(m_lblContext);
75 			Debug.Assert(!m_lblContext.AutoSize); // For RTL support
76 			m_lblContext.Text = m_strContext;
77 
78 			m_cmbEnc.Items.Add(KPRes.BinaryNoConv);
79 			foreach(StrEncodingInfo sei in StrUtil.Encodings)
80 				m_cmbEnc.Items.Add(sei.Name);
81 
82 			StrEncodingInfo seiGuess = BinaryDataClassifier.GetStringEncoding(
83 				m_pbData, out m_uStartOffset);
84 
85 			int iSel = 0;
86 			if(seiGuess != null)
87 				iSel = Math.Max(m_cmbEnc.FindStringExact(seiGuess.Name), 0);
88 			m_cmbEnc.SelectedIndex = iSel;
89 
90 			m_bInitializing = false;
91 			UpdateTextPreview();
92 		}
93 
OnFormClosed(object sender, FormClosedEventArgs e)94 		private void OnFormClosed(object sender, FormClosedEventArgs e)
95 		{
96 			GlobalWindowManager.RemoveWindow(this);
97 		}
98 
GetSelEnc()99 		private Encoding GetSelEnc()
100 		{
101 			StrEncodingInfo sei = StrUtil.GetEncoding(m_cmbEnc.Text);
102 			return ((sei != null) ? sei.Encoding : null);
103 		}
104 
UpdateTextPreview()105 		private void UpdateTextPreview()
106 		{
107 			if(m_bInitializing) return;
108 
109 			m_rtbPreview.Clear(); // Clear formatting
110 			try
111 			{
112 				Encoding enc = GetSelEnc();
113 				if(enc == null) throw new InvalidOperationException();
114 
115 				string str = (enc.GetString(m_pbData, (int)m_uStartOffset,
116 					m_pbData.Length - (int)m_uStartOffset) ?? string.Empty);
117 				m_rtbPreview.Text = StrUtil.ReplaceNulls(str);
118 			}
119 			catch(Exception) { m_rtbPreview.Text = string.Empty; }
120 		}
121 
OnEncSelectedIndexChanged(object sender, EventArgs e)122 		private void OnEncSelectedIndexChanged(object sender, EventArgs e)
123 		{
124 			UpdateTextPreview();
125 		}
126 
OnBtnOK(object sender, EventArgs e)127 		private void OnBtnOK(object sender, EventArgs e)
128 		{
129 			m_encSel = GetSelEnc();
130 		}
131 
OnBtnCancel(object sender, EventArgs e)132 		private void OnBtnCancel(object sender, EventArgs e)
133 		{
134 		}
135 	}
136 }
137