4
namespace Newtonsoft.Json.Utilities
6
internal class Base64Encoder
8
private const int Base64LineSize = 76;
9
private const int LineSizeInBytes = 57;
11
private readonly char[] _charsLine = new char[Base64LineSize];
12
private readonly TextWriter _writer;
14
private byte[] _leftOverBytes;
15
private int _leftOverBytesCount;
17
public Base64Encoder(TextWriter writer)
19
ValidationUtils.ArgumentNotNull(writer, "writer");
23
public void Encode(byte[] buffer, int index, int count)
26
throw new ArgumentNullException("buffer");
29
throw new ArgumentOutOfRangeException("index");
32
throw new ArgumentOutOfRangeException("count");
34
if (count > (buffer.Length - index))
35
throw new ArgumentOutOfRangeException("count");
37
if (_leftOverBytesCount > 0)
39
int leftOverBytesCount = _leftOverBytesCount;
40
while (leftOverBytesCount < 3 && count > 0)
42
_leftOverBytes[leftOverBytesCount++] = buffer[index++];
45
if (count == 0 && leftOverBytesCount < 3)
47
_leftOverBytesCount = leftOverBytesCount;
50
int num2 = Convert.ToBase64CharArray(_leftOverBytes, 0, 3, _charsLine, 0);
51
WriteChars(_charsLine, 0, num2);
53
_leftOverBytesCount = count % 3;
54
if (_leftOverBytesCount > 0)
56
count -= _leftOverBytesCount;
57
if (_leftOverBytes == null)
59
_leftOverBytes = new byte[3];
61
for (int i = 0; i < _leftOverBytesCount; i++)
63
_leftOverBytes[i] = buffer[(index + count) + i];
66
int num4 = index + count;
67
int length = LineSizeInBytes;
70
if ((index + length) > num4)
72
length = num4 - index;
74
int num6 = Convert.ToBase64CharArray(buffer, index, length, _charsLine, 0);
75
WriteChars(_charsLine, 0, num6);
82
if (_leftOverBytesCount > 0)
84
int count = Convert.ToBase64CharArray(_leftOverBytes, 0, _leftOverBytesCount, _charsLine, 0);
85
WriteChars(_charsLine, 0, count);
86
_leftOverBytesCount = 0;
90
private void WriteChars(char[] chars, int index, int count)
92
_writer.Write(chars, index, count);
2
// Copyright (c) 2007 James Newton-King
4
// Permission is hereby granted, free of charge, to any person
5
// obtaining a copy of this software and associated documentation
6
// files (the "Software"), to deal in the Software without
7
// restriction, including without limitation the rights to use,
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the
10
// Software is furnished to do so, subject to the following
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
// OTHER DEALINGS IN THE SOFTWARE.
29
namespace Newtonsoft.Json.Utilities
31
internal class Base64Encoder
33
private const int Base64LineSize = 76;
34
private const int LineSizeInBytes = 57;
36
private readonly char[] _charsLine = new char[Base64LineSize];
37
private readonly TextWriter _writer;
39
private byte[] _leftOverBytes;
40
private int _leftOverBytesCount;
42
public Base64Encoder(TextWriter writer)
44
ValidationUtils.ArgumentNotNull(writer, "writer");
48
public void Encode(byte[] buffer, int index, int count)
51
throw new ArgumentNullException("buffer");
54
throw new ArgumentOutOfRangeException("index");
57
throw new ArgumentOutOfRangeException("count");
59
if (count > (buffer.Length - index))
60
throw new ArgumentOutOfRangeException("count");
62
if (_leftOverBytesCount > 0)
64
int leftOverBytesCount = _leftOverBytesCount;
65
while (leftOverBytesCount < 3 && count > 0)
67
_leftOverBytes[leftOverBytesCount++] = buffer[index++];
70
if (count == 0 && leftOverBytesCount < 3)
72
_leftOverBytesCount = leftOverBytesCount;
75
int num2 = Convert.ToBase64CharArray(_leftOverBytes, 0, 3, _charsLine, 0);
76
WriteChars(_charsLine, 0, num2);
78
_leftOverBytesCount = count % 3;
79
if (_leftOverBytesCount > 0)
81
count -= _leftOverBytesCount;
82
if (_leftOverBytes == null)
84
_leftOverBytes = new byte[3];
86
for (int i = 0; i < _leftOverBytesCount; i++)
88
_leftOverBytes[i] = buffer[(index + count) + i];
91
int num4 = index + count;
92
int length = LineSizeInBytes;
95
if ((index + length) > num4)
97
length = num4 - index;
99
int num6 = Convert.ToBase64CharArray(buffer, index, length, _charsLine, 0);
100
WriteChars(_charsLine, 0, num6);
107
if (_leftOverBytesCount > 0)
109
int count = Convert.ToBase64CharArray(_leftOverBytes, 0, _leftOverBytesCount, _charsLine, 0);
110
WriteChars(_charsLine, 0, count);
111
_leftOverBytesCount = 0;
115
private void WriteChars(char[] chars, int index, int count)
117
_writer.Write(chars, index, count);
b'\\ No newline at end of file'