mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-19 13:02:13 +01:00
import C# implementation
This commit is contained in:
parent
b50ff920f0
commit
1d1a9d7933
7
.gitignore
vendored
7
.gitignore
vendored
@ -6,3 +6,10 @@ ruby/Makefile
|
|||||||
*.6
|
*.6
|
||||||
_obj
|
_obj
|
||||||
_test
|
_test
|
||||||
|
/csharp/msgpack.suo
|
||||||
|
/csharp/msgpack/bin
|
||||||
|
/csharp/msgpack/obj
|
||||||
|
/csharp/msgpack/msgpack.csproj.user
|
||||||
|
/csharp/msgpack.tests/obj
|
||||||
|
/csharp/msgpack.tests/bin
|
||||||
|
/csharp/msgpack.tests/msgpack.tests.csproj.user
|
||||||
|
26
csharp/msgpack.sln
Normal file
26
csharp/msgpack.sln
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 11.00
|
||||||
|
# Visual Studio 2010
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "msgpack", "msgpack\msgpack.csproj", "{E1809531-EC2A-4EA6-B0E8-CC815EDFAA2F}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "msgpack.tests", "msgpack.tests\msgpack.tests.csproj", "{CE24167B-8F0A-4670-BD1E-3C283311E86B}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{E1809531-EC2A-4EA6-B0E8-CC815EDFAA2F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{E1809531-EC2A-4EA6-B0E8-CC815EDFAA2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{E1809531-EC2A-4EA6-B0E8-CC815EDFAA2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{E1809531-EC2A-4EA6-B0E8-CC815EDFAA2F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{CE24167B-8F0A-4670-BD1E-3C283311E86B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{CE24167B-8F0A-4670-BD1E-3C283311E86B}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{CE24167B-8F0A-4670-BD1E-3C283311E86B}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{CE24167B-8F0A-4670-BD1E-3C283311E86B}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
36
csharp/msgpack.tests/AssemblyInfo.cs
Normal file
36
csharp/msgpack.tests/AssemblyInfo.cs
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
// General Information about an assembly is controlled through the following
|
||||||
|
// set of attributes. Change these attribute values to modify the information
|
||||||
|
// associated with an assembly.
|
||||||
|
[assembly: AssemblyTitle ("msgpack.tests")]
|
||||||
|
[assembly: AssemblyDescription ("")]
|
||||||
|
[assembly: AssemblyConfiguration ("")]
|
||||||
|
[assembly: AssemblyCompany ("Microsoft")]
|
||||||
|
[assembly: AssemblyProduct ("msgpack.tests")]
|
||||||
|
[assembly: AssemblyCopyright ("Copyright © Microsoft 2011")]
|
||||||
|
[assembly: AssemblyTrademark ("")]
|
||||||
|
[assembly: AssemblyCulture ("")]
|
||||||
|
|
||||||
|
// Setting ComVisible to false makes the types in this assembly not visible
|
||||||
|
// to COM components. If you need to access a type in this assembly from
|
||||||
|
// COM, set the ComVisible attribute to true on that type.
|
||||||
|
[assembly: ComVisible (false)]
|
||||||
|
|
||||||
|
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||||
|
[assembly: Guid ("82a4db7c-686b-4206-9cc9-9aae082d7e73")]
|
||||||
|
|
||||||
|
// Version information for an assembly consists of the following four values:
|
||||||
|
//
|
||||||
|
// Major Version
|
||||||
|
// Minor Version
|
||||||
|
// Build Number
|
||||||
|
// Revision
|
||||||
|
//
|
||||||
|
// You can specify all the values or you can default the Build and Revision Numbers
|
||||||
|
// by using the '*' as shown below:
|
||||||
|
// [assembly: AssemblyVersion("1.0.*")]
|
||||||
|
[assembly: AssemblyVersion ("1.0.0.0")]
|
||||||
|
[assembly: AssemblyFileVersion ("1.0.0.0")]
|
62
csharp/msgpack.tests/msgpack.tests.csproj
Normal file
62
csharp/msgpack.tests/msgpack.tests.csproj
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<PropertyGroup>
|
||||||
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
|
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||||
|
<ProductVersion>8.0.30703</ProductVersion>
|
||||||
|
<SchemaVersion>2.0</SchemaVersion>
|
||||||
|
<ProjectGuid>{CE24167B-8F0A-4670-BD1E-3C283311E86B}</ProjectGuid>
|
||||||
|
<OutputType>Library</OutputType>
|
||||||
|
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||||
|
<RootNamespace>msgpack.tests</RootNamespace>
|
||||||
|
<AssemblyName>msgpack.tests</AssemblyName>
|
||||||
|
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||||
|
<FileAlignment>512</FileAlignment>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
|
<DebugSymbols>true</DebugSymbols>
|
||||||
|
<DebugType>full</DebugType>
|
||||||
|
<Optimize>false</Optimize>
|
||||||
|
<OutputPath>bin\Debug\</OutputPath>
|
||||||
|
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||||
|
<DebugType>pdbonly</DebugType>
|
||||||
|
<Optimize>true</Optimize>
|
||||||
|
<OutputPath>bin\Release\</OutputPath>
|
||||||
|
<DefineConstants>TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup>
|
||||||
|
<StartupObject />
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Reference Include="System" />
|
||||||
|
<Reference Include="System.Core" />
|
||||||
|
<Reference Include="System.Xml.Linq" />
|
||||||
|
<Reference Include="System.Data.DataSetExtensions" />
|
||||||
|
<Reference Include="Microsoft.CSharp" />
|
||||||
|
<Reference Include="System.Data" />
|
||||||
|
<Reference Include="System.Xml" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Include="AssemblyInfo.cs" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\msgpack\msgpack.csproj">
|
||||||
|
<Project>{E1809531-EC2A-4EA6-B0E8-CC815EDFAA2F}</Project>
|
||||||
|
<Name>msgpack</Name>
|
||||||
|
</ProjectReference>
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
|
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||||
|
Other similar extension points exist, see Microsoft.Common.targets.
|
||||||
|
<Target Name="BeforeBuild">
|
||||||
|
</Target>
|
||||||
|
<Target Name="AfterBuild">
|
||||||
|
</Target>
|
||||||
|
-->
|
||||||
|
</Project>
|
36
csharp/msgpack/AssemblyInfo.cs
Normal file
36
csharp/msgpack/AssemblyInfo.cs
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
// General Information about an assembly is controlled through the following
|
||||||
|
// set of attributes. Change these attribute values to modify the information
|
||||||
|
// associated with an assembly.
|
||||||
|
[assembly: AssemblyTitle ("msgpack")]
|
||||||
|
[assembly: AssemblyDescription ("")]
|
||||||
|
[assembly: AssemblyConfiguration ("")]
|
||||||
|
[assembly: AssemblyCompany ("Microsoft")]
|
||||||
|
[assembly: AssemblyProduct ("msgpack")]
|
||||||
|
[assembly: AssemblyCopyright ("Copyright © Microsoft 2011")]
|
||||||
|
[assembly: AssemblyTrademark ("")]
|
||||||
|
[assembly: AssemblyCulture ("")]
|
||||||
|
|
||||||
|
// Setting ComVisible to false makes the types in this assembly not visible
|
||||||
|
// to COM components. If you need to access a type in this assembly from
|
||||||
|
// COM, set the ComVisible attribute to true on that type.
|
||||||
|
[assembly: ComVisible (false)]
|
||||||
|
|
||||||
|
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||||
|
[assembly: Guid ("aae99974-7a7d-4787-83ca-614779d54bd2")]
|
||||||
|
|
||||||
|
// Version information for an assembly consists of the following four values:
|
||||||
|
//
|
||||||
|
// Major Version
|
||||||
|
// Minor Version
|
||||||
|
// Build Number
|
||||||
|
// Revision
|
||||||
|
//
|
||||||
|
// You can specify all the values or you can default the Build and Revision Numbers
|
||||||
|
// by using the '*' as shown below:
|
||||||
|
// [assembly: AssemblyVersion("1.0.*")]
|
||||||
|
[assembly: AssemblyVersion ("1.0.0.0")]
|
||||||
|
[assembly: AssemblyFileVersion ("1.0.0.0")]
|
148
csharp/msgpack/BoxingPacker.cs
Normal file
148
csharp/msgpack/BoxingPacker.cs
Normal file
@ -0,0 +1,148 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace msgpack
|
||||||
|
{
|
||||||
|
public class BoxingPacker
|
||||||
|
{
|
||||||
|
static Type KeyValuePairDefinitionType;
|
||||||
|
|
||||||
|
static BoxingPacker ()
|
||||||
|
{
|
||||||
|
KeyValuePairDefinitionType = typeof (KeyValuePair<object,object>).GetGenericTypeDefinition ();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Pack (Stream strm, object o)
|
||||||
|
{
|
||||||
|
MsgPackWriter writer = new MsgPackWriter (strm);
|
||||||
|
Pack (writer, o);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Pack (MsgPackWriter writer, object o)
|
||||||
|
{
|
||||||
|
if (o == null) {
|
||||||
|
writer.WriteNil ();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Type t = o.GetType ();
|
||||||
|
if (t.IsPrimitive) {
|
||||||
|
if (t.Equals (typeof (int))) writer.Write ((int)o);
|
||||||
|
else if (t.Equals (typeof (uint))) writer.Write ((uint)o);
|
||||||
|
else if (t.Equals (typeof (float))) writer.Write ((float)o);
|
||||||
|
else if (t.Equals (typeof (double))) writer.Write ((double)o);
|
||||||
|
else if (t.Equals (typeof (long))) writer.Write ((long)o);
|
||||||
|
else if (t.Equals (typeof (ulong))) writer.Write ((ulong)o);
|
||||||
|
else if (t.Equals (typeof (bool))) writer.Write ((bool)o);
|
||||||
|
else if (t.Equals (typeof (byte))) writer.Write ((byte)o);
|
||||||
|
else if (t.Equals (typeof (sbyte))) writer.Write ((sbyte)o);
|
||||||
|
else if (t.Equals (typeof (short))) writer.Write ((short)o);
|
||||||
|
else if (t.Equals (typeof (ushort))) writer.Write ((ushort)o);
|
||||||
|
else throw new NotSupportedException (); // char?
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
IDictionary dic = o as IDictionary;
|
||||||
|
if (dic != null) {
|
||||||
|
writer.WriteMapHeader (dic.Count);
|
||||||
|
foreach (System.Collections.DictionaryEntry e in dic) {
|
||||||
|
Pack (writer, e.Key);
|
||||||
|
Pack (writer, e.Value);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (t.IsArray) {
|
||||||
|
Array ary = (Array)o;
|
||||||
|
Type et = t.GetElementType ();
|
||||||
|
|
||||||
|
// KeyValuePair<K,V>[] (Map Type)
|
||||||
|
if (et.IsGenericType && et.GetGenericTypeDefinition ().Equals (KeyValuePairDefinitionType)) {
|
||||||
|
PropertyInfo propKey = et.GetProperty ("Key");
|
||||||
|
PropertyInfo propValue = et.GetProperty ("Value");
|
||||||
|
writer.WriteMapHeader (ary.Length);
|
||||||
|
for (int i = 0; i < ary.Length; i ++) {
|
||||||
|
object e = ary.GetValue (i);
|
||||||
|
Pack (writer, propKey.GetValue (e, null));
|
||||||
|
Pack (writer, propValue.GetValue (e, null));
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Array
|
||||||
|
writer.WriteArrayHeader (ary.Length);
|
||||||
|
for (int i = 0; i < ary.Length; i ++)
|
||||||
|
Pack (writer, ary.GetValue (i));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public object Unpack (Stream strm)
|
||||||
|
{
|
||||||
|
MsgPackReader reader = new MsgPackReader (strm);
|
||||||
|
return Unpack (reader);
|
||||||
|
}
|
||||||
|
|
||||||
|
object Unpack (MsgPackReader reader)
|
||||||
|
{
|
||||||
|
if (!reader.Read ())
|
||||||
|
throw new FormatException ();
|
||||||
|
|
||||||
|
switch (reader.Type) {
|
||||||
|
case TypePrefixes.PositiveFixNum:
|
||||||
|
case TypePrefixes.NegativeFixNum:
|
||||||
|
case TypePrefixes.Int8:
|
||||||
|
case TypePrefixes.Int16:
|
||||||
|
case TypePrefixes.Int32:
|
||||||
|
return reader.ValueSigned;
|
||||||
|
case TypePrefixes.Int64:
|
||||||
|
return reader.ValueSigned64;
|
||||||
|
case TypePrefixes.UInt8:
|
||||||
|
case TypePrefixes.UInt16:
|
||||||
|
case TypePrefixes.UInt32:
|
||||||
|
return reader.ValueUnsigned;
|
||||||
|
case TypePrefixes.UInt64:
|
||||||
|
return reader.ValueUnsigned64;
|
||||||
|
case TypePrefixes.True:
|
||||||
|
return true;
|
||||||
|
case TypePrefixes.False:
|
||||||
|
return false;
|
||||||
|
case TypePrefixes.Float:
|
||||||
|
return reader.ValueFloat;
|
||||||
|
case TypePrefixes.Double:
|
||||||
|
return reader.ValueDouble;
|
||||||
|
case TypePrefixes.Nil:
|
||||||
|
return null;
|
||||||
|
case TypePrefixes.FixRaw:
|
||||||
|
case TypePrefixes.Raw16:
|
||||||
|
case TypePrefixes.Raw32:
|
||||||
|
byte[] raw = new byte[reader.Length];
|
||||||
|
reader.ReadValueRaw (raw, 0, raw.Length);
|
||||||
|
return raw;
|
||||||
|
case TypePrefixes.FixArray:
|
||||||
|
case TypePrefixes.Array16:
|
||||||
|
case TypePrefixes.Array32:
|
||||||
|
object[] ary = new object[reader.Length];
|
||||||
|
for (int i = 0; i < ary.Length; i ++)
|
||||||
|
ary[i] = Unpack (reader);
|
||||||
|
return ary;
|
||||||
|
case TypePrefixes.FixMap:
|
||||||
|
case TypePrefixes.Map16:
|
||||||
|
case TypePrefixes.Map32:
|
||||||
|
IDictionary<object, object> dic = new Dictionary<object, object> ((int)reader.Length);
|
||||||
|
int count = (int)reader.Length;
|
||||||
|
for (int i = 0; i < count; i ++) {
|
||||||
|
object k = Unpack (reader);
|
||||||
|
object v = Unpack (reader);
|
||||||
|
dic.Add (k, v);
|
||||||
|
}
|
||||||
|
return dic;
|
||||||
|
default:
|
||||||
|
throw new FormatException ();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
169
csharp/msgpack/MsgPackReader.cs
Normal file
169
csharp/msgpack/MsgPackReader.cs
Normal file
@ -0,0 +1,169 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace msgpack
|
||||||
|
{
|
||||||
|
public class MsgPackReader
|
||||||
|
{
|
||||||
|
Stream _strm;
|
||||||
|
byte[] _tmp0 = new byte[8];
|
||||||
|
byte[] _tmp1 = new byte[8];
|
||||||
|
|
||||||
|
public MsgPackReader (Stream strm)
|
||||||
|
{
|
||||||
|
_strm = strm;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TypePrefixes Type { get; private set; }
|
||||||
|
|
||||||
|
public bool ValueBoolean { get; private set; }
|
||||||
|
public uint Length { get; private set; }
|
||||||
|
|
||||||
|
public uint ValueUnsigned { get; private set; }
|
||||||
|
public ulong ValueUnsigned64 { get; private set; }
|
||||||
|
|
||||||
|
public int ValueSigned { get; private set; }
|
||||||
|
public long ValueSigned64 { get; private set; }
|
||||||
|
|
||||||
|
public float ValueFloat { get; private set; }
|
||||||
|
public double ValueDouble { get; private set; }
|
||||||
|
|
||||||
|
public bool Read ()
|
||||||
|
{
|
||||||
|
byte[] tmp0 = _tmp0, tmp1 = _tmp1;
|
||||||
|
int x = _strm.ReadByte ();
|
||||||
|
if (x < 0)
|
||||||
|
return false; // EOS
|
||||||
|
|
||||||
|
if (x >= 0x00 && x <= 0x7f) {
|
||||||
|
this.Type = TypePrefixes.PositiveFixNum;
|
||||||
|
} else if (x >= 0xe0 && x <= 0xff) {
|
||||||
|
this.Type = TypePrefixes.NegativeFixNum;
|
||||||
|
} else if (x >= 0xa0 && x <= 0xbf) {
|
||||||
|
this.Type = TypePrefixes.FixRaw;
|
||||||
|
} else if (x >= 0x90 && x <= 0x9f) {
|
||||||
|
this.Type = TypePrefixes.FixArray;
|
||||||
|
} else if (x >= 0x80 && x <= 0x8f) {
|
||||||
|
this.Type = TypePrefixes.FixMap;
|
||||||
|
} else {
|
||||||
|
this.Type = (TypePrefixes)x;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (this.Type) {
|
||||||
|
case TypePrefixes.Nil:
|
||||||
|
break;
|
||||||
|
case TypePrefixes.False:
|
||||||
|
ValueBoolean = false;
|
||||||
|
break;
|
||||||
|
case TypePrefixes.True:
|
||||||
|
ValueBoolean = true;
|
||||||
|
break;
|
||||||
|
case TypePrefixes.Float:
|
||||||
|
_strm.Read (tmp0, 0, 4);
|
||||||
|
if (BitConverter.IsLittleEndian) {
|
||||||
|
tmp1[0] = tmp0[3];
|
||||||
|
tmp1[1] = tmp0[2];
|
||||||
|
tmp1[2] = tmp0[1];
|
||||||
|
tmp1[3] = tmp0[0];
|
||||||
|
ValueFloat = BitConverter.ToSingle (tmp1, 0);
|
||||||
|
} else {
|
||||||
|
ValueFloat = BitConverter.ToSingle (tmp0, 0);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case TypePrefixes.Double:
|
||||||
|
_strm.Read (tmp0, 0, 8);
|
||||||
|
if (BitConverter.IsLittleEndian) {
|
||||||
|
tmp1[0] = tmp0[7];
|
||||||
|
tmp1[1] = tmp0[6];
|
||||||
|
tmp1[2] = tmp0[5];
|
||||||
|
tmp1[3] = tmp0[4];
|
||||||
|
tmp1[4] = tmp0[3];
|
||||||
|
tmp1[5] = tmp0[2];
|
||||||
|
tmp1[6] = tmp0[1];
|
||||||
|
tmp1[7] = tmp0[0];
|
||||||
|
ValueDouble = BitConverter.ToDouble (tmp1, 0);
|
||||||
|
} else {
|
||||||
|
ValueDouble = BitConverter.ToDouble (tmp0, 0);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case TypePrefixes.NegativeFixNum:
|
||||||
|
ValueSigned = (x & 0x1f) - 0x20;
|
||||||
|
break;
|
||||||
|
case TypePrefixes.PositiveFixNum:
|
||||||
|
ValueSigned = x & 0x7f;
|
||||||
|
break;
|
||||||
|
case TypePrefixes.UInt8:
|
||||||
|
x = _strm.ReadByte ();
|
||||||
|
if (x < 0)
|
||||||
|
throw new FormatException ();
|
||||||
|
ValueUnsigned = (uint)x;
|
||||||
|
break;
|
||||||
|
case TypePrefixes.UInt16:
|
||||||
|
if (_strm.Read (tmp0, 0, 2) != 2)
|
||||||
|
throw new FormatException ();
|
||||||
|
ValueUnsigned = ((uint)tmp0[0] << 8) | (uint)tmp0[1];
|
||||||
|
break;
|
||||||
|
case TypePrefixes.UInt32:
|
||||||
|
if (_strm.Read (tmp0, 0, 4) != 4)
|
||||||
|
throw new FormatException ();
|
||||||
|
ValueUnsigned = ((uint)tmp0[0] << 24) | ((uint)tmp0[1] << 16) | ((uint)tmp0[2] << 8) | (uint)tmp0[3];
|
||||||
|
break;
|
||||||
|
case TypePrefixes.UInt64:
|
||||||
|
if (_strm.Read (tmp0, 0, 8) != 8)
|
||||||
|
throw new FormatException ();
|
||||||
|
ValueUnsigned64 = ((ulong)tmp0[0] << 56) | ((ulong)tmp0[1] << 48) | ((ulong)tmp0[2] << 40) | ((ulong)tmp0[3] << 32) | ((ulong)tmp0[4] << 24) | ((ulong)tmp0[5] << 16) | ((ulong)tmp0[6] << 8) | (ulong)tmp0[7];
|
||||||
|
break;
|
||||||
|
case TypePrefixes.Int8:
|
||||||
|
x = _strm.ReadByte ();
|
||||||
|
if (x < 0)
|
||||||
|
throw new FormatException ();
|
||||||
|
ValueSigned = (sbyte)x;
|
||||||
|
break;
|
||||||
|
case TypePrefixes.Int16:
|
||||||
|
if (_strm.Read (tmp0, 0, 2) != 2)
|
||||||
|
throw new FormatException ();
|
||||||
|
ValueSigned = (short)((tmp0[0] << 8) | tmp0[1]);
|
||||||
|
break;
|
||||||
|
case TypePrefixes.Int32:
|
||||||
|
if (_strm.Read (tmp0, 0, 4) != 4)
|
||||||
|
throw new FormatException ();
|
||||||
|
ValueSigned = (tmp0[0] << 24) | (tmp0[1] << 16) | (tmp0[2] << 8) | tmp0[3];
|
||||||
|
break;
|
||||||
|
case TypePrefixes.Int64:
|
||||||
|
if (_strm.Read (tmp0, 0, 8) != 8)
|
||||||
|
throw new FormatException ();
|
||||||
|
ValueSigned64 = ((long)tmp0[0] << 56) | ((long)tmp0[1] << 48) | ((long)tmp0[2] << 40) | ((long)tmp0[3] << 32) | ((long)tmp0[4] << 24) | ((long)tmp0[5] << 16) | ((long)tmp0[6] << 8) | (long)tmp0[7];
|
||||||
|
break;
|
||||||
|
case TypePrefixes.FixRaw:
|
||||||
|
Length = (uint)(x & 0x1f);
|
||||||
|
break;
|
||||||
|
case TypePrefixes.FixArray:
|
||||||
|
case TypePrefixes.FixMap:
|
||||||
|
Length = (uint)(x & 0xf);
|
||||||
|
break;
|
||||||
|
case TypePrefixes.Raw16:
|
||||||
|
case TypePrefixes.Array16:
|
||||||
|
case TypePrefixes.Map16:
|
||||||
|
if (_strm.Read (tmp0, 0, 2) != 2)
|
||||||
|
throw new FormatException ();
|
||||||
|
Length = ((uint)tmp0[0] << 8) | (uint)tmp0[1];
|
||||||
|
break;
|
||||||
|
case TypePrefixes.Raw32:
|
||||||
|
case TypePrefixes.Array32:
|
||||||
|
case TypePrefixes.Map32:
|
||||||
|
if (_strm.Read (tmp0, 0, 4) != 4)
|
||||||
|
throw new FormatException ();
|
||||||
|
Length = ((uint)tmp0[0] << 24) | ((uint)tmp0[1] << 16) | ((uint)tmp0[2] << 8) | (uint)tmp0[3];
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new FormatException ();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int ReadValueRaw (byte[] buf, int offset, int count)
|
||||||
|
{
|
||||||
|
return _strm.Read (buf, offset, count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
246
csharp/msgpack/MsgPackWriter.cs
Normal file
246
csharp/msgpack/MsgPackWriter.cs
Normal file
@ -0,0 +1,246 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace msgpack
|
||||||
|
{
|
||||||
|
public class MsgPackWriter
|
||||||
|
{
|
||||||
|
Stream _strm;
|
||||||
|
Encoding _encoding = Encoding.UTF8;
|
||||||
|
byte[] _tmp = new byte[9];
|
||||||
|
|
||||||
|
public MsgPackWriter (Stream strm)
|
||||||
|
{
|
||||||
|
_strm = strm;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Write (byte x)
|
||||||
|
{
|
||||||
|
if (x < 128) {
|
||||||
|
_strm.WriteByte (x);
|
||||||
|
} else {
|
||||||
|
byte[] tmp = _tmp;
|
||||||
|
tmp[0] = 0xcc;
|
||||||
|
tmp[1] = x;
|
||||||
|
_strm.Write (tmp, 0, 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Write (ushort x)
|
||||||
|
{
|
||||||
|
if (x < 0x100) {
|
||||||
|
Write ((byte)x);
|
||||||
|
} else {
|
||||||
|
byte[] tmp = _tmp;
|
||||||
|
tmp[0] = 0xcd;
|
||||||
|
tmp[1] = (byte)(x >> 8);
|
||||||
|
tmp[2] = (byte)x;
|
||||||
|
_strm.Write (tmp, 0, 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Write (uint x)
|
||||||
|
{
|
||||||
|
if (x < 0x10000) {
|
||||||
|
Write ((ushort)x);
|
||||||
|
} else {
|
||||||
|
byte[] tmp = _tmp;
|
||||||
|
tmp[0] = 0xce;
|
||||||
|
tmp[1] = (byte)(x >> 24);
|
||||||
|
tmp[2] = (byte)(x >> 16);
|
||||||
|
tmp[3] = (byte)(x >> 8);
|
||||||
|
tmp[4] = (byte)x;
|
||||||
|
_strm.Write (tmp, 0, 5);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Write (ulong x)
|
||||||
|
{
|
||||||
|
if (x < 0x100000000) {
|
||||||
|
Write ((uint)x);
|
||||||
|
} else {
|
||||||
|
byte[] tmp = _tmp;
|
||||||
|
tmp[0] = 0xcf;
|
||||||
|
tmp[1] = (byte)(x >> 56);
|
||||||
|
tmp[2] = (byte)(x >> 48);
|
||||||
|
tmp[3] = (byte)(x >> 40);
|
||||||
|
tmp[4] = (byte)(x >> 32);
|
||||||
|
tmp[5] = (byte)(x >> 24);
|
||||||
|
tmp[6] = (byte)(x >> 16);
|
||||||
|
tmp[7] = (byte)(x >> 8);
|
||||||
|
tmp[8] = (byte)x;
|
||||||
|
_strm.Write (tmp, 0, 9);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Write (sbyte x)
|
||||||
|
{
|
||||||
|
if (x >= -32 && x <= -1) {
|
||||||
|
_strm.WriteByte ((byte)(0xe0 | (byte)x));
|
||||||
|
} else if (x >= 0 && x <= 127) {
|
||||||
|
_strm.WriteByte ((byte)x);
|
||||||
|
} else {
|
||||||
|
byte[] tmp = _tmp;
|
||||||
|
tmp[0] = 0xd0;
|
||||||
|
tmp[1] = (byte)x;
|
||||||
|
_strm.Write (tmp, 0, 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Write (short x)
|
||||||
|
{
|
||||||
|
if (x >= sbyte.MinValue && x <= sbyte.MaxValue) {
|
||||||
|
Write ((sbyte)x);
|
||||||
|
} else {
|
||||||
|
byte[] tmp = _tmp;
|
||||||
|
tmp[0] = 0xd1;
|
||||||
|
tmp[1] = (byte)(x >> 8);
|
||||||
|
tmp[2] = (byte)x;
|
||||||
|
_strm.Write (tmp, 0, 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Write (int x)
|
||||||
|
{
|
||||||
|
if (x >= short.MinValue && x <= short.MaxValue) {
|
||||||
|
Write ((short)x);
|
||||||
|
} else {
|
||||||
|
byte[] tmp = _tmp;
|
||||||
|
tmp[0] = 0xd2;
|
||||||
|
tmp[1] = (byte)(x >> 24);
|
||||||
|
tmp[2] = (byte)(x >> 16);
|
||||||
|
tmp[3] = (byte)(x >> 8);
|
||||||
|
tmp[4] = (byte)x;
|
||||||
|
_strm.Write (tmp, 0, 5);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Write (long x)
|
||||||
|
{
|
||||||
|
if (x >= int.MinValue && x <= int.MaxValue) {
|
||||||
|
Write ((int)x);
|
||||||
|
} else {
|
||||||
|
byte[] tmp = _tmp;
|
||||||
|
tmp[0] = 0xd3;
|
||||||
|
tmp[1] = (byte)(x >> 56);
|
||||||
|
tmp[2] = (byte)(x >> 48);
|
||||||
|
tmp[3] = (byte)(x >> 40);
|
||||||
|
tmp[4] = (byte)(x >> 32);
|
||||||
|
tmp[5] = (byte)(x >> 24);
|
||||||
|
tmp[6] = (byte)(x >> 16);
|
||||||
|
tmp[7] = (byte)(x >> 8);
|
||||||
|
tmp[8] = (byte)x;
|
||||||
|
_strm.Write (tmp, 0, 9);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void WriteNil ()
|
||||||
|
{
|
||||||
|
_strm.WriteByte (0xc0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Write (bool x)
|
||||||
|
{
|
||||||
|
_strm.WriteByte ((byte)(x ? 0xc3 : 0xc2));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Write (float x)
|
||||||
|
{
|
||||||
|
byte[] raw = BitConverter.GetBytes (x); // unsafeコードを使う?
|
||||||
|
byte[] tmp = _tmp;
|
||||||
|
|
||||||
|
tmp[0] = 0xca;
|
||||||
|
if (BitConverter.IsLittleEndian) {
|
||||||
|
tmp[1] = raw[3];
|
||||||
|
tmp[2] = raw[2];
|
||||||
|
tmp[3] = raw[1];
|
||||||
|
tmp[4] = raw[0];
|
||||||
|
} else {
|
||||||
|
tmp[1] = raw[0];
|
||||||
|
tmp[2] = raw[1];
|
||||||
|
tmp[3] = raw[2];
|
||||||
|
tmp[4] = raw[3];
|
||||||
|
}
|
||||||
|
_strm.Write (tmp, 0, 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Write (double x)
|
||||||
|
{
|
||||||
|
byte[] raw = BitConverter.GetBytes (x); // unsafeコードを使う?
|
||||||
|
byte[] tmp = _tmp;
|
||||||
|
|
||||||
|
tmp[0] = 0xcb;
|
||||||
|
if (BitConverter.IsLittleEndian) {
|
||||||
|
tmp[1] = raw[7];
|
||||||
|
tmp[2] = raw[6];
|
||||||
|
tmp[3] = raw[5];
|
||||||
|
tmp[4] = raw[4];
|
||||||
|
tmp[5] = raw[3];
|
||||||
|
tmp[6] = raw[2];
|
||||||
|
tmp[7] = raw[1];
|
||||||
|
tmp[8] = raw[0];
|
||||||
|
} else {
|
||||||
|
tmp[1] = raw[0];
|
||||||
|
tmp[2] = raw[1];
|
||||||
|
tmp[3] = raw[2];
|
||||||
|
tmp[4] = raw[3];
|
||||||
|
tmp[5] = raw[4];
|
||||||
|
tmp[6] = raw[5];
|
||||||
|
tmp[7] = raw[6];
|
||||||
|
tmp[8] = raw[7];
|
||||||
|
}
|
||||||
|
_strm.Write (tmp, 0, 9);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Write (byte[] bytes)
|
||||||
|
{
|
||||||
|
WriteRawHeader (bytes.Length);
|
||||||
|
_strm.Write (bytes, 0, bytes.Length);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void WriteRawHeader (int N)
|
||||||
|
{
|
||||||
|
WriteLengthHeader (N, 32, 0x96, 0xda, 0xdb);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void WriteArrayHeader (int N)
|
||||||
|
{
|
||||||
|
WriteLengthHeader (N, 16, 0x90, 0xdc, 0xdd);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void WriteMapHeader (int N)
|
||||||
|
{
|
||||||
|
WriteLengthHeader (N, 16, 0x80, 0xde, 0xdf);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WriteLengthHeader (int N, int fix_length, byte fix_prefix, byte len16bit_prefix, byte len32bit_prefix)
|
||||||
|
{
|
||||||
|
if (N < fix_length) {
|
||||||
|
_strm.WriteByte ((byte)(fix_prefix | N));
|
||||||
|
} else {
|
||||||
|
byte[] tmp = _tmp;
|
||||||
|
int header_len;
|
||||||
|
if (N < 0x10000) {
|
||||||
|
tmp[0] = len16bit_prefix;
|
||||||
|
tmp[1] = (byte)(N >> 8);
|
||||||
|
tmp[2] = (byte)N;
|
||||||
|
header_len = 3;
|
||||||
|
} else {
|
||||||
|
tmp[0] = len32bit_prefix;
|
||||||
|
tmp[1] = (byte)(N >> 24);
|
||||||
|
tmp[2] = (byte)(N >> 16);
|
||||||
|
tmp[3] = (byte)(N >> 8);
|
||||||
|
tmp[4] = (byte)N;
|
||||||
|
header_len = 5;
|
||||||
|
}
|
||||||
|
_strm.Write (tmp, 0, header_len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Write (String x)
|
||||||
|
{
|
||||||
|
Write (_encoding.GetBytes (x));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
32
csharp/msgpack/TypePrefixes.cs
Normal file
32
csharp/msgpack/TypePrefixes.cs
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
namespace msgpack
|
||||||
|
{
|
||||||
|
public enum TypePrefixes : byte
|
||||||
|
{
|
||||||
|
PositiveFixNum = 0x00, // 0x00 - 0x7f
|
||||||
|
NegativeFixNum = 0xe0, // 0xe0 - 0xff
|
||||||
|
|
||||||
|
Nil = 0xc0,
|
||||||
|
False = 0xc2,
|
||||||
|
True = 0xc3,
|
||||||
|
Float = 0xca,
|
||||||
|
Double = 0xcb,
|
||||||
|
UInt8 = 0xcc,
|
||||||
|
UInt16 = 0xcd,
|
||||||
|
UInt32 = 0xce,
|
||||||
|
UInt64 = 0xcf,
|
||||||
|
Int8 = 0xd0,
|
||||||
|
Int16 = 0xd1,
|
||||||
|
Int32 = 0xd2,
|
||||||
|
Int64 = 0xd3,
|
||||||
|
Raw16 = 0xda,
|
||||||
|
Raw32 = 0xdb,
|
||||||
|
Array16 = 0xdc,
|
||||||
|
Array32 = 0xdd,
|
||||||
|
Map16 = 0xde,
|
||||||
|
Map32 = 0xdf,
|
||||||
|
|
||||||
|
FixRaw = 0xa0, // 0xa0 - 0xbf
|
||||||
|
FixArray = 0x90, // 0x90 - 0x9f
|
||||||
|
FixMap = 0x80, // 0x80 - 0x8f
|
||||||
|
}
|
||||||
|
}
|
53
csharp/msgpack/msgpack.csproj
Normal file
53
csharp/msgpack/msgpack.csproj
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<PropertyGroup>
|
||||||
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
|
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||||
|
<ProductVersion>8.0.30703</ProductVersion>
|
||||||
|
<SchemaVersion>2.0</SchemaVersion>
|
||||||
|
<ProjectGuid>{E1809531-EC2A-4EA6-B0E8-CC815EDFAA2F}</ProjectGuid>
|
||||||
|
<OutputType>Library</OutputType>
|
||||||
|
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||||
|
<RootNamespace>msgpack</RootNamespace>
|
||||||
|
<AssemblyName>msgpack</AssemblyName>
|
||||||
|
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||||
|
<FileAlignment>512</FileAlignment>
|
||||||
|
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
|
<DebugSymbols>true</DebugSymbols>
|
||||||
|
<DebugType>full</DebugType>
|
||||||
|
<Optimize>false</Optimize>
|
||||||
|
<OutputPath>bin\Debug\</OutputPath>
|
||||||
|
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||||
|
<DebugType>pdbonly</DebugType>
|
||||||
|
<Optimize>true</Optimize>
|
||||||
|
<OutputPath>bin\Release\</OutputPath>
|
||||||
|
<DefineConstants>TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Reference Include="System" />
|
||||||
|
<Reference Include="System.Core" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Include="AssemblyInfo.cs" />
|
||||||
|
<Compile Include="BoxingPacker.cs" />
|
||||||
|
<Compile Include="MsgPackReader.cs" />
|
||||||
|
<Compile Include="MsgPackWriter.cs" />
|
||||||
|
<Compile Include="TypePrefixes.cs" />
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
|
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||||
|
Other similar extension points exist, see Microsoft.Common.targets.
|
||||||
|
<Target Name="BeforeBuild">
|
||||||
|
</Target>
|
||||||
|
<Target Name="AfterBuild">
|
||||||
|
</Target>
|
||||||
|
-->
|
||||||
|
</Project>
|
Loading…
x
Reference in New Issue
Block a user