Changeset 950


Ignore:
Timestamp:
Dec 4, 2009, 8:01:33 PM (12 years ago)
Author:
arnold
Message:

KeyPattern enriched with Serialize-Methods and other useful stuff

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrypPlugins/KeySearcher/KeyPattern.cs

    r939 r950  
    405405        }
    406406
     407        /*
     408         * ARNIES SANDKASTEN - ALLE FOLGENDEN METHODEN SIND FÜR DIE VERTEILTE VERWENDUNG
     409         * DES KEYPATTERNS NOTWENDIG ODER ABER EINFACH UM DAS KEYPATTERN SCHÖN ALS
     410         * GUILOGMESSAGE AUSGEBEN ZU KÖNNEN ;-)
     411         */
     412
    407413        //added by Christian Arnold - 2009.12.02
     414        /// <summary>
     415        /// returns type, key and pattern. If you want to get only the pattern for processing use GetPattern-method!
     416        /// </summary>
     417        /// <returns></returns>
    408418        public override string ToString()
    409419        {
    410420            return "Type: KeySearcher.KeyPattern. Key: '" + this.key + "', Pattern: '" + this.pattern + "'";
    411421        }
     422
     423        //added by Christian Arnold - 2009.12.03
     424        /// <summary>
     425        /// returns ONLY the pattern as a string!
     426        /// </summary>
     427        /// <returns></returns>
     428        public string GetPattern()
     429        {
     430            return this.pattern;
     431        }
     432
     433        #region Serialization methods and auxiliary variables
     434
     435        /// <summary>
     436        /// Used for serializing/deserializing the KeyPattern object. This string separates Key from Pattern. Example: [key]kpSeparator[pattern]
     437        /// </summary>
     438        private string kpSeperator = "#;#";
     439        private Encoding encoder = UTF8Encoding.UTF8;
     440
     441        /// <summary>
     442        /// Serialize all needful information to rebuild the existing pattern elsewhere
     443        /// </summary>
     444        /// <returns>string representation of all the needful information of the actual KeyPattern</returns>
     445        public string SerializeToString()
     446        {
     447            return (string)Serialize(false);
     448        }
     449
     450        /// <summary>
     451        /// Serialize all needful information to rebuild the existing pattern elsewhere
     452        /// </summary>
     453        /// <returns>byte representation of all the needful information of the actual KeyPattern</returns>
     454        public byte[] SerializeToByte()
     455        {
     456            return (byte[])Serialize(true);
     457        }
     458
     459        /// <summary>
     460        /// The whole serializing process.
     461        /// </summary>
     462        /// <param name="returnByte">Choose true, to get a KeyPattern serialized as an byte array, otherwise you will get a string.</param>
     463        /// <returns></returns>
     464        private object Serialize(bool returnByte)
     465        {
     466            object objReturn = null;
     467            if (this.key != null && this.pattern != null)
     468            {
     469                // TODO: implement testPattern-method
     470                //if (testPattern(pattern) && testKey(key))
     471                if (testKey(key))
     472                {
     473                    if (returnByte)
     474                        objReturn = encoder.GetBytes(key + kpSeperator + pattern);
     475                    else
     476                        objReturn = key + kpSeperator + pattern;
     477                }
     478                else
     479                {
     480                    throw (new Exception("Serializing KeyPattern canceled, because Key and/or Pattern aren't valid. "
     481                        + "Key: '" + key + "', Pattern: '" + pattern + "'.\n"));
     482                }
     483            }
     484            else
     485            {
     486                throw (new Exception("Serializing KeyPattern canceled, because Key and/or Pattern are NULL. Key: '" + key + "'. Pattern: '" + pattern + "'."));
     487            }
     488            return objReturn;
     489        }
     490
     491        private KeyPattern Deserialize(object represenatation)
     492        {
     493            KeyPattern keyPatternToReturn;
     494
     495            // casting stuff
     496            string sTemp = null;
     497            if (represenatation is byte[])
     498                sTemp = encoder.GetString(represenatation as byte[]);
     499            else if (represenatation is string)
     500                sTemp = represenatation as string;
     501            else
     502                throw (new Exception("Deserializing KeyPattern canceled, because parameter neither a byte array nor a string!"));
     503
     504            // disaggregate string representatiojn
     505            string key_temp = sTemp.Substring(0, sTemp.IndexOf(kpSeperator));
     506            int beginOfPattern = sTemp.IndexOf(kpSeperator) + kpSeperator.Length;
     507            string pattern_temp = sTemp.Substring(beginOfPattern, sTemp.Length - beginOfPattern);
     508
     509            // test extracted pattern and key!
     510            // TODO: implement testPattern-method
     511            //if (testPattern(pattern_temp) && testKey(key_temp))
     512            if (testKey(key_temp))
     513            {
     514                // TODO: use Pattern-property in future
     515                keyPatternToReturn = new KeyPattern(pattern_temp);
     516                // TODO: use Key-property in future
     517                keyPatternToReturn.initKeyIteration(key_temp);
     518                return keyPatternToReturn;
     519            }
     520            else
     521            {
     522                throw (new Exception("Deserializing KeyPattern canceled, because Key or Pattern aren't valid. "
     523                    + "Key: '" + key_temp + "', Pattern: '" + pattern_temp + "'.\n"));
     524            }
     525        }
     526
     527        public KeyPattern DeserializeFromString(string sKeyPattern)
     528        {
     529            return Deserialize(sKeyPattern);
     530        }
     531
     532        /// <summary>
     533        /// Deserialize a byte-representation of an KeyPattern object. Returns a full-initialized KeyPattern object.
     534        /// </summary>
     535        /// <param name="bKeyPattern">byte-representation of an keypattern object</param>
     536        /// <returns>a full-initialized KeyPattern object</returns>
     537        public KeyPattern DeserializeFromByte(byte[] bKeyPattern)
     538        {
     539            return Deserialize(bKeyPattern);
     540        }
     541
     542        #endregion
    412543
    413544    }
Note: See TracChangeset for help on using the changeset viewer.