forked from enyim/EnyimMemcached
-
Notifications
You must be signed in to change notification settings - Fork 7
/
BinaryConverter.cs
128 lines (107 loc) · 3.39 KB
/
BinaryConverter.cs
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
using System;
using System.Text;
namespace Enyim.Caching.Memcached.Operations.Binary
{
internal static class BinaryConverter
{
public static unsafe int DecodeInt16(byte* buffer, int offset)
{
return ((int)(buffer[offset]) << 8) + buffer[offset + 1];
}
public static unsafe int DecodeInt32(ArraySegment<byte> segment, int offset)
{
fixed (byte* buffer = segment.Array)
{
byte* ptr = buffer + segment.Offset + offset;
return DecodeInt32(buffer, 0);
}
}
public static unsafe int DecodeInt32(byte* buffer, int offset)
{
buffer += offset;
return (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
}
public static unsafe ulong DecodeUInt64(byte[] buffer, int offset)
{
fixed (byte* ptr = buffer)
{
return DecodeUInt64(ptr, offset);
}
}
public static unsafe ulong DecodeUInt64(byte* buffer, int offset)
{
buffer += offset;
int part1 = (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
int part2 = (buffer[4] << 24) | (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
return (ulong)(((long)part2) | (part1 << 32));
}
public static unsafe void EncodeUInt32(uint value, byte[] buffer, int offset)
{
fixed (byte* bufferPtr = buffer)
{
EncodeUInt32(value, bufferPtr, offset);
}
}
public static unsafe void EncodeUInt32(uint value, byte* buffer, int offset)
{
byte* ptr = buffer + offset;
ptr[0] = (byte)(value >> 24);
ptr[1] = (byte)(value >> 16);
ptr[2] = (byte)(value >> 8);
ptr[3] = (byte)(value & 255);
}
public static unsafe void EncodeUInt64(ulong value, byte[] buffer, int offset)
{
fixed (byte* bufferPtr = buffer)
{
EncodeUInt64(value, bufferPtr, offset);
}
}
public static unsafe void EncodeUInt64(ulong value, byte* buffer, int offset)
{
byte* ptr = buffer + offset;
ptr[0] = (byte)(value >> 56);
ptr[1] = (byte)(value >> 48);
ptr[2] = (byte)(value >> 40);
ptr[3] = (byte)(value >> 32);
ptr[4] = (byte)(value >> 24);
ptr[5] = (byte)(value >> 16);
ptr[6] = (byte)(value >> 8);
ptr[7] = (byte)(value & 255);
}
public static byte[] EncodeKey(string key)
{
if (String.IsNullOrEmpty(key)) return null;
return Encoding.UTF8.GetBytes(key);
}
public static string DecodeKey(byte[] data)
{
if (data == null || data.Length == 0) return null;
return Encoding.UTF8.GetString(data);
}
public static string DecodeKey(byte[] data, int index, int count)
{
if (data == null || data.Length == 0 || count == 0) return null;
return Encoding.UTF8.GetString(data, index, count);
}
}
}
#region [ License information ]
/* ************************************************************
*
* Copyright (c) 2010 Attila Kiskó, enyim.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ************************************************************/
#endregion