Ignore:
Timestamp:
Aug 12, 2009, 9:49:16 PM (12 years ago)
Author:
Arno Wacker
Message:
  • Added octal-string decoding and encoding to the Stream-to-String/String-to-Stream converter plug-ins.
  • Updated the converter sample to show also octal-string encoding.

This closes #25.

Location:
trunk/CrypPlugins/StringToStreamConvertor
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrypPlugins/StringToStreamConvertor/StringToStreamConverter.cs

    r432 r433  
    397397                {
    398398                    case StringToStreamConverterSettings.EncodingTypes.Default:
    399                     // outputStream = new CryptoolStream();
    400399                        outputBytes = Encoding.Default.GetBytes(value);
    401400                        outputStream.OpenRead(this.GetPluginInfoAttribute().Caption, outputBytes);
     
    404403                        try
    405404                        {
    406                           // outputStream = new CryptoolStream();
    407405                            outputBytes = Convert.FromBase64String(value);
    408406                            outputStream.OpenRead(this.GetPluginInfoAttribute().Caption, outputBytes);
     
    418416                        try
    419417                        {
    420                             outputBytes = convertStringToByteArray(value);
     418                            outputBytes = convertHexStringToByteArray(value);
    421419                            outputStream.OpenRead(this.GetPluginInfoAttribute().Caption, outputBytes);
    422420                        }
     
    427425                        }
    428426                        break;
     427                    case StringToStreamConverterSettings.EncodingTypes.OctalStringBinary:
     428                        try
     429                        {
     430                            outputBytes = convertOctalStringToByteArray(value);
     431                            outputStream.OpenRead(this.GetPluginInfoAttribute().Caption, outputBytes);
     432                        }
     433                        catch (Exception ex)
     434                        {
     435                            ShowStatusBarMessage("Error converting input! Not a valid octal-string (" + ex.Message + ")", NotificationLevel.Error);
     436                            return;
     437                        }
     438                        break;
    429439                    case StringToStreamConverterSettings.EncodingTypes.Unicode:
    430                         // outputStream = new CryptoolStream();
    431440                        outputBytes = Encoding.Unicode.GetBytes(value);
    432441                        outputStream.OpenRead(this.GetPluginInfoAttribute().Caption, outputBytes);
    433442                        break;
    434443                    case StringToStreamConverterSettings.EncodingTypes.UTF7:
    435                         // outputStream = new CryptoolStream();
    436444                        outputBytes = Encoding.UTF7.GetBytes(value);
    437445                        outputStream.OpenRead(this.GetPluginInfoAttribute().Caption, outputBytes);
    438446                        break;
    439447                    case StringToStreamConverterSettings.EncodingTypes.UTF8:
    440                         // outputStream = new CryptoolStream();
    441448                        outputBytes = Encoding.UTF8.GetBytes(value);
    442449                        outputStream.OpenRead(this.GetPluginInfoAttribute().Caption, outputBytes);
    443450                        break;
    444451                    case StringToStreamConverterSettings.EncodingTypes.UTF32:
    445                         // outputStream = new CryptoolStream();
    446452                        outputBytes = Encoding.UTF32.GetBytes(value);
    447453                        outputStream.OpenRead(this.GetPluginInfoAttribute().Caption, outputBytes);
    448454                        break;
    449455                    case StringToStreamConverterSettings.EncodingTypes.ASCII:
    450                         // outputStream = new CryptoolStream();
    451456                        outputBytes = Encoding.ASCII.GetBytes(value);
    452457                        outputStream.OpenRead(this.GetPluginInfoAttribute().Caption, outputBytes);
    453458                        break;
    454459                    case StringToStreamConverterSettings.EncodingTypes.BigEndianUnicode:
    455                         // outputStream = new CryptoolStream();
    456460                        outputBytes = Encoding.BigEndianUnicode.GetBytes(value);
    457461                        outputStream.OpenRead(this.GetPluginInfoAttribute().Caption, outputBytes);
    458462                        break;
    459463                    default:
    460                         // outputStream = new CryptoolStream();
    461464                        outputBytes = Encoding.Default.GetBytes(value);
    462465                        outputStream.OpenRead(this.GetPluginInfoAttribute().Caption, outputBytes);
     
    481484
    482485
    483         private byte[] convertStringToByteArray(String hexString)
     486        private byte[] convertHexStringToByteArray(String hexString)
    484487        {
    485488            if (null == hexString)
     
    510513
    511514
     515        private byte[] convertOctalStringToByteArray(String octalString)
     516        {
     517            if (null == octalString)
     518                return new byte[0];
     519
     520            StringBuilder cleanOctalString = new StringBuilder();
     521
     522            //cleanup the input
     523            foreach (char c in octalString)
     524            {
     525                if (char.IsDigit(c) && c!='8' && c!='9')
     526                    cleanOctalString.Append(c);
     527            }
     528
     529            int numberChars = cleanOctalString.Length;
     530
     531            if (numberChars < 3) // Need at least 3 chars to make one byte
     532                return new byte[0];
     533
     534            byte[] bytes = new byte[numberChars / 3];
     535
     536            for (int i = 0; i < numberChars; i += 3)
     537            {
     538                bytes[i / 3] = Convert.ToByte(cleanOctalString.ToString().Substring(i, 3), 8);
     539            }
     540            return bytes;
     541        }
     542
    512543        private void ShowStatusBarMessage(string message, NotificationLevel logLevel)
    513544        {
  • trunk/CrypPlugins/StringToStreamConvertor/StringToStreamConverterSettings.cs

    r432 r433  
    215215        #region Public StringToStreamConverter specific properties
    216216
    217         public enum EncodingTypes { Default = 0, Base64Binary = 1, HexStringBinary = 2, Unicode = 3, UTF7 = 4, UTF8 = 5, UTF32 = 6, ASCII = 7, BigEndianUnicode = 8 };
     217        public enum EncodingTypes { Default = 0, Base64Binary = 1, HexStringBinary = 2, OctalStringBinary = 3, Unicode = 4, UTF7 = 5, UTF8 = 6, UTF32 = 7, ASCII = 8, BigEndianUnicode = 9 };
    218218
    219219        /// <summary>
     
    249249        /// Encoding property used in the Settings pane.
    250250        /// </summary>
    251         [ContextMenu("Output stream encoding", "Choose the encoding of the output stream. (Byte encoding)", 1, DisplayLevel.Experienced, ContextMenuControlType.ComboBox, null, new string[] { "Default", "Binary data (convert from base64)", "Binary data (convert from hex-string)", "Unicode", "UTF-7", "UTF-8", "UTF-32", "ASCII", "Big endian unicode" })]
    252         [TaskPane("Output stream encoding", "Choose the encoding of the output stream. (Byte encoding)", "", 1, false, DisplayLevel.Experienced, ControlType.ComboBox, new string[] { "Default", "Binary data (convert from base64)", "Binary data (convert from hex-string)", "Unicode", "UTF-7", "UTF-8", "UTF-32", "ASCII", "Big endian unicode" })]
     251        [ContextMenu("Output stream encoding", "Choose the encoding of the output stream. (Byte encoding)", 1, DisplayLevel.Experienced, ContextMenuControlType.ComboBox, null, new string[] { "Default", "Binary data (convert from base64)", "Binary data (convert from hex-string)", "Binary data (convert from octal-string)", "Unicode", "UTF-7", "UTF-8", "UTF-32", "ASCII", "Big endian unicode" })]
     252        [TaskPane("Output stream encoding", "Choose the encoding of the output stream. (Byte encoding)", "", 1, false, DisplayLevel.Experienced, ControlType.ComboBox, new string[] { "Default", "Binary data (convert from base64)", "Binary data (convert from hex-string)", "Binary data (convert from octal-string)", "Unicode", "UTF-7", "UTF-8", "UTF-32", "ASCII", "Big endian unicode" })]
    253253        public int EncodingSetting
    254254        {
Note: See TracChangeset for help on using the changeset viewer.