/// <summary> /// See this[] /// </summary> /// <param name="index">The index</param> /// <returns>The sub key pattern</returns> private KeyPattern GetAtIndex(BigInteger index) { //calculate the wildcard positions on which we want to split: int[] splittingPositions = new int[pattern.wildcardList.Count]; for (int k = pattern.wildcardList.Count - 1; k >= 0; k--) { splittingPositions[k] = (int)(index % splittingQuotient[k]); index /= splittingQuotient[k]; } Debug.Assert(index == 0); //split up the sub pattern parts: KeyPattern subpart = new KeyPattern(pattern.GetPattern()); subpart.wildcardList = new ArrayList(); for (int k = 0; k < pattern.wildcardList.Count; k++) { Wildcard subwc = ((Wildcard)pattern.wildcardList[k]); char[] values = new char[256]; int sublength = subwc.size() / splittingQuotient[k]; for (int i = 0; i < sublength; i++) { values[i] = subwc.getChar(i + splittingPositions[k] * sublength); } Wildcard newwc = new Wildcard(values, sublength); subpart.wildcardList.Add(newwc); } return(subpart); }
/// <summary> /// Returns the key pattern representation for the merged subkeys from index "from" to index "to". /// </summary> /// <returns></returns> public string GetPatternRangeRepresentation(BigInteger from, BigInteger to) { KeyPattern mergedPattern = new KeyPattern(pattern.GetPattern()); mergedPattern.wildcardList = new ArrayList(); var rangeLength = to - from + 1; for (int i = 0; i < pattern.wildcardList.Count; i++) { //Add all chars of wildcard i from all subpattern in ranche together: HashSet <char> charSet = new HashSet <char>(); for (BigInteger c = from; c <= to; c++) { var wc = (Wildcard)(this[c].wildcardList[i]); for (int x = 0; x < wc.getLength(); x++) { charSet.Add(wc.getChars()[x]); } } //Add merged chars to merged pattern: var mergedChars = new char[charSet.Count]; charSet.CopyTo(mergedChars); mergedPattern.wildcardList.Add(new Wildcard(mergedChars, charSet.Count)); } return(mergedPattern.WildcardKey); }
/// <summary> /// returns the "movements" of the key, i.e. how each relevant wildcard has to be "rotated" to produce the next key. /// </summary> /// <returns></returns> public IKeyMovement[] getKeyMovements() { KeyPattern fullKeyPattern = new KeyPattern(pattern); fullKeyPattern.WildcardKey = pattern; int arraySize = 0; foreach (Wildcard wc in this.wildcardList) { if (wc.getLength() > 1) { arraySize++; } } IKeyMovement[] result = new IKeyMovement[arraySize]; int c = 0; for (int i = 0; i < wildcardList.Count; i++) { if (((Wildcard)this.wildcardList[i]).getLength() > 1) { result[c] = getWildcardMovement((Wildcard)this.wildcardList[i], (Wildcard)fullKeyPattern.wildcardList[i]); c++; } } return(result); }
/// <summary> /// Deserialize a byte-representation of an KeyPattern object. Returns a full-initialized KeyPattern object. /// </summary> /// <param name="serializedPattern">byte-representation of an keypattern object</param> /// <returns>a full-initialized KeyPattern object</returns> public KeyPattern Deserialize(byte[] serializedPattern) { KeyPattern keyPatternToReturn; string wildcardKey_temp; string pattern_temp; MemoryStream memStream = new MemoryStream(serializedPattern); try { /* So i always have the same byte length for int32 values */ int iTest = 500; int int32ByteLen = BitConverter.GetBytes(iTest).Length; // Wildcard length and value byte[] byteLen = new byte[int32ByteLen]; memStream.Read(byteLen, 0, byteLen.Length); byte[] byteWildcard = new byte[BitConverter.ToInt32(byteLen, 0)]; memStream.Read(byteWildcard, 0, byteWildcard.Length); wildcardKey_temp = encoder.GetString(byteWildcard, 0, byteWildcard.Length); // Pattern length and value memStream.Read(byteLen, 0, byteLen.Length); byte[] bytePattern = new byte[BitConverter.ToInt32(byteLen, 0)]; memStream.Read(bytePattern, 0, bytePattern.Length); pattern_temp = encoder.GetString(bytePattern, 0, bytePattern.Length); } catch (Exception ex) { throw ex; } finally { memStream.Flush(); memStream.Close(); memStream.Dispose(); } //int iWildCardLen = serializedPattern[0]; //wildcardKey_temp = encoder.GetString(serializedPattern, 1, iWildCardLen); //int iPatternLen = serializedPattern[iWildCardLen + 1]; //pattern_temp = encoder.GetString(serializedPattern, iWildCardLen + 2, iPatternLen); keyPatternToReturn = new KeyPattern(pattern_temp); // test extracted pattern and wildcardKey! if (keyPatternToReturn.testWildcardKey(wildcardKey_temp)) { keyPatternToReturn.WildcardKey = wildcardKey_temp; return(keyPatternToReturn); } else { throw (new Exception("Deserializing KeyPattern canceled, because WildcardKey or Pattern aren't valid. " + "WildcardKey: '" + wildcardKey_temp + "', Pattern: '" + pattern_temp + "'.\n")); } }
public KeyPatternPool(KeyPattern pattern, BigInteger partsize) { this.partsize = partsize; this.pattern = pattern; splittingQuotient = new int[pattern.wildcardList.Count]; CalculateSplitting(); splittingCounter = new int[pattern.wildcardList.Count]; }
public void Push(KeyPattern pattern) { counter--; if (!Contains(pattern)) { stack.Push(pattern); } else { throw new Exception("Pattern already given."); } }
public KeyPattern(byte[] serializedPattern) { KeyPattern deserializedPattern = Deserialize(serializedPattern); // set deserialized Pattern to actual pattern this.pattern = deserializedPattern.pattern; this.WildcardKey = deserializedPattern.WildcardKey; //this.wildcardList = deserializedPattern.wildcardList; if (deserializedPattern == null) { throw new Exception("Invalid byte[] representation of KeyPattern!"); } }
public bool Contains(KeyPattern pattern) { if (pattern.wildcardList.Count != this.pattern.wildcardList.Count) { return(false); } if (pattern.GetPattern() != this.pattern.GetPattern()) { return(false); } bool equal = true; for (int k = 0; k < pattern.wildcardList.Count; k++) { Wildcard wc = ((Wildcard)pattern.wildcardList[k]); Wildcard thiswc = ((Wildcard)this.pattern.wildcardList[k]); if (wc.size() != (thiswc.size() / splittingQuotient[k])) { return(false); } bool bolContains2 = true; int begin = equal ? splittingCounter[k] : 0; for (int j = begin; j < splittingQuotient[k]; j++) { bool bolContains = true; for (int i = 0; i < wc.size(); i++) { if (wc.getChar(i - wc.count()) != thiswc.getChar(i + j * wc.size())) { bolContains = false; break; } } if (bolContains) { equal = (j == splittingCounter[k]); bolContains2 = true; break; } } if (!bolContains2) { return(false); } } return(!equal); }
public KeyPattern Pop() { if (stack.Count != 0) { counter++; return((KeyPattern)stack.Pop()); } if (end) { return(null); } KeyPattern part = new KeyPattern(pattern.GetPattern()); part.wildcardList = new ArrayList(); for (int k = 0; k < pattern.wildcardList.Count; k++) { Wildcard wc = ((Wildcard)pattern.wildcardList[k]); char[] values = new char[256]; int length = wc.size() / splittingQuotient[k]; for (int i = 0; i < length; i++) { values[i] = wc.getChar(i + splittingCounter[k] * length); } Wildcard newwc = new Wildcard(values, length); part.wildcardList.Add(newwc); } if (!SuccCounter()) { end = true; } counter++; return(part); }
public KeyPattern[] split() { KeyPattern[] patterns = new KeyPattern[2]; for (int i = 0; i < 2; i++) { patterns[i] = new KeyPattern(pattern); patterns[i].wildcardList = new ArrayList(); } bool s = false; for (int i = 0; i < wildcardList.Count; i++) { Wildcard wc = ((Wildcard)wildcardList[i]); if (!s && (wc.size() - wc.count()) > 1) { Wildcard[] wcs = wc.split(); patterns[0].wildcardList.Add(wcs[0]); patterns[1].wildcardList.Add(wcs[1]); s = true; } else { patterns[0].wildcardList.Add(new Wildcard(wc)); Wildcard copy = new Wildcard(wc); if (s) { copy.resetCounter(); } patterns[1].wildcardList.Add(copy); } } if (!s) { return(null); } return(patterns); }
public bool Contains(byte[] serializedJob) { KeyPattern deserializedPattern = new KeyPattern(serializedJob); return(Contains(deserializedPattern)); }