forked from ststeiger/PdfSharpCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRawEncoding.cs
More file actions
151 lines (143 loc) · 7.08 KB
/
Copy pathRawEncoding.cs
File metadata and controls
151 lines (143 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#region PDFsharp - A .NET library for processing PDF
//
// Authors:
// Stefan Lange
//
// Copyright (c) 2005-2016 empira Software GmbH, Cologne Area (Germany)
//
// http://www.PdfSharpCore.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 PdfSharpCore.Pdf.Internal
{
/// <summary>
/// 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.
/// </summary>
/// <remarks>
/// Raw encoded strings represent an array of bytes. Therefore a character greater than
/// 255 is not valid in a raw encoded string.
/// </remarks>
public sealed class RawEncoding : Encoding
{
/// <summary>
/// Initializes a new instance of the <see cref="RawEncoding"/> class.
/// </summary>
// ReSharper disable EmptyConstructor
public RawEncoding()
{ }
// ReSharper restore EmptyConstructor
/// <summary>
/// When overridden in a derived class, calculates the number of bytes produced by encoding a set of characters from the specified character array.
/// </summary>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
/// <returns>
/// The number of bytes produced by encoding the specified characters.
/// </returns>
public override int GetByteCount(char[] chars, int index, int count)
{
return count;
}
/// <summary>
/// When overridden in a derived class, encodes a set of characters from the specified character array into the specified byte array.
/// </summary>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
/// <returns>
/// The actual number of bytes written into <paramref name="bytes"/>.
/// </returns>
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;
}
/// <summary>
/// When overridden in a derived class, calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.
/// </summary>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
/// <returns>
/// The number of characters produced by decoding the specified sequence of bytes.
/// </returns>
public override int GetCharCount(byte[] bytes, int index, int count)
{
return count;
}
/// <summary>
/// When overridden in a derived class, decodes a sequence of bytes from the specified byte array into the specified character array.
/// </summary>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
/// <returns>
/// The actual number of characters written into <paramref name="chars"/>.
/// </returns>
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;
}
/// <summary>
/// When overridden in a derived class, calculates the maximum number of bytes produced by encoding the specified number of characters.
/// </summary>
/// <param name="charCount">The number of characters to encode.</param>
/// <returns>
/// The maximum number of bytes produced by encoding the specified number of characters.
/// </returns>
public override int GetMaxByteCount(int charCount)
{
return charCount;
}
/// <summary>
/// When overridden in a derived class, calculates the maximum number of characters produced by decoding the specified number of bytes.
/// </summary>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <returns>
/// The maximum number of characters produced by decoding the specified number of bytes.
/// </returns>
public override int GetMaxCharCount(int byteCount)
{
return byteCount;
}
}
}