Matroska-Solution / Matroska / NEbml / Core / EbmlWriter.cs
Code · 270 lines · 7799 bytes
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270/* Copyright (c) 2011-2020 Oleg Zee

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.
 * */

using System;
using System.IO;
using System.Linq;
using System.Text;

namespace NEbml.Core
{
	/// <summary>
	/// Writes EBML file
	/// </summary>
	public class EbmlWriter
	{
		protected readonly Stream _stream;

		/// <summary>
		/// Initializes a new instance of the EbmlWriter class
		/// </summary>
		/// <param name="stream"></param>
		public EbmlWriter(Stream stream)
		{
			_stream = stream ?? throw new ArgumentNullException(nameof(stream));
		}

		/// <summary>
		/// Starts nested stream. Upon disposal of nested stream, the size is calculated and written to nested element header.
		/// Example:
		/// using(var data = writer.StartMasterElement(InnerDataElementId))
		/// {
		///     data.WriteInt(DataItem1Id, 139874)
		///     data.WriteUtf(DataItem2Id, "Hello world");
		/// }
		/// </summary>
		/// <param name="elementId"></param>
		/// <returns></returns>
		public MasterBlockWriter StartMasterElement(VInt elementId)
		{
			return new MasterBlockWriter(this, elementId);
		}

		/// <summary>
		/// Writes element header
		/// </summary>
		/// <param name="elementId"></param>
		/// <param name="size"></param>
		/// <returns></returns>
		public int WriteElementHeader(VInt elementId, VInt size)
		{
			return elementId.Write(_stream) + size.Write(_stream);
		}

		#region Writing atomic elements

		/// <summary>
		/// Writes signed integer value
		/// </summary>
		/// <param name="elementId"></param>
		/// <param name="value"></param>
		/// <returns></returns>
		public int Write(VInt elementId, long value)
		{
			var length = value == 0 ? 0u : 1u;
			unchecked
			{
				var mask = (long)0xffffffffffffff80L;
				for (var expected = value < 0L ? mask : 0L; length < 8 && (mask & value) != expected; mask <<= 8, expected <<= 8)
				{
					length++;
				}
			}

			return elementId.Write(_stream) + EncodeWidth(length).Write(_stream) + WriteInt(value, length);
		}

		/// <summary>
		/// Writes unsigned integer value
		/// </summary>
		/// <param name="elementId"></param>
		/// <param name="value"></param>
		/// <returns></returns>
		public int Write(VInt elementId, ulong value)
		{
			var length = 0u;
			for (var mask = UInt64.MaxValue; (value & mask) != 0 && length < 8; mask <<= 8)
			{
				length++;
			}

			return elementId.Write(_stream) + EncodeWidth(length).Write(_stream) + WriteUInt(value, length);
		}

		/// <summary>
		/// Writes datetime value
		/// </summary>
		/// <param name="elementId"></param>
		/// <param name="dateTime"></param>
		/// <returns></returns>
		public int Write(VInt elementId, DateTime dateTime)
		{
			var d = (dateTime - EbmlReader.MilleniumStart).Ticks * 100;
			return elementId.Write(_stream) + EncodeWidth(8).Write(_stream) + WriteInt(d, 8);
		}

		/// <summary>
		/// Writes floating point number
		/// </summary>
		/// <param name="elementId"></param>
		/// <param name="value"></param>
		/// <returns></returns>
		public int Write(VInt elementId, float value)
		{
			var u = new Union {fval = value}.uival;
			return elementId.Write(_stream) + EncodeWidth(4).Write(_stream) + WriteInt(u, 4);
		}

		/// <summary>
		/// Writes floating point number with double precision
		/// </summary>
		/// <param name="elementId"></param>
		/// <param name="value"></param>
		/// <returns></returns>
		public int Write(VInt elementId, double value)
		{
			var u = new Union { dval = value }.ulval;
			return elementId.Write(_stream) + EncodeWidth(8).Write(_stream) + WriteUInt(u, 8);
		}

		/// <summary>
		/// Writes binary data
		/// </summary>
		/// <param name="elementId"></param>
		/// <param name="data"></param>
		/// <param name="offset"></param>
		/// <param name="length"></param>
		/// <returns></returns>
		public int Write(VInt elementId, byte[] data, int offset, int length)
		{
			var headLen = elementId.Write(_stream) + EncodeWidth((uint) length).Write(_stream);
			_stream.Write(data, offset, length);
			return headLen + length;
		}

		/// <summary>
		/// Writes binary data
		/// </summary>
		/// <param name="elementId"></param>
		/// <param name="data"></param>
		/// <returns></returns>
		public int Write(VInt elementId, byte[] data)
		{
			return Write(elementId, data, 0, data.Length);
		}

		/// <summary>
		/// Writes string in ASCII encoding
		/// </summary>
		/// <param name="elementId"></param>
		/// <param name="value"></param>
		/// <returns></returns>
		public int WriteAscii(VInt elementId, string value)
		{
			if (value == null) throw new ArgumentNullException(nameof(value));
			var buffer = string.IsNullOrEmpty(value) ? new byte[0] : Encoding.ASCII.GetBytes(value);
			return Write(elementId, buffer, 0, buffer.Length);
		}

		/// <summary>
		/// Writes string in UTF-8 encoding
		/// </summary>
		/// <param name="elementId"></param>
		/// <param name="value"></param>
		/// <returns></returns>
		public int WriteUtf(VInt elementId, string value)
		{
			if (value == null) throw new ArgumentNullException(nameof(value));
			if (!value.IsNormalized(NormalizationForm.FormC))
			{
				value = value.Normalize(NormalizationForm.FormC);
			}
			var buffer = string.IsNullOrEmpty(value) ? new byte[0] : Encoding.UTF8.GetBytes(value);
			return Write(elementId, buffer, 0, buffer.Length);
		}

		/// <summary>
		/// Writes raw binary data
		/// </summary>
		/// <param name="buffer"></param>
		/// <param name="offset"></param>
		/// <param name="length"></param>
		/// <returns></returns>
		public int Write(byte[] buffer, int offset, int length)
		{
			_stream.Write(buffer, offset, length);
			return length;
		}

		#endregion

		#region Implementation

		private int WriteInt(Int64 value, uint length)
		{
			if(length > 8) throw new ArgumentOutOfRangeException(nameof(length));
			if (value == 0 && length == 0) return 0;

			var buffer = new byte[length];

			var p = (int)length;
			for (var data = value; --p >= 0; data >>= 8)
			{
				buffer[p] = (byte)(data & 0xff);
			}

			return Write(buffer, 0, (int)length);
		}

		private int WriteUInt(UInt64 value, uint length)
		{
			if (length > 8) throw new ArgumentOutOfRangeException(nameof(length));
			if (value == 0 && length == 0) return 0;

			var buffer = new byte[length];

			var p = (int)length;
			for (var data = value; --p >= 0; data >>= 8)
			{
				buffer[p] = (byte)(data & 0xff);
			}

			return Write(buffer, 0, (int) length);
		}

		[ThreadStatic] // avoid thread sync code
		private static VInt[]? __smallSizes;

		private const int CachedWidthCount = 20;

		private static VInt EncodeWidth(uint width)
		{
			if (__smallSizes == null)
			{
				__smallSizes = Enumerable.Range(0, CachedWidthCount).Select(i => VInt.EncodeSize((ulong)i)).ToArray();
			}
			return width < CachedWidthCount ? __smallSizes[width] : VInt.EncodeSize(width);
		}

		#endregion
	}
}