Showing the changes to Source/Libraries/GSF.Unity/GSF.Core/ByteEncoding.cs (1 of 2684 files). Show all
Unified · Split
| @@ -1,767 +1,767 @@ | |||
| 1 | //****************************************************************************************************** | 1 | //****************************************************************************************************** |
| 2 | // ByteEncoding.cs - Gbtc | 2 | // ByteEncoding.cs - Gbtc |
| 3 | // | 3 | // |
| 4 | // Copyright � 2010, Grid Protection Alliance. All Rights Reserved. | 4 | // Copyright � 2010, Grid Protection Alliance. All Rights Reserved. |
| 5 | // | 5 | // |
| 6 | // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See | 6 | // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See |
| 7 | // the NOTICE file distributed with this work for additional information regarding copyright ownership. | 7 | // the NOTICE file distributed with this work for additional information regarding copyright ownership. |
| 8 | // The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may | 8 | // The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may |
| 9 | // not use this file except in compliance with the License. You may obtain a copy of the License at: | 9 | // not use this file except in compliance with the License. You may obtain a copy of the License at: |
| 10 | // | 10 | // |
| 11 | // http://www.opensource.org/licenses/eclipse-1.0.php | 11 | // http://www.opensource.org/licenses/eclipse-1.0.php |
| 12 | // | 12 | // |
| 13 | // Unless agreed to in writing, the subject software distributed under the License is distributed on an | 13 | // Unless agreed to in writing, the subject software distributed under the License is distributed on an |
| 14 | // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the | 14 | // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the |
| 15 | // License for the specific language governing permissions and limitations. | 15 | // License for the specific language governing permissions and limitations. |
| 16 | // | 16 | // |
| 17 | // Code Modification History: | 17 | // Code Modification History: |
| 18 | // ---------------------------------------------------------------------------------------------------- | 18 | // ---------------------------------------------------------------------------------------------------- |
| 19 | // 01/25/2008 - J. Ritchie Carroll | 19 | // 01/25/2008 - J. Ritchie Carroll |
| 20 | // Initial version of source generated. | 20 | // Initial version of source generated. |
| 21 | // 09/14/2009 - Stephen C. Wills | 21 | // 09/14/2009 - Stephen C. Wills |
| 22 | // Added new header and license agreement. | 22 | // Added new header and license agreement. |
| 23 | // 10/8/2012 - Danyelle Gilliam | 23 | // 10/8/2012 - Danyelle Gilliam |
| 24 | // Modified Header | 24 | // Modified Header |
| 25 | // | 25 | // |
| 26 | //****************************************************************************************************** | 26 | //****************************************************************************************************** |
| 27 | 27 | ||
| 28 | 28 | ||
| 29 | 29 | ||
| 30 | 30 | ||
| 31 | #region [ Contributor License Agreements ] | 31 | #region [ Contributor License Agreements ] |
| 32 | 32 | ||
| 33 | /**************************************************************************\ | 33 | /**************************************************************************\ |
| 34 | Copyright � 2009 - J. Ritchie Carroll | 34 | Copyright � 2009 - J. Ritchie Carroll |
| 35 | All rights reserved. | 35 | All rights reserved. |
| 36 | 36 | ||
| 37 | Redistribution and use in source and binary forms, with or without | 37 | Redistribution and use in source and binary forms, with or without |
| 38 | modification, are permitted provided that the following conditions | 38 | modification, are permitted provided that the following conditions |
| 39 | are met: | 39 | are met: |
| 40 | 40 | ||
| 41 | * Redistributions of source code must retain the above copyright | 41 | * Redistributions of source code must retain the above copyright |
| 42 | notice, this list of conditions and the following disclaimer. | 42 | notice, this list of conditions and the following disclaimer. |
| 43 | 43 | ||
| 44 | * Redistributions in binary form must reproduce the above | 44 | * Redistributions in binary form must reproduce the above |
| 45 | copyright notice, this list of conditions and the following | 45 | copyright notice, this list of conditions and the following |
| 46 | disclaimer in the documentation and/or other materials provided | 46 | disclaimer in the documentation and/or other materials provided |
| 47 | with the distribution. | 47 | with the distribution. |
| 48 | 48 | ||
| 49 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY | 49 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY |
| 50 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 50 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 51 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 51 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 52 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | 52 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 53 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 53 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 54 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 54 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 55 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 55 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 56 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 56 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 57 | OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 57 | OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 58 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 58 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 59 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 59 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 60 | 60 | ||
| 61 | \**************************************************************************/ | 61 | \**************************************************************************/ |
| 62 | 62 | ||
| 63 | #endregion | 63 | #endregion |
| 64 | 64 | ||
| 65 | using System; | 65 | using System; |
| 66 | using System.ComponentModel; | 66 | using System.ComponentModel; |
| 67 | using System.Text; | 67 | using System.Text; |
| 68 | using System.Text.RegularExpressions; | 68 | using System.Text.RegularExpressions; |
| 69 | 69 | ||
| 70 | namespace GSF | 70 | namespace GSF |
| 71 | { | 71 | { |
| 72 | /// <summary> | 72 | /// <summary> |
| 73 | /// Defines a set of methods used to convert byte buffers to and from user presentable data formats. | 73 | /// Defines a set of methods used to convert byte buffers to and from user presentable data formats. |
| 74 | /// </summary> | 74 | /// </summary> |
| 75 | public abstract class ByteEncoding | 75 | public abstract class ByteEncoding |
| 76 | { | 76 | { |
| 77 | #region [ Members ] | 77 | #region [ Members ] |
| 78 | 78 | ||
| 79 | // Nested Types | 79 | // Nested Types |
| 80 | #region [ Hexadecimal Encoding Class ] | 80 | #region [ Hexadecimal Encoding Class ] |
| 81 | 81 | ||
| 82 | /// <summary> | 82 | /// <summary> |
| 83 | /// Handles conversion of byte buffers to and from a hexadecimal data format. | 83 | /// Handles conversion of byte buffers to and from a hexadecimal data format. |
| 84 | /// </summary> | 84 | /// </summary> |
| 85 | [EditorBrowsable(EditorBrowsableState.Never)] | 85 | [EditorBrowsable(EditorBrowsableState.Never)] |
| 86 | public class HexadecimalEncoding : ByteEncoding | 86 | public class HexadecimalEncoding : ByteEncoding |
| 87 | { | 87 | { |
| 88 | internal HexadecimalEncoding() | 88 | internal HexadecimalEncoding() |
| 89 | { | 89 | { |
| 90 | // This class is meant for internal instatiation only. | 90 | // This class is meant for internal instatiation only. |
| 91 | } | 91 | } |
| 92 | 92 | ||
| 93 | /// <summary>Decodes given string back into a byte buffer.</summary> | 93 | /// <summary>Decodes given string back into a byte buffer.</summary> |
| 94 | /// <param name="hexData">Encoded hexadecimal data string to decode.</param> | 94 | /// <param name="hexData">Encoded hexadecimal data string to decode.</param> |
| 95 | /// <param name="spacingCharacter">Original spacing character that was inserted between encoded bytes.</param> | 95 | /// <param name="spacingCharacter">Original spacing character that was inserted between encoded bytes.</param> |
| 96 | /// <returns>Decoded bytes.</returns> | 96 | /// <returns>Decoded bytes.</returns> |
| 97 | public override byte[] GetBytes(string hexData, char spacingCharacter) | 97 | public override byte[] GetBytes(string hexData, char spacingCharacter) |
| 98 | { | 98 | { |
| 99 | if (!string.IsNullOrEmpty(hexData)) | 99 | if (!string.IsNullOrEmpty(hexData)) |
| 100 | { | 100 | { |
| 101 | // Removes spacing characters, if needed. | 101 | // Removes spacing characters, if needed. |
| 102 | hexData = hexData.Trim(); | 102 | hexData = hexData.Trim(); |
| 103 | if (spacingCharacter != NoSpacing) | 103 | if (spacingCharacter != NoSpacing) |
| 104 | hexData = hexData.Replace(spacingCharacter.ToString(), ""); | 104 | hexData = hexData.Replace(spacingCharacter.ToString(), ""); |
| 105 | 105 | ||
| 106 | // Processes the string only if it has data in hex format (Example: 48 65 6C 6C 21). | 106 | // Processes the string only if it has data in hex format (Example: 48 65 6C 6C 21). |
| 107 | if (Regex.Matches(hexData, "[^a-fA-F0-9]").Count == 0) | 107 | if (Regex.Matches(hexData, "[^a-fA-F0-9]").Count == 0) |
| 108 | { | 108 | { |
| 109 | // Trims the end of the string to discard any additional characters, if present in the string, | 109 | // Trims the end of the string to discard any additional characters, if present in the string, |
| 110 | // that would prevent the string from being a hex encoded string. | 110 | // that would prevent the string from being a hex encoded string. |
| 111 | // Note: Requires that each character be represented by its 2 character hex value. | 111 | // Note: Requires that each character be represented by its 2 character hex value. |
| 112 | hexData = hexData.Substring(0, hexData.Length - hexData.Length % 2); | 112 | hexData = hexData.Substring(0, hexData.Length - hexData.Length % 2); |
| 113 | 113 | ||
| 114 | byte[] bytes = new byte[hexData.Length / 2]; | 114 | byte[] bytes = new byte[hexData.Length / 2]; |
| 115 | int index = 0; | 115 | int index = 0; |
| 116 | 116 | ||
| 117 | for (int x = 0; x <= hexData.Length - 1; x += 2) | 117 | for (int x = 0; x <= hexData.Length - 1; x += 2) |
| 118 | { | 118 | { |
| 119 | bytes[index] = Convert.ToByte(hexData.Substring(x, 2), 16); | 119 | bytes[index] = Convert.ToByte(hexData.Substring(x, 2), 16); |
| 120 | index++; | 120 | index++; |
| 121 | } | 121 | } |
| 122 | 122 | ||
| 123 | return bytes; | 123 | return bytes; |
| 124 | } | 124 | } |
| 125 | else | 125 | else |
| 126 | { | 126 | { |
| 127 | throw new ArgumentException("Input string is not a valid hex encoded string - invalid characters encountered", "hexData"); | 127 | throw new ArgumentException("Input string is not a valid hex encoded string - invalid characters encountered", "hexData"); |
| 128 | } | 128 | } |
| 129 | } | 129 | } |
| 130 | else | 130 | else |
| 131 | { | 131 | { |
| 132 | throw new ArgumentNullException("hexData", "Input string cannot be null or empty"); | 132 | throw new ArgumentNullException("hexData", "Input string cannot be null or empty"); |
| 133 | } | 133 | } |
| 134 | } | 134 | } |
| 135 | 135 | ||
| 136 | /// <summary>Encodes given buffer into a user presentable representation.</summary> | 136 | /// <summary>Encodes given buffer into a user presentable representation.</summary> |
| 137 | /// <param name="bytes">Bytes to encode.</param> | 137 | /// <param name="bytes">Bytes to encode.</param> |
| 138 | /// <param name="offset">Offset into buffer to begin encoding.</param> | 138 | /// <param name="offset">Offset into buffer to begin encoding.</param> |
| 139 | /// <param name="length">Length of buffer to encode.</param> | 139 | /// <param name="length">Length of buffer to encode.</param> |
| 140 | /// <param name="spacingCharacter">Spacing character to place between encoded bytes.</param> | 140 | /// <param name="spacingCharacter">Spacing character to place between encoded bytes.</param> |
| 141 | /// <returns>String of encoded bytes.</returns> | 141 | /// <returns>String of encoded bytes.</returns> |
| 142 | public override string GetString(byte[] bytes, int offset, int length, char spacingCharacter) | 142 | public override string GetString(byte[] bytes, int offset, int length, char spacingCharacter) |
| 143 | { | 143 | { |
| 144 | return BytesToString(bytes, offset, length, spacingCharacter, "X2"); | 144 | return BytesToString(bytes, offset, length, spacingCharacter, "X2"); |
| 145 | } | 145 | } |
| 146 | } | 146 | } |
| 147 | 147 | ||
| 148 | #endregion | 148 | #endregion |
| 149 | 149 | ||
| 150 | #region [ Decimal Encoding Class ] | 150 | #region [ Decimal Encoding Class ] |
| 151 | 151 | ||
| 152 | /// <summary> | 152 | /// <summary> |
| 153 | /// Handles conversion of byte buffers to and from a decimal data format. | 153 | /// Handles conversion of byte buffers to and from a decimal data format. |
| 154 | /// </summary> | 154 | /// </summary> |
| 155 | [EditorBrowsable(EditorBrowsableState.Never)] | 155 | [EditorBrowsable(EditorBrowsableState.Never)] |
| 156 | public class DecimalEncoding : ByteEncoding | 156 | public class DecimalEncoding : ByteEncoding |
| 157 | { | 157 | { |
| 158 | internal DecimalEncoding() | 158 | internal DecimalEncoding() |
| 159 | { | 159 | { |
| 160 | // This class is meant for internal instatiation only. | 160 | // This class is meant for internal instatiation only. |
| 161 | } | 161 | } |
| 162 | 162 | ||
| 163 | /// <summary>Decodes given string back into a byte buffer.</summary> | 163 | /// <summary>Decodes given string back into a byte buffer.</summary> |
| 164 | /// <param name="decData">Encoded decimal data string to decode.</param> | 164 | /// <param name="decData">Encoded decimal data string to decode.</param> |
| 165 | /// <param name="spacingCharacter">Original spacing character that was inserted between encoded bytes.</param> | 165 | /// <param name="spacingCharacter">Original spacing character that was inserted between encoded bytes.</param> |
| 166 | /// <returns>Decoded bytes.</returns> | 166 | /// <returns>Decoded bytes.</returns> |
| 167 | public override byte[] GetBytes(string decData, char spacingCharacter) | 167 | public override byte[] GetBytes(string decData, char spacingCharacter) |
| 168 | { | 168 | { |
| 169 | if (!string.IsNullOrEmpty(decData)) | 169 | if (!string.IsNullOrEmpty(decData)) |
| 170 | { | 170 | { |
| 171 | // Removes spacing characters, if needed. | 171 | // Removes spacing characters, if needed. |
| 172 | decData = decData.Trim(); | 172 | decData = decData.Trim(); |
| 173 | if (spacingCharacter != NoSpacing) | 173 | if (spacingCharacter != NoSpacing) |
| 174 | decData = decData.Replace(spacingCharacter.ToString(), ""); | 174 | decData = decData.Replace(spacingCharacter.ToString(), ""); |
| 175 | 175 | ||
| 176 | // Processes the string only if it has data in decimal format (Example: 072 101 108 108 033). | 176 | // Processes the string only if it has data in decimal format (Example: 072 101 108 108 033). |
| 177 | if (Regex.Matches(decData, "[^0-9]").Count == 0) | 177 | if (Regex.Matches(decData, "[^0-9]").Count == 0) |
| 178 | { | 178 | { |
| 179 | // Trims the end of the string to discard any additional characters, if present in the | 179 | // Trims the end of the string to discard any additional characters, if present in the |
| 180 | // string, that would prevent the string from being an integer encoded string. | 180 | // string, that would prevent the string from being an integer encoded string. |
| 181 | // Note: Requires that each character be represented by its 3 character decimal value. | 181 | // Note: Requires that each character be represented by its 3 character decimal value. |
| 182 | decData = decData.Substring(0, decData.Length - decData.Length % 3); | 182 | decData = decData.Substring(0, decData.Length - decData.Length % 3); |
| 183 | 183 | ||
| 184 | byte[] bytes = new byte[decData.Length / 3]; | 184 | byte[] bytes = new byte[decData.Length / 3]; |
| 185 | int index = 0; | 185 | int index = 0; |
| 186 | 186 | ||
| 187 | for (int x = 0; x <= decData.Length - 1; x += 3) | 187 | for (int x = 0; x <= decData.Length - 1; x += 3) |
| 188 | { | 188 | { |
| 189 | bytes[index] = Convert.ToByte(decData.Substring(x, 3), 10); | 189 | bytes[index] = Convert.ToByte(decData.Substring(x, 3), 10); |
| 190 | index++; | 190 | index++; |
| 191 | } | 191 | } |
| 192 | 192 | ||
| 193 | return bytes; | 193 | return bytes; |
| 194 | } | 194 | } |
| 195 | else | 195 | else |
| 196 | { | 196 | { |
| 197 | throw new ArgumentException("Input string is not a valid decimal encoded string - invalid characters encountered", "decData"); | 197 | throw new ArgumentException("Input string is not a valid decimal encoded string - invalid characters encountered", "decData"); |
| 198 | } | 198 | } |
| 199 | } | 199 | } |
| 200 | else | 200 | else |
| 201 | { | 201 | { |
| 202 | throw new ArgumentNullException("decData", "Input string cannot be null or empty"); | 202 | throw new ArgumentNullException("decData", "Input string cannot be null or empty"); |
| 203 | } | 203 | } |
| 204 | } | 204 | } |
| 205 | 205 | ||
| 206 | /// <summary>Encodes given buffer into a user presentable representation.</summary> | 206 | /// <summary>Encodes given buffer into a user presentable representation.</summary> |
| 207 | /// <param name="bytes">Bytes to encode.</param> | 207 | /// <param name="bytes">Bytes to encode.</param> |
| 208 | /// <param name="offset">Offset into buffer to begin encoding.</param> | 208 | /// <param name="offset">Offset into buffer to begin encoding.</param> |
| 209 | /// <param name="length">Length of buffer to encode.</param> | 209 | /// <param name="length">Length of buffer to encode.</param> |
| 210 | /// <param name="spacingCharacter">Spacing character to place between encoded bytes.</param> | 210 | /// <param name="spacingCharacter">Spacing character to place between encoded bytes.</param> |
| 211 | /// <returns>String of encoded bytes.</returns> | 211 | /// <returns>String of encoded bytes.</returns> |
| 212 | public override string GetString(byte[] bytes, int offset, int length, char spacingCharacter) | 212 | public override string GetString(byte[] bytes, int offset, int length, char spacingCharacter) |
| 213 | { | 213 | { |
| 214 | return BytesToString(bytes, offset, length, spacingCharacter, "D3"); | 214 | return BytesToString(bytes, offset, length, spacingCharacter, "D3"); |
| 215 | } | 215 | } |
| 216 | } | 216 | } |
| 217 | 217 | ||
| 218 | #endregion | 218 | #endregion |
| 219 | 219 | ||
| 220 | #region [ Binary Encoding Class ] | 220 | #region [ Binary Encoding Class ] |
| 221 | 221 | ||
| 222 | /// <summary> | 222 | /// <summary> |
| 223 | /// Handles conversion of byte buffers to and from a binary (i.e., 0 and 1's) data format. | 223 | /// Handles conversion of byte buffers to and from a binary (i.e., 0 and 1's) data format. |
| 224 | /// </summary> | 224 | /// </summary> |
| 225 | [EditorBrowsable(EditorBrowsableState.Never)] | 225 | [EditorBrowsable(EditorBrowsableState.Never)] |
| 226 | public class BinaryEncoding : ByteEncoding | 226 | public class BinaryEncoding : ByteEncoding |
| 227 | { | 227 | { |
| 228 | private string[] m_byteImages; | 228 | private string[] m_byteImages; |
| 229 | private bool m_reverse; | 229 | private bool m_reverse; |
| 230 | 230 | ||
| 231 | // This class is meant for internal instantiation only. | 231 | // This class is meant for internal instantiation only. |
| 232 | internal BinaryEncoding(Endianness targetEndianness) | 232 | internal BinaryEncoding(Endianness targetEndianness) |
| 233 | { | 233 | { |
| 234 | if (targetEndianness == Endianness.BigEndian) | 234 | if (targetEndianness == Endianness.BigEndian) |
| 235 | { | 235 | { |
| 236 | if (BitConverter.IsLittleEndian) | 236 | if (BitConverter.IsLittleEndian) |
| 237 | { | 237 | { |
| 238 | // If OS is little endian and we want big endian, this reverses the bit order. | 238 | // If OS is little endian and we want big endian, this reverses the bit order. |
| 239 | m_reverse = true; | 239 | m_reverse = true; |
| 240 | } | 240 | } |
| 241 | else | 241 | else |
| 242 | { | 242 | { |
| 243 | // If OS is big endian and we want big endian, this keeps the OS bit order. | 243 | // If OS is big endian and we want big endian, this keeps the OS bit order. |
| 244 | m_reverse = false; | 244 | m_reverse = false; |
| 245 | } | 245 | } |
| 246 | } | 246 | } |
| 247 | else | 247 | else |
| 248 | { | 248 | { |
| 249 | if (BitConverter.IsLittleEndian) | 249 | if (BitConverter.IsLittleEndian) |
| 250 | { | 250 | { |
| 251 | // If OS is little endian and we want little endian, this keeps OS bit order. | 251 | // If OS is little endian and we want little endian, this keeps OS bit order. |
| 252 | m_reverse = false; | 252 | m_reverse = false; |
| 253 | } | 253 | } |
| 254 | else | 254 | else |
| 255 | { | 255 | { |
| 256 | // If OS is big endian and we want little endian, this reverses the bit order. | 256 | // If OS is big endian and we want little endian, this reverses the bit order. |
| 257 | m_reverse = true; | 257 | m_reverse = true; |
| 258 | } | 258 | } |
| 259 | } | 259 | } |
| 260 | } | 260 | } |
| 261 | 261 | ||
| 262 | /// <summary>Decodes given string back into a byte buffer.</summary> | 262 | /// <summary>Decodes given string back into a byte buffer.</summary> |
| 263 | /// <param name="binaryData">Encoded binary data string to decode.</param> | 263 | /// <param name="binaryData">Encoded binary data string to decode.</param> |
| 264 | /// <param name="spacingCharacter">Original spacing character that was inserted between encoded bytes.</param> | 264 | /// <param name="spacingCharacter">Original spacing character that was inserted between encoded bytes.</param> |
| 265 | /// <returns>Decoded bytes.</returns> | 265 | /// <returns>Decoded bytes.</returns> |
| 266 | public override byte[] GetBytes(string binaryData, char spacingCharacter) | 266 | public override byte[] GetBytes(string binaryData, char spacingCharacter) |
| 267 | { | 267 | { |
| 268 | if (!string.IsNullOrEmpty(binaryData)) | 268 | if (!string.IsNullOrEmpty(binaryData)) |
| 269 | { | 269 | { |
| 270 | // Removes spacing characters, if needed. | 270 | // Removes spacing characters, if needed. |
| 271 | binaryData = binaryData.Trim(); | 271 | binaryData = binaryData.Trim(); |
| 272 | if (spacingCharacter != NoSpacing) | 272 | if (spacingCharacter != NoSpacing) |
| 273 | binaryData = binaryData.Replace(spacingCharacter.ToString(), ""); | 273 | binaryData = binaryData.Replace(spacingCharacter.ToString(), ""); |
| 274 | 274 | ||
| 275 | // Processes the string only if it has data in binary format (Example: 01010110 1010101). | 275 | // Processes the string only if it has data in binary format (Example: 01010110 1010101). |
| 276 | if (Regex.Matches(binaryData, "[^0-1]").Count == 0) | 276 | if (Regex.Matches(binaryData, "[^0-1]").Count == 0) |
| 277 | { | 277 | { |
| 278 | // Trims the end of the string to discard any additional characters, if present in the | 278 | // Trims the end of the string to discard any additional characters, if present in the |
| 279 | // string, that would prevent the string from being a binary encoded string. | 279 | // string, that would prevent the string from being a binary encoded string. |
| 280 | // Note: Requires each character be represented by its 8 character binary value. | 280 | // Note: Requires each character be represented by its 8 character binary value. |
| 281 | binaryData = binaryData.Substring(0, binaryData.Length - binaryData.Length % 8); | 281 | binaryData = binaryData.Substring(0, binaryData.Length - binaryData.Length % 8); |
| 282 | 282 | ||
| 283 | byte[] bytes = new byte[binaryData.Length / 8]; | 283 | byte[] bytes = new byte[binaryData.Length / 8]; |
| 284 | int index = 0; | 284 | int index = 0; |
| 285 | 285 | ||
| 286 | for (int x = 0; x <= binaryData.Length - 1; x += 8) | 286 | for (int x = 0; x <= binaryData.Length - 1; x += 8) |
| 287 | { | 287 | { |
| 288 | bytes[index] = (byte)Bits.Nil; | 288 | bytes[index] = (byte)Bits.Nil; |
| 289 | 289 | ||
| 290 | if (m_reverse) | 290 | if (m_reverse) |
| 291 | { | 291 | { |
| 292 | if (binaryData[x + 7] == '1') | 292 | if (binaryData[x + 7] == '1') |
| 293 | bytes[index] = bytes[index].SetBits(Bits.Bit00); | 293 | bytes[index] = bytes[index].SetBits(Bits.Bit00); |
| 294 | if (binaryData[x + 6] == '1') | 294 | if (binaryData[x + 6] == '1') |
| 295 | bytes[index] = bytes[index].SetBits(Bits.Bit01); | 295 | bytes[index] = bytes[index].SetBits(Bits.Bit01); |
| 296 | if (binaryData[x + 5] == '1') | 296 | if (binaryData[x + 5] == '1') |
| 297 | bytes[index] = bytes[index].SetBits(Bits.Bit02); | 297 | bytes[index] = bytes[index].SetBits(Bits.Bit02); |
| 298 | if (binaryData[x + 4] == '1') | 298 | if (binaryData[x + 4] == '1') |
| 299 | bytes[index] = bytes[index].SetBits(Bits.Bit03); | 299 | bytes[index] = bytes[index].SetBits(Bits.Bit03); |
| 300 | if (binaryData[x + 3] == '1') | 300 | if (binaryData[x + 3] == '1') |
| 301 | bytes[index] = bytes[index].SetBits(Bits.Bit04); | 301 | bytes[index] = bytes[index].SetBits(Bits.Bit04); |
| 302 | if (binaryData[x + 2] == '1') | 302 | if (binaryData[x + 2] == '1') |
| 303 | bytes[index] = bytes[index].SetBits(Bits.Bit05); | 303 | bytes[index] = bytes[index].SetBits(Bits.Bit05); |
| 304 | if (binaryData[x + 1] == '1') | 304 | if (binaryData[x + 1] == '1') |
| 305 | bytes[index] = bytes[index].SetBits(Bits.Bit06); | 305 | bytes[index] = bytes[index].SetBits(Bits.Bit06); |
| 306 | if (binaryData[x + 0] == '1') | 306 | if (binaryData[x + 0] == '1') |
| 307 | bytes[index] = bytes[index].SetBits(Bits.Bit07); | 307 | bytes[index] = bytes[index].SetBits(Bits.Bit07); |
| 308 | } | 308 | } |
| 309 | else | 309 | else |
| 310 | { | 310 | { |
| 311 | if (binaryData[x + 0] == '1') | 311 | if (binaryData[x + 0] == '1') |
| 312 | bytes[index] = bytes[index].SetBits(Bits.Bit00); | 312 | bytes[index] = bytes[index].SetBits(Bits.Bit00); |
| 313 | if (binaryData[x + 1] == '1') | 313 | if (binaryData[x + 1] == '1') |
| 314 | bytes[index] = bytes[index].SetBits(Bits.Bit01); | 314 | bytes[index] = bytes[index].SetBits(Bits.Bit01); |
| 315 | if (binaryData[x + 2] == '1') | 315 | if (binaryData[x + 2] == '1') |
| 316 | bytes[index] = bytes[index].SetBits(Bits.Bit02); | 316 | bytes[index] = bytes[index].SetBits(Bits.Bit02); |
| 317 | if (binaryData[x + 3] == '1') | 317 | if (binaryData[x + 3] == '1') |
| 318 | bytes[index] = bytes[index].SetBits(Bits.Bit03); | 318 | bytes[index] = bytes[index].SetBits(Bits.Bit03); |
| 319 | if (binaryData[x + 4] == '1') | 319 | if (binaryData[x + 4] == '1') |
| 320 | bytes[index] = bytes[index].SetBits(Bits.Bit04); | 320 | bytes[index] = bytes[index].SetBits(Bits.Bit04); |
| 321 | if (binaryData[x + 5] == '1') | 321 | if (binaryData[x + 5] == '1') |
| 322 | bytes[index] = bytes[index].SetBits(Bits.Bit05); | 322 | bytes[index] = bytes[index].SetBits(Bits.Bit05); |
| 323 | if (binaryData[x + 6] == '1') | 323 | if (binaryData[x + 6] == '1') |
| 324 | bytes[index] = bytes[index].SetBits(Bits.Bit06); | 324 | bytes[index] = bytes[index].SetBits(Bits.Bit06); |
| 325 | if (binaryData[x + 7] == '1') | 325 | if (binaryData[x + 7] == '1') |
| 326 | bytes[index] = bytes[index].SetBits(Bits.Bit07); | 326 | bytes[index] = bytes[index].SetBits(Bits.Bit07); |
| 327 | } | 327 | } |
| 328 | 328 | ||
| 329 | index++; | 329 | index++; |
| 330 | } | 330 | } |
| 331 | 331 | ||
| 332 | return bytes; | 332 | return bytes; |
| 333 | } | 333 | } |
| 334 | else | 334 | else |
| 335 | { | 335 | { |
| 336 | throw new ArgumentException("Input string is not a valid binary encoded string - invalid characters encountered", "binaryData"); | 336 | throw new ArgumentException("Input string is not a valid binary encoded string - invalid characters encountered", "binaryData"); |
| 337 | } | 337 | } |
| 338 | } | 338 | } |
| 339 | else | 339 | else |
| 340 | { | 340 | { |
| 341 | throw new ArgumentNullException("binaryData", "Input string cannot be null or empty"); | 341 | throw new ArgumentNullException("binaryData", "Input string cannot be null or empty"); |
| 342 | } | 342 | } |
| 343 | } | 343 | } |
| 344 | 344 | ||
| 345 | /// <summary>Encodes given buffer into a user presentable representation.</summary> | 345 | /// <summary>Encodes given buffer into a user presentable representation.</summary> |
| 346 | /// <param name="bytes">Bytes to encode.</param> | 346 | /// <param name="bytes">Bytes to encode.</param> |
| 347 | /// <param name="offset">Offset into buffer to begin encoding.</param> | 347 | /// <param name="offset">Offset into buffer to begin encoding.</param> |
| 348 | /// <param name="length">Length of buffer to encode.</param> | 348 | /// <param name="length">Length of buffer to encode.</param> |
| 349 | /// <param name="spacingCharacter">Spacing character to place between encoded bytes.</param> | 349 | /// <param name="spacingCharacter">Spacing character to place between encoded bytes.</param> |
| 350 | /// <returns>String of encoded bytes.</returns> | 350 | /// <returns>String of encoded bytes.</returns> |
| 351 | public override string GetString(byte[] bytes, int offset, int length, char spacingCharacter) | 351 | public override string GetString(byte[] bytes, int offset, int length, char spacingCharacter) |
| 352 | { | 352 | { |
| 353 | if ((object)bytes == null) | 353 | if ((object)bytes == null) |
| 354 | throw new ArgumentNullException("bytes", "Input buffer cannot be null"); | 354 | throw new ArgumentNullException("bytes", "Input buffer cannot be null"); |
| 355 | 355 | ||
| 356 | // Initializes byte image array on first call for speed in future calls. | 356 | // Initializes byte image array on first call for speed in future calls. |
| 357 | if ((object)m_byteImages == null) | 357 | if ((object)m_byteImages == null) |
| 358 | { | 358 | { |
| 359 | StringBuilder byteImage; | 359 | StringBuilder byteImage; |
| 360 | 360 | ||
| 361 | m_byteImages = new string[256]; | 361 | m_byteImages = new string[256]; |
| 362 | 362 | ||
| 363 | for (int imageByte = byte.MinValue; imageByte <= byte.MaxValue; imageByte++) | 363 | for (int imageByte = byte.MinValue; imageByte <= byte.MaxValue; imageByte++) |
| 364 | { | 364 | { |
| 365 | byteImage = new StringBuilder(); | 365 | byteImage = new StringBuilder(); |
| 366 | 366 | ||
| 367 | if (m_reverse) | 367 | if (m_reverse) |
| 368 | { | 368 | { |
| 369 | if (imageByte.CheckBits(Bits.Bit07)) | 369 | if (imageByte.CheckBits(Bits.Bit07)) |
| 370 | byteImage.Append('1'); | 370 | byteImage.Append('1'); |
| 371 | else | 371 | else |
| 372 | byteImage.Append('0'); | 372 | byteImage.Append('0'); |
| 373 | if (imageByte.CheckBits(Bits.Bit06)) | 373 | if (imageByte.CheckBits(Bits.Bit06)) |
| 374 | byteImage.Append('1'); | 374 | byteImage.Append('1'); |
| 375 | else | 375 | else |
| 376 | byteImage.Append('0'); | 376 | byteImage.Append('0'); |
| 377 | if (imageByte.CheckBits(Bits.Bit05)) | 377 | if (imageByte.CheckBits(Bits.Bit05)) |
| 378 | byteImage.Append('1'); | 378 | byteImage.Append('1'); |
| 379 | else | 379 | else |
| 380 | byteImage.Append('0'); | 380 | byteImage.Append('0'); |
| 381 | if (imageByte.CheckBits(Bits.Bit04)) | 381 | if (imageByte.CheckBits(Bits.Bit04)) |
| 382 | byteImage.Append('1'); | 382 | byteImage.Append('1'); |
| 383 | else | 383 | else |
| 384 | byteImage.Append('0'); | 384 | byteImage.Append('0'); |
| 385 | if (imageByte.CheckBits(Bits.Bit03)) | 385 | if (imageByte.CheckBits(Bits.Bit03)) |
| 386 | byteImage.Append('1'); | 386 | byteImage.Append('1'); |
| 387 | else | 387 | else |
| 388 | byteImage.Append('0'); | 388 | byteImage.Append('0'); |
| 389 | if (imageByte.CheckBits(Bits.Bit02)) | 389 | if (imageByte.CheckBits(Bits.Bit02)) |
| 390 | byteImage.Append('1'); | 390 | byteImage.Append('1'); |
| 391 | else | 391 | else |
| 392 | byteImage.Append('0'); | 392 | byteImage.Append('0'); |
| 393 | if (imageByte.CheckBits(Bits.Bit01)) | 393 | if (imageByte.CheckBits(Bits.Bit01)) |
| 394 | byteImage.Append('1'); | 394 | byteImage.Append('1'); |
| 395 | else | 395 | else |
| 396 | byteImage.Append('0'); | 396 | byteImage.Append('0'); |
| 397 | if (imageByte.CheckBits(Bits.Bit00)) | 397 | if (imageByte.CheckBits(Bits.Bit00)) |
| 398 | byteImage.Append('1'); | 398 | byteImage.Append('1'); |
| 399 | else | 399 | else |
| 400 | byteImage.Append('0'); | 400 | byteImage.Append('0'); |
| 401 | } | 401 | } |
| 402 | else | 402 | else |
| 403 | { | 403 | { |
| 404 | if (imageByte.CheckBits(Bits.Bit00)) | 404 | if (imageByte.CheckBits(Bits.Bit00)) |
| 405 | byteImage.Append('1'); | 405 | byteImage.Append('1'); |
| 406 | else | 406 | else |
| 407 | byteImage.Append('0'); | 407 | byteImage.Append('0'); |
| 408 | if (imageByte.CheckBits(Bits.Bit01)) | 408 | if (imageByte.CheckBits(Bits.Bit01)) |
| 409 | byteImage.Append('1'); | 409 | byteImage.Append('1'); |
| 410 | else | 410 | else |
| 411 | byteImage.Append('0'); | 411 | byteImage.Append('0'); |
| 412 | if (imageByte.CheckBits(Bits.Bit02)) | 412 | if (imageByte.CheckBits(Bits.Bit02)) |
| 413 | byteImage.Append('1'); | 413 | byteImage.Append('1'); |
| 414 | else | 414 | else |
| 415 | byteImage.Append('0'); | 415 | byteImage.Append('0'); |
| 416 | if (imageByte.CheckBits(Bits.Bit03)) | 416 | if (imageByte.CheckBits(Bits.Bit03)) |
| 417 | byteImage.Append('1'); | 417 | byteImage.Append('1'); |
| 418 | else | 418 | else |
| 419 | byteImage.Append('0'); | 419 | byteImage.Append('0'); |
| 420 | if (imageByte.CheckBits(Bits.Bit04)) | 420 | if (imageByte.CheckBits(Bits.Bit04)) |
| 421 | byteImage.Append('1'); | 421 | byteImage.Append('1'); |
| 422 | else | 422 | else |
| 423 | byteImage.Append('0'); | 423 | byteImage.Append('0'); |
| 424 | if (imageByte.CheckBits(Bits.Bit05)) | 424 | if (imageByte.CheckBits(Bits.Bit05)) |
| 425 | byteImage.Append('1'); | 425 | byteImage.Append('1'); |
| 426 | else | 426 | else |
| 427 | byteImage.Append('0'); | 427 | byteImage.Append('0'); |
| 428 | if (imageByte.CheckBits(Bits.Bit06)) | 428 | if (imageByte.CheckBits(Bits.Bit06)) |
| 429 | byteImage.Append('1'); | 429 | byteImage.Append('1'); |
| 430 | else | 430 | else |
| 431 | byteImage.Append('0'); | 431 | byteImage.Append('0'); |
| 432 | if (imageByte.CheckBits(Bits.Bit07)) | 432 | if (imageByte.CheckBits(Bits.Bit07)) |
| 433 | byteImage.Append('1'); | 433 | byteImage.Append('1'); |
| 434 | else | 434 | else |
| 435 | byteImage.Append('0'); | 435 | byteImage.Append('0'); |
| 436 | } | 436 | } |
| 437 | 437 | ||
| 438 | m_byteImages[imageByte] = byteImage.ToString(); | 438 | m_byteImages[imageByte] = byteImage.ToString(); |
| 439 | } | 439 | } |
| 440 | } | 440 | } |
| 441 | 441 | ||
| 442 | StringBuilder binaryImage = new StringBuilder(); | 442 | StringBuilder binaryImage = new StringBuilder(); |
| 443 | 443 | ||
| 444 | for (int x = 0; x < length; x++) | 444 | for (int x = 0; x < length; x++) |
| 445 | { | 445 | { |
| 446 | if (spacingCharacter != NoSpacing && x > 0) | 446 | if (spacingCharacter != NoSpacing && x > 0) |
| 447 | binaryImage.Append(spacingCharacter); | 447 | binaryImage.Append(spacingCharacter); |
| 448 | binaryImage.Append(m_byteImages[bytes[offset + x]]); | 448 | binaryImage.Append(m_byteImages[bytes[offset + x]]); |
| 449 | } | 449 | } |
| 450 | 450 | ||
| 451 | return binaryImage.ToString(); | 451 | return binaryImage.ToString(); |
| 452 | } | 452 | } |
| 453 | } | 453 | } |
| 454 | 454 | ||
| 455 | #endregion | 455 | #endregion |
| 456 | 456 | ||
| 457 | #region [ Base64 Encoding Class ] | 457 | #region [ Base64 Encoding Class ] |
| 458 | 458 | ||
| 459 | /// <summary> | 459 | /// <summary> |
| 460 | /// Handles conversion of byte buffers to and from a base64 data format. | 460 | /// Handles conversion of byte buffers to and from a base64 data format. |
| 461 | /// </summary> | 461 | /// </summary> |
| 462 | [EditorBrowsable(EditorBrowsableState.Never)] | 462 | [EditorBrowsable(EditorBrowsableState.Never)] |
| 463 | public class Base64Encoding : ByteEncoding | 463 | public class Base64Encoding : ByteEncoding |
| 464 | { | 464 | { |
| 465 | internal Base64Encoding() | 465 | internal Base64Encoding() |
| 466 | { | 466 | { |
| 467 | // This class is meant for internal instantiation only. | 467 | // This class is meant for internal instantiation only. |
| 468 | } | 468 | } |
| 469 | 469 | ||
| 470 | /// <summary>Decodes given string back into a byte buffer.</summary> | 470 | /// <summary>Decodes given string back into a byte buffer.</summary> |
| 471 | /// <param name="binaryData">Encoded binary data string to decode.</param> | 471 | /// <param name="binaryData">Encoded binary data string to decode.</param> |
| 472 | /// <param name="spacingCharacter">Original spacing character that was inserted between encoded bytes.</param> | 472 | /// <param name="spacingCharacter">Original spacing character that was inserted between encoded bytes.</param> |
| 473 | /// <returns>Decoded bytes.</returns> | 473 | /// <returns>Decoded bytes.</returns> |
| 474 | public override byte[] GetBytes(string binaryData, char spacingCharacter) | 474 | public override byte[] GetBytes(string binaryData, char spacingCharacter) |
| 475 | { | 475 | { |
| 476 | // Removes spacing characters, if needed. | 476 | // Removes spacing characters, if needed. |
| 477 | binaryData = binaryData.Trim(); | 477 | binaryData = binaryData.Trim(); |
| 478 | if (spacingCharacter != NoSpacing) | 478 | if (spacingCharacter != NoSpacing) |
| 479 | binaryData = binaryData.Replace(spacingCharacter.ToString(), ""); | 479 | binaryData = binaryData.Replace(spacingCharacter.ToString(), ""); |
| 480 | return Convert.FromBase64String(binaryData); | 480 | return Convert.FromBase64String(binaryData); |
| 481 | } | 481 | } |
| 482 | 482 | ||
| 483 | /// <summary>Encodes given buffer into a user presentable representation.</summary> | 483 | /// <summary>Encodes given buffer into a user presentable representation.</summary> |
| 484 | /// <param name="bytes">Bytes to encode.</param> | 484 | /// <param name="bytes">Bytes to encode.</param> |
| 485 | /// <param name="offset">Offset into buffer to begin encoding.</param> | 485 | /// <param name="offset">Offset into buffer to begin encoding.</param> |
| 486 | /// <param name="length">Length of buffer to encode.</param> | 486 | /// <param name="length">Length of buffer to encode.</param> |
| 487 | /// <param name="spacingCharacter">Spacing character to place between encoded bytes.</param> | 487 | /// <param name="spacingCharacter">Spacing character to place between encoded bytes.</param> |
| 488 | /// <returns>String of encoded bytes.</returns> | 488 | /// <returns>String of encoded bytes.</returns> |
| 489 | public override string GetString(byte[] bytes, int offset, int length, char spacingCharacter) | 489 | public override string GetString(byte[] bytes, int offset, int length, char spacingCharacter) |
| 490 | { | 490 | { |
| 491 | if ((object)bytes == null) | 491 | if ((object)bytes == null) |
| 492 | throw new ArgumentNullException("bytes", "Input buffer cannot be null"); | 492 | throw new ArgumentNullException("bytes", "Input buffer cannot be null"); |
| 493 | 493 | ||
| 494 | string base64String = Convert.ToBase64String(bytes, offset, length); | 494 | string base64String = Convert.ToBase64String(bytes, offset, length); |
| 495 | 495 | ||
| 496 | if (spacingCharacter == NoSpacing) | 496 | if (spacingCharacter == NoSpacing) |
| 497 | { | 497 | { |
| 498 | return base64String; | 498 | return base64String; |
| 499 | } | 499 | } |
| 500 | else | 500 | else |
| 501 | { | 501 | { |
| 502 | StringBuilder base64Image = new StringBuilder(); | 502 | StringBuilder base64Image = new StringBuilder(); |
| 503 | 503 | ||
| 504 | for (int x = 0; x <= base64String.Length - 1; x++) | 504 | for (int x = 0; x <= base64String.Length - 1; x++) |
| 505 | { | 505 | { |
| 506 | if (x > 0) | 506 | if (x > 0) |
| 507 | base64Image.Append(spacingCharacter); | 507 | base64Image.Append(spacingCharacter); |
| 508 | base64Image.Append(base64String[x]); | 508 | base64Image.Append(base64String[x]); |
| 509 | } | 509 | } |
| 510 | 510 | ||
| 511 | return base64Image.ToString(); | 511 | return base64Image.ToString(); |
| 512 | } | 512 | } |
| 513 | } | 513 | } |
| 514 | } | 514 | } |
| 515 | 515 | ||
| 516 | #endregion | 516 | #endregion |
| 517 | 517 | ||
| 518 | #region [ ASCII Encoding Class ] | 518 | #region [ ASCII Encoding Class ] |
| 519 | 519 | ||
| 520 | /// <summary> | 520 | /// <summary> |
| 521 | /// Handles conversion of byte buffers to and from a ASCII data format. | 521 | /// Handles conversion of byte buffers to and from a ASCII data format. |
| 522 | /// </summary> | 522 | /// </summary> |
| 523 | [EditorBrowsable(EditorBrowsableState.Never)] | 523 | [EditorBrowsable(EditorBrowsableState.Never)] |
| 524 | public class ASCIIEncoding : ByteEncoding | 524 | public class ASCIIEncoding : ByteEncoding |
| 525 | { | 525 | { |
| 526 | internal ASCIIEncoding() | 526 | internal ASCIIEncoding() |
| 527 | { | 527 | { |
| 528 | // This class is meant for internal instantiation only. | 528 | // This class is meant for internal instantiation only. |
| 529 | } | 529 | } |
| 530 | 530 | ||
| 531 | /// <summary>Decodes given string back into a byte buffer.</summary> | 531 | /// <summary>Decodes given string back into a byte buffer.</summary> |
| 532 | /// <param name="binaryData">Encoded binary data string to decode.</param> | 532 | /// <param name="binaryData">Encoded binary data string to decode.</param> |
| 533 | /// <param name="spacingCharacter">Original spacing character that was inserted between encoded bytes.</param> | 533 | /// <param name="spacingCharacter">Original spacing character that was inserted between encoded bytes.</param> |
| 534 | /// <returns>Decoded bytes.</returns> | 534 | /// <returns>Decoded bytes.</returns> |
| 535 | public override byte[] GetBytes(string binaryData, char spacingCharacter) | 535 | public override byte[] GetBytes(string binaryData, char spacingCharacter) |
| 536 | { | 536 | { |
| 537 | // Removes spacing characters, if needed. | 537 | // Removes spacing characters, if needed. |
| 538 | binaryData = binaryData.Trim(); | 538 | binaryData = binaryData.Trim(); |
| 539 | if (spacingCharacter != NoSpacing) | 539 | if (spacingCharacter != NoSpacing) |
| 540 | binaryData = binaryData.Replace(spacingCharacter.ToString(), ""); | 540 | binaryData = binaryData.Replace(spacingCharacter.ToString(), ""); |
| 541 | return Encoding.ASCII.GetBytes(binaryData); | 541 | return Encoding.ASCII.GetBytes(binaryData); |
| 542 | } | 542 | } |
| 543 | 543 | ||
| 544 | /// <summary>Encodes given buffer into a user presentable representation.</summary> | 544 | /// <summary>Encodes given buffer into a user presentable representation.</summary> |
| 545 | /// <param name="bytes">Bytes to encode.</param> | 545 | /// <param name="bytes">Bytes to encode.</param> |
| 546 | /// <param name="offset">Offset into buffer to begin encoding.</param> | 546 | /// <param name="offset">Offset into buffer to begin encoding.</param> |
| 547 | /// <param name="length">Length of buffer to encode.</param> | 547 | /// <param name="length">Length of buffer to encode.</param> |
| 548 | /// <param name="spacingCharacter">Spacing character to place between encoded bytes.</param> | 548 | /// <param name="spacingCharacter">Spacing character to place between encoded bytes.</param> |
| 549 | /// <returns>String of encoded bytes.</returns> | 549 | /// <returns>String of encoded bytes.</returns> |
| 550 | public override string GetString(byte[] bytes, int offset, int length, char spacingCharacter) | 550 | public override string GetString(byte[] bytes, int offset, int length, char spacingCharacter) |
| 551 | { | 551 | { |
| 552 | if ((object)bytes == null) | 552 | if ((object)bytes == null) |
| 553 | throw new ArgumentNullException("bytes", "Input buffer cannot be null"); | 553 | throw new ArgumentNullException("bytes", "Input buffer cannot be null"); |
| 554 | 554 | ||
| 555 | string asciiString = Encoding.ASCII.GetString(bytes, offset, length); | 555 | string asciiString = Encoding.ASCII.GetString(bytes, offset, length); |
| 556 | 556 | ||
| 557 | if (spacingCharacter == NoSpacing) | 557 | if (spacingCharacter == NoSpacing) |
| 558 | { | 558 | { |
| 559 | return asciiString; | 559 | return asciiString; |
| 560 | } | 560 | } |
| 561 | else | 561 | else |
| 562 | { | 562 | { |
| 563 | StringBuilder asciiImage = new StringBuilder(); | 563 | StringBuilder asciiImage = new StringBuilder(); |
| 564 | 564 | ||
| 565 | for (int x = 0; x <= asciiString.Length - 1; x++) | 565 | for (int x = 0; x <= asciiString.Length - 1; x++) |
| 566 | { | 566 | { |
| 567 | if (x > 0) | 567 | if (x > 0) |
| 568 | asciiImage.Append(spacingCharacter); | 568 | asciiImage.Append(spacingCharacter); |
| 569 | asciiImage.Append(asciiString[x]); | 569 | asciiImage.Append(asciiString[x]); |
| 570 | } | 570 | } |
| 571 | 571 | ||
| 572 | return asciiImage.ToString(); | 572 | return asciiImage.ToString(); |
| 573 | } | 573 | } |
| 574 | } | 574 | } |
| 575 | } | 575 | } |
| 576 | 576 | ||
| 577 | #endregion | 577 | #endregion |
| 578 | 578 | ||
| 579 | /// <summary> | 579 | /// <summary> |
| 580 | /// Constant used to specify that "no spacing" should be used for data conversion. | 580 | /// Constant used to specify that "no spacing" should be used for data conversion. |
| 581 | /// </summary> | 581 | /// </summary> |
| 582 | public const char NoSpacing = char.MinValue; | 582 | public const char NoSpacing = char.MinValue; |
| 583 | 583 | ||
| 584 | #endregion | 584 | #endregion |
| 585 | 585 | ||
| 586 | #region [ Methods ] | 586 | #region [ Methods ] |
| 587 | 587 | ||
| 588 | /// <summary>Encodes given buffer into a user presentable representation.</summary> | 588 | /// <summary>Encodes given buffer into a user presentable representation.</summary> |
| 589 | /// <param name="bytes">Bytes to encode.</param> | 589 | /// <param name="bytes">Bytes to encode.</param> |
| 590 | /// <returns>String representation of byte array.</returns> | 590 | /// <returns>String representation of byte array.</returns> |
| 591 | public virtual string GetString(byte[] bytes) | 591 | public virtual string GetString(byte[] bytes) |
| 592 | { | 592 | { |
| 593 | return GetString(bytes, NoSpacing); | 593 | return GetString(bytes, NoSpacing); |
| 594 | } | 594 | } |
| 595 | 595 | ||
| 596 | /// <summary>Encodes given buffer into a user presentable representation.</summary> | 596 | /// <summary>Encodes given buffer into a user presentable representation.</summary> |
| 597 | /// <param name="bytes">Bytes to encode.</param> | 597 | /// <param name="bytes">Bytes to encode.</param> |
| 598 | /// <param name="spacingCharacter">Spacing character to place between encoded bytes.</param> | 598 | /// <param name="spacingCharacter">Spacing character to place between encoded bytes.</param> |
| 599 | /// <returns>String of encoded bytes.</returns> | 599 | /// <returns>String of encoded bytes.</returns> |
| 600 | public virtual string GetString(byte[] bytes, char spacingCharacter) | 600 | public virtual string GetString(byte[] bytes, char spacingCharacter) |
| 601 | { | 601 | { |
| 602 | if ((object)bytes == null) | 602 | if ((object)bytes == null) |
| 603 | throw new ArgumentNullException("bytes", "Input buffer cannot be null"); | 603 | throw new ArgumentNullException("bytes", "Input buffer cannot be null"); |
| 604 | 604 | ||
| 605 | return GetString(bytes, 0, bytes.Length, spacingCharacter); | 605 | return GetString(bytes, 0, bytes.Length, spacingCharacter); |
| 606 | } | 606 | } |
| 607 | 607 | ||
| 608 | /// <summary>Encodes given buffer into a user presentable representation.</summary> | 608 | /// <summary>Encodes given buffer into a user presentable representation.</summary> |
| 609 | /// <param name="bytes">Bytes to encode.</param> | 609 | /// <param name="bytes">Bytes to encode.</param> |
| 610 | /// <param name="offset">Offset into buffer to begin encoding.</param> | 610 | /// <param name="offset">Offset into buffer to begin encoding.</param> |
| 611 | /// <param name="length">Length of buffer to encode.</param> | 611 | /// <param name="length">Length of buffer to encode.</param> |
| 612 | /// <returns>String of encoded bytes.</returns> | 612 | /// <returns>String of encoded bytes.</returns> |
| 613 | public virtual string GetString(byte[] bytes, int offset, int length) | 613 | public virtual string GetString(byte[] bytes, int offset, int length) |
| 614 | { | 614 | { |
| 615 | if ((object)bytes == null) | 615 | if ((object)bytes == null) |
| 616 | throw new ArgumentNullException("bytes", "Input buffer cannot be null"); | 616 | throw new ArgumentNullException("bytes", "Input buffer cannot be null"); |
| 617 | 617 | ||
| 618 | return GetString(bytes, offset, length, NoSpacing); | 618 | return GetString(bytes, offset, length, NoSpacing); |
| 619 | } | 619 | } |
| 620 | 620 | ||
| 621 | /// <summary>Encodes given buffer into a user presentable representation.</summary> | 621 | /// <summary>Encodes given buffer into a user presentable representation.</summary> |
| 622 | /// <param name="bytes">Bytes to encode.</param> | 622 | /// <param name="bytes">Bytes to encode.</param> |
| 623 | /// <param name="offset">Offset into buffer to begin encoding.</param> | 623 | /// <param name="offset">Offset into buffer to begin encoding.</param> |
| 624 | /// <param name="length">Length of buffer to encode.</param> | 624 | /// <param name="length">Length of buffer to encode.</param> |
| 625 | /// <param name="spacingCharacter">Spacing character to place between encoded bytes.</param> | 625 | /// <param name="spacingCharacter">Spacing character to place between encoded bytes.</param> |
| 626 | /// <returns>String of encoded bytes.</returns> | 626 | /// <returns>String of encoded bytes.</returns> |
| 627 | public abstract string GetString(byte[] bytes, int offset, int length, char spacingCharacter); | 627 | public abstract string GetString(byte[] bytes, int offset, int length, char spacingCharacter); |
| 628 | 628 | ||
| 629 | /// <summary>Decodes given string back into a byte buffer.</summary> | 629 | /// <summary>Decodes given string back into a byte buffer.</summary> |
| 630 | /// <param name="value">Encoded string to decode.</param> | 630 | /// <param name="value">Encoded string to decode.</param> |
| 631 | /// <returns>Decoded bytes.</returns> | 631 | /// <returns>Decoded bytes.</returns> |
| 632 | public virtual byte[] GetBytes(string value) | 632 | public virtual byte[] GetBytes(string value) |
| 633 | { | 633 | { |
| 634 | if (string.IsNullOrEmpty(value)) | 634 | if (string.IsNullOrEmpty(value)) |
| 635 | throw new ArgumentNullException("value", "Input string cannot be null"); | 635 | throw new ArgumentNullException("value", "Input string cannot be null"); |
| 636 | return GetBytes(value, NoSpacing); | 636 | return GetBytes(value, NoSpacing); |
| 637 | } | 637 | } |
| 638 | 638 | ||
| 639 | /// <summary>Decodes given string back into a byte buffer.</summary> | 639 | /// <summary>Decodes given string back into a byte buffer.</summary> |
| 640 | /// <param name="value">Encoded string to decode.</param> | 640 | /// <param name="value">Encoded string to decode.</param> |
| 641 | /// <param name="spacingCharacter">Original spacing character that was inserted between encoded bytes.</param> | 641 | /// <param name="spacingCharacter">Original spacing character that was inserted between encoded bytes.</param> |
| 642 | /// <returns>Decoded bytes</returns> | 642 | /// <returns>Decoded bytes</returns> |
| 643 | public abstract byte[] GetBytes(string value, char spacingCharacter); | 643 | public abstract byte[] GetBytes(string value, char spacingCharacter); |
| 644 | 644 | ||
| 645 | #endregion | 645 | #endregion |
| 646 | 646 | ||
| 647 | #region [ Static ] | 647 | #region [ Static ] |
| 648 | 648 | ||
| 649 | // Static Fields | 649 | // Static Fields |
| 650 | private static ByteEncoding s_hexadecimalEncoding; | 650 | private static ByteEncoding s_hexadecimalEncoding; |
| 651 | private static ByteEncoding s_decimalEncoding; | 651 | private static ByteEncoding s_decimalEncoding; |
| 652 | private static ByteEncoding s_bigEndianBinaryEncoding; | 652 | private static ByteEncoding s_bigEndianBinaryEncoding; |
| 653 | private static ByteEncoding s_littleEndianBinaryEncoding; | 653 | private static ByteEncoding s_littleEndianBinaryEncoding; |
| 654 | private static ByteEncoding s_base64Encoding; | 654 | private static ByteEncoding s_base64Encoding; |
| 655 | private static ByteEncoding s_asciiEncoding; | 655 | private static ByteEncoding s_asciiEncoding; |
| 656 | 656 | ||
| 657 | /// <summary>Handles encoding and decoding of a byte buffer into a hexadecimal-based presentation format.</summary> | 657 | /// <summary>Handles encoding and decoding of a byte buffer into a hexadecimal-based presentation format.</summary> |
| 658 | public static ByteEncoding Hexadecimal | 658 | public static ByteEncoding Hexadecimal |
| 659 | { | 659 | { |
| 660 | get | 660 | get |
| 661 | { | 661 | { |
| 662 | if ((object)s_hexadecimalEncoding == null) | 662 | if ((object)s_hexadecimalEncoding == null) |
| 663 | s_hexadecimalEncoding = new HexadecimalEncoding(); | 663 | s_hexadecimalEncoding = new HexadecimalEncoding(); |
| 664 | 664 | ||
| 665 | return s_hexadecimalEncoding; | 665 | return s_hexadecimalEncoding; |
| 666 | } | 666 | } |
| 667 | } | 667 | } |
| 668 | 668 | ||
| 669 | /// <summary>Handles encoding and decoding of a byte buffer into an integer-based presentation format.</summary> | 669 | /// <summary>Handles encoding and decoding of a byte buffer into an integer-based presentation format.</summary> |
| 670 | public static ByteEncoding Decimal | 670 | public static ByteEncoding Decimal |
| 671 | { | 671 | { |
| 672 | get | 672 | get |
| 673 | { | 673 | { |
| 674 | if ((object)s_decimalEncoding == null) | 674 | if ((object)s_decimalEncoding == null) |
| 675 | s_decimalEncoding = new DecimalEncoding(); | 675 | s_decimalEncoding = new DecimalEncoding(); |
| 676 | 676 | ||
| 677 | return s_decimalEncoding; | 677 | return s_decimalEncoding; |
| 678 | } | 678 | } |
| 679 | } | 679 | } |
| 680 | 680 | ||
| 681 | /// <summary>Handles encoding and decoding of a byte buffer into a big-endian binary (i.e., 0 and 1's) based | 681 | /// <summary>Handles encoding and decoding of a byte buffer into a big-endian binary (i.e., 0 and 1's) based |
| 682 | /// presentation format.</summary> | 682 | /// presentation format.</summary> |
| 683 | /// <remarks> | 683 | /// <remarks> |
| 684 | /// Although endianness is typically used in the context of byte order (see <see cref="GSF.EndianOrder"/> to handle byte | 684 | /// Although endianness is typically used in the context of byte order (see <see cref="GSF.EndianOrder"/> to handle byte |
| 685 | /// order swapping), this property allows you visualize "bits" in big-endian order, right-to-left. Note that bits are | 685 | /// order swapping), this property allows you visualize "bits" in big-endian order, right-to-left. Note that bits are |
| 686 | /// normally stored in the same order as their bytes.). | 686 | /// normally stored in the same order as their bytes.). |
| 687 | /// </remarks> | 687 | /// </remarks> |
| 688 | public static ByteEncoding BigEndianBinary | 688 | public static ByteEncoding BigEndianBinary |
| 689 | { | 689 | { |
| 690 | get | 690 | get |
| 691 | { | 691 | { |
| 692 | if ((object)s_bigEndianBinaryEncoding == null) | 692 | if ((object)s_bigEndianBinaryEncoding == null) |
| 693 | s_bigEndianBinaryEncoding = new BinaryEncoding(Endianness.BigEndian); | 693 | s_bigEndianBinaryEncoding = new BinaryEncoding(Endianness.BigEndian); |
| 694 | 694 | ||
| 695 | return s_bigEndianBinaryEncoding; | 695 | return s_bigEndianBinaryEncoding; |
| 696 | } | 696 | } |
| 697 | } | 697 | } |
| 698 | 698 | ||
| 699 | /// <summary>Handles encoding and decoding of a byte buffer into a little-endian binary (i.e., 0 and 1's) based | 699 | /// <summary>Handles encoding and decoding of a byte buffer into a little-endian binary (i.e., 0 and 1's) based |
| 700 | /// presentation format.</summary> | 700 | /// presentation format.</summary> |
| 701 | /// <remarks> | 701 | /// <remarks> |
| 702 | /// Although endianness is typically used in the context of byte order (see <see cref="GSF.EndianOrder"/> to handle byte | 702 | /// Although endianness is typically used in the context of byte order (see <see cref="GSF.EndianOrder"/> to handle byte |
| 703 | /// order swapping), this property allows you visualize "bits" in little-endian order, left-to-right. Note that bits are | 703 | /// order swapping), this property allows you visualize "bits" in little-endian order, left-to-right. Note that bits are |
| 704 | /// normally stored in the same order as their bytes. | 704 | /// normally stored in the same order as their bytes. |
| 705 | /// </remarks> | 705 | /// </remarks> |
| 706 | public static ByteEncoding LittleEndianBinary | 706 | public static ByteEncoding LittleEndianBinary |
| 707 | { | 707 | { |
| 708 | get | 708 | get |
| 709 | { | 709 | { |
| 710 | if ((object)s_littleEndianBinaryEncoding == null) | 710 | if ((object)s_littleEndianBinaryEncoding == null) |
| 711 | s_littleEndianBinaryEncoding = new BinaryEncoding(Endianness.LittleEndian); | 711 | s_littleEndianBinaryEncoding = new BinaryEncoding(Endianness.LittleEndian); |
| 712 | 712 | ||
| 713 | return s_littleEndianBinaryEncoding; | 713 | return s_littleEndianBinaryEncoding; |
| 714 | } | 714 | } |
| 715 | } | 715 | } |
| 716 | 716 | ||
| 717 | /// <summary>Handles encoding and decoding of a byte buffer into a base64 presentation format.</summary> | 717 | /// <summary>Handles encoding and decoding of a byte buffer into a base64 presentation format.</summary> |
| 718 | public static ByteEncoding Base64 | 718 | public static ByteEncoding Base64 |
| 719 | { | 719 | { |
| 720 | get | 720 | get |
| 721 | { | 721 | { |
| 722 | if ((object)s_base64Encoding == null) | 722 | if ((object)s_base64Encoding == null) |
| 723 | s_base64Encoding = new Base64Encoding(); | 723 | s_base64Encoding = new Base64Encoding(); |
| 724 | 724 | ||
| 725 | return s_base64Encoding; | 725 | return s_base64Encoding; |
| 726 | } | 726 | } |
| 727 | } | 727 | } |
| 728 | 728 | ||
| 729 | /// <summary>Handles encoding and decoding of a byte buffer into an ASCII character presentation format.</summary> | 729 | /// <summary>Handles encoding and decoding of a byte buffer into an ASCII character presentation format.</summary> |
| 730 | public static ByteEncoding ASCII | 730 | public static ByteEncoding ASCII |
| 731 | { | 731 | { |
| 732 | get | 732 | get |
| 733 | { | 733 | { |
| 734 | if ((object)s_asciiEncoding == null) | 734 | if ((object)s_asciiEncoding == null) |
| 735 | s_asciiEncoding = new ASCIIEncoding(); | 735 | s_asciiEncoding = new ASCIIEncoding(); |
| 736 | 736 | ||
| 737 | return s_asciiEncoding; | 737 | return s_asciiEncoding; |
| 738 | } | 738 | } |
| 739 | } | 739 | } |
| 740 | 740 | ||
| 741 | /// <summary>Handles byte to string conversions for implementations that are available from Byte.ToString.</summary> | 741 | /// <summary>Handles byte to string conversions for implementations that are available from Byte.ToString.</summary> |
| 742 | /// <param name="bytes">Encoded string to decode.</param> | 742 | /// <param name="bytes">Encoded string to decode.</param> |
| 743 | /// <param name="offset">Offset into byte array to begin decoding straing at.</param> | 743 | /// <param name="offset">Offset into byte array to begin decoding straing at.</param> |
| 744 | /// <param name="length">Number of bytes to decode starting at <paramref name="offset"/></param> | 744 | /// <param name="length">Number of bytes to decode starting at <paramref name="offset"/></param> |
| 745 | /// <param name="spacingCharacter">Character to insert between each byte</param> | 745 | /// <param name="spacingCharacter">Character to insert between each byte</param> |
| 746 | /// <param name="format">String decoding format.</param> | 746 | /// <param name="format">String decoding format.</param> |
| 747 | /// <returns>Decoded string</returns> | 747 | /// <returns>Decoded string</returns> |
| 748 | internal static string BytesToString(byte[] bytes, int offset, int length, char spacingCharacter, string format) | 748 | internal static string BytesToString(byte[] bytes, int offset, int length, char spacingCharacter, string format) |
| 749 | { | 749 | { |
| 750 | if ((object)bytes == null) | 750 | if ((object)bytes == null) |
| 751 | throw new ArgumentNullException("bytes", "Input buffer cannot be null"); | 751 | throw new ArgumentNullException("bytes", "Input buffer cannot be null"); |
| 752 | 752 | ||
| 753 | StringBuilder byteString = new StringBuilder(); | 753 | StringBuilder byteString = new StringBuilder(); |
| 754 | 754 | ||
| 755 | for (int x = 0; x <= length - 1; x++) | 755 | for (int x = 0; x <= length - 1; x++) |
| 756 | { | 756 | { |
| 757 | if (spacingCharacter != NoSpacing && x > 0) | 757 | if (spacingCharacter != NoSpacing && x > 0) |
| 758 | byteString.Append(spacingCharacter); | 758 | byteString.Append(spacingCharacter); |
| 759 | byteString.Append(bytes[x + offset].ToString(format)); | 759 | byteString.Append(bytes[x + offset].ToString(format)); |
| 760 | } | 760 | } |
| 761 | 761 | ||
| 762 | return byteString.ToString(); | 762 | return byteString.ToString(); |
| 763 | } | 763 | } |
| 764 | 764 | ||
| 765 | #endregion | 765 | #endregion |
| 766 | } | 766 | } |
| 767 | } | 767 | } |