Welcome to mirror list, hosted at ThFree Co, Russian Federation.

OldSpecFormatter.cs « Formatters « MessagePack « src - github.com/aspnet/MessagePack-CSharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4531de6eb7ebe6f1a07a3dad62120cb11baf82e0 (plain)
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
using System;

namespace MessagePack.Formatters
{
    /// <summary>
    /// Serialize by .NET native DateTime binary format.
    /// </summary>
    public sealed class NativeDateTimeFormatter : IMessagePackFormatter<DateTime>
    {
        public static readonly NativeDateTimeFormatter Instance = new NativeDateTimeFormatter();

        public int Serialize(ref byte[] bytes, int offset, DateTime value, IFormatterResolver formatterResolver)
        {
            var dateData = value.ToBinary();
            return MessagePackBinary.WriteInt64(ref bytes, offset, dateData);
        }

        public DateTime Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            if (MessagePackBinary.GetMessagePackType(bytes, offset) == MessagePackType.Extension)
            {
                return DateTimeFormatter.Instance.Deserialize(bytes, offset, formatterResolver, out readSize);
            }

            var dateData = MessagePackBinary.ReadInt64(bytes, offset, out readSize);
            return DateTime.FromBinary(dateData);
        }
    }

    public sealed class NativeDateTimeArrayFormatter : IMessagePackFormatter<DateTime[]>
    {
        public static readonly NativeDateTimeArrayFormatter Instance = new NativeDateTimeArrayFormatter();

        public int Serialize(ref byte[] bytes, int offset, DateTime[] value, IFormatterResolver formatterResolver)
        {
            if (value == null)
            {
                return MessagePackBinary.WriteNil(ref bytes, offset);
            }
            else
            {
                var startOffset = offset;
                offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, value.Length);
                for (int i = 0; i < value.Length; i++)
                {
                    offset += MessagePackBinary.WriteInt64(ref bytes, offset, value[i].ToBinary());
                }

                return offset - startOffset;
            }
        }

        public DateTime[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                readSize = 1;
                return null;
            }
            else
            {
                var startOffset = offset;

                var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
                offset += readSize;
                var array = new DateTime[len];
                for (int i = 0; i < array.Length; i++)
                {
                    var dateData = MessagePackBinary.ReadInt64(bytes, offset, out readSize);
                    array[i] = DateTime.FromBinary(dateData);
                    offset += readSize;
                }
                readSize = offset - startOffset;
                return array;
            }
        }
    }

    // Old-Spec
    // bin 8, bin 16, bin 32, str 8, str 16, str 32 -> fixraw or raw 16 or raw 32
    // fixraw -> fixstr, raw16 -> str16, raw32 -> str32
    // https://github.com/msgpack/msgpack/blob/master/spec-old.md

    /// <summary>
    /// Old-MessagePack spec's string formatter.
    /// </summary>
    public sealed class OldSpecStringFormatter : IMessagePackFormatter<string>
    {
        public static readonly OldSpecStringFormatter Instance = new OldSpecStringFormatter();

        // Old spec does not exists str 8 format.
        public int Serialize(ref byte[] bytes, int offset, string value, IFormatterResolver formatterResolver)
        {
            if (value == null) return MessagePackBinary.WriteNil(ref bytes, offset);

            MessagePackBinary.EnsureCapacity(ref bytes, offset, StringEncoding.UTF8.GetMaxByteCount(value.Length) + 5);

            int useOffset;
            if (value.Length <= MessagePackRange.MaxFixStringLength)
            {
                useOffset = 1;
            }
            else if (value.Length <= ushort.MaxValue)
            {
                useOffset = 3;
            }
            else
            {
                useOffset = 5;
            }

            // skip length area
            var writeBeginOffset = offset + useOffset;
            var byteCount = StringEncoding.UTF8.GetBytes(value, 0, value.Length, bytes, writeBeginOffset);

            // move body and write prefix
            if (byteCount <= MessagePackRange.MaxFixStringLength)
            {
                if (useOffset != 1)
                {
                    Buffer.BlockCopy(bytes, writeBeginOffset, bytes, offset + 1, byteCount);
                }
                bytes[offset] = (byte)(MessagePackCode.MinFixStr | byteCount);
                return byteCount + 1;
            }
            else if (byteCount <= ushort.MaxValue)
            {
                if (useOffset != 3)
                {
                    Buffer.BlockCopy(bytes, writeBeginOffset, bytes, offset + 3, byteCount);
                }

                bytes[offset] = MessagePackCode.Str16;
                bytes[offset + 1] = unchecked((byte)(byteCount >> 8));
                bytes[offset + 2] = unchecked((byte)byteCount);
                return byteCount + 3;
            }
            else
            {
                if (useOffset != 5)
                {
                    Buffer.BlockCopy(bytes, writeBeginOffset, bytes, offset + 5, byteCount);
                }

                bytes[offset] = MessagePackCode.Str32;
                bytes[offset + 1] = unchecked((byte)(byteCount >> 24));
                bytes[offset + 2] = unchecked((byte)(byteCount >> 16));
                bytes[offset + 3] = unchecked((byte)(byteCount >> 8));
                bytes[offset + 4] = unchecked((byte)byteCount);
                return byteCount + 5;
            }
        }

        public string Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            return MessagePackBinary.ReadString(bytes, offset, out readSize);
        }
    }

    /// <summary>
    /// Old-MessagePack spec's binary formatter.
    /// </summary>
    public sealed class OldSpecBinaryFormatter : IMessagePackFormatter<byte[]>
    {
        public static readonly OldSpecBinaryFormatter Instance = new OldSpecBinaryFormatter();

        public int Serialize(ref byte[] bytes, int offset, byte[] value, IFormatterResolver formatterResolver)
        {
            if (value == null) return MessagePackBinary.WriteNil(ref bytes, offset);

            var byteCount = value.Length;

            if (byteCount <= MessagePackRange.MaxFixStringLength)
            {
                MessagePackBinary.EnsureCapacity(ref bytes, offset, byteCount + 1);

                bytes[offset] = (byte)(MessagePackCode.MinFixStr | byteCount);
                Buffer.BlockCopy(value, 0, bytes, offset + 1, byteCount);
                return byteCount + 1;
            }
            else if (byteCount <= ushort.MaxValue)
            {
                MessagePackBinary.EnsureCapacity(ref bytes, offset, byteCount + 3);

                bytes[offset] = MessagePackCode.Str16;
                bytes[offset + 1] = unchecked((byte)(byteCount >> 8));
                bytes[offset + 2] = unchecked((byte)byteCount);
                Buffer.BlockCopy(value, 0, bytes, offset + 3, byteCount);
                return byteCount + 3;
            }
            else
            {
                MessagePackBinary.EnsureCapacity(ref bytes, offset, byteCount + 5);

                bytes[offset] = MessagePackCode.Str32;
                bytes[offset + 1] = unchecked((byte)(byteCount >> 24));
                bytes[offset + 2] = unchecked((byte)(byteCount >> 16));
                bytes[offset + 3] = unchecked((byte)(byteCount >> 8));
                bytes[offset + 4] = unchecked((byte)byteCount);
                Buffer.BlockCopy(value, 0, bytes, offset + 5, byteCount);
                return byteCount + 5;
            }
        }

        public byte[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            var type = MessagePackBinary.GetMessagePackType(bytes, offset);
            if (type == MessagePackType.Nil)
            {
                readSize = 1;
                return null;
            }
            else if (type == MessagePackType.Binary)
            {
                return MessagePackBinary.ReadBytes(bytes, offset, out readSize);
            }
            else if (type == MessagePackType.String)
            {
                var code = bytes[offset];
                unchecked
                {
                    if (MessagePackCode.MinFixStr <= code && code <= MessagePackCode.MaxFixStr)
                    {
                        var length = bytes[offset] & 0x1F;
                        readSize = length + 1;
                        var result = new byte[length];
                        Buffer.BlockCopy(bytes, offset + 1, result, 0, result.Length);
                        return result;
                    }
                    else if (code == MessagePackCode.Str8)
                    {
                        var length = (int)bytes[offset + 1];
                        readSize = length + 2;
                        var result = new byte[length];
                        Buffer.BlockCopy(bytes, offset + 2, result, 0, result.Length);
                        return result;
                    }
                    else if (code == MessagePackCode.Str16)
                    {
                        var length = (bytes[offset + 1] << 8) + (bytes[offset + 2]);
                        readSize = length + 3;
                        var result = new byte[length];
                        Buffer.BlockCopy(bytes, offset + 3, result, 0, result.Length);
                        return result;
                    }
                    else if (code == MessagePackCode.Str32)
                    {
                        var length = (int)((uint)(bytes[offset + 1] << 24) | (uint)(bytes[offset + 2] << 16) | (uint)(bytes[offset + 3] << 8) | (uint)bytes[offset + 4]);
                        readSize = length + 5;
                        var result = new byte[length];
                        Buffer.BlockCopy(bytes, offset + 5, result, 0, result.Length);
                        return result;
                    }
                }
            }

            throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
        }
    }
}