#region PDFsharp - A .NET library for processing PDF // // Authors: // Stefan Lange // // Copyright (c) 2005-2017 empira Software GmbH, Cologne Area (Germany) // // http://www.pdfsharp.com // http://sourceforge.net/projects/pdfsharp // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. #endregion using System.Text; namespace PdfSharp.Pdf.Internal { /// /// An encoder for raw strings. The raw encoding is simply the identity relation between /// characters and bytes. PDFsharp internally works with raw encoded strings instead of /// byte arrays because strings are much more handy than byte arrays. /// /// /// Raw encoded strings represent an array of bytes. Therefore a character greater than /// 255 is not valid in a raw encoded string. /// public sealed class RawEncoding : Encoding { /// /// Initializes a new instance of the class. /// // ReSharper disable EmptyConstructor public RawEncoding() { } // ReSharper restore EmptyConstructor /// /// When overridden in a derived class, calculates the number of bytes produced by encoding a set of characters from the specified character array. /// /// The character array containing the set of characters to encode. /// The index of the first character to encode. /// The number of characters to encode. /// /// The number of bytes produced by encoding the specified characters. /// public override int GetByteCount(char[] chars, int index, int count) { return count; } /// /// When overridden in a derived class, encodes a set of characters from the specified character array into the specified byte array. /// /// The character array containing the set of characters to encode. /// The index of the first character to encode. /// The number of characters to encode. /// The byte array to contain the resulting sequence of bytes. /// The index at which to start writing the resulting sequence of bytes. /// /// The actual number of bytes written into . /// public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) { for (int count = charCount; count > 0; charIndex++, byteIndex++, count--) { #if DEBUG_ if ((uint) chars[charIndex] > 255) Debug-Break.Break(true); #endif //Debug.Assert((uint)chars[charIndex] < 256, "Raw string contains invalid character with a value > 255."); bytes[byteIndex] = (byte)chars[charIndex]; //#warning Here is a HACK that must not be ignored! // HACK: // bytes[byteIndex] = (byte)chars[charIndex]; } return charCount; } /// /// When overridden in a derived class, calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. /// /// The byte array containing the sequence of bytes to decode. /// The index of the first byte to decode. /// The number of bytes to decode. /// /// The number of characters produced by decoding the specified sequence of bytes. /// public override int GetCharCount(byte[] bytes, int index, int count) { return count; } /// /// When overridden in a derived class, decodes a sequence of bytes from the specified byte array into the specified character array. /// /// The byte array containing the sequence of bytes to decode. /// The index of the first byte to decode. /// The number of bytes to decode. /// The character array to contain the resulting set of characters. /// The index at which to start writing the resulting set of characters. /// /// The actual number of characters written into . /// public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) { for (int count = byteCount; count > 0; byteIndex++, charIndex++, count--) chars[charIndex] = (char)bytes[byteIndex]; return byteCount; } /// /// When overridden in a derived class, calculates the maximum number of bytes produced by encoding the specified number of characters. /// /// The number of characters to encode. /// /// The maximum number of bytes produced by encoding the specified number of characters. /// public override int GetMaxByteCount(int charCount) { return charCount; } /// /// When overridden in a derived class, calculates the maximum number of characters produced by decoding the specified number of bytes. /// /// The number of bytes to decode. /// /// The maximum number of characters produced by decoding the specified number of bytes. /// public override int GetMaxCharCount(int byteCount) { return byteCount; } #if SILVERLIGHT /// /// When overridden in a derived class, decodes all the bytes in the specified byte array into a string. /// /// The byte array containing the sequence of bytes to decode. /// /// A containing the results of decoding the specified sequence of bytes. /// public string GetString(byte[] bytes) { return GetString(bytes, 0, bytes.Length); } #endif } }