using System; using System.Collections.Generic; namespace Woodpecker.Specialized.Encoding { /// /// Provides Base64 encoding and decoding. /// public static class base64Encoding { /// /// Encodes an integer to a Base64 string. /// /// The integer to encode. public static string Encode(int i) { try { string s = ""; for (int x = 1; x <= 2; x++) s += (char)((byte)(64 + (i >> 6 * (2 - x) & 0x3f))); return s; } catch { return ""; } } /// /// Decodes a Base64 string to to an integer. /// /// The string to decode. public static int Decode(string s) { char[] val = s.ToCharArray(); try { int intTot = 0; int y = 0; for (int x = (val.Length - 1); x >= 0; x--) { int intTmp = (int)(byte)((val[x] - 64)); if (y > 0) intTmp = intTmp * (int)(Math.Pow(64,y)); intTot += intTmp; y++; } return intTot; } catch { return -1; } } /// /// Searches through a given string for the xxth parameter and returns it. If not found, then "" is returned. /// /// The string to search through. /// The parameter to look for, 0 will return the first parameter, 1 the second, 2 the third etc. public static string getParameter(string s, int parameterLocation) { try { int j = 0; while (s.Length > 0) { int i = Decode(s.Substring(0, 2)); if (j == parameterLocation) return s.Substring(2, i); s = s.Substring(i + 2); j++; } } catch { } return ""; } /// /// Searches through a given string for a parameter with a ID and returns it. If not found, then "" is returned. /// /// The string to search through. /// The ID of the parameter to get. public static string getStructuredParameter(string s, int paramID) { try { int Cycles = 0; float maxCyles = s.Length / 4; while (Cycles <= maxCyles) { int cID = Decode(s.Substring(0, 2)); int cLength = Decode(s.Substring(2, 2)); if (cID == paramID) return s.Substring(4, cLength); s = s.Substring(cLength + 4); } } catch { } return ""; } /// /// Gets all parameters from a string encoded with Base64 headers, and returns it as a string array. /// /// The content to get the parameters off. public static string[] getParameters(string messageContent) { List res = new List(); try { while (messageContent.Length > 0) { int v = Decode(messageContent.Substring(0, 2)); res.Add(messageContent.Substring(2, v)); messageContent = messageContent.Substring(2 + v); } } catch { } return res.ToArray(); } } }