mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-04-25 03:24:47 +01:00
merge common implementations and server implementations
This commit is contained in:
110
Emby.Server.Implementations/IO/SharpCifs/Util/Base64.cs
Normal file
110
Emby.Server.Implementations/IO/SharpCifs/Util/Base64.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
// This code is derived from jcifs smb client library <jcifs at samba dot org>
|
||||
// Ported by J. Arturo <webmaster at komodosoft dot net>
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace SharpCifs.Util
|
||||
{
|
||||
public class Base64
|
||||
{
|
||||
private static readonly string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
/// <summary>Base-64 encodes the supplied block of data.</summary>
|
||||
/// <remarks>
|
||||
/// Base-64 encodes the supplied block of data. Line wrapping is not
|
||||
/// applied on output.
|
||||
/// </remarks>
|
||||
/// <param name="bytes">The block of data that is to be Base-64 encoded.</param>
|
||||
/// <returns>A <code>String</code> containing the encoded data.</returns>
|
||||
public static string Encode(byte[] bytes)
|
||||
{
|
||||
int length = bytes.Length;
|
||||
if (length == 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
StringBuilder buffer = new StringBuilder((int)Math.Ceiling(length / 3d) * 4);
|
||||
int remainder = length % 3;
|
||||
length -= remainder;
|
||||
int block;
|
||||
int i = 0;
|
||||
while (i < length)
|
||||
{
|
||||
block = ((bytes[i++] & unchecked(0xff)) << 16) | ((bytes[i++] & unchecked(
|
||||
0xff)) << 8) | (bytes[i++] & unchecked(0xff));
|
||||
buffer.Append(Alphabet[(int)(((uint)block) >> 18)]);
|
||||
buffer.Append(Alphabet[((int)(((uint)block) >> 12)) & unchecked(0x3f)]);
|
||||
buffer.Append(Alphabet[((int)(((uint)block) >> 6)) & unchecked(0x3f)]);
|
||||
buffer.Append(Alphabet[block & unchecked(0x3f)]);
|
||||
}
|
||||
if (remainder == 0)
|
||||
{
|
||||
return buffer.ToString();
|
||||
}
|
||||
if (remainder == 1)
|
||||
{
|
||||
block = (bytes[i] & unchecked(0xff)) << 4;
|
||||
buffer.Append(Alphabet[(int)(((uint)block) >> 6)]);
|
||||
buffer.Append(Alphabet[block & unchecked(0x3f)]);
|
||||
buffer.Append("==");
|
||||
return buffer.ToString();
|
||||
}
|
||||
block = (((bytes[i++] & unchecked(0xff)) << 8) | ((bytes[i]) & unchecked(0xff))) << 2;
|
||||
buffer.Append(Alphabet[(int)(((uint)block) >> 12)]);
|
||||
buffer.Append(Alphabet[((int)(((uint)block) >> 6)) & unchecked(0x3f)]);
|
||||
buffer.Append(Alphabet[block & unchecked(0x3f)]);
|
||||
buffer.Append("=");
|
||||
return buffer.ToString();
|
||||
}
|
||||
|
||||
/// <summary>Decodes the supplied Base-64 encoded string.</summary>
|
||||
/// <remarks>Decodes the supplied Base-64 encoded string.</remarks>
|
||||
/// <param name="string">The Base-64 encoded string that is to be decoded.</param>
|
||||
/// <returns>A <code>byte[]</code> containing the decoded data block.</returns>
|
||||
public static byte[] Decode(string @string)
|
||||
{
|
||||
int length = @string.Length;
|
||||
if (length == 0)
|
||||
{
|
||||
return new byte[0];
|
||||
}
|
||||
int pad = (@string[length - 2] == '=') ? 2 : (@string[length - 1] == '=') ? 1 : 0;
|
||||
int size = length * 3 / 4 - pad;
|
||||
byte[] buffer = new byte[size];
|
||||
int block;
|
||||
int i = 0;
|
||||
int index = 0;
|
||||
while (i < length)
|
||||
{
|
||||
block = (Alphabet.IndexOf(@string[i++]) & unchecked(0xff)) << 18 | (Alphabet
|
||||
.IndexOf(@string[i++]) & unchecked(0xff)) << 12 | (Alphabet.IndexOf(@string
|
||||
[i++]) & unchecked(0xff)) << 6 | (Alphabet.IndexOf(@string[i++]) & unchecked(
|
||||
0xff));
|
||||
buffer[index++] = unchecked((byte)((int)(((uint)block) >> 16)));
|
||||
if (index < size)
|
||||
{
|
||||
buffer[index++] = unchecked((byte)(((int)(((uint)block) >> 8)) & unchecked(0xff)));
|
||||
}
|
||||
if (index < size)
|
||||
{
|
||||
buffer[index++] = unchecked((byte)(block & unchecked(0xff)));
|
||||
}
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
568
Emby.Server.Implementations/IO/SharpCifs/Util/DES.cs
Normal file
568
Emby.Server.Implementations/IO/SharpCifs/Util/DES.cs
Normal file
@@ -0,0 +1,568 @@
|
||||
// This code is derived from jcifs smb client library <jcifs at samba dot org>
|
||||
// Ported by J. Arturo <webmaster at komodosoft dot net>
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
using System;
|
||||
|
||||
namespace SharpCifs.Util
|
||||
{
|
||||
/// <summary>
|
||||
/// This code is derived from the above source
|
||||
/// JCIFS API
|
||||
/// Norbert Hranitzky
|
||||
/// <p>and modified again by Michael B.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This code is derived from the above source
|
||||
/// JCIFS API
|
||||
/// Norbert Hranitzky
|
||||
/// <p>and modified again by Michael B. Allen
|
||||
/// </remarks>
|
||||
public class DES
|
||||
{
|
||||
private int[] _encryptKeys = new int[32];
|
||||
|
||||
private int[] _decryptKeys = new int[32];
|
||||
|
||||
private int[] _tempInts = new int[2];
|
||||
|
||||
public DES()
|
||||
{
|
||||
}
|
||||
|
||||
public DES(byte[] key)
|
||||
{
|
||||
// DesCipher - the DES encryption method
|
||||
//
|
||||
// The meat of this code is by Dave Zimmerman <dzimm@widget.com>, and is:
|
||||
//
|
||||
// Copyright (c) 1996 Widget Workshop, Inc. All Rights Reserved.
|
||||
//
|
||||
// Permission to use, copy, modify, and distribute this software
|
||||
// and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
|
||||
// without fee is hereby granted, provided that this copyright notice is kept
|
||||
// intact.
|
||||
//
|
||||
// WIDGET WORKSHOP MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
|
||||
// OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||||
// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
// PARTICULAR PURPOSE, OR NON-INFRINGEMENT. WIDGET WORKSHOP SHALL NOT BE LIABLE
|
||||
// FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
|
||||
// DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
|
||||
//
|
||||
// THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
|
||||
// CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
|
||||
// PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
|
||||
// NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
|
||||
// SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
|
||||
// SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
|
||||
// PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). WIDGET WORKSHOP
|
||||
// SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
|
||||
// HIGH RISK ACTIVITIES.
|
||||
//
|
||||
//
|
||||
// The rest is:
|
||||
//
|
||||
// Copyright (C) 1996 by Jef Poskanzer <jef@acme.com>. All rights reserved.
|
||||
//
|
||||
// Copyright (C) 1996 by Wolfgang Platzer
|
||||
// email: wplatzer@iaik.tu-graz.ac.at
|
||||
//
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
// 1. Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
// SUCH DAMAGE.
|
||||
//
|
||||
// Constructor, byte-array key.
|
||||
if (key.Length == 7)
|
||||
{
|
||||
byte[] key8 = new byte[8];
|
||||
MakeSmbKey(key, key8);
|
||||
SetKey(key8);
|
||||
}
|
||||
else
|
||||
{
|
||||
SetKey(key);
|
||||
}
|
||||
}
|
||||
|
||||
public static void MakeSmbKey(byte[] key7, byte[] key8)
|
||||
{
|
||||
int i;
|
||||
key8[0] = unchecked((byte)((key7[0] >> 1) & unchecked(0xff)));
|
||||
key8[1] = unchecked((byte)((((key7[0] & unchecked(0x01)) << 6) | (((key7[1
|
||||
] & unchecked(0xff)) >> 2) & unchecked(0xff))) & unchecked(0xff)));
|
||||
key8[2] = unchecked((byte)((((key7[1] & unchecked(0x03)) << 5) | (((key7[2
|
||||
] & unchecked(0xff)) >> 3) & unchecked(0xff))) & unchecked(0xff)));
|
||||
key8[3] = unchecked((byte)((((key7[2] & unchecked(0x07)) << 4) | (((key7[3
|
||||
] & unchecked(0xff)) >> 4) & unchecked(0xff))) & unchecked(0xff)));
|
||||
key8[4] = unchecked((byte)((((key7[3] & unchecked(0x0F)) << 3) | (((key7[4
|
||||
] & unchecked(0xff)) >> 5) & unchecked(0xff))) & unchecked(0xff)));
|
||||
key8[5] = unchecked((byte)((((key7[4] & unchecked(0x1F)) << 2) | (((key7[5
|
||||
] & unchecked(0xff)) >> 6) & unchecked(0xff))) & unchecked(0xff)));
|
||||
key8[6] = unchecked((byte)((((key7[5] & unchecked(0x3F)) << 1) | (((key7[6
|
||||
] & unchecked(0xff)) >> 7) & unchecked(0xff))) & unchecked(0xff)));
|
||||
key8[7] = unchecked((byte)(key7[6] & unchecked(0x7F)));
|
||||
for (i = 0; i < 8; i++)
|
||||
{
|
||||
key8[i] = unchecked((byte)(key8[i] << 1));
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the key.
|
||||
public virtual void SetKey(byte[] key)
|
||||
{
|
||||
// CHECK PAROTY TBD
|
||||
Deskey(key, true, _encryptKeys);
|
||||
Deskey(key, false, _decryptKeys);
|
||||
}
|
||||
|
||||
// Turn an 8-byte key into internal keys.
|
||||
private void Deskey(byte[] keyBlock, bool encrypting, int[] knL)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
int l;
|
||||
int m;
|
||||
int n;
|
||||
int[] pc1M = new int[56];
|
||||
int[] pcr = new int[56];
|
||||
int[] kn = new int[32];
|
||||
for (j = 0; j < 56; ++j)
|
||||
{
|
||||
l = _pc1[j];
|
||||
m = l & 0x7;
|
||||
pc1M[j] = ((keyBlock[(int)(((uint)l) >> 3)] & _bytebit[m]) != 0) ? 1 : 0;
|
||||
}
|
||||
for (i = 0; i < 16; ++i)
|
||||
{
|
||||
if (encrypting)
|
||||
{
|
||||
m = i << 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
m = (15 - i) << 1;
|
||||
}
|
||||
n = m + 1;
|
||||
kn[m] = kn[n] = 0;
|
||||
for (j = 0; j < 28; ++j)
|
||||
{
|
||||
l = j + _totrot[i];
|
||||
if (l < 28)
|
||||
{
|
||||
pcr[j] = pc1M[l];
|
||||
}
|
||||
else
|
||||
{
|
||||
pcr[j] = pc1M[l - 28];
|
||||
}
|
||||
}
|
||||
for (j = 28; j < 56; ++j)
|
||||
{
|
||||
l = j + _totrot[i];
|
||||
if (l < 56)
|
||||
{
|
||||
pcr[j] = pc1M[l];
|
||||
}
|
||||
else
|
||||
{
|
||||
pcr[j] = pc1M[l - 28];
|
||||
}
|
||||
}
|
||||
for (j = 0; j < 24; ++j)
|
||||
{
|
||||
if (pcr[_pc2[j]] != 0)
|
||||
{
|
||||
kn[m] |= _bigbyte[j];
|
||||
}
|
||||
if (pcr[_pc2[j + 24]] != 0)
|
||||
{
|
||||
kn[n] |= _bigbyte[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
Cookey(kn, knL);
|
||||
}
|
||||
|
||||
private void Cookey(int[] raw, int[] knL)
|
||||
{
|
||||
int raw0;
|
||||
int raw1;
|
||||
int rawi;
|
||||
int knLi;
|
||||
int i;
|
||||
for (i = 0, rawi = 0, knLi = 0; i < 16; ++i)
|
||||
{
|
||||
raw0 = raw[rawi++];
|
||||
raw1 = raw[rawi++];
|
||||
knL[knLi] = (raw0 & unchecked(0x00fc0000)) << 6;
|
||||
knL[knLi] |= (raw0 & unchecked(0x00000fc0)) << 10;
|
||||
knL[knLi] |= (int)(((uint)(raw1 & unchecked(0x00fc0000))) >> 10);
|
||||
knL[knLi] |= (int)(((uint)(raw1 & unchecked(0x00000fc0))) >> 6);
|
||||
++knLi;
|
||||
knL[knLi] = (raw0 & unchecked(0x0003f000)) << 12;
|
||||
knL[knLi] |= (raw0 & unchecked(0x0000003f)) << 16;
|
||||
knL[knLi] |= (int)(((uint)(raw1 & unchecked(0x0003f000))) >> 4);
|
||||
knL[knLi] |= (raw1 & unchecked(0x0000003f));
|
||||
++knLi;
|
||||
}
|
||||
}
|
||||
|
||||
/// Encrypt a block of eight bytes.
|
||||
private void Encrypt(byte[] clearText, int clearOff, byte[] cipherText, int cipherOff
|
||||
)
|
||||
{
|
||||
SquashBytesToInts(clearText, clearOff, _tempInts, 0, 2);
|
||||
Des(_tempInts, _tempInts, _encryptKeys);
|
||||
SpreadIntsToBytes(_tempInts, 0, cipherText, cipherOff, 2);
|
||||
}
|
||||
|
||||
/// Decrypt a block of eight bytes.
|
||||
private void Decrypt(byte[] cipherText, int cipherOff, byte[] clearText, int clearOff
|
||||
)
|
||||
{
|
||||
SquashBytesToInts(cipherText, cipherOff, _tempInts, 0, 2);
|
||||
Des(_tempInts, _tempInts, _decryptKeys);
|
||||
SpreadIntsToBytes(_tempInts, 0, clearText, clearOff, 2);
|
||||
}
|
||||
|
||||
// The DES function.
|
||||
private void Des(int[] inInts, int[] outInts, int[] keys)
|
||||
{
|
||||
int fval;
|
||||
int work;
|
||||
int right;
|
||||
int leftt;
|
||||
int round;
|
||||
int keysi = 0;
|
||||
leftt = inInts[0];
|
||||
right = inInts[1];
|
||||
work = (((int)(((uint)leftt) >> 4)) ^ right) & unchecked(0x0f0f0f0f);
|
||||
right ^= work;
|
||||
leftt ^= (work << 4);
|
||||
work = (((int)(((uint)leftt) >> 16)) ^ right) & unchecked(0x0000ffff);
|
||||
right ^= work;
|
||||
leftt ^= (work << 16);
|
||||
work = (((int)(((uint)right) >> 2)) ^ leftt) & unchecked(0x33333333);
|
||||
leftt ^= work;
|
||||
right ^= (work << 2);
|
||||
work = (((int)(((uint)right) >> 8)) ^ leftt) & unchecked(0x00ff00ff);
|
||||
leftt ^= work;
|
||||
right ^= (work << 8);
|
||||
right = (right << 1) | (((int)(((uint)right) >> 31)) & 1);
|
||||
work = (leftt ^ right) & unchecked((int)(0xaaaaaaaa));
|
||||
leftt ^= work;
|
||||
right ^= work;
|
||||
leftt = (leftt << 1) | (((int)(((uint)leftt) >> 31)) & 1);
|
||||
for (round = 0; round < 8; ++round)
|
||||
{
|
||||
work = (right << 28) | ((int)(((uint)right) >> 4));
|
||||
work ^= keys[keysi++];
|
||||
fval = _sp7[work & unchecked(0x0000003f)];
|
||||
fval |= _sp5[((int)(((uint)work) >> 8)) & unchecked(0x0000003f)];
|
||||
fval |= _sp3[((int)(((uint)work) >> 16)) & unchecked(0x0000003f)];
|
||||
fval |= _sp1[((int)(((uint)work) >> 24)) & unchecked(0x0000003f)];
|
||||
work = right ^ keys[keysi++];
|
||||
fval |= _sp8[work & unchecked(0x0000003f)];
|
||||
fval |= _sp6[((int)(((uint)work) >> 8)) & unchecked(0x0000003f)];
|
||||
fval |= _sp4[((int)(((uint)work) >> 16)) & unchecked(0x0000003f)];
|
||||
fval |= _sp2[((int)(((uint)work) >> 24)) & unchecked(0x0000003f)];
|
||||
leftt ^= fval;
|
||||
work = (leftt << 28) | ((int)(((uint)leftt) >> 4));
|
||||
work ^= keys[keysi++];
|
||||
fval = _sp7[work & unchecked(0x0000003f)];
|
||||
fval |= _sp5[((int)(((uint)work) >> 8)) & unchecked(0x0000003f)];
|
||||
fval |= _sp3[((int)(((uint)work) >> 16)) & unchecked(0x0000003f)];
|
||||
fval |= _sp1[((int)(((uint)work) >> 24)) & unchecked(0x0000003f)];
|
||||
work = leftt ^ keys[keysi++];
|
||||
fval |= _sp8[work & unchecked(0x0000003f)];
|
||||
fval |= _sp6[((int)(((uint)work) >> 8)) & unchecked(0x0000003f)];
|
||||
fval |= _sp4[((int)(((uint)work) >> 16)) & unchecked(0x0000003f)];
|
||||
fval |= _sp2[((int)(((uint)work) >> 24)) & unchecked(0x0000003f)];
|
||||
right ^= fval;
|
||||
}
|
||||
right = (right << 31) | ((int)(((uint)right) >> 1));
|
||||
work = (leftt ^ right) & unchecked((int)(0xaaaaaaaa));
|
||||
leftt ^= work;
|
||||
right ^= work;
|
||||
leftt = (leftt << 31) | ((int)(((uint)leftt) >> 1));
|
||||
work = (((int)(((uint)leftt) >> 8)) ^ right) & unchecked(0x00ff00ff);
|
||||
right ^= work;
|
||||
leftt ^= (work << 8);
|
||||
work = (((int)(((uint)leftt) >> 2)) ^ right) & unchecked(0x33333333);
|
||||
right ^= work;
|
||||
leftt ^= (work << 2);
|
||||
work = (((int)(((uint)right) >> 16)) ^ leftt) & unchecked(0x0000ffff);
|
||||
leftt ^= work;
|
||||
right ^= (work << 16);
|
||||
work = (((int)(((uint)right) >> 4)) ^ leftt) & unchecked(0x0f0f0f0f);
|
||||
leftt ^= work;
|
||||
right ^= (work << 4);
|
||||
outInts[0] = right;
|
||||
outInts[1] = leftt;
|
||||
}
|
||||
|
||||
/// Encrypt a block of bytes.
|
||||
public virtual void Encrypt(byte[] clearText, byte[] cipherText)
|
||||
{
|
||||
Encrypt(clearText, 0, cipherText, 0);
|
||||
}
|
||||
|
||||
/// Decrypt a block of bytes.
|
||||
public virtual void Decrypt(byte[] cipherText, byte[] clearText)
|
||||
{
|
||||
Decrypt(cipherText, 0, clearText, 0);
|
||||
}
|
||||
|
||||
/// <summary>encrypts an array where the length must be a multiple of 8</summary>
|
||||
public virtual byte[] Encrypt(byte[] clearText)
|
||||
{
|
||||
int length = clearText.Length;
|
||||
if (length % 8 != 0)
|
||||
{
|
||||
Console.Out.WriteLine("Array must be a multiple of 8");
|
||||
return null;
|
||||
}
|
||||
byte[] cipherText = new byte[length];
|
||||
int count = length / 8;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
Encrypt(clearText, i * 8, cipherText, i * 8);
|
||||
}
|
||||
return cipherText;
|
||||
}
|
||||
|
||||
/// <summary>decrypts an array where the length must be a multiple of 8</summary>
|
||||
public virtual byte[] Decrypt(byte[] cipherText)
|
||||
{
|
||||
int length = cipherText.Length;
|
||||
if (length % 8 != 0)
|
||||
{
|
||||
Console.Out.WriteLine("Array must be a multiple of 8");
|
||||
return null;
|
||||
}
|
||||
byte[] clearText = new byte[length];
|
||||
int count = length / 8;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
Encrypt(cipherText, i * 8, clearText, i * 8);
|
||||
}
|
||||
return clearText;
|
||||
}
|
||||
|
||||
private static byte[] _bytebit = { unchecked(unchecked(0x80)), unchecked(unchecked(0x40)), unchecked(unchecked(0x20)), unchecked(unchecked(0x10)), unchecked(unchecked(0x08)), unchecked(unchecked(0x04)), unchecked(unchecked(0x02)), unchecked(unchecked(0x01)) };
|
||||
|
||||
private static int[] _bigbyte = { unchecked(0x800000), unchecked(
|
||||
0x400000), unchecked(0x200000), unchecked(0x100000), unchecked(
|
||||
0x080000), unchecked(0x040000), unchecked(0x020000), unchecked(
|
||||
0x010000), unchecked(0x008000), unchecked(0x004000), unchecked(
|
||||
0x002000), unchecked(0x001000), unchecked(0x000800), unchecked(
|
||||
0x000400), unchecked(0x000200), unchecked(0x000100), unchecked(
|
||||
0x000080), unchecked(0x000040), unchecked(0x000020), unchecked(
|
||||
0x000010), unchecked(0x000008), unchecked(0x000004), unchecked(
|
||||
0x000002), unchecked(0x000001) };
|
||||
|
||||
private static byte[] _pc1 = { unchecked(56), unchecked(48)
|
||||
, unchecked(40), unchecked(32), unchecked(24), unchecked(16), unchecked(8), unchecked(0), unchecked(57), unchecked(49), unchecked(41), unchecked(33), unchecked(25), unchecked(17), unchecked(9), unchecked(1), unchecked(58), unchecked(
|
||||
50), unchecked(42), unchecked(34), unchecked(26), unchecked(
|
||||
18), unchecked(10), unchecked(2), unchecked(59), unchecked(
|
||||
51), unchecked(43), unchecked(35), unchecked(62), unchecked(
|
||||
54), unchecked(46), unchecked(38), unchecked(30), unchecked(
|
||||
22), unchecked(14), unchecked(6), unchecked(61), unchecked(
|
||||
53), unchecked(45), unchecked(37), unchecked(29), unchecked(
|
||||
21), unchecked(13), unchecked(5), unchecked(60), unchecked(
|
||||
52), unchecked(44), unchecked(36), unchecked(28), unchecked(
|
||||
20), unchecked(12), unchecked(4), unchecked(27), unchecked(
|
||||
19), unchecked(11), unchecked(3) };
|
||||
|
||||
private static int[] _totrot = { 1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19,
|
||||
21, 23, 25, 27, 28 };
|
||||
|
||||
private static byte[] _pc2 = { unchecked(13), unchecked(16)
|
||||
, unchecked(10), unchecked(23), unchecked(0), unchecked(4), unchecked(2), unchecked(27), unchecked(14), unchecked(5), unchecked(20), unchecked(9), unchecked(22), unchecked(18), unchecked(11), unchecked(3), unchecked(25), unchecked(7), unchecked(15), unchecked(6), unchecked(26), unchecked(19), unchecked(12), unchecked(1), unchecked(40), unchecked(51), unchecked(30), unchecked(36), unchecked(46), unchecked(54), unchecked(29), unchecked(39), unchecked(50), unchecked(
|
||||
44), unchecked(32), unchecked(47), unchecked(43), unchecked(
|
||||
48), unchecked(38), unchecked(55), unchecked(33), unchecked(
|
||||
52), unchecked(45), unchecked(41), unchecked(49), unchecked(
|
||||
35), unchecked(28), unchecked(31) };
|
||||
|
||||
private static int[] _sp1 = { unchecked(0x01010400), unchecked(0x00000000), unchecked(0x00010000), unchecked(0x01010404), unchecked(
|
||||
0x01010004), unchecked(0x00010404), unchecked(0x00000004),
|
||||
unchecked(0x00010000), unchecked(0x00000400), unchecked(0x01010400), unchecked(0x01010404), unchecked(0x00000400), unchecked(0x01000404), unchecked(0x01010004), unchecked(0x01000000), unchecked(
|
||||
0x00000004), unchecked(0x00000404), unchecked(0x01000400),
|
||||
unchecked(0x01000400), unchecked(0x00010400), unchecked(0x00010400), unchecked(0x01010000), unchecked(0x01010000), unchecked(0x01000404), unchecked(0x00010004), unchecked(0x01000004), unchecked(
|
||||
0x01000004), unchecked(0x00010004), unchecked(0x00000000),
|
||||
unchecked(0x00000404), unchecked(0x00010404), unchecked(0x01000000), unchecked(0x00010000), unchecked(0x01010404), unchecked(0x00000004), unchecked(0x01010000), unchecked(0x01010400), unchecked(
|
||||
0x01000000), unchecked(0x01000000), unchecked(0x00000400),
|
||||
unchecked(0x01010004), unchecked(0x00010000), unchecked(0x00010400), unchecked(0x01000004), unchecked(0x00000400), unchecked(0x00000004), unchecked(0x01000404), unchecked(0x00010404), unchecked(
|
||||
0x01010404), unchecked(0x00010004), unchecked(0x01010000),
|
||||
unchecked(0x01000404), unchecked(0x01000004), unchecked(0x00000404), unchecked(0x00010404), unchecked(0x01010400), unchecked(0x00000404), unchecked(0x01000400), unchecked(0x01000400), unchecked(
|
||||
0x00000000), unchecked(0x00010004), unchecked(0x00010400),
|
||||
unchecked(0x00000000), unchecked(0x01010004) };
|
||||
|
||||
private static int[] _sp2 = { unchecked((int)(0x80108020)), unchecked((int
|
||||
)(0x80008000)), unchecked(0x00008000), unchecked(0x00108020), unchecked(
|
||||
0x00100000), unchecked(0x00000020), unchecked((int)(0x80100020)),
|
||||
unchecked((int)(0x80008020)), unchecked((int)(0x80000020)), unchecked((int)(0x80108020
|
||||
)), unchecked((int)(0x80108000)), unchecked((int)(0x80000000)), unchecked((int)(
|
||||
0x80008000)), unchecked(0x00100000), unchecked(0x00000020), unchecked(
|
||||
(int)(0x80100020)), unchecked(0x00108000), unchecked(0x00100020),
|
||||
unchecked((int)(0x80008020)), unchecked(0x00000000), unchecked((int)(0x80000000
|
||||
)), unchecked(0x00008000), unchecked(0x00108020), unchecked((int)(
|
||||
0x80100000)), unchecked(0x00100020), unchecked((int)(0x80000020)), unchecked(
|
||||
0x00000000), unchecked(0x00108000), unchecked(0x00008020),
|
||||
unchecked((int)(0x80108000)), unchecked((int)(0x80100000)), unchecked(0x00008020), unchecked(0x00000000), unchecked(0x00108020), unchecked((int)(
|
||||
0x80100020)), unchecked(0x00100000), unchecked((int)(0x80008020)), unchecked(
|
||||
(int)(0x80100000)), unchecked((int)(0x80108000)), unchecked(0x00008000),
|
||||
unchecked((int)(0x80100000)), unchecked((int)(0x80008000)), unchecked(0x00000020), unchecked((int)(0x80108020)), unchecked(0x00108020), unchecked(0x00000020), unchecked(0x00008000), unchecked((int)(0x80000000)), unchecked(
|
||||
0x00008020), unchecked((int)(0x80108000)), unchecked(0x00100000),
|
||||
unchecked((int)(0x80000020)), unchecked(0x00100020), unchecked((int)(0x80008020
|
||||
)), unchecked((int)(0x80000020)), unchecked(0x00100020), unchecked(0x00108000), unchecked(0x00000000), unchecked((int)(0x80008000)), unchecked(
|
||||
0x00008020), unchecked((int)(0x80000000)), unchecked((int)(0x80100020)),
|
||||
unchecked((int)(0x80108020)), unchecked(0x00108000) };
|
||||
|
||||
private static int[] _sp3 = { unchecked(0x00000208), unchecked(0x08020200), unchecked(0x00000000), unchecked(0x08020008), unchecked(
|
||||
0x08000200), unchecked(0x00000000), unchecked(0x00020208),
|
||||
unchecked(0x08000200), unchecked(0x00020008), unchecked(0x08000008), unchecked(0x08000008), unchecked(0x00020000), unchecked(0x08020208), unchecked(0x00020008), unchecked(0x08020000), unchecked(
|
||||
0x00000208), unchecked(0x08000000), unchecked(0x00000008),
|
||||
unchecked(0x08020200), unchecked(0x00000200), unchecked(0x00020200), unchecked(0x08020000), unchecked(0x08020008), unchecked(0x00020208), unchecked(0x08000208), unchecked(0x00020200), unchecked(
|
||||
0x00020000), unchecked(0x08000208), unchecked(0x00000008),
|
||||
unchecked(0x08020208), unchecked(0x00000200), unchecked(0x08000000), unchecked(0x08020200), unchecked(0x08000000), unchecked(0x00020008), unchecked(0x00000208), unchecked(0x00020000), unchecked(
|
||||
0x08020200), unchecked(0x08000200), unchecked(0x00000000),
|
||||
unchecked(0x00000200), unchecked(0x00020008), unchecked(0x08020208), unchecked(0x08000200), unchecked(0x08000008), unchecked(0x00000200), unchecked(0x00000000), unchecked(0x08020008), unchecked(
|
||||
0x08000208), unchecked(0x00020000), unchecked(0x08000000),
|
||||
unchecked(0x08020208), unchecked(0x00000008), unchecked(0x00020208), unchecked(0x00020200), unchecked(0x08000008), unchecked(0x08020000), unchecked(0x08000208), unchecked(0x00000208), unchecked(
|
||||
0x08020000), unchecked(0x00020208), unchecked(0x00000008),
|
||||
unchecked(0x08020008), unchecked(0x00020200) };
|
||||
|
||||
private static int[] _sp4 = { unchecked(0x00802001), unchecked(0x00002081), unchecked(0x00002081), unchecked(0x00000080), unchecked(
|
||||
0x00802080), unchecked(0x00800081), unchecked(0x00800001),
|
||||
unchecked(0x00002001), unchecked(0x00000000), unchecked(0x00802000), unchecked(0x00802000), unchecked(0x00802081), unchecked(0x00000081), unchecked(0x00000000), unchecked(0x00800080), unchecked(
|
||||
0x00800001), unchecked(0x00000001), unchecked(0x00002000),
|
||||
unchecked(0x00800000), unchecked(0x00802001), unchecked(0x00000080), unchecked(0x00800000), unchecked(0x00002001), unchecked(0x00002080), unchecked(0x00800081), unchecked(0x00000001), unchecked(
|
||||
0x00002080), unchecked(0x00800080), unchecked(0x00002000),
|
||||
unchecked(0x00802080), unchecked(0x00802081), unchecked(0x00000081), unchecked(0x00800080), unchecked(0x00800001), unchecked(0x00802000), unchecked(0x00802081), unchecked(0x00000081), unchecked(
|
||||
0x00000000), unchecked(0x00000000), unchecked(0x00802000),
|
||||
unchecked(0x00002080), unchecked(0x00800080), unchecked(0x00800081), unchecked(0x00000001), unchecked(0x00802001), unchecked(0x00002081), unchecked(0x00002081), unchecked(0x00000080), unchecked(
|
||||
0x00802081), unchecked(0x00000081), unchecked(0x00000001),
|
||||
unchecked(0x00002000), unchecked(0x00800001), unchecked(0x00002001), unchecked(0x00802080), unchecked(0x00800081), unchecked(0x00002001), unchecked(0x00002080), unchecked(0x00800000), unchecked(
|
||||
0x00802001), unchecked(0x00000080), unchecked(0x00800000),
|
||||
unchecked(0x00002000), unchecked(0x00802080) };
|
||||
|
||||
private static int[] _sp5 = { unchecked(0x00000100), unchecked(0x02080100), unchecked(0x02080000), unchecked(0x42000100), unchecked(
|
||||
0x00080000), unchecked(0x00000100), unchecked(0x40000000),
|
||||
unchecked(0x02080000), unchecked(0x40080100), unchecked(0x00080000), unchecked(0x02000100), unchecked(0x40080100), unchecked(0x42000100), unchecked(0x42080000), unchecked(0x00080100), unchecked(
|
||||
0x40000000), unchecked(0x02000000), unchecked(0x40080000),
|
||||
unchecked(0x40080000), unchecked(0x00000000), unchecked(0x40000100), unchecked(0x42080100), unchecked(0x42080100), unchecked(0x02000100), unchecked(0x42080000), unchecked(0x40000100), unchecked(
|
||||
0x00000000), unchecked(0x42000000), unchecked(0x02080100),
|
||||
unchecked(0x02000000), unchecked(0x42000000), unchecked(0x00080100), unchecked(0x00080000), unchecked(0x42000100), unchecked(0x00000100), unchecked(0x02000000), unchecked(0x40000000), unchecked(
|
||||
0x02080000), unchecked(0x42000100), unchecked(0x40080100),
|
||||
unchecked(0x02000100), unchecked(0x40000000), unchecked(0x42080000), unchecked(0x02080100), unchecked(0x40080100), unchecked(0x00000100), unchecked(0x02000000), unchecked(0x42080000), unchecked(
|
||||
0x42080100), unchecked(0x00080100), unchecked(0x42000000),
|
||||
unchecked(0x42080100), unchecked(0x02080000), unchecked(0x00000000), unchecked(0x40080000), unchecked(0x42000000), unchecked(0x00080100), unchecked(0x02000100), unchecked(0x40000100), unchecked(
|
||||
0x00080000), unchecked(0x00000000), unchecked(0x40080000),
|
||||
unchecked(0x02080100), unchecked(0x40000100) };
|
||||
|
||||
private static int[] _sp6 = { unchecked(0x20000010), unchecked(0x20400000), unchecked(0x00004000), unchecked(0x20404010), unchecked(
|
||||
0x20400000), unchecked(0x00000010), unchecked(0x20404010),
|
||||
unchecked(0x00400000), unchecked(0x20004000), unchecked(0x00404010), unchecked(0x00400000), unchecked(0x20000010), unchecked(0x00400010), unchecked(0x20004000), unchecked(0x20000000), unchecked(
|
||||
0x00004010), unchecked(0x00000000), unchecked(0x00400010),
|
||||
unchecked(0x20004010), unchecked(0x00004000), unchecked(0x00404000), unchecked(0x20004010), unchecked(0x00000010), unchecked(0x20400010), unchecked(0x20400010), unchecked(0x00000000), unchecked(
|
||||
0x00404010), unchecked(0x20404000), unchecked(0x00004010),
|
||||
unchecked(0x00404000), unchecked(0x20404000), unchecked(0x20000000), unchecked(0x20004000), unchecked(0x00000010), unchecked(0x20400010), unchecked(0x00404000), unchecked(0x20404010), unchecked(
|
||||
0x00400000), unchecked(0x00004010), unchecked(0x20000010),
|
||||
unchecked(0x00400000), unchecked(0x20004000), unchecked(0x20000000), unchecked(0x00004010), unchecked(0x20000010), unchecked(0x20404010), unchecked(0x00404000), unchecked(0x20400000), unchecked(
|
||||
0x00404010), unchecked(0x20404000), unchecked(0x00000000),
|
||||
unchecked(0x20400010), unchecked(0x00000010), unchecked(0x00004000), unchecked(0x20400000), unchecked(0x00404010), unchecked(0x00004000), unchecked(0x00400010), unchecked(0x20004010), unchecked(
|
||||
0x00000000), unchecked(0x20404000), unchecked(0x20000000),
|
||||
unchecked(0x00400010), unchecked(0x20004010) };
|
||||
|
||||
private static int[] _sp7 = { unchecked(0x00200000), unchecked(0x04200002), unchecked(0x04000802), unchecked(0x00000000), unchecked(
|
||||
0x00000800), unchecked(0x04000802), unchecked(0x00200802),
|
||||
unchecked(0x04200800), unchecked(0x04200802), unchecked(0x00200000), unchecked(0x00000000), unchecked(0x04000002), unchecked(0x00000002), unchecked(0x04000000), unchecked(0x04200002), unchecked(
|
||||
0x00000802), unchecked(0x04000800), unchecked(0x00200802),
|
||||
unchecked(0x00200002), unchecked(0x04000800), unchecked(0x04000002), unchecked(0x04200000), unchecked(0x04200800), unchecked(0x00200002), unchecked(0x04200000), unchecked(0x00000800), unchecked(
|
||||
0x00000802), unchecked(0x04200802), unchecked(0x00200800),
|
||||
unchecked(0x00000002), unchecked(0x04000000), unchecked(0x00200800), unchecked(0x04000000), unchecked(0x00200800), unchecked(0x00200000), unchecked(0x04000802), unchecked(0x04000802), unchecked(
|
||||
0x04200002), unchecked(0x04200002), unchecked(0x00000002),
|
||||
unchecked(0x00200002), unchecked(0x04000000), unchecked(0x04000800), unchecked(0x00200000), unchecked(0x04200800), unchecked(0x00000802), unchecked(0x00200802), unchecked(0x04200800), unchecked(
|
||||
0x00000802), unchecked(0x04000002), unchecked(0x04200802),
|
||||
unchecked(0x04200000), unchecked(0x00200800), unchecked(0x00000000), unchecked(0x00000002), unchecked(0x04200802), unchecked(0x00000000), unchecked(0x00200802), unchecked(0x04200000), unchecked(
|
||||
0x00000800), unchecked(0x04000002), unchecked(0x04000800),
|
||||
unchecked(0x00000800), unchecked(0x00200002) };
|
||||
|
||||
private static int[] _sp8 = { unchecked(0x10001040), unchecked(0x00001000), unchecked(0x00040000), unchecked(0x10041040), unchecked(
|
||||
0x10000000), unchecked(0x10001040), unchecked(0x00000040),
|
||||
unchecked(0x10000000), unchecked(0x00040040), unchecked(0x10040000), unchecked(0x10041040), unchecked(0x00041000), unchecked(0x10041000), unchecked(0x00041040), unchecked(0x00001000), unchecked(
|
||||
0x00000040), unchecked(0x10040000), unchecked(0x10000040),
|
||||
unchecked(0x10001000), unchecked(0x00001040), unchecked(0x00041000), unchecked(0x00040040), unchecked(0x10040040), unchecked(0x10041000), unchecked(0x00001040), unchecked(0x00000000), unchecked(
|
||||
0x00000000), unchecked(0x10040040), unchecked(0x10000040),
|
||||
unchecked(0x10001000), unchecked(0x00041040), unchecked(0x00040000), unchecked(0x00041040), unchecked(0x00040000), unchecked(0x10041000), unchecked(0x00001000), unchecked(0x00000040), unchecked(
|
||||
0x10040040), unchecked(0x00001000), unchecked(0x00041040),
|
||||
unchecked(0x10001000), unchecked(0x00000040), unchecked(0x10000040), unchecked(0x10040000), unchecked(0x10040040), unchecked(0x10000000), unchecked(0x00040000), unchecked(0x10001040), unchecked(
|
||||
0x00000000), unchecked(0x10041040), unchecked(0x00040040),
|
||||
unchecked(0x10000040), unchecked(0x10040000), unchecked(0x10001000), unchecked(0x10001040), unchecked(0x00000000), unchecked(0x10041040), unchecked(0x00041000), unchecked(0x00041000), unchecked(
|
||||
0x00001040), unchecked(0x00001040), unchecked(0x00040040),
|
||||
unchecked(0x10000000), unchecked(0x10041000) };
|
||||
|
||||
// Tables, permutations, S-boxes, etc.
|
||||
/// Squash bytes down to ints.
|
||||
public static void SquashBytesToInts(byte[] inBytes, int inOff, int[] outInts, int
|
||||
outOff, int intLen)
|
||||
{
|
||||
for (int i = 0; i < intLen; ++i)
|
||||
{
|
||||
outInts[outOff + i] = ((inBytes[inOff + i * 4] & unchecked(0xff)) << 24) |
|
||||
((inBytes[inOff + i * 4 + 1] & unchecked(0xff)) << 16) | ((inBytes[inOff
|
||||
+ i * 4 + 2] & unchecked(0xff)) << 8) | (inBytes[inOff + i * 4 + 3] & unchecked(
|
||||
0xff));
|
||||
}
|
||||
}
|
||||
|
||||
/// Spread ints into bytes.
|
||||
public static void SpreadIntsToBytes(int[] inInts, int inOff, byte[] outBytes, int
|
||||
outOff, int intLen)
|
||||
{
|
||||
for (int i = 0; i < intLen; ++i)
|
||||
{
|
||||
outBytes[outOff + i * 4] = unchecked((byte)((int)(((uint)inInts[inOff + i]) >> 24
|
||||
)));
|
||||
outBytes[outOff + i * 4 + 1] = unchecked((byte)((int)(((uint)inInts[inOff + i]) >>
|
||||
16)));
|
||||
outBytes[outOff + i * 4 + 2] = unchecked((byte)((int)(((uint)inInts[inOff + i]) >>
|
||||
8)));
|
||||
outBytes[outOff + i * 4 + 3] = unchecked((byte)inInts[inOff + i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
401
Emby.Server.Implementations/IO/SharpCifs/Util/Encdec.cs
Normal file
401
Emby.Server.Implementations/IO/SharpCifs/Util/Encdec.cs
Normal file
@@ -0,0 +1,401 @@
|
||||
// This code is derived from jcifs smb client library <jcifs at samba dot org>
|
||||
// Ported by J. Arturo <webmaster at komodosoft dot net>
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
using System;
|
||||
using System.IO;
|
||||
using SharpCifs.Util.Sharpen;
|
||||
|
||||
namespace SharpCifs.Util
|
||||
{
|
||||
public class Encdec
|
||||
{
|
||||
public const long MillisecondsBetween1970And1601 = 11644473600000L;
|
||||
|
||||
public const long SecBetweeen1904And1970 = 2082844800L;
|
||||
|
||||
public const int Time1970Sec32Be = 1;
|
||||
|
||||
public const int Time1970Sec32Le = 2;
|
||||
|
||||
public const int Time1904Sec32Be = 3;
|
||||
|
||||
public const int Time1904Sec32Le = 4;
|
||||
|
||||
public const int Time1601Nanos64Le = 5;
|
||||
|
||||
public const int Time1601Nanos64Be = 6;
|
||||
|
||||
public const int Time1970Millis64Be = 7;
|
||||
|
||||
public const int Time1970Millis64Le = 8;
|
||||
|
||||
public static int Enc_uint16be(short s, byte[] dst, int di)
|
||||
{
|
||||
dst[di++] = unchecked((byte)((s >> 8) & unchecked(0xFF)));
|
||||
dst[di] = unchecked((byte)(s & unchecked(0xFF)));
|
||||
return 2;
|
||||
}
|
||||
|
||||
public static int Enc_uint32be(int i, byte[] dst, int di)
|
||||
{
|
||||
dst[di++] = unchecked((byte)((i >> 24) & unchecked(0xFF)));
|
||||
dst[di++] = unchecked((byte)((i >> 16) & unchecked(0xFF)));
|
||||
dst[di++] = unchecked((byte)((i >> 8) & unchecked(0xFF)));
|
||||
dst[di] = unchecked((byte)(i & unchecked(0xFF)));
|
||||
return 4;
|
||||
}
|
||||
|
||||
public static int Enc_uint16le(short s, byte[] dst, int di)
|
||||
{
|
||||
dst[di++] = unchecked((byte)(s & unchecked(0xFF)));
|
||||
dst[di] = unchecked((byte)((s >> 8) & unchecked(0xFF)));
|
||||
return 2;
|
||||
}
|
||||
|
||||
public static int Enc_uint32le(int i, byte[] dst, int di)
|
||||
{
|
||||
dst[di++] = unchecked((byte)(i & unchecked(0xFF)));
|
||||
dst[di++] = unchecked((byte)((i >> 8) & unchecked(0xFF)));
|
||||
dst[di++] = unchecked((byte)((i >> 16) & unchecked(0xFF)));
|
||||
dst[di] = unchecked((byte)((i >> 24) & unchecked(0xFF)));
|
||||
return 4;
|
||||
}
|
||||
|
||||
public static short Dec_uint16be(byte[] src, int si)
|
||||
{
|
||||
return (short)(((src[si] & unchecked(0xFF)) << 8) | (src[si + 1] & unchecked(
|
||||
0xFF)));
|
||||
}
|
||||
|
||||
public static int Dec_uint32be(byte[] src, int si)
|
||||
{
|
||||
return ((src[si] & unchecked(0xFF)) << 24) | ((src[si + 1] & unchecked(0xFF)) << 16) | ((src[si + 2] & unchecked(0xFF)) << 8) | (src[si + 3]
|
||||
& unchecked(0xFF));
|
||||
}
|
||||
|
||||
public static short Dec_uint16le(byte[] src, int si)
|
||||
{
|
||||
return (short)((src[si] & unchecked(0xFF)) | ((src[si + 1] & unchecked(0xFF)) << 8));
|
||||
}
|
||||
|
||||
public static int Dec_uint32le(byte[] src, int si)
|
||||
{
|
||||
return (src[si] & unchecked(0xFF)) | ((src[si + 1] & unchecked(0xFF
|
||||
)) << 8) | ((src[si + 2] & unchecked(0xFF)) << 16) | ((src[si + 3] & unchecked(
|
||||
0xFF)) << 24);
|
||||
}
|
||||
|
||||
public static int Enc_uint64be(long l, byte[] dst, int di)
|
||||
{
|
||||
Enc_uint32be((int)(l & unchecked(0xFFFFFFFFL)), dst, di + 4);
|
||||
Enc_uint32be((int)((l >> 32) & unchecked(0xFFFFFFFFL)), dst, di);
|
||||
return 8;
|
||||
}
|
||||
|
||||
public static int Enc_uint64le(long l, byte[] dst, int di)
|
||||
{
|
||||
Enc_uint32le((int)(l & unchecked(0xFFFFFFFFL)), dst, di);
|
||||
Enc_uint32le((int)((l >> 32) & unchecked(0xFFFFFFFFL)), dst, di + 4);
|
||||
return 8;
|
||||
}
|
||||
|
||||
public static long Dec_uint64be(byte[] src, int si)
|
||||
{
|
||||
long l;
|
||||
l = Dec_uint32be(src, si) & unchecked(0xFFFFFFFFL);
|
||||
l <<= 32;
|
||||
l |= Dec_uint32be(src, si + 4) & unchecked(0xFFFFFFFFL);
|
||||
return l;
|
||||
}
|
||||
|
||||
public static long Dec_uint64le(byte[] src, int si)
|
||||
{
|
||||
long l;
|
||||
l = Dec_uint32le(src, si + 4) & unchecked(0xFFFFFFFFL);
|
||||
l <<= 32;
|
||||
l |= Dec_uint32le(src, si) & unchecked(0xFFFFFFFFL);
|
||||
return l;
|
||||
}
|
||||
|
||||
public static int Enc_floatle(float f, byte[] dst, int di)
|
||||
{
|
||||
return Enc_uint32le((int)BitConverter.DoubleToInt64Bits(f), dst, di);
|
||||
}
|
||||
|
||||
public static int Enc_floatbe(float f, byte[] dst, int di)
|
||||
{
|
||||
return Enc_uint32be((int)BitConverter.DoubleToInt64Bits(f), dst, di);
|
||||
}
|
||||
|
||||
public static float Dec_floatle(byte[] src, int si)
|
||||
{
|
||||
return (float)BitConverter.Int64BitsToDouble(Dec_uint32le(src, si));
|
||||
}
|
||||
|
||||
public static float Dec_floatbe(byte[] src, int si)
|
||||
{
|
||||
return (float)BitConverter.Int64BitsToDouble(Dec_uint32be(src, si));
|
||||
}
|
||||
|
||||
public static int Enc_doublele(double d, byte[] dst, int di)
|
||||
{
|
||||
return Enc_uint64le(BitConverter.DoubleToInt64Bits(d), dst, di);
|
||||
}
|
||||
|
||||
public static int Enc_doublebe(double d, byte[] dst, int di)
|
||||
{
|
||||
return Enc_uint64be(BitConverter.DoubleToInt64Bits(d), dst, di);
|
||||
}
|
||||
|
||||
public static double Dec_doublele(byte[] src, int si)
|
||||
{
|
||||
return BitConverter.Int64BitsToDouble(Dec_uint64le(src, si));
|
||||
}
|
||||
|
||||
public static double Dec_doublebe(byte[] src, int si)
|
||||
{
|
||||
return BitConverter.Int64BitsToDouble(Dec_uint64be(src, si));
|
||||
}
|
||||
|
||||
public static int Enc_time(DateTime date, byte[] dst, int di, int enc)
|
||||
{
|
||||
long t;
|
||||
switch (enc)
|
||||
{
|
||||
case Time1970Sec32Be:
|
||||
{
|
||||
return Enc_uint32be((int)(date.GetTime() / 1000L), dst, di);
|
||||
}
|
||||
|
||||
case Time1970Sec32Le:
|
||||
{
|
||||
return Enc_uint32le((int)(date.GetTime() / 1000L), dst, di);
|
||||
}
|
||||
|
||||
case Time1904Sec32Be:
|
||||
{
|
||||
return Enc_uint32be((int)((date.GetTime() / 1000L + SecBetweeen1904And1970) &
|
||||
unchecked((int)(0xFFFFFFFF))), dst, di);
|
||||
}
|
||||
|
||||
case Time1904Sec32Le:
|
||||
{
|
||||
return Enc_uint32le((int)((date.GetTime() / 1000L + SecBetweeen1904And1970) &
|
||||
unchecked((int)(0xFFFFFFFF))), dst, di);
|
||||
}
|
||||
|
||||
case Time1601Nanos64Be:
|
||||
{
|
||||
t = (date.GetTime() + MillisecondsBetween1970And1601) * 10000L;
|
||||
return Enc_uint64be(t, dst, di);
|
||||
}
|
||||
|
||||
case Time1601Nanos64Le:
|
||||
{
|
||||
t = (date.GetTime() + MillisecondsBetween1970And1601) * 10000L;
|
||||
return Enc_uint64le(t, dst, di);
|
||||
}
|
||||
|
||||
case Time1970Millis64Be:
|
||||
{
|
||||
return Enc_uint64be(date.GetTime(), dst, di);
|
||||
}
|
||||
|
||||
case Time1970Millis64Le:
|
||||
{
|
||||
return Enc_uint64le(date.GetTime(), dst, di);
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
throw new ArgumentException("Unsupported time encoding");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static DateTime Dec_time(byte[] src, int si, int enc)
|
||||
{
|
||||
long t;
|
||||
switch (enc)
|
||||
{
|
||||
case Time1970Sec32Be:
|
||||
{
|
||||
return Sharpen.Extensions.CreateDate(Dec_uint32be(src, si) * 1000L);
|
||||
}
|
||||
|
||||
case Time1970Sec32Le:
|
||||
{
|
||||
return Sharpen.Extensions.CreateDate(Dec_uint32le(src, si) * 1000L);
|
||||
}
|
||||
|
||||
case Time1904Sec32Be:
|
||||
{
|
||||
return Sharpen.Extensions.CreateDate(((Dec_uint32be(src, si) & unchecked(0xFFFFFFFFL)) - SecBetweeen1904And1970) * 1000L);
|
||||
}
|
||||
|
||||
case Time1904Sec32Le:
|
||||
{
|
||||
return Sharpen.Extensions.CreateDate(((Dec_uint32le(src, si) & unchecked(0xFFFFFFFFL)) - SecBetweeen1904And1970) * 1000L);
|
||||
}
|
||||
|
||||
case Time1601Nanos64Be:
|
||||
{
|
||||
t = Dec_uint64be(src, si);
|
||||
return Sharpen.Extensions.CreateDate(t / 10000L - MillisecondsBetween1970And1601
|
||||
);
|
||||
}
|
||||
|
||||
case Time1601Nanos64Le:
|
||||
{
|
||||
t = Dec_uint64le(src, si);
|
||||
return Sharpen.Extensions.CreateDate(t / 10000L - MillisecondsBetween1970And1601
|
||||
);
|
||||
}
|
||||
|
||||
case Time1970Millis64Be:
|
||||
{
|
||||
return Sharpen.Extensions.CreateDate(Dec_uint64be(src, si));
|
||||
}
|
||||
|
||||
case Time1970Millis64Le:
|
||||
{
|
||||
return Sharpen.Extensions.CreateDate(Dec_uint64le(src, si));
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
throw new ArgumentException("Unsupported time encoding");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <exception cref="System.IO.IOException"></exception>
|
||||
public static int Enc_utf8(string str, byte[] dst, int di, int dlim)
|
||||
{
|
||||
int start = di;
|
||||
int ch;
|
||||
int strlen = str.Length;
|
||||
for (int i = 0; di < dlim && i < strlen; i++)
|
||||
{
|
||||
ch = str[i];
|
||||
if ((ch >= unchecked(0x0001)) && (ch <= unchecked(0x007F)))
|
||||
{
|
||||
dst[di++] = unchecked((byte)ch);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ch > unchecked(0x07FF))
|
||||
{
|
||||
if ((dlim - di) < 3)
|
||||
{
|
||||
break;
|
||||
}
|
||||
dst[di++] = unchecked((byte)(unchecked(0xE0) | ((ch >> 12) & unchecked(0x0F))));
|
||||
dst[di++] = unchecked((byte)(unchecked(0x80) | ((ch >> 6) & unchecked(0x3F))));
|
||||
dst[di++] = unchecked((byte)(unchecked(0x80) | ((ch >> 0) & unchecked(0x3F))));
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((dlim - di) < 2)
|
||||
{
|
||||
break;
|
||||
}
|
||||
dst[di++] = unchecked((byte)(unchecked(0xC0) | ((ch >> 6) & unchecked(0x1F))));
|
||||
dst[di++] = unchecked((byte)(unchecked(0x80) | ((ch >> 0) & unchecked(0x3F))));
|
||||
}
|
||||
}
|
||||
}
|
||||
return di - start;
|
||||
}
|
||||
|
||||
/// <exception cref="System.IO.IOException"></exception>
|
||||
public static string Dec_utf8(byte[] src, int si, int slim)
|
||||
{
|
||||
char[] uni = new char[slim - si];
|
||||
int ui;
|
||||
int ch;
|
||||
for (ui = 0; si < slim && (ch = src[si++] & unchecked(0xFF)) != 0; ui++)
|
||||
{
|
||||
if (ch < unchecked(0x80))
|
||||
{
|
||||
uni[ui] = (char)ch;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((ch & unchecked(0xE0)) == unchecked(0xC0))
|
||||
{
|
||||
if ((slim - si) < 2)
|
||||
{
|
||||
break;
|
||||
}
|
||||
uni[ui] = (char)((ch & unchecked(0x1F)) << 6);
|
||||
ch = src[si++] & unchecked(0xFF);
|
||||
uni[ui] |= (char)((char)ch & unchecked(0x3F));
|
||||
if ((ch & unchecked(0xC0)) != unchecked(0x80) || uni[ui] < unchecked(
|
||||
0x80))
|
||||
{
|
||||
throw new IOException("Invalid UTF-8 sequence");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((ch & unchecked(0xF0)) == unchecked(0xE0))
|
||||
{
|
||||
if ((slim - si) < 3)
|
||||
{
|
||||
break;
|
||||
}
|
||||
uni[ui] = (char)((ch & unchecked(0x0F)) << 12);
|
||||
ch = src[si++] & unchecked(0xFF);
|
||||
if ((ch & unchecked(0xC0)) != unchecked(0x80))
|
||||
{
|
||||
throw new IOException("Invalid UTF-8 sequence");
|
||||
}
|
||||
uni[ui] |= (char)((char)(ch & unchecked(0x3F)) << 6);
|
||||
ch = src[si++] & unchecked(0xFF);
|
||||
uni[ui] |= (char)((char)ch & unchecked(0x3F));
|
||||
if ((ch & unchecked(0xC0)) != unchecked(0x80) || uni[ui] < unchecked(
|
||||
0x800))
|
||||
{
|
||||
throw new IOException("Invalid UTF-8 sequence");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IOException("Unsupported UTF-8 sequence");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return new string(uni, 0, ui);
|
||||
}
|
||||
|
||||
/// <exception cref="System.IO.IOException"></exception>
|
||||
public static string Dec_ucs2le(byte[] src, int si, int slim, char[] buf)
|
||||
{
|
||||
int bi;
|
||||
for (bi = 0; (si + 1) < slim; bi++, si += 2)
|
||||
{
|
||||
buf[bi] = (char)Dec_uint16le(src, si);
|
||||
if (buf[bi] == '\0')
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
return new string(buf, 0, bi);
|
||||
}
|
||||
}
|
||||
}
|
||||
146
Emby.Server.Implementations/IO/SharpCifs/Util/HMACT64.cs
Normal file
146
Emby.Server.Implementations/IO/SharpCifs/Util/HMACT64.cs
Normal file
@@ -0,0 +1,146 @@
|
||||
// This code is derived from jcifs smb client library <jcifs at samba dot org>
|
||||
// Ported by J. Arturo <webmaster at komodosoft dot net>
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
using System;
|
||||
using SharpCifs.Util.Sharpen;
|
||||
|
||||
namespace SharpCifs.Util
|
||||
{
|
||||
/// <summary>This is an implementation of the HMACT64 keyed hashing algorithm.</summary>
|
||||
/// <remarks>
|
||||
/// This is an implementation of the HMACT64 keyed hashing algorithm.
|
||||
/// HMACT64 is defined by Luke Leighton as a modified HMAC-MD5 (RFC 2104)
|
||||
/// in which the key is truncated at 64 bytes (rather than being hashed
|
||||
/// via MD5).
|
||||
/// </remarks>
|
||||
public class Hmact64 : MessageDigest
|
||||
{
|
||||
private const int BlockLength = 64;
|
||||
|
||||
private const byte Ipad = unchecked(unchecked(0x36));
|
||||
|
||||
private const byte Opad = unchecked(unchecked(0x5c));
|
||||
|
||||
private MessageDigest _md5;
|
||||
|
||||
private byte[] _ipad = new byte[BlockLength];
|
||||
|
||||
private byte[] _opad = new byte[BlockLength];
|
||||
|
||||
/// <summary>Creates an HMACT64 instance which uses the given secret key material.</summary>
|
||||
/// <remarks>Creates an HMACT64 instance which uses the given secret key material.</remarks>
|
||||
/// <param name="key">The key material to use in hashing.</param>
|
||||
public Hmact64(byte[] key)
|
||||
{
|
||||
int length = Math.Min(key.Length, BlockLength);
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
_ipad[i] = unchecked((byte)(key[i] ^ Ipad));
|
||||
_opad[i] = unchecked((byte)(key[i] ^ Opad));
|
||||
}
|
||||
for (int i1 = length; i1 < BlockLength; i1++)
|
||||
{
|
||||
_ipad[i1] = Ipad;
|
||||
_opad[i1] = Opad;
|
||||
}
|
||||
try
|
||||
{
|
||||
_md5 = GetInstance("MD5");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new InvalidOperationException(ex.Message);
|
||||
}
|
||||
EngineReset();
|
||||
}
|
||||
|
||||
|
||||
protected byte[] EngineDigest()
|
||||
{
|
||||
byte[] digest = _md5.Digest();
|
||||
_md5.Update(_opad);
|
||||
return _md5.Digest(digest);
|
||||
}
|
||||
|
||||
protected int EngineDigest(byte[] buf, int offset, int len)
|
||||
{
|
||||
byte[] digest = _md5.Digest();
|
||||
_md5.Update(_opad);
|
||||
_md5.Update(digest);
|
||||
try
|
||||
{
|
||||
_md5.Digest(buf, offset, len);
|
||||
|
||||
return len;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
protected int EngineGetDigestLength()
|
||||
{
|
||||
return _md5.GetDigestLength();
|
||||
}
|
||||
|
||||
protected void EngineReset()
|
||||
{
|
||||
_md5.Reset();
|
||||
_md5.Update(_ipad);
|
||||
}
|
||||
|
||||
protected void EngineUpdate(byte b)
|
||||
{
|
||||
_md5.Update(b);
|
||||
}
|
||||
|
||||
protected void EngineUpdate(byte[] input, int offset, int len)
|
||||
{
|
||||
_md5.Update(input, offset, len);
|
||||
}
|
||||
|
||||
public override byte[] Digest()
|
||||
{
|
||||
return EngineDigest();
|
||||
}
|
||||
|
||||
public override int GetDigestLength()
|
||||
{
|
||||
return EngineGetDigestLength();
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
EngineReset();
|
||||
}
|
||||
|
||||
public override void Update(byte[] b)
|
||||
{
|
||||
EngineUpdate(b, 0, b.Length);
|
||||
}
|
||||
|
||||
public override void Update(byte b)
|
||||
{
|
||||
EngineUpdate(b);
|
||||
}
|
||||
|
||||
public override void Update(byte[] b, int offset, int len)
|
||||
{
|
||||
EngineUpdate(b, offset, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
191
Emby.Server.Implementations/IO/SharpCifs/Util/Hexdump.cs
Normal file
191
Emby.Server.Implementations/IO/SharpCifs/Util/Hexdump.cs
Normal file
@@ -0,0 +1,191 @@
|
||||
// This code is derived from jcifs smb client library <jcifs at samba dot org>
|
||||
// Ported by J. Arturo <webmaster at komodosoft dot net>
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util
|
||||
{
|
||||
public class Hexdump
|
||||
{
|
||||
private static readonly string Nl = @"\r\n"; //Runtime.GetProperty("line.separator");
|
||||
|
||||
private static readonly int NlLength = Nl.Length;
|
||||
|
||||
private static readonly char[] SpaceChars = { ' ', ' ', ' ', ' ', ' '
|
||||
, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '
|
||||
, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '
|
||||
, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
|
||||
|
||||
public static readonly char[] HexDigits = { '0', '1', '2', '3', '4',
|
||||
'5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
|
||||
|
||||
|
||||
private static bool IsIsoControl(char c)
|
||||
{
|
||||
return (c >= '\u0000' && c <= '\u001F') || (c >= '\u007F' && c <= '\u009F');
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate "hexdump" output of the buffer at src like the following:
|
||||
/// <p><blockquote><pre>
|
||||
/// 00000: 04 d2 29 00 00 01 00 00 00 00 00 01 20 45 47 46 |..).........
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Generate "hexdump" output of the buffer at src like the following:
|
||||
/// <p><blockquote><pre>
|
||||
/// 00000: 04 d2 29 00 00 01 00 00 00 00 00 01 20 45 47 46 |..)......... EGF|
|
||||
/// 00010: 43 45 46 45 45 43 41 43 41 43 41 43 41 43 41 43 |CEFEECACACACACAC|
|
||||
/// 00020: 41 43 41 43 41 43 41 43 41 43 41 41 44 00 00 20 |ACACACACACAAD.. |
|
||||
/// 00030: 00 01 c0 0c 00 20 00 01 00 00 00 00 00 06 20 00 |..... ........ .|
|
||||
/// 00040: ac 22 22 e1 |."". |
|
||||
/// </blockquote></pre>
|
||||
/// </remarks>
|
||||
public static void ToHexdump(TextWriter ps, byte[] src, int srcIndex, int length)
|
||||
{
|
||||
if (length == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int s = length % 16;
|
||||
int r = (s == 0) ? length / 16 : length / 16 + 1;
|
||||
char[] c = new char[r * (74 + NlLength)];
|
||||
char[] d = new char[16];
|
||||
int i;
|
||||
int si = 0;
|
||||
int ci = 0;
|
||||
do
|
||||
{
|
||||
ToHexChars(si, c, ci, 5);
|
||||
ci += 5;
|
||||
c[ci++] = ':';
|
||||
do
|
||||
{
|
||||
if (si == length)
|
||||
{
|
||||
int n = 16 - s;
|
||||
Array.Copy(SpaceChars, 0, c, ci, n * 3);
|
||||
ci += n * 3;
|
||||
Array.Copy(SpaceChars, 0, d, s, n);
|
||||
break;
|
||||
}
|
||||
c[ci++] = ' ';
|
||||
i = src[srcIndex + si] & 0xFF;
|
||||
ToHexChars(i, c, ci, 2);
|
||||
ci += 2;
|
||||
if (i < 0 || IsIsoControl((char)i))
|
||||
{
|
||||
d[si % 16] = '.';
|
||||
}
|
||||
else
|
||||
{
|
||||
d[si % 16] = (char)i;
|
||||
}
|
||||
}
|
||||
while ((++si % 16) != 0);
|
||||
c[ci++] = ' ';
|
||||
c[ci++] = ' ';
|
||||
c[ci++] = '|';
|
||||
Array.Copy(d, 0, c, ci, 16);
|
||||
ci += 16;
|
||||
c[ci++] = '|';
|
||||
//Sharpen.Runtime.GetCharsForString(NL, 0, NL_LENGTH, c, ci);
|
||||
c = Nl.ToCharArray(0, NlLength);
|
||||
ci += NlLength;
|
||||
}
|
||||
while (si < length);
|
||||
ps.WriteLine(c);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This is an alternative to the <code>java.lang.Integer.toHexString</cod>
|
||||
/// method.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This is an alternative to the <code>java.lang.Integer.toHexString</cod>
|
||||
/// method. It is an efficient relative that also will pad the left side so
|
||||
/// that the result is <code>size</code> digits.
|
||||
/// </remarks>
|
||||
public static string ToHexString(int val, int size)
|
||||
{
|
||||
char[] c = new char[size];
|
||||
ToHexChars(val, c, 0, size);
|
||||
return new string(c);
|
||||
}
|
||||
|
||||
public static string ToHexString(long val, int size)
|
||||
{
|
||||
char[] c = new char[size];
|
||||
ToHexChars(val, c, 0, size);
|
||||
return new string(c);
|
||||
}
|
||||
|
||||
public static string ToHexString(byte[] src, int srcIndex, int size)
|
||||
{
|
||||
char[] c = new char[size];
|
||||
size = (size % 2 == 0) ? size / 2 : size / 2 + 1;
|
||||
for (int i = 0, j = 0; i < size; i++)
|
||||
{
|
||||
c[j++] = HexDigits[(src[i] >> 4) & 0x0F];
|
||||
if (j == c.Length)
|
||||
{
|
||||
break;
|
||||
}
|
||||
c[j++] = HexDigits[src[i] & 0x0F];
|
||||
}
|
||||
return new string(c);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This is the same as
|
||||
/// <see cref="ToHexString(int, int)">ToHexString(int, int)</see>
|
||||
/// but provides a more practical form when trying to avoid
|
||||
/// <see cref="string">string</see>
|
||||
/// concatenation and
|
||||
/// <see cref="System.Text.StringBuilder">System.Text.StringBuilder</see>
|
||||
/// .
|
||||
/// </summary>
|
||||
public static void ToHexChars(int val, char[] dst, int dstIndex, int size)
|
||||
{
|
||||
while (size > 0)
|
||||
{
|
||||
int i = dstIndex + size - 1;
|
||||
if (i < dst.Length)
|
||||
{
|
||||
dst[i] = HexDigits[val & 0x000F];
|
||||
}
|
||||
if (val != 0)
|
||||
{
|
||||
val = (int)(((uint)val) >> 4);
|
||||
}
|
||||
size--;
|
||||
}
|
||||
}
|
||||
|
||||
public static void ToHexChars(long val, char[] dst, int dstIndex, int size)
|
||||
{
|
||||
while (size > 0)
|
||||
{
|
||||
dst[dstIndex + size - 1] = HexDigits[(int)(val & 0x000FL)];
|
||||
if (val != 0)
|
||||
{
|
||||
val = (long)(((ulong)val) >> 4);
|
||||
}
|
||||
size--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
78
Emby.Server.Implementations/IO/SharpCifs/Util/LogStream.cs
Normal file
78
Emby.Server.Implementations/IO/SharpCifs/Util/LogStream.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
// This code is derived from jcifs smb client library <jcifs at samba dot org>
|
||||
// Ported by J. Arturo <webmaster at komodosoft dot net>
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
//using Windows.Storage;
|
||||
//using Windows.UI.Notifications;
|
||||
using SharpCifs.Util.Sharpen;
|
||||
|
||||
namespace SharpCifs.Util
|
||||
{
|
||||
/// <summary>
|
||||
/// 0 - nothing
|
||||
/// 1 - critical [default]
|
||||
/// 2 - basic info can be logged under load
|
||||
/// 3 - almost everything
|
||||
/// N - debugging
|
||||
/// </summary>
|
||||
public class LogStream: PrintWriter
|
||||
{
|
||||
private static LogStream _inst = null;
|
||||
|
||||
public int Level = 1;
|
||||
|
||||
public void SetLevel(int level)
|
||||
{
|
||||
this.Level = level;
|
||||
}
|
||||
|
||||
public LogStream(TextWriter other) : base(other)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This must be called before <tt>getInstance</tt> is called or
|
||||
/// it will have no effect.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This must be called before <tt>getInstance</tt> is called or
|
||||
/// it will have no effect.
|
||||
/// </remarks>
|
||||
public static void SetInstance(TextWriter other)
|
||||
{
|
||||
//inst = new Jcifs.Util.LogStream();
|
||||
_inst = new LogStream(other);
|
||||
}
|
||||
|
||||
public static LogStream GetInstance()
|
||||
{
|
||||
if (_inst == null)
|
||||
{
|
||||
SetInstance(Console.Error);
|
||||
}
|
||||
return _inst;
|
||||
}
|
||||
|
||||
public override Encoding Encoding
|
||||
{
|
||||
get { throw new NotImplementedException(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
347
Emby.Server.Implementations/IO/SharpCifs/Util/MD4.cs
Normal file
347
Emby.Server.Implementations/IO/SharpCifs/Util/MD4.cs
Normal file
@@ -0,0 +1,347 @@
|
||||
// This code is derived from jcifs smb client library <jcifs at samba dot org>
|
||||
// Ported by J. Arturo <webmaster at komodosoft dot net>
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
using System;
|
||||
using SharpCifs.Util.Sharpen;
|
||||
|
||||
namespace SharpCifs.Util
|
||||
{
|
||||
/// <summary>Implements the MD4 message digest algorithm in Java.</summary>
|
||||
/// <remarks>
|
||||
/// Implements the MD4 message digest algorithm in Java.
|
||||
/// <p>
|
||||
/// <b>References:</b>
|
||||
/// <ol>
|
||||
/// <li> Ronald L. Rivest,
|
||||
/// "<a href="http://www.roxen.com/rfc/rfc1320.html">
|
||||
/// The MD4 Message-Digest Algorithm</a>",
|
||||
/// IETF RFC-1320 (informational).
|
||||
/// </ol>
|
||||
/// <p><b>$Revision: 1.2 $</b>
|
||||
/// </remarks>
|
||||
/// <author>Raif S. Naffah</author>
|
||||
public class Md4 : MessageDigest
|
||||
{
|
||||
/// <summary>The size in bytes of the input block to the tranformation algorithm.</summary>
|
||||
/// <remarks>The size in bytes of the input block to the tranformation algorithm.</remarks>
|
||||
private const int BlockLength = 64;
|
||||
|
||||
/// <summary>4 32-bit words (interim result)</summary>
|
||||
private int[] _context = new int[4];
|
||||
|
||||
/// <summary>Number of bytes processed so far mod.</summary>
|
||||
/// <remarks>Number of bytes processed so far mod. 2 power of 64.</remarks>
|
||||
private long _count;
|
||||
|
||||
/// <summary>512 bits input buffer = 16 x 32-bit words holds until reaches 512 bits.</summary>
|
||||
/// <remarks>512 bits input buffer = 16 x 32-bit words holds until reaches 512 bits.</remarks>
|
||||
private byte[] _buffer = new byte[BlockLength];
|
||||
|
||||
/// <summary>512 bits work buffer = 16 x 32-bit words</summary>
|
||||
private int[] _x = new int[16];
|
||||
|
||||
public Md4()
|
||||
{
|
||||
// This file is currently unlocked (change this line if you lock the file)
|
||||
//
|
||||
// $Log: MD4.java,v $
|
||||
// Revision 1.2 1998/01/05 03:41:19 iang
|
||||
// Added references only.
|
||||
//
|
||||
// Revision 1.1.1.1 1997/11/03 22:36:56 hopwood
|
||||
// + Imported to CVS (tagged as 'start').
|
||||
//
|
||||
// Revision 0.1.0.0 1997/07/14 R. Naffah
|
||||
// + original version
|
||||
//
|
||||
// $Endlog$
|
||||
// MD4 specific object variables
|
||||
//...........................................................................
|
||||
// = 512 / 8;
|
||||
// Constructors
|
||||
//...........................................................................
|
||||
EngineReset();
|
||||
}
|
||||
|
||||
/// <summary>This constructor is here to implement cloneability of this class.</summary>
|
||||
/// <remarks>This constructor is here to implement cloneability of this class.</remarks>
|
||||
private Md4(Md4 md) : this()
|
||||
{
|
||||
_context = (int[])md._context.Clone();
|
||||
_buffer = (byte[])md._buffer.Clone();
|
||||
_count = md._count;
|
||||
}
|
||||
|
||||
// Cloneable method implementation
|
||||
//...........................................................................
|
||||
/// <summary>Returns a copy of this MD object.</summary>
|
||||
/// <remarks>Returns a copy of this MD object.</remarks>
|
||||
public object Clone()
|
||||
{
|
||||
return new Md4(this);
|
||||
}
|
||||
|
||||
// JCE methods
|
||||
//...........................................................................
|
||||
/// <summary>
|
||||
/// Resets this object disregarding any temporary data present at the
|
||||
/// time of the invocation of this call.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Resets this object disregarding any temporary data present at the
|
||||
/// time of the invocation of this call.
|
||||
/// </remarks>
|
||||
protected void EngineReset()
|
||||
{
|
||||
// initial values of MD4 i.e. A, B, C, D
|
||||
// as per rfc-1320; they are low-order byte first
|
||||
_context[0] = unchecked(0x67452301);
|
||||
_context[1] = unchecked((int)(0xEFCDAB89));
|
||||
_context[2] = unchecked((int)(0x98BADCFE));
|
||||
_context[3] = unchecked(0x10325476);
|
||||
_count = 0L;
|
||||
for (int i = 0; i < BlockLength; i++)
|
||||
{
|
||||
_buffer[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Continues an MD4 message digest using the input byte.</summary>
|
||||
/// <remarks>Continues an MD4 message digest using the input byte.</remarks>
|
||||
protected void EngineUpdate(byte b)
|
||||
{
|
||||
// compute number of bytes still unhashed; ie. present in buffer
|
||||
int i = (int)(_count % BlockLength);
|
||||
_count++;
|
||||
// update number of bytes
|
||||
_buffer[i] = b;
|
||||
if (i == BlockLength - 1)
|
||||
{
|
||||
Transform(_buffer, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>MD4 block update operation.</summary>
|
||||
/// <remarks>
|
||||
/// MD4 block update operation.
|
||||
/// <p>
|
||||
/// Continues an MD4 message digest operation, by filling the buffer,
|
||||
/// transform(ing) data in 512-bit message block(s), updating the variables
|
||||
/// context and count, and leaving (buffering) the remaining bytes in buffer
|
||||
/// for the next update or finish.
|
||||
/// </remarks>
|
||||
/// <param name="input">input block</param>
|
||||
/// <param name="offset">start of meaningful bytes in input</param>
|
||||
/// <param name="len">count of bytes in input block to consider</param>
|
||||
protected void EngineUpdate(byte[] input, int offset, int len)
|
||||
{
|
||||
// make sure we don't exceed input's allocated size/length
|
||||
if (offset < 0 || len < 0 || (long)offset + len > input.Length)
|
||||
{
|
||||
throw new IndexOutOfRangeException();
|
||||
}
|
||||
// compute number of bytes still unhashed; ie. present in buffer
|
||||
int bufferNdx = (int)(_count % BlockLength);
|
||||
_count += len;
|
||||
// update number of bytes
|
||||
int partLen = BlockLength - bufferNdx;
|
||||
int i = 0;
|
||||
if (len >= partLen)
|
||||
{
|
||||
Array.Copy(input, offset, _buffer, bufferNdx, partLen);
|
||||
Transform(_buffer, 0);
|
||||
for (i = partLen; i + BlockLength - 1 < len; i += BlockLength)
|
||||
{
|
||||
Transform(input, offset + i);
|
||||
}
|
||||
bufferNdx = 0;
|
||||
}
|
||||
// buffer remaining input
|
||||
if (i < len)
|
||||
{
|
||||
Array.Copy(input, offset + i, _buffer, bufferNdx, len - i);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Completes the hash computation by performing final operations such
|
||||
/// as padding.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Completes the hash computation by performing final operations such
|
||||
/// as padding. At the return of this engineDigest, the MD engine is
|
||||
/// reset.
|
||||
/// </remarks>
|
||||
/// <returns>the array of bytes for the resulting hash value.</returns>
|
||||
protected byte[] EngineDigest()
|
||||
{
|
||||
// pad output to 56 mod 64; as RFC1320 puts it: congruent to 448 mod 512
|
||||
int bufferNdx = (int)(_count % BlockLength);
|
||||
int padLen = (bufferNdx < 56) ? (56 - bufferNdx) : (120 - bufferNdx);
|
||||
// padding is alwas binary 1 followed by binary 0s
|
||||
byte[] tail = new byte[padLen + 8];
|
||||
tail[0] = unchecked(unchecked(0x80));
|
||||
// append length before final transform:
|
||||
// save number of bits, casting the long to an array of 8 bytes
|
||||
// save low-order byte first.
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
tail[padLen + i] = unchecked((byte)((long)(((ulong)(_count * 8)) >> (8 * i))));
|
||||
}
|
||||
EngineUpdate(tail, 0, tail.Length);
|
||||
byte[] result = new byte[16];
|
||||
// cast this MD4's context (array of 4 ints) into an array of 16 bytes.
|
||||
for (int i1 = 0; i1 < 4; i1++)
|
||||
{
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
result[i1 * 4 + j] = unchecked((byte)((int)(((uint)_context[i1]) >> (8 * j))));
|
||||
}
|
||||
}
|
||||
// reset the engine
|
||||
EngineReset();
|
||||
return result;
|
||||
}
|
||||
|
||||
// own methods
|
||||
//...........................................................................
|
||||
/// <summary>MD4 basic transformation.</summary>
|
||||
/// <remarks>
|
||||
/// MD4 basic transformation.
|
||||
/// <p>
|
||||
/// Transforms context based on 512 bits from input block starting
|
||||
/// from the offset'th byte.
|
||||
/// </remarks>
|
||||
/// <param name="block">input sub-array.</param>
|
||||
/// <param name="offset">starting position of sub-array.</param>
|
||||
private void Transform(byte[] block, int offset)
|
||||
{
|
||||
// encodes 64 bytes from input block into an array of 16 32-bit
|
||||
// entities. Use A as a temp var.
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
_x[i] = (block[offset++] & unchecked(0xFF)) | (block[offset++] & unchecked(
|
||||
0xFF)) << 8 | (block[offset++] & unchecked(0xFF)) << 16 | (block[offset
|
||||
++] & unchecked(0xFF)) << 24;
|
||||
}
|
||||
int a = _context[0];
|
||||
int b = _context[1];
|
||||
int c = _context[2];
|
||||
int d = _context[3];
|
||||
a = Ff(a, b, c, d, _x[0], 3);
|
||||
d = Ff(d, a, b, c, _x[1], 7);
|
||||
c = Ff(c, d, a, b, _x[2], 11);
|
||||
b = Ff(b, c, d, a, _x[3], 19);
|
||||
a = Ff(a, b, c, d, _x[4], 3);
|
||||
d = Ff(d, a, b, c, _x[5], 7);
|
||||
c = Ff(c, d, a, b, _x[6], 11);
|
||||
b = Ff(b, c, d, a, _x[7], 19);
|
||||
a = Ff(a, b, c, d, _x[8], 3);
|
||||
d = Ff(d, a, b, c, _x[9], 7);
|
||||
c = Ff(c, d, a, b, _x[10], 11);
|
||||
b = Ff(b, c, d, a, _x[11], 19);
|
||||
a = Ff(a, b, c, d, _x[12], 3);
|
||||
d = Ff(d, a, b, c, _x[13], 7);
|
||||
c = Ff(c, d, a, b, _x[14], 11);
|
||||
b = Ff(b, c, d, a, _x[15], 19);
|
||||
a = Gg(a, b, c, d, _x[0], 3);
|
||||
d = Gg(d, a, b, c, _x[4], 5);
|
||||
c = Gg(c, d, a, b, _x[8], 9);
|
||||
b = Gg(b, c, d, a, _x[12], 13);
|
||||
a = Gg(a, b, c, d, _x[1], 3);
|
||||
d = Gg(d, a, b, c, _x[5], 5);
|
||||
c = Gg(c, d, a, b, _x[9], 9);
|
||||
b = Gg(b, c, d, a, _x[13], 13);
|
||||
a = Gg(a, b, c, d, _x[2], 3);
|
||||
d = Gg(d, a, b, c, _x[6], 5);
|
||||
c = Gg(c, d, a, b, _x[10], 9);
|
||||
b = Gg(b, c, d, a, _x[14], 13);
|
||||
a = Gg(a, b, c, d, _x[3], 3);
|
||||
d = Gg(d, a, b, c, _x[7], 5);
|
||||
c = Gg(c, d, a, b, _x[11], 9);
|
||||
b = Gg(b, c, d, a, _x[15], 13);
|
||||
a = Hh(a, b, c, d, _x[0], 3);
|
||||
d = Hh(d, a, b, c, _x[8], 9);
|
||||
c = Hh(c, d, a, b, _x[4], 11);
|
||||
b = Hh(b, c, d, a, _x[12], 15);
|
||||
a = Hh(a, b, c, d, _x[2], 3);
|
||||
d = Hh(d, a, b, c, _x[10], 9);
|
||||
c = Hh(c, d, a, b, _x[6], 11);
|
||||
b = Hh(b, c, d, a, _x[14], 15);
|
||||
a = Hh(a, b, c, d, _x[1], 3);
|
||||
d = Hh(d, a, b, c, _x[9], 9);
|
||||
c = Hh(c, d, a, b, _x[5], 11);
|
||||
b = Hh(b, c, d, a, _x[13], 15);
|
||||
a = Hh(a, b, c, d, _x[3], 3);
|
||||
d = Hh(d, a, b, c, _x[11], 9);
|
||||
c = Hh(c, d, a, b, _x[7], 11);
|
||||
b = Hh(b, c, d, a, _x[15], 15);
|
||||
_context[0] += a;
|
||||
_context[1] += b;
|
||||
_context[2] += c;
|
||||
_context[3] += d;
|
||||
}
|
||||
|
||||
// The basic MD4 atomic functions.
|
||||
private int Ff(int a, int b, int c, int d, int x, int s)
|
||||
{
|
||||
int t = a + ((b & c) | (~b & d)) + x;
|
||||
return t << s | (int)(((uint)t) >> (32 - s));
|
||||
}
|
||||
|
||||
private int Gg(int a, int b, int c, int d, int x, int s)
|
||||
{
|
||||
int t = a + ((b & (c | d)) | (c & d)) + x + unchecked(0x5A827999);
|
||||
return t << s | (int)(((uint)t) >> (32 - s));
|
||||
}
|
||||
|
||||
private int Hh(int a, int b, int c, int d, int x, int s)
|
||||
{
|
||||
int t = a + (b ^ c ^ d) + x + unchecked(0x6ED9EBA1);
|
||||
return t << s | (int)(((uint)t) >> (32 - s));
|
||||
}
|
||||
|
||||
public override byte[] Digest()
|
||||
{
|
||||
return EngineDigest();
|
||||
}
|
||||
|
||||
public override int GetDigestLength()
|
||||
{
|
||||
return EngineDigest().Length;
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
EngineReset();
|
||||
}
|
||||
|
||||
public override void Update(byte[] b)
|
||||
{
|
||||
EngineUpdate(b, 0, b.Length);
|
||||
}
|
||||
|
||||
public override void Update(byte b)
|
||||
{
|
||||
EngineUpdate(b);
|
||||
}
|
||||
|
||||
public override void Update(byte[] b, int offset, int len)
|
||||
{
|
||||
EngineUpdate(b, offset, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
68
Emby.Server.Implementations/IO/SharpCifs/Util/RC4.cs
Normal file
68
Emby.Server.Implementations/IO/SharpCifs/Util/RC4.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
// This code is derived from jcifs smb client library <jcifs at samba dot org>
|
||||
// Ported by J. Arturo <webmaster at komodosoft dot net>
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
namespace SharpCifs.Util
|
||||
{
|
||||
public class Rc4
|
||||
{
|
||||
internal byte[] S;
|
||||
|
||||
internal int I;
|
||||
|
||||
internal int J;
|
||||
|
||||
public Rc4()
|
||||
{
|
||||
}
|
||||
|
||||
public Rc4(byte[] key)
|
||||
{
|
||||
Init(key, 0, key.Length);
|
||||
}
|
||||
|
||||
public virtual void Init(byte[] key, int ki, int klen)
|
||||
{
|
||||
S = new byte[256];
|
||||
for (I = 0; I < 256; I++)
|
||||
{
|
||||
S[I] = unchecked((byte)I);
|
||||
}
|
||||
for (I = J = 0; I < 256; I++)
|
||||
{
|
||||
J = (J + key[ki + I % klen] + S[I]) & unchecked(0xff);
|
||||
byte t = S[I];
|
||||
S[I] = S[J];
|
||||
S[J] = t;
|
||||
}
|
||||
I = J = 0;
|
||||
}
|
||||
|
||||
public virtual void Update(byte[] src, int soff, int slen, byte[] dst, int doff)
|
||||
{
|
||||
int slim;
|
||||
slim = soff + slen;
|
||||
while (soff < slim)
|
||||
{
|
||||
I = (I + 1) & unchecked(0xff);
|
||||
J = (J + S[I]) & unchecked(0xff);
|
||||
byte t = S[I];
|
||||
S[I] = S[J];
|
||||
S[J] = t;
|
||||
dst[doff++] = unchecked((byte)(src[soff++] ^ S[(S[I] + S[J]) & unchecked(0xff)]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public abstract class AbstractMap<T, TU> : IDictionary<T, TU>
|
||||
{
|
||||
public virtual void Clear ()
|
||||
{
|
||||
EntrySet ().Clear ();
|
||||
}
|
||||
|
||||
public virtual bool ContainsKey (object name)
|
||||
{
|
||||
return EntrySet ().Any (p => p.Key.Equals ((T)name));
|
||||
}
|
||||
|
||||
public abstract ICollection<KeyValuePair<T, TU>> EntrySet ();
|
||||
|
||||
public virtual TU Get (object key)
|
||||
{
|
||||
return EntrySet ().Where (p => p.Key.Equals (key)).Select (p => p.Value).FirstOrDefault ();
|
||||
}
|
||||
|
||||
protected virtual IEnumerator<KeyValuePair<T, TU>> InternalGetEnumerator ()
|
||||
{
|
||||
return EntrySet ().GetEnumerator ();
|
||||
}
|
||||
|
||||
public virtual bool IsEmpty ()
|
||||
{
|
||||
return !EntrySet ().Any ();
|
||||
}
|
||||
|
||||
public virtual TU Put (T key, TU value)
|
||||
{
|
||||
throw new NotSupportedException ();
|
||||
}
|
||||
|
||||
public virtual TU Remove (object key)
|
||||
{
|
||||
Iterator<TU> iterator = EntrySet () as Iterator<TU>;
|
||||
if (iterator == null) {
|
||||
throw new NotSupportedException ();
|
||||
}
|
||||
while (iterator.HasNext ()) {
|
||||
TU local = iterator.Next ();
|
||||
if (local.Equals ((T)key)) {
|
||||
iterator.Remove ();
|
||||
return local;
|
||||
}
|
||||
}
|
||||
return default(TU);
|
||||
}
|
||||
|
||||
void ICollection<KeyValuePair<T, TU>>.Add (KeyValuePair<T, TU> item)
|
||||
{
|
||||
Put (item.Key, item.Value);
|
||||
}
|
||||
|
||||
bool ICollection<KeyValuePair<T, TU>>.Contains (KeyValuePair<T, TU> item)
|
||||
{
|
||||
throw new NotImplementedException ();
|
||||
}
|
||||
|
||||
void ICollection<KeyValuePair<T, TU>>.CopyTo (KeyValuePair<T, TU>[] array, int arrayIndex)
|
||||
{
|
||||
EntrySet ().CopyTo (array, arrayIndex);
|
||||
}
|
||||
|
||||
bool ICollection<KeyValuePair<T, TU>>.Remove (KeyValuePair<T, TU> item)
|
||||
{
|
||||
Remove (item.Key);
|
||||
return true;
|
||||
}
|
||||
|
||||
void IDictionary<T, TU>.Add (T key, TU value)
|
||||
{
|
||||
Put (key, value);
|
||||
}
|
||||
|
||||
bool IDictionary<T, TU>.ContainsKey (T key)
|
||||
{
|
||||
return ContainsKey (key);
|
||||
}
|
||||
|
||||
bool IDictionary<T, TU>.Remove (T key)
|
||||
{
|
||||
if (ContainsKey (key)) {
|
||||
Remove (key);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IDictionary<T, TU>.TryGetValue (T key, out TU value)
|
||||
{
|
||||
if (ContainsKey (key)) {
|
||||
value = Get (key);
|
||||
return true;
|
||||
}
|
||||
value = default(TU);
|
||||
return false;
|
||||
}
|
||||
|
||||
IEnumerator<KeyValuePair<T, TU>> IEnumerable<KeyValuePair<T, TU>>.GetEnumerator ()
|
||||
{
|
||||
return InternalGetEnumerator ();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator ()
|
||||
{
|
||||
return InternalGetEnumerator ();
|
||||
}
|
||||
|
||||
public virtual int Count {
|
||||
get { return EntrySet ().Count; }
|
||||
}
|
||||
|
||||
public TU this[T key] {
|
||||
get { return Get (key); }
|
||||
set { Put (key, value); }
|
||||
}
|
||||
|
||||
public virtual IEnumerable<T> Keys {
|
||||
get { return EntrySet ().Select (p => p.Key); }
|
||||
}
|
||||
|
||||
int ICollection<KeyValuePair<T, TU>>.Count {
|
||||
get { return Count; }
|
||||
}
|
||||
|
||||
bool ICollection<KeyValuePair<T, TU>>.IsReadOnly {
|
||||
get { return false; }
|
||||
}
|
||||
|
||||
ICollection<T> IDictionary<T, TU>.Keys {
|
||||
get { return Keys.ToList (); }
|
||||
}
|
||||
|
||||
ICollection<TU> IDictionary<T, TU>.Values {
|
||||
get { return Values.ToList (); }
|
||||
}
|
||||
|
||||
public virtual IEnumerable<TU> Values {
|
||||
get { return EntrySet ().Select (p => p.Value); }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class Arrays
|
||||
{
|
||||
public static List<T> AsList<T> (params T[] array)
|
||||
{
|
||||
return array.ToList ();
|
||||
}
|
||||
|
||||
public static bool Equals<T> (T[] a1, T[] a2)
|
||||
{
|
||||
if (a1.Length != a2.Length) {
|
||||
return false;
|
||||
}
|
||||
return !a1.Where((t, i) => !t.Equals(a2[i])).Any();
|
||||
}
|
||||
|
||||
public static void Fill<T> (T[] array, T val)
|
||||
{
|
||||
Fill (array, 0, array.Length, val);
|
||||
}
|
||||
|
||||
public static void Fill<T> (T[] array, int start, int end, T val)
|
||||
{
|
||||
for (int i = start; i < end; i++) {
|
||||
array[i] = val;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Sort (string[] array)
|
||||
{
|
||||
Array.Sort (array, (s1,s2) => string.CompareOrdinal (s1,s2));
|
||||
}
|
||||
|
||||
public static void Sort<T> (T[] array)
|
||||
{
|
||||
Array.Sort (array);
|
||||
}
|
||||
|
||||
public static void Sort<T> (T[] array, IComparer<T> c)
|
||||
{
|
||||
Array.Sort (array, c);
|
||||
}
|
||||
|
||||
public static void Sort<T> (T[] array, int start, int count)
|
||||
{
|
||||
Array.Sort (array, start, count);
|
||||
}
|
||||
|
||||
public static void Sort<T> (T[] array, int start, int count, IComparer<T> c)
|
||||
{
|
||||
Array.Sort (array, start, count, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class BufferedReader : StreamReader
|
||||
{
|
||||
public BufferedReader (InputStreamReader r) : base(r.BaseStream)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
//
|
||||
// BufferedWriter.cs
|
||||
//
|
||||
// Author:
|
||||
// Lluis Sanchez Gual <lluis@novell.com>
|
||||
//
|
||||
// Copyright (c) 2010 Novell, Inc (http://www.novell.com)
|
||||
//
|
||||
// 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.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class BufferedWriter
|
||||
{
|
||||
StreamWriter _writer;
|
||||
|
||||
public BufferedWriter (StreamWriter w)
|
||||
{
|
||||
_writer = w;
|
||||
}
|
||||
|
||||
public void Write (string s)
|
||||
{
|
||||
_writer.Write (s);
|
||||
}
|
||||
|
||||
public void NewLine ()
|
||||
{
|
||||
_writer.WriteLine ();
|
||||
}
|
||||
|
||||
public void Close ()
|
||||
{
|
||||
//Stream.`Close` method deleted
|
||||
//_writer.Close ();
|
||||
_writer.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class CharBuffer : CharSequence
|
||||
{
|
||||
internal string Wrapped;
|
||||
|
||||
public override string ToString ()
|
||||
{
|
||||
return Wrapped;
|
||||
}
|
||||
|
||||
public static CharBuffer Wrap (string str)
|
||||
{
|
||||
CharBuffer buffer = new CharBuffer ();
|
||||
buffer.Wrapped = str;
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using System.Text;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class CharSequence
|
||||
{
|
||||
public static implicit operator CharSequence (string str)
|
||||
{
|
||||
return new StringCharSequence (str);
|
||||
}
|
||||
|
||||
public static implicit operator CharSequence (StringBuilder str)
|
||||
{
|
||||
return new StringCharSequence (str.ToString ());
|
||||
}
|
||||
}
|
||||
|
||||
class StringCharSequence: CharSequence
|
||||
{
|
||||
string _str;
|
||||
|
||||
public StringCharSequence (string str)
|
||||
{
|
||||
this._str = str;
|
||||
}
|
||||
|
||||
public override string ToString ()
|
||||
{
|
||||
return _str;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal static class Collections<T>
|
||||
{
|
||||
static readonly IList<T> Empty = new T [0];
|
||||
public static IList<T> EmptySet {
|
||||
get { return Empty; }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Collections
|
||||
{
|
||||
public static bool AddAll<T> (ICollection<T> list, IEnumerable toAdd)
|
||||
{
|
||||
foreach (T t in toAdd)
|
||||
list.Add (t);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static TV Remove<TK, TV> (IDictionary<TK, TV> map, TK toRemove) where TK : class
|
||||
{
|
||||
TV local;
|
||||
if (map.TryGetValue (toRemove, out local)) {
|
||||
map.Remove (toRemove);
|
||||
return local;
|
||||
}
|
||||
return default(TV);
|
||||
}
|
||||
|
||||
|
||||
public static T[] ToArray<T> (ICollection<T> list)
|
||||
{
|
||||
T[] array = new T[list.Count];
|
||||
list.CopyTo (array, 0);
|
||||
return array;
|
||||
}
|
||||
|
||||
public static T[] ToArray<T>(List<object> list)
|
||||
{
|
||||
T[] array = new T[list.Count];
|
||||
for(int c = 0; c < list.Count; c++)
|
||||
{
|
||||
array[c] = (T)list[c];
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
|
||||
public static TU[] ToArray<T,TU> (ICollection<T> list, TU[] res) where T:TU
|
||||
{
|
||||
if (res.Length < list.Count)
|
||||
res = new TU [list.Count];
|
||||
|
||||
int n = 0;
|
||||
foreach (T t in list)
|
||||
res [n++] = t;
|
||||
|
||||
if (res.Length > list.Count)
|
||||
res [list.Count] = default (T);
|
||||
return res;
|
||||
}
|
||||
|
||||
public static IDictionary<TK,TV> EmptyMap<TK,TV> ()
|
||||
{
|
||||
return new Dictionary<TK,TV> ();
|
||||
}
|
||||
|
||||
public static IList<T> EmptyList<T> ()
|
||||
{
|
||||
return Collections<T>.EmptySet;
|
||||
}
|
||||
|
||||
public static ICollection<T> EmptySet<T> ()
|
||||
{
|
||||
return Collections<T>.EmptySet;
|
||||
}
|
||||
|
||||
public static IList<T> NCopies<T> (int n, T elem)
|
||||
{
|
||||
List<T> list = new List<T> (n);
|
||||
while (n-- > 0) {
|
||||
list.Add (elem);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static void Reverse<T> (IList<T> list)
|
||||
{
|
||||
int end = list.Count - 1;
|
||||
int index = 0;
|
||||
while (index < end) {
|
||||
T tmp = list [index];
|
||||
list [index] = list [end];
|
||||
list [end] = tmp;
|
||||
++index;
|
||||
--end;
|
||||
}
|
||||
}
|
||||
|
||||
public static ICollection<T> Singleton<T> (T item)
|
||||
{
|
||||
List<T> list = new List<T> (1);
|
||||
list.Add (item);
|
||||
return list;
|
||||
}
|
||||
|
||||
public static IList<T> SingletonList<T> (T item)
|
||||
{
|
||||
List<T> list = new List<T> (1);
|
||||
list.Add (item);
|
||||
return list;
|
||||
}
|
||||
|
||||
public static IList<T> SynchronizedList<T> (IList<T> list)
|
||||
{
|
||||
return new SynchronizedList<T> (list);
|
||||
}
|
||||
|
||||
public static ICollection<T> UnmodifiableCollection<T> (ICollection<T> list)
|
||||
{
|
||||
return list;
|
||||
}
|
||||
|
||||
public static IList<T> UnmodifiableList<T> (IList<T> list)
|
||||
{
|
||||
return new ReadOnlyCollection<T> (list);
|
||||
}
|
||||
|
||||
public static ICollection<T> UnmodifiableSet<T> (ICollection<T> list)
|
||||
{
|
||||
return list;
|
||||
}
|
||||
|
||||
public static IDictionary<TK,TV> UnmodifiableMap<TK,TV> (IDictionary<TK,TV> dict)
|
||||
{
|
||||
return dict;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class ConcurrentHashMap<T, TU> : AbstractMap<T, TU>, IConcurrentMap<T, TU>
|
||||
{
|
||||
private Dictionary<T, TU> _table;
|
||||
|
||||
public ConcurrentHashMap ()
|
||||
{
|
||||
_table = new Dictionary<T, TU> ();
|
||||
}
|
||||
|
||||
public ConcurrentHashMap (int initialCapacity, float loadFactor, int concurrencyLevel)
|
||||
{
|
||||
_table = new Dictionary<T, TU> (initialCapacity);
|
||||
}
|
||||
|
||||
public override void Clear ()
|
||||
{
|
||||
lock (_table) {
|
||||
_table = new Dictionary<T, TU> ();
|
||||
}
|
||||
}
|
||||
|
||||
public override bool ContainsKey (object name)
|
||||
{
|
||||
return _table.ContainsKey ((T)name);
|
||||
}
|
||||
|
||||
public override ICollection<KeyValuePair<T, TU>> EntrySet ()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
public override TU Get (object key)
|
||||
{
|
||||
TU local;
|
||||
_table.TryGetValue ((T)key, out local);
|
||||
return local;
|
||||
}
|
||||
|
||||
protected override IEnumerator<KeyValuePair<T, TU>> InternalGetEnumerator ()
|
||||
{
|
||||
return _table.GetEnumerator ();
|
||||
}
|
||||
|
||||
public override bool IsEmpty ()
|
||||
{
|
||||
return _table.Count == 0;
|
||||
}
|
||||
|
||||
public override TU Put (T key, TU value)
|
||||
{
|
||||
lock (_table) {
|
||||
TU old = Get (key);
|
||||
Dictionary<T, TU> newTable = new Dictionary<T, TU> (_table);
|
||||
newTable[key] = value;
|
||||
_table = newTable;
|
||||
return old;
|
||||
}
|
||||
}
|
||||
|
||||
public TU PutIfAbsent (T key, TU value)
|
||||
{
|
||||
lock (_table) {
|
||||
if (!ContainsKey (key)) {
|
||||
Dictionary<T, TU> newTable = new Dictionary<T, TU> (_table);
|
||||
newTable[key] = value;
|
||||
_table = newTable;
|
||||
return value;
|
||||
}
|
||||
return Get (key);
|
||||
}
|
||||
}
|
||||
|
||||
public override TU Remove (object key)
|
||||
{
|
||||
lock (_table) {
|
||||
TU old = Get ((T)key);
|
||||
Dictionary<T, TU> newTable = new Dictionary<T, TU> (_table);
|
||||
newTable.Remove ((T)key);
|
||||
_table = newTable;
|
||||
return old;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Remove (object key, object value)
|
||||
{
|
||||
lock (_table) {
|
||||
if (ContainsKey (key) && value.Equals (Get (key))) {
|
||||
Dictionary<T, TU> newTable = new Dictionary<T, TU> (_table);
|
||||
newTable.Remove ((T)key);
|
||||
_table = newTable;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Replace (T key, TU oldValue, TU newValue)
|
||||
{
|
||||
lock (_table) {
|
||||
if (ContainsKey (key) && oldValue.Equals (Get (key))) {
|
||||
Dictionary<T, TU> newTable = new Dictionary<T, TU> (_table);
|
||||
newTable[key] = newValue;
|
||||
_table = newTable;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override IEnumerable<T> Keys {
|
||||
get { return _table.Keys; }
|
||||
}
|
||||
|
||||
public override IEnumerable<TU> Values {
|
||||
get { return _table.Values; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public abstract class DateFormat
|
||||
{
|
||||
public const int Default = 2;
|
||||
|
||||
public static DateFormat GetDateTimeInstance (int dateStyle, int timeStyle)
|
||||
{
|
||||
return GetDateTimeInstance (dateStyle, timeStyle, CultureInfo.CurrentCulture);
|
||||
}
|
||||
|
||||
public static DateFormat GetDateTimeInstance (int dateStyle, int timeStyle, CultureInfo aLocale)
|
||||
{
|
||||
return new SimpleDateFormat (aLocale.DateTimeFormat.FullDateTimePattern, aLocale);
|
||||
}
|
||||
|
||||
TimeZoneInfo _timeZone;
|
||||
|
||||
public abstract DateTime Parse (string value);
|
||||
|
||||
public TimeZoneInfo GetTimeZone ()
|
||||
{
|
||||
return _timeZone;
|
||||
}
|
||||
|
||||
public void SetTimeZone (TimeZoneInfo timeZone)
|
||||
{
|
||||
this._timeZone = timeZone;
|
||||
}
|
||||
|
||||
public abstract string Format (DateTime time);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class EnumeratorWrapper<T> : Iterator<T>
|
||||
{
|
||||
object _collection;
|
||||
IEnumerator<T> _e;
|
||||
T _lastVal;
|
||||
bool _more;
|
||||
bool _copied;
|
||||
|
||||
public EnumeratorWrapper (object collection, IEnumerator<T> e)
|
||||
{
|
||||
this._e = e;
|
||||
this._collection = collection;
|
||||
_more = e.MoveNext ();
|
||||
}
|
||||
|
||||
public override bool HasNext ()
|
||||
{
|
||||
return _more;
|
||||
}
|
||||
|
||||
public override T Next ()
|
||||
{
|
||||
if (!_more)
|
||||
throw new NoSuchElementException ();
|
||||
_lastVal = _e.Current;
|
||||
_more = _e.MoveNext ();
|
||||
return _lastVal;
|
||||
}
|
||||
|
||||
public override void Remove ()
|
||||
{
|
||||
ICollection<T> col = _collection as ICollection<T>;
|
||||
if (col == null) {
|
||||
throw new NotSupportedException ();
|
||||
}
|
||||
if (_more && !_copied) {
|
||||
// Read the remaining elements, since the current enumerator
|
||||
// will be invalid after removing the element
|
||||
List<T> remaining = new List<T> ();
|
||||
do {
|
||||
remaining.Add (_e.Current);
|
||||
} while (_e.MoveNext ());
|
||||
_e = remaining.GetEnumerator ();
|
||||
_e.MoveNext ();
|
||||
_copied = true;
|
||||
}
|
||||
col.Remove (_lastVal);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
//
|
||||
// Exceptions.cs
|
||||
//
|
||||
// Author:
|
||||
// Lluis Sanchez Gual <lluis@novell.com>
|
||||
//
|
||||
// Copyright (c) 2010 Novell, Inc (http://www.novell.com)
|
||||
//
|
||||
// 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;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class VirtualMachineError : Error
|
||||
{
|
||||
}
|
||||
|
||||
public class StackOverflowError : VirtualMachineError
|
||||
{
|
||||
}
|
||||
|
||||
public class BrokenBarrierException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
internal class BufferUnderflowException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
public class CharacterCodingException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
public class DataFormatException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
public class EofException : Exception
|
||||
{
|
||||
public EofException ()
|
||||
{
|
||||
}
|
||||
|
||||
public EofException (string msg) : base(msg)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class Error : Exception
|
||||
{
|
||||
public Error ()
|
||||
{
|
||||
}
|
||||
|
||||
public Error (Exception ex) : base("Runtime Exception", ex)
|
||||
{
|
||||
}
|
||||
|
||||
public Error (string msg) : base(msg)
|
||||
{
|
||||
}
|
||||
|
||||
public Error (string msg, Exception ex) : base(msg, ex)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class ExecutionException : Exception
|
||||
{
|
||||
public ExecutionException (Exception inner): base ("Execution failed", inner)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class InstantiationException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
public class InterruptedIoException : Exception
|
||||
{
|
||||
public InterruptedIoException (string msg) : base(msg)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class MissingResourceException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
public class NoSuchAlgorithmException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
public class NoSuchElementException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
internal class NoSuchMethodException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
internal class OverlappingFileLockException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
public class ParseException : Exception
|
||||
{
|
||||
public ParseException ()
|
||||
{
|
||||
}
|
||||
|
||||
public ParseException (string msg, int errorOffset) : base(string.Format ("Msg: {0}. Error Offset: {1}", msg, errorOffset))
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class RuntimeException : Exception
|
||||
{
|
||||
public RuntimeException ()
|
||||
{
|
||||
}
|
||||
|
||||
public RuntimeException (Exception ex) : base("Runtime Exception", ex)
|
||||
{
|
||||
}
|
||||
|
||||
public RuntimeException (string msg) : base(msg)
|
||||
{
|
||||
}
|
||||
|
||||
public RuntimeException (string msg, Exception ex) : base(msg, ex)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
internal class StringIndexOutOfBoundsException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
public class UnknownHostException : Exception
|
||||
{
|
||||
public UnknownHostException ()
|
||||
{
|
||||
}
|
||||
|
||||
public UnknownHostException(string message) : base(message)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public UnknownHostException (Exception ex): base ("Host not found", ex)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class UnsupportedEncodingException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
internal class UriSyntaxException : Exception
|
||||
{
|
||||
public UriSyntaxException (string s, string msg) : base(s + " " + msg)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
internal class ZipException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
public class GitException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
public class ConnectException: Exception
|
||||
{
|
||||
public ConnectException (string msg): base (msg)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
class KeyManagementException: Exception
|
||||
{
|
||||
}
|
||||
|
||||
class IllegalCharsetNameException: Exception
|
||||
{
|
||||
public IllegalCharsetNameException (string msg): base (msg)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
class UnsupportedCharsetException: Exception
|
||||
{
|
||||
public UnsupportedCharsetException (string msg): base (msg)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,696 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
//using Windows.Networking;
|
||||
//using Windows.Networking.Sockets;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
|
||||
|
||||
public static class Extensions
|
||||
{
|
||||
private static readonly long EpochTicks;
|
||||
|
||||
static Extensions()
|
||||
{
|
||||
DateTime time = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||
EpochTicks = time.Ticks;
|
||||
}
|
||||
|
||||
public static void Add<T>(this IList<T> list, int index, T item)
|
||||
{
|
||||
list.Insert(index, item);
|
||||
}
|
||||
|
||||
public static void AddFirst<T>(this IList<T> list, T item)
|
||||
{
|
||||
list.Insert(0, item);
|
||||
}
|
||||
|
||||
public static void AddLast<T>(this IList<T> list, T item)
|
||||
{
|
||||
list.Add(item);
|
||||
}
|
||||
|
||||
public static void RemoveLast<T>(this IList<T> list)
|
||||
{
|
||||
if (list.Count > 0)
|
||||
list.Remove(list.Count - 1);
|
||||
}
|
||||
|
||||
public static StringBuilder AppendRange(this StringBuilder sb, string str, int start, int end)
|
||||
{
|
||||
return sb.Append(str, start, end - start);
|
||||
}
|
||||
|
||||
public static StringBuilder Delete(this StringBuilder sb, int start, int end)
|
||||
{
|
||||
return sb.Remove(start, end - start);
|
||||
}
|
||||
|
||||
public static void SetCharAt(this StringBuilder sb, int index, char c)
|
||||
{
|
||||
sb[index] = c;
|
||||
}
|
||||
|
||||
public static int IndexOf(this StringBuilder sb, string str)
|
||||
{
|
||||
return sb.ToString().IndexOf(str);
|
||||
}
|
||||
|
||||
public static int BitCount(int val)
|
||||
{
|
||||
uint num = (uint)val;
|
||||
int count = 0;
|
||||
for (int i = 0; i < 32; i++)
|
||||
{
|
||||
if ((num & 1) != 0)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
num >>= 1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public static IndexOutOfRangeException CreateIndexOutOfRangeException(int index)
|
||||
{
|
||||
return new IndexOutOfRangeException("Index: " + index);
|
||||
}
|
||||
|
||||
public static string Decode(this Encoding e, byte[] chars, int start, int len)
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] bom = e.GetPreamble();
|
||||
if (bom != null && bom.Length > 0)
|
||||
{
|
||||
if (len >= bom.Length)
|
||||
{
|
||||
int pos = start;
|
||||
bool hasBom = true;
|
||||
for (int n = 0; n < bom.Length && hasBom; n++)
|
||||
{
|
||||
if (bom[n] != chars[pos++])
|
||||
hasBom = false;
|
||||
}
|
||||
if (hasBom)
|
||||
{
|
||||
len -= pos - start;
|
||||
start = pos;
|
||||
}
|
||||
}
|
||||
}
|
||||
return e.GetString(chars, start, len);
|
||||
}
|
||||
catch (DecoderFallbackException)
|
||||
{
|
||||
throw new CharacterCodingException();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static Encoding GetEncoding(string name)
|
||||
{
|
||||
// Encoding e = Encoding.GetEncoding (name, EncoderFallback.ExceptionFallback, DecoderFallback.ExceptionFallback);
|
||||
try
|
||||
{
|
||||
|
||||
Encoding e = Encoding.GetEncoding(name.Replace('_', '-'));
|
||||
if (e is UTF8Encoding)
|
||||
return new UTF8Encoding(false, true);
|
||||
|
||||
return e;
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
throw new UnsupportedCharsetException(name);
|
||||
}
|
||||
}
|
||||
|
||||
public static ICollection<KeyValuePair<T, TU>> EntrySet<T, TU>(this IDictionary<T, TU> s)
|
||||
{
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
public static bool AddItem<T>(this IList<T> list, T item)
|
||||
{
|
||||
list.Add(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool AddItem<T>(this ICollection<T> list, T item)
|
||||
{
|
||||
list.Add(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static TU Get<T, TU>(this IDictionary<T, TU> d, T key)
|
||||
{
|
||||
TU val;
|
||||
d.TryGetValue(key, out val);
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
public static TU Put<T, TU>(this IDictionary<T, TU> d, T key, TU value)
|
||||
{
|
||||
TU old;
|
||||
d.TryGetValue(key, out old);
|
||||
d[key] = value;
|
||||
return old;
|
||||
}
|
||||
|
||||
public static void PutAll<T, TU>(this IDictionary<T, TU> d, IDictionary<T, TU> values)
|
||||
{
|
||||
foreach (KeyValuePair<T, TU> val in values)
|
||||
d[val.Key] = val.Value;
|
||||
}
|
||||
|
||||
|
||||
public static CultureInfo GetEnglishCulture()
|
||||
{
|
||||
return new CultureInfo("en-US");
|
||||
}
|
||||
|
||||
public static T GetFirst<T>(this IList<T> list)
|
||||
{
|
||||
return ((list.Count == 0) ? default(T) : list[0]);
|
||||
}
|
||||
|
||||
public static CultureInfo GetGermanCulture()
|
||||
{
|
||||
CultureInfo r = new CultureInfo("de-DE");
|
||||
return r;
|
||||
}
|
||||
|
||||
public static T GetLast<T>(this IList<T> list)
|
||||
{
|
||||
return ((list.Count == 0) ? default(T) : list[list.Count - 1]);
|
||||
}
|
||||
|
||||
public static int GetOffset(this TimeZoneInfo tzone, long date)
|
||||
{
|
||||
return (int)tzone.GetUtcOffset(MillisToDateTimeOffset(date, 0).DateTime).TotalMilliseconds;
|
||||
}
|
||||
|
||||
public static long GetTime(this DateTime dateTime)
|
||||
{
|
||||
return new DateTimeOffset(DateTime.SpecifyKind(dateTime, DateTimeKind.Utc), TimeSpan.Zero).ToMillisecondsSinceEpoch();
|
||||
}
|
||||
|
||||
public static void InitCause(this Exception ex, Exception cause)
|
||||
{
|
||||
Console.WriteLine(cause);
|
||||
}
|
||||
|
||||
public static bool IsEmpty<T>(this ICollection<T> col)
|
||||
{
|
||||
return (col.Count == 0);
|
||||
}
|
||||
|
||||
public static bool IsEmpty<T>(this Stack<T> col)
|
||||
{
|
||||
return (col.Count == 0);
|
||||
}
|
||||
|
||||
public static bool IsLower(this char c)
|
||||
{
|
||||
return char.IsLower(c);
|
||||
}
|
||||
|
||||
public static bool IsUpper(this char c)
|
||||
{
|
||||
return char.IsUpper(c);
|
||||
}
|
||||
|
||||
public static Iterator<T> Iterator<T>(this ICollection<T> col)
|
||||
{
|
||||
return new EnumeratorWrapper<T>(col, col.GetEnumerator());
|
||||
}
|
||||
|
||||
public static Iterator<T> Iterator<T>(this IEnumerable<T> col)
|
||||
{
|
||||
return new EnumeratorWrapper<T>(col, col.GetEnumerator());
|
||||
}
|
||||
|
||||
public static T Last<T>(this ICollection<T> col)
|
||||
{
|
||||
IList<T> list = col as IList<T>;
|
||||
if (list != null)
|
||||
{
|
||||
return list[list.Count - 1];
|
||||
}
|
||||
return col.Last();
|
||||
}
|
||||
|
||||
public static int LowestOneBit(int val)
|
||||
{
|
||||
return (1 << NumberOfTrailingZeros(val));
|
||||
}
|
||||
|
||||
public static bool Matches(this string str, string regex)
|
||||
{
|
||||
Regex regex2 = new Regex(regex);
|
||||
return regex2.IsMatch(str);
|
||||
}
|
||||
|
||||
public static DateTime CreateDate(long milliSecondsSinceEpoch)
|
||||
{
|
||||
long num = EpochTicks + (milliSecondsSinceEpoch * 10000);
|
||||
return new DateTime(num);
|
||||
}
|
||||
|
||||
public static DateTime CreateDateFromUTC(long milliSecondsSinceEpoch)
|
||||
{
|
||||
long num = EpochTicks + (milliSecondsSinceEpoch * 10000);
|
||||
return new DateTime(num, DateTimeKind.Utc);
|
||||
}
|
||||
|
||||
|
||||
public static DateTimeOffset MillisToDateTimeOffset(long milliSecondsSinceEpoch, long offsetMinutes)
|
||||
{
|
||||
TimeSpan offset = TimeSpan.FromMinutes(offsetMinutes);
|
||||
long num = EpochTicks + (milliSecondsSinceEpoch * 10000);
|
||||
return new DateTimeOffset(num + offset.Ticks, offset);
|
||||
}
|
||||
|
||||
public static int NumberOfLeadingZeros(int val)
|
||||
{
|
||||
uint num = (uint)val;
|
||||
int count = 0;
|
||||
while ((num & 0x80000000) == 0)
|
||||
{
|
||||
num = num << 1;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public static int NumberOfTrailingZeros(int val)
|
||||
{
|
||||
uint num = (uint)val;
|
||||
int count = 0;
|
||||
while ((num & 1) == 0)
|
||||
{
|
||||
num = num >> 1;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public static int Read(this StreamReader reader, char[] data)
|
||||
{
|
||||
return reader.Read(data, 0, data.Length);
|
||||
}
|
||||
|
||||
public static T Remove<T>(this IList<T> list, T item)
|
||||
{
|
||||
int index = list.IndexOf(item);
|
||||
if (index == -1)
|
||||
{
|
||||
return default(T);
|
||||
}
|
||||
T local = list[index];
|
||||
list.RemoveAt(index);
|
||||
return local;
|
||||
}
|
||||
|
||||
public static T Remove<T>(this IList<T> list, int i)
|
||||
{
|
||||
T old;
|
||||
try
|
||||
{
|
||||
old = list[i];
|
||||
list.RemoveAt(i);
|
||||
}
|
||||
catch (IndexOutOfRangeException)
|
||||
{
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
return old;
|
||||
}
|
||||
|
||||
public static T RemoveFirst<T>(this IList<T> list)
|
||||
{
|
||||
return list.Remove(0);
|
||||
}
|
||||
|
||||
public static string ReplaceAll(this string str, string regex, string replacement)
|
||||
{
|
||||
Regex rgx = new Regex(regex);
|
||||
|
||||
if (replacement.IndexOfAny(new[] { '\\', '$' }) != -1)
|
||||
{
|
||||
// Back references not yet supported
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int n = 0; n < replacement.Length; n++)
|
||||
{
|
||||
char c = replacement[n];
|
||||
if (c == '$')
|
||||
throw new NotSupportedException("Back references not supported");
|
||||
if (c == '\\')
|
||||
c = replacement[++n];
|
||||
sb.Append(c);
|
||||
}
|
||||
replacement = sb.ToString();
|
||||
}
|
||||
|
||||
return rgx.Replace(str, replacement);
|
||||
}
|
||||
|
||||
public static bool RegionMatches(this string str, bool ignoreCase, int toOffset, string other, int ooffset, int len)
|
||||
{
|
||||
if (toOffset < 0 || ooffset < 0 || toOffset + len > str.Length || ooffset + len > other.Length)
|
||||
return false;
|
||||
return string.Compare(str, toOffset, other, ooffset, len) == 0;
|
||||
}
|
||||
|
||||
public static T Set<T>(this IList<T> list, int index, T item)
|
||||
{
|
||||
T old = list[index];
|
||||
list[index] = item;
|
||||
return old;
|
||||
}
|
||||
|
||||
public static int Signum(long val)
|
||||
{
|
||||
if (val < 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if (val > 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static void RemoveAll<T, TU>(this ICollection<T> col, ICollection<TU> items) where TU : T
|
||||
{
|
||||
foreach (var u in items)
|
||||
col.Remove(u);
|
||||
}
|
||||
|
||||
public static bool ContainsAll<T, TU>(this ICollection<T> col, ICollection<TU> items) where TU : T
|
||||
{
|
||||
foreach (var u in items)
|
||||
if (!col.Any(n => (ReferenceEquals(n, u)) || n.Equals(u)))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool Contains<T>(this ICollection<T> col, object item)
|
||||
{
|
||||
if (!(item is T))
|
||||
return false;
|
||||
return col.Any(n => (ReferenceEquals(n, item)) || n.Equals(item));
|
||||
}
|
||||
|
||||
public static void Sort<T>(this IList<T> list)
|
||||
{
|
||||
List<T> sorted = new List<T>(list);
|
||||
sorted.Sort();
|
||||
for (int i = 0; i < list.Count; i++)
|
||||
{
|
||||
list[i] = sorted[i];
|
||||
}
|
||||
}
|
||||
|
||||
public static void Sort<T>(this IList<T> list, IComparer<T> comparer)
|
||||
{
|
||||
List<T> sorted = new List<T>(list);
|
||||
sorted.Sort(comparer);
|
||||
for (int i = 0; i < list.Count; i++)
|
||||
{
|
||||
list[i] = sorted[i];
|
||||
}
|
||||
}
|
||||
|
||||
public static string[] Split(this string str, string regex)
|
||||
{
|
||||
return str.Split(regex, 0);
|
||||
}
|
||||
|
||||
public static string[] Split(this string str, string regex, int limit)
|
||||
{
|
||||
Regex rgx = new Regex(regex);
|
||||
List<string> list = new List<string>();
|
||||
int startIndex = 0;
|
||||
if (limit != 1)
|
||||
{
|
||||
int nm = 1;
|
||||
foreach (Match match in rgx.Matches(str))
|
||||
{
|
||||
list.Add(str.Substring(startIndex, match.Index - startIndex));
|
||||
startIndex = match.Index + match.Length;
|
||||
if (limit > 0 && ++nm == limit)
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (startIndex < str.Length)
|
||||
{
|
||||
list.Add(str.Substring(startIndex));
|
||||
}
|
||||
if (limit >= 0)
|
||||
{
|
||||
int count = list.Count - 1;
|
||||
while ((count >= 0) && (list[count].Length == 0))
|
||||
{
|
||||
count--;
|
||||
}
|
||||
list.RemoveRange(count + 1, (list.Count - count) - 1);
|
||||
}
|
||||
return list.ToArray();
|
||||
}
|
||||
|
||||
public static IList<T> SubList<T>(this IList<T> list, int start, int len)
|
||||
{
|
||||
List<T> sublist = new List<T>(len);
|
||||
for (int i = start; i < (start + len); i++)
|
||||
{
|
||||
sublist.Add(list[i]);
|
||||
}
|
||||
return sublist;
|
||||
}
|
||||
|
||||
public static char[] ToCharArray(this string str)
|
||||
{
|
||||
char[] destination = new char[str.Length];
|
||||
str.CopyTo(0, destination, 0, str.Length);
|
||||
return destination;
|
||||
}
|
||||
|
||||
public static long ToMillisecondsSinceEpoch(this DateTime dateTime)
|
||||
{
|
||||
if (dateTime.Kind != DateTimeKind.Utc)
|
||||
{
|
||||
throw new ArgumentException("dateTime is expected to be expressed as a UTC DateTime", "dateTime");
|
||||
}
|
||||
return new DateTimeOffset(DateTime.SpecifyKind(dateTime, DateTimeKind.Utc), TimeSpan.Zero).ToMillisecondsSinceEpoch();
|
||||
}
|
||||
|
||||
public static long ToMillisecondsSinceEpoch(this DateTimeOffset dateTimeOffset)
|
||||
{
|
||||
return (((dateTimeOffset.Ticks - dateTimeOffset.Offset.Ticks) - EpochTicks) / TimeSpan.TicksPerMillisecond);
|
||||
}
|
||||
|
||||
public static string ToOctalString(int val)
|
||||
{
|
||||
return Convert.ToString(val, 8);
|
||||
}
|
||||
|
||||
public static string ToHexString(int val)
|
||||
{
|
||||
return Convert.ToString(val, 16);
|
||||
}
|
||||
|
||||
public static string ToString(object val)
|
||||
{
|
||||
return val.ToString();
|
||||
}
|
||||
|
||||
public static string ToString(int val, int bas)
|
||||
{
|
||||
return Convert.ToString(val, bas);
|
||||
}
|
||||
|
||||
public static IList<TU> UpcastTo<T, TU>(this IList<T> s) where T : TU
|
||||
{
|
||||
List<TU> list = new List<TU>(s.Count);
|
||||
for (int i = 0; i < s.Count; i++)
|
||||
{
|
||||
list.Add(s[i]);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static ICollection<TU> UpcastTo<T, TU>(this ICollection<T> s) where T : TU
|
||||
{
|
||||
List<TU> list = new List<TU>(s.Count);
|
||||
foreach (var v in s)
|
||||
{
|
||||
list.Add(v);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static T ValueOf<T>(T val)
|
||||
{
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
//use? for NUnit-testing?
|
||||
//public static string GetTestName(object obj)
|
||||
//{
|
||||
// return GetTestName();
|
||||
//}
|
||||
|
||||
//public static string GetTestName()
|
||||
//{
|
||||
// MethodBase met;
|
||||
// int n = 0;
|
||||
// do
|
||||
// {
|
||||
// met = new StackFrame(n).GetMethod();
|
||||
// if (met != null)
|
||||
// {
|
||||
// foreach (Attribute at in met.GetCustomAttributes(true))
|
||||
// {
|
||||
// if (at.GetType().FullName == "NUnit.Framework.TestAttribute")
|
||||
// {
|
||||
// // Convert back to camel case
|
||||
// string name = met.Name;
|
||||
// if (char.IsUpper(name[0]))
|
||||
// name = char.ToLower(name[0]) + name.Substring(1);
|
||||
// return name;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// n++;
|
||||
// } while (met != null);
|
||||
// return "";
|
||||
//}
|
||||
|
||||
public static string GetHostAddress(this IPAddress addr)
|
||||
{
|
||||
return addr.ToString();
|
||||
}
|
||||
|
||||
|
||||
public static IPAddress GetAddressByName(string host)
|
||||
{
|
||||
if (host == "0.0.0.0")
|
||||
{
|
||||
return IPAddress.Any;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return IPAddress.Parse(host);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static IPAddress[] GetAddressesByName(string host)
|
||||
{
|
||||
//IReadOnlyList<EndpointPair> data = null;
|
||||
|
||||
//try
|
||||
//{
|
||||
// Task.Run(async () =>
|
||||
// {
|
||||
// data = await DatagramSocket.GetEndpointPairsAsync(new HostName(host), "0");
|
||||
// }).Wait();
|
||||
//}
|
||||
//catch (Exception ex)
|
||||
//{
|
||||
// return null;
|
||||
//}
|
||||
|
||||
//return data != null
|
||||
// ? data.Where(i => i.RemoteHostName.Type == HostNameType.Ipv4)
|
||||
// .GroupBy(i => i.RemoteHostName.DisplayName)
|
||||
// .Select(i => IPAddress.Parse(i.First().RemoteHostName.DisplayName))
|
||||
// .ToArray()
|
||||
// : null;
|
||||
|
||||
//get v4-address only
|
||||
var entry = Task.Run(() => System.Net.Dns.GetHostEntryAsync(host))
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
return entry.AddressList
|
||||
.Where(addr => addr.AddressFamily == AddressFamily.InterNetwork)
|
||||
.ToArray();
|
||||
|
||||
}
|
||||
|
||||
public static string GetImplementationVersion(this Assembly asm)
|
||||
{
|
||||
return asm.GetName().Version.ToString();
|
||||
}
|
||||
|
||||
public static string GetHost(this Uri uri)
|
||||
{
|
||||
return string.IsNullOrEmpty(uri.Host) ? "" : uri.Host;
|
||||
}
|
||||
|
||||
public static string GetUserInfo(this Uri uri)
|
||||
{
|
||||
return string.IsNullOrEmpty(uri.UserInfo) ? null : uri.UserInfo;
|
||||
}
|
||||
|
||||
public static string GetQuery(this Uri uri)
|
||||
{
|
||||
return string.IsNullOrEmpty(uri.Query) ? null : uri.Query;
|
||||
}
|
||||
|
||||
public static int GetLocalPort(this Socket socket)
|
||||
{
|
||||
return ((IPEndPoint)socket.LocalEndPoint).Port;
|
||||
}
|
||||
|
||||
public static int GetPort(this Socket socket)
|
||||
{
|
||||
return ((IPEndPoint)socket.RemoteEndPoint).Port;
|
||||
}
|
||||
|
||||
public static IPAddress GetInetAddress(this Socket socket)
|
||||
{
|
||||
return ((IPEndPoint)socket.RemoteEndPoint).Address;
|
||||
}
|
||||
|
||||
|
||||
/*public static bool RemoveElement(this ArrayList list, object elem)
|
||||
{
|
||||
int i = list.IndexOf(elem);
|
||||
if (i == -1)
|
||||
return false;
|
||||
list.RemoveAt(i);
|
||||
return true;
|
||||
}*/
|
||||
|
||||
public static Semaphore CreateSemaphore(int count)
|
||||
{
|
||||
return new Semaphore(count, int.MaxValue);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class FileInputStream : InputStream
|
||||
{
|
||||
public FileInputStream (FilePath file) : this(file.GetPath ())
|
||||
{
|
||||
}
|
||||
|
||||
public FileInputStream (string file)
|
||||
{
|
||||
if (!File.Exists (file)) {
|
||||
throw new FileNotFoundException ("File not found", file);
|
||||
}
|
||||
Wrapped = new FileStream (file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class FileOutputStream : OutputStream
|
||||
{
|
||||
public FileOutputStream (FilePath file): this (file.GetPath (), false)
|
||||
{
|
||||
}
|
||||
|
||||
public FileOutputStream (string file): this (file, false)
|
||||
{
|
||||
}
|
||||
|
||||
public FileOutputStream (FilePath file, bool append) : this(file.GetPath (), append)
|
||||
{
|
||||
}
|
||||
|
||||
public FileOutputStream (string file, bool append)
|
||||
{
|
||||
try {
|
||||
if (append) {
|
||||
Wrapped = File.Open (file, FileMode.Append, FileAccess.Write);
|
||||
} else {
|
||||
Wrapped = File.Open (file, FileMode.Create, FileAccess.Write);
|
||||
}
|
||||
} catch (DirectoryNotFoundException) {
|
||||
throw new FileNotFoundException ("File not found: " + file);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,313 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class FilePath
|
||||
{
|
||||
private string _path;
|
||||
private static long _tempCounter;
|
||||
|
||||
public FilePath ()
|
||||
{
|
||||
}
|
||||
|
||||
public FilePath (string path)
|
||||
: this ((string) null, path)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public FilePath (FilePath other, string child)
|
||||
: this ((string) other, child)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public FilePath (string other, string child)
|
||||
{
|
||||
if (other == null) {
|
||||
_path = child;
|
||||
} else {
|
||||
while (!string.IsNullOrEmpty(child) && (child[0] == Path.DirectorySeparatorChar || child[0] == Path.AltDirectorySeparatorChar))
|
||||
child = child.Substring (1);
|
||||
|
||||
if (!string.IsNullOrEmpty(other) && other[other.Length - 1] == Path.VolumeSeparatorChar)
|
||||
other += Path.DirectorySeparatorChar;
|
||||
|
||||
_path = Path.Combine (other, child);
|
||||
}
|
||||
}
|
||||
|
||||
public static implicit operator FilePath (string name)
|
||||
{
|
||||
return new FilePath (name);
|
||||
}
|
||||
|
||||
public static implicit operator string (FilePath filePath)
|
||||
{
|
||||
return filePath == null ? null : filePath._path;
|
||||
}
|
||||
|
||||
public override bool Equals (object obj)
|
||||
{
|
||||
FilePath other = obj as FilePath;
|
||||
if (other == null)
|
||||
return false;
|
||||
return GetCanonicalPath () == other.GetCanonicalPath ();
|
||||
}
|
||||
|
||||
public override int GetHashCode ()
|
||||
{
|
||||
return _path.GetHashCode ();
|
||||
}
|
||||
|
||||
public bool CreateNewFile ()
|
||||
{
|
||||
try {
|
||||
//Stream.`Close` method deleted
|
||||
//File.Open (_path, FileMode.CreateNew).Close ();
|
||||
File.Open(_path, FileMode.CreateNew).Dispose();
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static FilePath CreateTempFile ()
|
||||
{
|
||||
return new FilePath (Path.GetTempFileName ());
|
||||
}
|
||||
|
||||
public static FilePath CreateTempFile (string prefix, string suffix)
|
||||
{
|
||||
return CreateTempFile (prefix, suffix, null);
|
||||
}
|
||||
|
||||
public static FilePath CreateTempFile (string prefix, string suffix, FilePath directory)
|
||||
{
|
||||
string file;
|
||||
if (prefix == null) {
|
||||
throw new ArgumentNullException ("prefix");
|
||||
}
|
||||
if (prefix.Length < 3) {
|
||||
throw new ArgumentException ("prefix must have at least 3 characters");
|
||||
}
|
||||
string str = (directory == null) ? Path.GetTempPath () : directory.GetPath ();
|
||||
do {
|
||||
file = Path.Combine (str, prefix + Interlocked.Increment (ref _tempCounter) + suffix);
|
||||
} while (File.Exists (file));
|
||||
|
||||
new FileOutputStream (file).Close ();
|
||||
return new FilePath (file);
|
||||
}
|
||||
|
||||
|
||||
public void DeleteOnExit ()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public FilePath GetAbsoluteFile ()
|
||||
{
|
||||
return new FilePath (Path.GetFullPath (_path));
|
||||
}
|
||||
|
||||
public string GetAbsolutePath ()
|
||||
{
|
||||
return Path.GetFullPath (_path);
|
||||
}
|
||||
|
||||
public FilePath GetCanonicalFile ()
|
||||
{
|
||||
return new FilePath (GetCanonicalPath ());
|
||||
}
|
||||
|
||||
public string GetCanonicalPath ()
|
||||
{
|
||||
string p = Path.GetFullPath (_path);
|
||||
p.TrimEnd (Path.DirectorySeparatorChar);
|
||||
return p;
|
||||
}
|
||||
|
||||
public string GetName ()
|
||||
{
|
||||
return Path.GetFileName (_path);
|
||||
}
|
||||
|
||||
public FilePath GetParentFile ()
|
||||
{
|
||||
return new FilePath (Path.GetDirectoryName (_path));
|
||||
}
|
||||
|
||||
public string GetPath ()
|
||||
{
|
||||
return _path;
|
||||
}
|
||||
|
||||
public bool IsAbsolute ()
|
||||
{
|
||||
return Path.IsPathRooted (_path);
|
||||
}
|
||||
|
||||
public bool IsDirectory ()
|
||||
{
|
||||
return false; // FileHelper.Instance.IsDirectory(this);
|
||||
}
|
||||
|
||||
public bool IsFile ()
|
||||
{
|
||||
return false; //FileHelper.Instance.IsFile (this);
|
||||
}
|
||||
|
||||
public long LastModified ()
|
||||
{
|
||||
return 0; // FileHelper.Instance.LastModified(this);
|
||||
}
|
||||
|
||||
public long Length ()
|
||||
{
|
||||
return 0; // FileHelper.Instance.Length(this);
|
||||
}
|
||||
|
||||
public string[] List ()
|
||||
{
|
||||
return List (null);
|
||||
}
|
||||
|
||||
public string[] List (IFilenameFilter filter)
|
||||
{
|
||||
try {
|
||||
if (IsFile ())
|
||||
return null;
|
||||
List<string> list = new List<string> ();
|
||||
foreach (string filePth in Directory.GetFileSystemEntries (_path)) {
|
||||
string fileName = Path.GetFileName (filePth);
|
||||
if ((filter == null) || filter.Accept (this, fileName)) {
|
||||
list.Add (fileName);
|
||||
}
|
||||
}
|
||||
return list.ToArray ();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public FilePath[] ListFiles ()
|
||||
{
|
||||
try {
|
||||
if (IsFile ())
|
||||
return null;
|
||||
List<FilePath> list = new List<FilePath> ();
|
||||
foreach (string filePath in Directory.GetFileSystemEntries (_path)) {
|
||||
list.Add (new FilePath (filePath));
|
||||
}
|
||||
return list.ToArray ();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static void MakeDirWritable (string dir)
|
||||
{
|
||||
//FileHelper.Instance.MakeDirWritable (dir);
|
||||
}
|
||||
|
||||
static void MakeFileWritable (string file)
|
||||
{
|
||||
//FileHelper.Instance.MakeFileWritable (file);
|
||||
}
|
||||
|
||||
public bool Mkdir ()
|
||||
{
|
||||
try {
|
||||
if (Directory.Exists (_path))
|
||||
return false;
|
||||
Directory.CreateDirectory (_path);
|
||||
return true;
|
||||
} catch (Exception) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Mkdirs ()
|
||||
{
|
||||
try {
|
||||
if (Directory.Exists (_path))
|
||||
return false;
|
||||
Directory.CreateDirectory (_path);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool RenameTo (FilePath file)
|
||||
{
|
||||
return RenameTo (file._path);
|
||||
}
|
||||
|
||||
public bool RenameTo (string name)
|
||||
{
|
||||
return false; // FileHelper.Instance.RenameTo(this, name);
|
||||
}
|
||||
|
||||
public bool SetLastModified (long milis)
|
||||
{
|
||||
return false; // FileHelper.Instance.SetLastModified(this, milis);
|
||||
}
|
||||
|
||||
public bool SetReadOnly ()
|
||||
{
|
||||
return false; // FileHelper.Instance.SetReadOnly(this);
|
||||
}
|
||||
|
||||
public Uri ToUri ()
|
||||
{
|
||||
return new Uri (_path);
|
||||
}
|
||||
|
||||
// Don't change the case of this method, since ngit does reflection on it
|
||||
public bool CanExecute ()
|
||||
{
|
||||
return false; // FileHelper.Instance.CanExecute(this);
|
||||
}
|
||||
|
||||
// Don't change the case of this method, since ngit does reflection on it
|
||||
public bool SetExecutable (bool exec)
|
||||
{
|
||||
return false; // FileHelper.Instance.SetExecutable(this, exec);
|
||||
}
|
||||
|
||||
public string GetParent ()
|
||||
{
|
||||
string p = Path.GetDirectoryName (_path);
|
||||
if (string.IsNullOrEmpty(p) || p == _path)
|
||||
return null;
|
||||
return p;
|
||||
}
|
||||
|
||||
public override string ToString ()
|
||||
{
|
||||
return _path;
|
||||
}
|
||||
|
||||
static internal string PathSeparator {
|
||||
get { return Path.PathSeparator.ToString (); }
|
||||
}
|
||||
|
||||
static internal char PathSeparatorChar {
|
||||
get { return Path.PathSeparator; }
|
||||
}
|
||||
|
||||
static internal char SeparatorChar {
|
||||
get { return Path.DirectorySeparatorChar; }
|
||||
}
|
||||
|
||||
static internal string Separator {
|
||||
get { return Path.DirectorySeparatorChar.ToString (); }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class FileReader : InputStreamReader
|
||||
{
|
||||
//public FileReader (FilePath f) : base(f.GetPath ())
|
||||
//{
|
||||
//}
|
||||
//path -> fileStream
|
||||
public FileReader(InputStream s) : base(s)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
//resharper said, nobody using this. i believe it.
|
||||
//internal class FileWriter : StreamWriter
|
||||
//{
|
||||
// public FileWriter (FilePath path) : base(path.GetPath ())
|
||||
// {
|
||||
// }
|
||||
|
||||
// public FileWriter Append (string sequence)
|
||||
// {
|
||||
// Write (sequence);
|
||||
// return this;
|
||||
// }
|
||||
//}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class FilterInputStream : InputStream
|
||||
{
|
||||
protected InputStream In;
|
||||
|
||||
public FilterInputStream (InputStream s)
|
||||
{
|
||||
In = s;
|
||||
}
|
||||
|
||||
public override int Available ()
|
||||
{
|
||||
return In.Available ();
|
||||
}
|
||||
|
||||
public override void Close ()
|
||||
{
|
||||
In.Close ();
|
||||
}
|
||||
|
||||
public override void Mark (int readlimit)
|
||||
{
|
||||
In.Mark (readlimit);
|
||||
}
|
||||
|
||||
public override bool MarkSupported ()
|
||||
{
|
||||
return In.MarkSupported ();
|
||||
}
|
||||
|
||||
public override int Read ()
|
||||
{
|
||||
return In.Read ();
|
||||
}
|
||||
|
||||
public override int Read (byte[] buf)
|
||||
{
|
||||
return In.Read (buf);
|
||||
}
|
||||
|
||||
public override int Read (byte[] b, int off, int len)
|
||||
{
|
||||
return In.Read (b, off, len);
|
||||
}
|
||||
|
||||
public override void Reset ()
|
||||
{
|
||||
In.Reset ();
|
||||
}
|
||||
|
||||
public override long Skip (long cnt)
|
||||
{
|
||||
return In.Skip (cnt);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class FilterOutputStream : OutputStream
|
||||
{
|
||||
protected OutputStream Out;
|
||||
|
||||
public FilterOutputStream (OutputStream os)
|
||||
{
|
||||
Out = os;
|
||||
}
|
||||
|
||||
public override void Close ()
|
||||
{
|
||||
Out.Close ();
|
||||
}
|
||||
|
||||
public override void Flush ()
|
||||
{
|
||||
Out.Flush ();
|
||||
}
|
||||
|
||||
public override void Write (byte[] b)
|
||||
{
|
||||
Out.Write (b);
|
||||
}
|
||||
|
||||
public override void Write (int b)
|
||||
{
|
||||
Out.Write (b);
|
||||
}
|
||||
|
||||
public override void Write (byte[] b, int offset, int len)
|
||||
{
|
||||
Out.Write (b, offset, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class Hashtable : Dictionary<object, object>
|
||||
{
|
||||
public void Put(object key, object value)
|
||||
{
|
||||
Add(key, value);
|
||||
}
|
||||
|
||||
public object Get(object key)
|
||||
{
|
||||
var m_key = Keys.SingleOrDefault(k => k.Equals(key));
|
||||
|
||||
return m_key != null ? this[m_key] : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
//
|
||||
// HttpURLConnection.cs
|
||||
//
|
||||
// Author:
|
||||
// Lluis Sanchez Gual <lluis@novell.com>
|
||||
//
|
||||
// Copyright (c) 2010 Novell, Inc (http://www.novell.com)
|
||||
//
|
||||
// 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;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class UrlConnection
|
||||
{
|
||||
protected Uri Url;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal interface ICallable<T>
|
||||
{
|
||||
T Call ();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal interface IConcurrentMap<T, TU> : IDictionary<T, TU>
|
||||
{
|
||||
TU PutIfAbsent (T key, TU value);
|
||||
bool Remove (object key, object value);
|
||||
bool Replace (T key, TU oldValue, TU newValue);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public interface IExecutor
|
||||
{
|
||||
void Execute (IRunnable runnable);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public interface IFilenameFilter
|
||||
{
|
||||
bool Accept (FilePath dir, string name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal interface IFuture<T>
|
||||
{
|
||||
bool Cancel (bool mayInterruptIfRunning);
|
||||
T Get ();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal interface IPrivilegedAction<T>
|
||||
{
|
||||
T Run ();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public interface IRunnable
|
||||
{
|
||||
void Run ();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class InputStream : IDisposable
|
||||
{
|
||||
private long _mark;
|
||||
protected Stream Wrapped;
|
||||
protected Stream BaseStream;
|
||||
|
||||
public static implicit operator InputStream (Stream s)
|
||||
{
|
||||
return Wrap (s);
|
||||
}
|
||||
|
||||
public static implicit operator Stream (InputStream s)
|
||||
{
|
||||
return s.GetWrappedStream ();
|
||||
}
|
||||
|
||||
public virtual int Available ()
|
||||
{
|
||||
if (Wrapped is WrappedSystemStream)
|
||||
return ((WrappedSystemStream)Wrapped).InputStream.Available ();
|
||||
return 0;
|
||||
}
|
||||
|
||||
public virtual void Close ()
|
||||
{
|
||||
if (Wrapped != null) {
|
||||
//Stream.`Close` method deleted
|
||||
//Wrapped.Close ();
|
||||
Wrapped.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose ()
|
||||
{
|
||||
Close ();
|
||||
}
|
||||
|
||||
internal Stream GetWrappedStream ()
|
||||
{
|
||||
// Always create a wrapper stream (not directly Wrapped) since the subclass
|
||||
// may be overriding methods that need to be called when used through the Stream class
|
||||
return new WrappedSystemStream (this);
|
||||
}
|
||||
|
||||
public virtual void Mark (int readlimit)
|
||||
{
|
||||
if (Wrapped is WrappedSystemStream)
|
||||
((WrappedSystemStream)Wrapped).InputStream.Mark (readlimit);
|
||||
else {
|
||||
if (BaseStream is WrappedSystemStream)
|
||||
((WrappedSystemStream)BaseStream).OnMark (readlimit);
|
||||
if (Wrapped != null)
|
||||
_mark = Wrapped.Position;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual bool MarkSupported ()
|
||||
{
|
||||
if (Wrapped is WrappedSystemStream)
|
||||
return ((WrappedSystemStream)Wrapped).InputStream.MarkSupported ();
|
||||
return ((Wrapped != null) && Wrapped.CanSeek);
|
||||
}
|
||||
|
||||
public virtual int Read ()
|
||||
{
|
||||
if (Wrapped == null) {
|
||||
throw new NotImplementedException ();
|
||||
}
|
||||
return Wrapped.ReadByte ();
|
||||
}
|
||||
|
||||
public virtual int Read (byte[] buf)
|
||||
{
|
||||
return Read (buf, 0, buf.Length);
|
||||
}
|
||||
|
||||
public virtual int Read (byte[] b, int off, int len)
|
||||
{
|
||||
if (Wrapped is WrappedSystemStream)
|
||||
return ((WrappedSystemStream)Wrapped).InputStream.Read (b, off, len);
|
||||
|
||||
if (Wrapped != null) {
|
||||
int num = Wrapped.Read (b, off, len);
|
||||
return ((num <= 0) ? -1 : num);
|
||||
}
|
||||
int totalRead = 0;
|
||||
while (totalRead < len) {
|
||||
int nr = Read ();
|
||||
if (nr == -1)
|
||||
return -1;
|
||||
b[off + totalRead] = (byte)nr;
|
||||
totalRead++;
|
||||
}
|
||||
return totalRead;
|
||||
}
|
||||
|
||||
public virtual void Reset ()
|
||||
{
|
||||
if (Wrapped is WrappedSystemStream)
|
||||
((WrappedSystemStream)Wrapped).InputStream.Reset ();
|
||||
else {
|
||||
if (Wrapped == null)
|
||||
throw new IOException ();
|
||||
Wrapped.Position = _mark;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual long Skip (long cnt)
|
||||
{
|
||||
if (Wrapped is WrappedSystemStream)
|
||||
return ((WrappedSystemStream)Wrapped).InputStream.Skip (cnt);
|
||||
|
||||
long n = cnt;
|
||||
while (n > 0) {
|
||||
if (Read () == -1)
|
||||
return cnt - n;
|
||||
n--;
|
||||
}
|
||||
return cnt - n;
|
||||
}
|
||||
|
||||
internal virtual bool CanSeek ()
|
||||
{
|
||||
if (Wrapped != null)
|
||||
return Wrapped.CanSeek;
|
||||
return false;
|
||||
}
|
||||
|
||||
internal virtual long Position {
|
||||
get
|
||||
{
|
||||
if (Wrapped != null)
|
||||
return Wrapped.Position;
|
||||
throw new NotSupportedException ();
|
||||
}
|
||||
set {
|
||||
if (Wrapped != null)
|
||||
Wrapped.Position = value;
|
||||
else
|
||||
throw new NotSupportedException ();
|
||||
}
|
||||
}
|
||||
|
||||
public virtual long Length
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Wrapped != null)
|
||||
{
|
||||
return Wrapped.Length;
|
||||
}
|
||||
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
|
||||
static internal InputStream Wrap (Stream s)
|
||||
{
|
||||
InputStream stream = new InputStream ();
|
||||
stream.Wrapped = s;
|
||||
return stream;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class InputStreamReader : StreamReader
|
||||
{
|
||||
//Stream(string path) constructor deleted
|
||||
//protected InputStreamReader (string file) : base(file)
|
||||
//{
|
||||
//}
|
||||
|
||||
public InputStreamReader (InputStream s) : base(s.GetWrappedStream ())
|
||||
{
|
||||
}
|
||||
|
||||
public InputStreamReader (InputStream s, string encoding) : base(s.GetWrappedStream (), Encoding.GetEncoding (encoding))
|
||||
{
|
||||
}
|
||||
|
||||
public InputStreamReader (InputStream s, Encoding e) : base(s.GetWrappedStream (), e)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public interface ITerator
|
||||
{
|
||||
bool HasNext ();
|
||||
object Next ();
|
||||
void Remove ();
|
||||
}
|
||||
|
||||
public abstract class Iterator<T> : IEnumerator<T>, ITerator
|
||||
{
|
||||
private T _lastValue;
|
||||
|
||||
object ITerator.Next ()
|
||||
{
|
||||
return Next ();
|
||||
}
|
||||
|
||||
public abstract bool HasNext ();
|
||||
public abstract T Next ();
|
||||
public abstract void Remove ();
|
||||
|
||||
bool IEnumerator.MoveNext ()
|
||||
{
|
||||
if (HasNext ()) {
|
||||
_lastValue = Next ();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void IEnumerator.Reset ()
|
||||
{
|
||||
throw new NotImplementedException ();
|
||||
}
|
||||
|
||||
void IDisposable.Dispose ()
|
||||
{
|
||||
}
|
||||
|
||||
T IEnumerator<T>.Current {
|
||||
get { return _lastValue; }
|
||||
}
|
||||
|
||||
object IEnumerator.Current {
|
||||
get { return _lastValue; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class LinkageError : Exception
|
||||
{
|
||||
public LinkageError (string msg) : base(msg)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
275
Emby.Server.Implementations/IO/SharpCifs/Util/Sharpen/MD5.cs
Normal file
275
Emby.Server.Implementations/IO/SharpCifs/Util/Sharpen/MD5.cs
Normal file
@@ -0,0 +1,275 @@
|
||||
//Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{ // **************************************************************
|
||||
// * Raw implementation of the MD5 hash algorithm
|
||||
// * from RFC 1321.
|
||||
// *
|
||||
// * Written By: Reid Borsuk and Jenny Zheng
|
||||
// * Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// **************************************************************
|
||||
|
||||
// Simple struct for the (a,b,c,d) which is used to compute the mesage digest.
|
||||
struct AbcdStruct
|
||||
{
|
||||
public uint A;
|
||||
public uint B;
|
||||
public uint C;
|
||||
public uint D;
|
||||
}
|
||||
|
||||
public sealed class Md5Core
|
||||
{
|
||||
//Prevent CSC from adding a default public constructor
|
||||
private Md5Core() { }
|
||||
|
||||
public static byte[] GetHash(string input, Encoding encoding)
|
||||
{
|
||||
if (null == input)
|
||||
throw new ArgumentNullException("input", "Unable to calculate hash over null input data");
|
||||
if (null == encoding)
|
||||
throw new ArgumentNullException("encoding", "Unable to calculate hash over a string without a default encoding. Consider using the GetHash(string) overload to use UTF8 Encoding");
|
||||
|
||||
byte[] target = encoding.GetBytes(input);
|
||||
|
||||
return GetHash(target);
|
||||
}
|
||||
|
||||
public static byte[] GetHash(string input)
|
||||
{
|
||||
return GetHash(input, new UTF8Encoding());
|
||||
}
|
||||
|
||||
public static string GetHashString(byte[] input)
|
||||
{
|
||||
if (null == input)
|
||||
throw new ArgumentNullException("input", "Unable to calculate hash over null input data");
|
||||
|
||||
string retval = BitConverter.ToString(GetHash(input));
|
||||
retval = retval.Replace("-", "");
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
public static string GetHashString(string input, Encoding encoding)
|
||||
{
|
||||
if (null == input)
|
||||
throw new ArgumentNullException("input", "Unable to calculate hash over null input data");
|
||||
if (null == encoding)
|
||||
throw new ArgumentNullException("encoding", "Unable to calculate hash over a string without a default encoding. Consider using the GetHashString(string) overload to use UTF8 Encoding");
|
||||
|
||||
byte[] target = encoding.GetBytes(input);
|
||||
|
||||
return GetHashString(target);
|
||||
}
|
||||
|
||||
public static string GetHashString(string input)
|
||||
{
|
||||
return GetHashString(input, new UTF8Encoding());
|
||||
}
|
||||
|
||||
public static byte[] GetHash(byte[] input)
|
||||
{
|
||||
if (null == input)
|
||||
throw new ArgumentNullException("input", "Unable to calculate hash over null input data");
|
||||
|
||||
//Intitial values defined in RFC 1321
|
||||
AbcdStruct abcd = new AbcdStruct();
|
||||
abcd.A = 0x67452301;
|
||||
abcd.B = 0xefcdab89;
|
||||
abcd.C = 0x98badcfe;
|
||||
abcd.D = 0x10325476;
|
||||
|
||||
//We pass in the input array by block, the final block of data must be handled specialy for padding & length embeding
|
||||
int startIndex = 0;
|
||||
while (startIndex <= input.Length - 64)
|
||||
{
|
||||
GetHashBlock(input, ref abcd, startIndex);
|
||||
startIndex += 64;
|
||||
}
|
||||
// The final data block.
|
||||
return GetHashFinalBlock(input, startIndex, input.Length - startIndex, abcd, (Int64)input.Length * 8);
|
||||
}
|
||||
|
||||
internal static byte[] GetHashFinalBlock(byte[] input, int ibStart, int cbSize, AbcdStruct abcd, Int64 len)
|
||||
{
|
||||
byte[] working = new byte[64];
|
||||
byte[] length = BitConverter.GetBytes(len);
|
||||
|
||||
//Padding is a single bit 1, followed by the number of 0s required to make size congruent to 448 modulo 512. Step 1 of RFC 1321
|
||||
//The CLR ensures that our buffer is 0-assigned, we don't need to explicitly set it. This is why it ends up being quicker to just
|
||||
//use a temporary array rather then doing in-place assignment (5% for small inputs)
|
||||
Array.Copy(input, ibStart, working, 0, cbSize);
|
||||
working[cbSize] = 0x80;
|
||||
|
||||
//We have enough room to store the length in this chunk
|
||||
if (cbSize < 56)
|
||||
{
|
||||
Array.Copy(length, 0, working, 56, 8);
|
||||
GetHashBlock(working, ref abcd, 0);
|
||||
}
|
||||
else //We need an aditional chunk to store the length
|
||||
{
|
||||
GetHashBlock(working, ref abcd, 0);
|
||||
//Create an entirely new chunk due to the 0-assigned trick mentioned above, to avoid an extra function call clearing the array
|
||||
working = new byte[64];
|
||||
Array.Copy(length, 0, working, 56, 8);
|
||||
GetHashBlock(working, ref abcd, 0);
|
||||
}
|
||||
byte[] output = new byte[16];
|
||||
Array.Copy(BitConverter.GetBytes(abcd.A), 0, output, 0, 4);
|
||||
Array.Copy(BitConverter.GetBytes(abcd.B), 0, output, 4, 4);
|
||||
Array.Copy(BitConverter.GetBytes(abcd.C), 0, output, 8, 4);
|
||||
Array.Copy(BitConverter.GetBytes(abcd.D), 0, output, 12, 4);
|
||||
return output;
|
||||
}
|
||||
|
||||
// Performs a single block transform of MD5 for a given set of ABCD inputs
|
||||
/* If implementing your own hashing framework, be sure to set the initial ABCD correctly according to RFC 1321:
|
||||
// A = 0x67452301;
|
||||
// B = 0xefcdab89;
|
||||
// C = 0x98badcfe;
|
||||
// D = 0x10325476;
|
||||
*/
|
||||
internal static void GetHashBlock(byte[] input, ref AbcdStruct abcdValue, int ibStart)
|
||||
{
|
||||
uint[] temp = Converter(input, ibStart);
|
||||
uint a = abcdValue.A;
|
||||
uint b = abcdValue.B;
|
||||
uint c = abcdValue.C;
|
||||
uint d = abcdValue.D;
|
||||
|
||||
a = R1(a, b, c, d, temp[0], 7, 0xd76aa478);
|
||||
d = R1(d, a, b, c, temp[1], 12, 0xe8c7b756);
|
||||
c = R1(c, d, a, b, temp[2], 17, 0x242070db);
|
||||
b = R1(b, c, d, a, temp[3], 22, 0xc1bdceee);
|
||||
a = R1(a, b, c, d, temp[4], 7, 0xf57c0faf);
|
||||
d = R1(d, a, b, c, temp[5], 12, 0x4787c62a);
|
||||
c = R1(c, d, a, b, temp[6], 17, 0xa8304613);
|
||||
b = R1(b, c, d, a, temp[7], 22, 0xfd469501);
|
||||
a = R1(a, b, c, d, temp[8], 7, 0x698098d8);
|
||||
d = R1(d, a, b, c, temp[9], 12, 0x8b44f7af);
|
||||
c = R1(c, d, a, b, temp[10], 17, 0xffff5bb1);
|
||||
b = R1(b, c, d, a, temp[11], 22, 0x895cd7be);
|
||||
a = R1(a, b, c, d, temp[12], 7, 0x6b901122);
|
||||
d = R1(d, a, b, c, temp[13], 12, 0xfd987193);
|
||||
c = R1(c, d, a, b, temp[14], 17, 0xa679438e);
|
||||
b = R1(b, c, d, a, temp[15], 22, 0x49b40821);
|
||||
|
||||
a = R2(a, b, c, d, temp[1], 5, 0xf61e2562);
|
||||
d = R2(d, a, b, c, temp[6], 9, 0xc040b340);
|
||||
c = R2(c, d, a, b, temp[11], 14, 0x265e5a51);
|
||||
b = R2(b, c, d, a, temp[0], 20, 0xe9b6c7aa);
|
||||
a = R2(a, b, c, d, temp[5], 5, 0xd62f105d);
|
||||
d = R2(d, a, b, c, temp[10], 9, 0x02441453);
|
||||
c = R2(c, d, a, b, temp[15], 14, 0xd8a1e681);
|
||||
b = R2(b, c, d, a, temp[4], 20, 0xe7d3fbc8);
|
||||
a = R2(a, b, c, d, temp[9], 5, 0x21e1cde6);
|
||||
d = R2(d, a, b, c, temp[14], 9, 0xc33707d6);
|
||||
c = R2(c, d, a, b, temp[3], 14, 0xf4d50d87);
|
||||
b = R2(b, c, d, a, temp[8], 20, 0x455a14ed);
|
||||
a = R2(a, b, c, d, temp[13], 5, 0xa9e3e905);
|
||||
d = R2(d, a, b, c, temp[2], 9, 0xfcefa3f8);
|
||||
c = R2(c, d, a, b, temp[7], 14, 0x676f02d9);
|
||||
b = R2(b, c, d, a, temp[12], 20, 0x8d2a4c8a);
|
||||
|
||||
a = R3(a, b, c, d, temp[5], 4, 0xfffa3942);
|
||||
d = R3(d, a, b, c, temp[8], 11, 0x8771f681);
|
||||
c = R3(c, d, a, b, temp[11], 16, 0x6d9d6122);
|
||||
b = R3(b, c, d, a, temp[14], 23, 0xfde5380c);
|
||||
a = R3(a, b, c, d, temp[1], 4, 0xa4beea44);
|
||||
d = R3(d, a, b, c, temp[4], 11, 0x4bdecfa9);
|
||||
c = R3(c, d, a, b, temp[7], 16, 0xf6bb4b60);
|
||||
b = R3(b, c, d, a, temp[10], 23, 0xbebfbc70);
|
||||
a = R3(a, b, c, d, temp[13], 4, 0x289b7ec6);
|
||||
d = R3(d, a, b, c, temp[0], 11, 0xeaa127fa);
|
||||
c = R3(c, d, a, b, temp[3], 16, 0xd4ef3085);
|
||||
b = R3(b, c, d, a, temp[6], 23, 0x04881d05);
|
||||
a = R3(a, b, c, d, temp[9], 4, 0xd9d4d039);
|
||||
d = R3(d, a, b, c, temp[12], 11, 0xe6db99e5);
|
||||
c = R3(c, d, a, b, temp[15], 16, 0x1fa27cf8);
|
||||
b = R3(b, c, d, a, temp[2], 23, 0xc4ac5665);
|
||||
|
||||
a = R4(a, b, c, d, temp[0], 6, 0xf4292244);
|
||||
d = R4(d, a, b, c, temp[7], 10, 0x432aff97);
|
||||
c = R4(c, d, a, b, temp[14], 15, 0xab9423a7);
|
||||
b = R4(b, c, d, a, temp[5], 21, 0xfc93a039);
|
||||
a = R4(a, b, c, d, temp[12], 6, 0x655b59c3);
|
||||
d = R4(d, a, b, c, temp[3], 10, 0x8f0ccc92);
|
||||
c = R4(c, d, a, b, temp[10], 15, 0xffeff47d);
|
||||
b = R4(b, c, d, a, temp[1], 21, 0x85845dd1);
|
||||
a = R4(a, b, c, d, temp[8], 6, 0x6fa87e4f);
|
||||
d = R4(d, a, b, c, temp[15], 10, 0xfe2ce6e0);
|
||||
c = R4(c, d, a, b, temp[6], 15, 0xa3014314);
|
||||
b = R4(b, c, d, a, temp[13], 21, 0x4e0811a1);
|
||||
a = R4(a, b, c, d, temp[4], 6, 0xf7537e82);
|
||||
d = R4(d, a, b, c, temp[11], 10, 0xbd3af235);
|
||||
c = R4(c, d, a, b, temp[2], 15, 0x2ad7d2bb);
|
||||
b = R4(b, c, d, a, temp[9], 21, 0xeb86d391);
|
||||
|
||||
abcdValue.A = unchecked(a + abcdValue.A);
|
||||
abcdValue.B = unchecked(b + abcdValue.B);
|
||||
abcdValue.C = unchecked(c + abcdValue.C);
|
||||
abcdValue.D = unchecked(d + abcdValue.D);
|
||||
}
|
||||
|
||||
//Manually unrolling these equations nets us a 20% performance improvement
|
||||
private static uint R1(uint a, uint b, uint c, uint d, uint x, int s, uint t)
|
||||
{
|
||||
// (b + LSR((a + F(b, c, d) + x + t), s))
|
||||
//F(x, y, z) ((x & y) | ((x ^ 0xFFFFFFFF) & z))
|
||||
return unchecked(b + Lsr((a + ((b & c) | ((b ^ 0xFFFFFFFF) & d)) + x + t), s));
|
||||
}
|
||||
|
||||
private static uint R2(uint a, uint b, uint c, uint d, uint x, int s, uint t)
|
||||
{
|
||||
// (b + LSR((a + G(b, c, d) + x + t), s))
|
||||
//G(x, y, z) ((x & z) | (y & (z ^ 0xFFFFFFFF)))
|
||||
return unchecked(b + Lsr((a + ((b & d) | (c & (d ^ 0xFFFFFFFF))) + x + t), s));
|
||||
}
|
||||
|
||||
private static uint R3(uint a, uint b, uint c, uint d, uint x, int s, uint t)
|
||||
{
|
||||
// (b + LSR((a + H(b, c, d) + k + i), s))
|
||||
//H(x, y, z) (x ^ y ^ z)
|
||||
return unchecked(b + Lsr((a + (b ^ c ^ d) + x + t), s));
|
||||
}
|
||||
|
||||
private static uint R4(uint a, uint b, uint c, uint d, uint x, int s, uint t)
|
||||
{
|
||||
// (b + LSR((a + I(b, c, d) + k + i), s))
|
||||
//I(x, y, z) (y ^ (x | (z ^ 0xFFFFFFFF)))
|
||||
return unchecked(b + Lsr((a + (c ^ (b | (d ^ 0xFFFFFFFF))) + x + t), s));
|
||||
}
|
||||
|
||||
// Implementation of left rotate
|
||||
// s is an int instead of a uint becuase the CLR requires the argument passed to >>/<< is of
|
||||
// type int. Doing the demoting inside this function would add overhead.
|
||||
private static uint Lsr(uint i, int s)
|
||||
{
|
||||
return ((i << s) | (i >> (32 - s)));
|
||||
}
|
||||
|
||||
//Convert input array into array of UInts
|
||||
private static uint[] Converter(byte[] input, int ibStart)
|
||||
{
|
||||
if (null == input)
|
||||
throw new ArgumentNullException("input", "Unable convert null array to array of uInts");
|
||||
|
||||
uint[] result = new uint[16];
|
||||
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
result[i] = input[ibStart + i * 4];
|
||||
result[i] += (uint)input[ibStart + i * 4 + 1] << 8;
|
||||
result[i] += (uint)input[ibStart + i * 4 + 2] << 16;
|
||||
result[i] += (uint)input[ibStart + i * 4 + 3] << 24;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
//Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
|
||||
using System;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
// **************************************************************
|
||||
// * Raw implementation of the MD5 hash algorithm
|
||||
// * from RFC 1321.
|
||||
// *
|
||||
// * Written By: Reid Borsuk and Jenny Zheng
|
||||
// * Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// **************************************************************
|
||||
|
||||
|
||||
#if SILVERLIGHT
|
||||
#else
|
||||
//public class MD5Managed : MD5
|
||||
#endif
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class Md5Managed : HashAlgorithm
|
||||
|
||||
{
|
||||
public static Md5Managed Create()
|
||||
{
|
||||
return new Md5Managed();
|
||||
}
|
||||
|
||||
private byte[] _data;
|
||||
private AbcdStruct _abcd;
|
||||
private Int64 _totalLength;
|
||||
private int _dataSize;
|
||||
|
||||
public Md5Managed()
|
||||
{
|
||||
//field cannot access
|
||||
//HashSizeValue = 0x80;
|
||||
Initialize();
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
_data = new byte[64];
|
||||
_dataSize = 0;
|
||||
_totalLength = 0;
|
||||
_abcd = new AbcdStruct();
|
||||
//Intitial values as defined in RFC 1321
|
||||
_abcd.A = 0x67452301;
|
||||
_abcd.B = 0xefcdab89;
|
||||
_abcd.C = 0x98badcfe;
|
||||
_abcd.D = 0x10325476;
|
||||
}
|
||||
|
||||
protected override void HashCore(byte[] array, int ibStart, int cbSize)
|
||||
{
|
||||
int startIndex = ibStart;
|
||||
int totalArrayLength = _dataSize + cbSize;
|
||||
if (totalArrayLength >= 64)
|
||||
{
|
||||
Array.Copy(array, startIndex, _data, _dataSize, 64 - _dataSize);
|
||||
// Process message of 64 bytes (512 bits)
|
||||
Md5Core.GetHashBlock(_data, ref _abcd, 0);
|
||||
startIndex += 64 - _dataSize;
|
||||
totalArrayLength -= 64;
|
||||
while (totalArrayLength >= 64)
|
||||
{
|
||||
Array.Copy(array, startIndex, _data, 0, 64);
|
||||
Md5Core.GetHashBlock(array, ref _abcd, startIndex);
|
||||
totalArrayLength -= 64;
|
||||
startIndex += 64;
|
||||
}
|
||||
_dataSize = totalArrayLength;
|
||||
Array.Copy(array, startIndex, _data, 0, totalArrayLength);
|
||||
}
|
||||
else
|
||||
{
|
||||
Array.Copy(array, startIndex, _data, _dataSize, cbSize);
|
||||
_dataSize = totalArrayLength;
|
||||
}
|
||||
_totalLength += cbSize;
|
||||
}
|
||||
|
||||
protected override byte[] HashFinal()
|
||||
{
|
||||
//field cannot access
|
||||
//HashValue = Md5Core.GetHashFinalBlock(_data, 0, _dataSize, _abcd, _totalLength * 8);
|
||||
//return HashValue;
|
||||
return Md5Core.GetHashFinalBlock(_data, 0, _dataSize, _abcd, _totalLength * 8);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class Matcher
|
||||
{
|
||||
private int _current;
|
||||
private MatchCollection _matches;
|
||||
private Regex _regex;
|
||||
private string _str;
|
||||
|
||||
internal Matcher (Regex regex, string str)
|
||||
{
|
||||
this._regex = regex;
|
||||
this._str = str;
|
||||
}
|
||||
|
||||
public int End ()
|
||||
{
|
||||
if ((_matches == null) || (_current >= _matches.Count)) {
|
||||
throw new InvalidOperationException ();
|
||||
}
|
||||
return (_matches[_current].Index + _matches[_current].Length);
|
||||
}
|
||||
|
||||
public bool Find ()
|
||||
{
|
||||
if (_matches == null) {
|
||||
_matches = _regex.Matches (_str);
|
||||
_current = 0;
|
||||
}
|
||||
return (_current < _matches.Count);
|
||||
}
|
||||
|
||||
public bool Find (int index)
|
||||
{
|
||||
_matches = _regex.Matches (_str, index);
|
||||
_current = 0;
|
||||
return (_matches.Count > 0);
|
||||
}
|
||||
|
||||
public string Group (int n)
|
||||
{
|
||||
if ((_matches == null) || (_current >= _matches.Count)) {
|
||||
throw new InvalidOperationException ();
|
||||
}
|
||||
Group grp = _matches[_current].Groups[n];
|
||||
return grp.Success ? grp.Value : null;
|
||||
}
|
||||
|
||||
public bool Matches ()
|
||||
{
|
||||
_matches = null;
|
||||
return Find ();
|
||||
}
|
||||
|
||||
public string ReplaceFirst (string txt)
|
||||
{
|
||||
return _regex.Replace (_str, txt, 1);
|
||||
}
|
||||
|
||||
public Matcher Reset (CharSequence str)
|
||||
{
|
||||
return Reset (str.ToString ());
|
||||
}
|
||||
|
||||
public Matcher Reset (string str)
|
||||
{
|
||||
_matches = null;
|
||||
this._str = str;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int Start ()
|
||||
{
|
||||
if ((_matches == null) || (_current >= _matches.Count)) {
|
||||
throw new InvalidOperationException ();
|
||||
}
|
||||
return _matches[_current].Index;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public abstract class MessageDigest
|
||||
{
|
||||
public void Digest (byte[] buffer, int o, int len)
|
||||
{
|
||||
byte[] d = Digest ();
|
||||
d.CopyTo (buffer, o);
|
||||
}
|
||||
|
||||
public byte[] Digest (byte[] buffer)
|
||||
{
|
||||
Update (buffer);
|
||||
return Digest ();
|
||||
}
|
||||
|
||||
public abstract byte[] Digest ();
|
||||
public abstract int GetDigestLength ();
|
||||
public static MessageDigest GetInstance (string algorithm)
|
||||
{
|
||||
switch (algorithm.ToLower ()) {
|
||||
case "sha-1":
|
||||
//System.Security.CryptographySHA1Managed not found
|
||||
//return new MessageDigest<SHA1Managed> ();
|
||||
return new MessageDigest<System.Security.Cryptography.SHA1>();
|
||||
case "md5":
|
||||
return new MessageDigest<Md5Managed> ();
|
||||
}
|
||||
throw new NotSupportedException (string.Format ("The requested algorithm \"{0}\" is not supported.", algorithm));
|
||||
}
|
||||
|
||||
public abstract void Reset ();
|
||||
public abstract void Update (byte[] b);
|
||||
public abstract void Update (byte b);
|
||||
public abstract void Update (byte[] b, int offset, int len);
|
||||
}
|
||||
|
||||
|
||||
public class MessageDigest<TAlgorithm> : MessageDigest where TAlgorithm : HashAlgorithm //, new() //use static `Create` method
|
||||
{
|
||||
private TAlgorithm _hash;
|
||||
//private CryptoStream _stream; //don't work .NET Core
|
||||
private MemoryStream _stream;
|
||||
|
||||
|
||||
public MessageDigest ()
|
||||
{
|
||||
Init ();
|
||||
}
|
||||
|
||||
public override byte[] Digest ()
|
||||
{
|
||||
//CryptoStream -> MemoryStream, needless method
|
||||
//_stream.FlushFinalBlock ();
|
||||
|
||||
//HashAlgorithm.`Hash` property deleted
|
||||
//byte[] hash = _hash.Hash;
|
||||
byte[] hash = _hash.ComputeHash(_stream.ToArray());
|
||||
|
||||
Reset ();
|
||||
return hash;
|
||||
}
|
||||
|
||||
public void Dispose ()
|
||||
{
|
||||
if (_stream != null) {
|
||||
_stream.Dispose ();
|
||||
}
|
||||
_stream = null;
|
||||
}
|
||||
|
||||
public override int GetDigestLength ()
|
||||
{
|
||||
return (_hash.HashSize / 8);
|
||||
}
|
||||
|
||||
private void Init ()
|
||||
{
|
||||
//use static `Create` method
|
||||
//_hash = Activator.CreateInstance<TAlgorithm> ();
|
||||
var createMethod = typeof(TAlgorithm).GetRuntimeMethod("Create", new Type[0]);
|
||||
_hash = (TAlgorithm)createMethod.Invoke(null, new object[] {});
|
||||
|
||||
//HashAlgorithm cannot cast `ICryptoTransform` on .NET Core, gave up using CryptoStream.
|
||||
//_stream = new CryptoStream(Stream.Null, _hash, CryptoStreamMode.Write);
|
||||
//_stream = new CryptoStream(_tmpStream, (ICryptoTransform)_hash, CryptoStreamMode.Write);
|
||||
_stream = new MemoryStream();
|
||||
}
|
||||
|
||||
public override void Reset ()
|
||||
{
|
||||
Dispose ();
|
||||
Init ();
|
||||
}
|
||||
|
||||
public override void Update (byte[] input)
|
||||
{
|
||||
_stream.Write (input, 0, input.Length);
|
||||
}
|
||||
|
||||
public override void Update (byte input)
|
||||
{
|
||||
_stream.WriteByte (input);
|
||||
}
|
||||
|
||||
public override void Update (byte[] input, int index, int count)
|
||||
{
|
||||
if (count < 0)
|
||||
Console.WriteLine ("Argh!");
|
||||
_stream.Write (input, index, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class NetworkStream : Stream
|
||||
{
|
||||
SocketEx _socket;
|
||||
|
||||
public NetworkStream(SocketEx socket)
|
||||
{
|
||||
_socket = socket;
|
||||
}
|
||||
|
||||
public override bool CanRead
|
||||
{
|
||||
get { throw new NotImplementedException(); }
|
||||
}
|
||||
|
||||
public override bool CanSeek
|
||||
{
|
||||
get { throw new NotImplementedException(); }
|
||||
}
|
||||
|
||||
public override bool CanWrite
|
||||
{
|
||||
get { throw new NotImplementedException(); }
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
// throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override long Length
|
||||
{
|
||||
get { throw new NotImplementedException(); }
|
||||
}
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
set
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
return _socket.Receive(buffer, offset, count);
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
_socket.Send(buffer, offset, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class ObjectInputStream : InputStream
|
||||
{
|
||||
private BinaryReader _reader;
|
||||
|
||||
public ObjectInputStream (InputStream s)
|
||||
{
|
||||
_reader = new BinaryReader (s.GetWrappedStream ());
|
||||
}
|
||||
|
||||
public int ReadInt ()
|
||||
{
|
||||
return _reader.ReadInt32 ();
|
||||
}
|
||||
|
||||
public object ReadObject ()
|
||||
{
|
||||
throw new NotImplementedException ();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class ObjectOutputStream : OutputStream
|
||||
{
|
||||
private BinaryWriter _bw;
|
||||
|
||||
public ObjectOutputStream (OutputStream os)
|
||||
{
|
||||
_bw = new BinaryWriter (os.GetWrappedStream ());
|
||||
}
|
||||
|
||||
public virtual void WriteInt (int i)
|
||||
{
|
||||
_bw.Write (i);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class OutputStream : IDisposable
|
||||
{
|
||||
protected Stream Wrapped;
|
||||
|
||||
public static implicit operator OutputStream (Stream s)
|
||||
{
|
||||
return Wrap (s);
|
||||
}
|
||||
|
||||
public static implicit operator Stream (OutputStream s)
|
||||
{
|
||||
return s.GetWrappedStream ();
|
||||
}
|
||||
|
||||
public virtual void Close ()
|
||||
{
|
||||
if (Wrapped != null) {
|
||||
//Stream.`Close` method deleted
|
||||
//Wrapped.Close ();
|
||||
Wrapped.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose ()
|
||||
{
|
||||
Close ();
|
||||
}
|
||||
|
||||
public virtual void Flush ()
|
||||
{
|
||||
if (Wrapped != null) {
|
||||
Wrapped.Flush ();
|
||||
}
|
||||
}
|
||||
|
||||
internal Stream GetWrappedStream ()
|
||||
{
|
||||
// Always create a wrapper stream (not directly Wrapped) since the subclass
|
||||
// may be overriding methods that need to be called when used through the Stream class
|
||||
return new WrappedSystemStream (this);
|
||||
}
|
||||
|
||||
static internal OutputStream Wrap (Stream s)
|
||||
{
|
||||
OutputStream stream = new OutputStream ();
|
||||
stream.Wrapped = s;
|
||||
return stream;
|
||||
}
|
||||
|
||||
public virtual void Write (int b)
|
||||
{
|
||||
if (Wrapped is WrappedSystemStream)
|
||||
((WrappedSystemStream)Wrapped).OutputStream.Write (b);
|
||||
else {
|
||||
if (Wrapped == null)
|
||||
throw new NotImplementedException ();
|
||||
Wrapped.WriteByte ((byte)b);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Write (byte[] b)
|
||||
{
|
||||
Write (b, 0, b.Length);
|
||||
}
|
||||
|
||||
public virtual void Write (byte[] b, int offset, int len)
|
||||
{
|
||||
if (Wrapped is WrappedSystemStream)
|
||||
((WrappedSystemStream)Wrapped).OutputStream.Write (b, offset, len);
|
||||
else {
|
||||
if (Wrapped != null) {
|
||||
Wrapped.Write (b, offset, len);
|
||||
} else {
|
||||
for (int i = 0; i < len; i++) {
|
||||
Write (b[i + offset]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class OutputStreamWriter : StreamWriter
|
||||
{
|
||||
public OutputStreamWriter (OutputStream stream) : base(stream.GetWrappedStream ())
|
||||
{
|
||||
}
|
||||
|
||||
public OutputStreamWriter (OutputStream stream, string encoding) : base(stream.GetWrappedStream (), Extensions.GetEncoding (encoding))
|
||||
{
|
||||
}
|
||||
|
||||
public OutputStreamWriter (OutputStream stream, Encoding encoding) : base(stream.GetWrappedStream (), encoding)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class PipedInputStream : InputStream
|
||||
{
|
||||
private byte[] _oneBuffer;
|
||||
public const int PipeSize = 1024;
|
||||
|
||||
protected byte[] Buffer;
|
||||
private bool _closed;
|
||||
private ManualResetEvent _dataEvent;
|
||||
private int _end;
|
||||
private int _start;
|
||||
private object _thisLock;
|
||||
private bool _allowGrow = false;
|
||||
|
||||
public int In {
|
||||
get { return _start; }
|
||||
set { _start = value; }
|
||||
}
|
||||
|
||||
public int Out {
|
||||
get { return _end; }
|
||||
set { _end = value; }
|
||||
}
|
||||
|
||||
public PipedInputStream ()
|
||||
{
|
||||
_thisLock = new object ();
|
||||
_dataEvent = new ManualResetEvent (false);
|
||||
Buffer = new byte[PipeSize + 1];
|
||||
}
|
||||
|
||||
public PipedInputStream (PipedOutputStream os): this ()
|
||||
{
|
||||
os.Attach (this);
|
||||
}
|
||||
|
||||
public override void Close ()
|
||||
{
|
||||
lock (_thisLock) {
|
||||
_closed = true;
|
||||
_dataEvent.Set ();
|
||||
}
|
||||
}
|
||||
|
||||
public override int Available ()
|
||||
{
|
||||
lock (_thisLock) {
|
||||
if (_start <= _end) {
|
||||
return (_end - _start);
|
||||
}
|
||||
return ((Buffer.Length - _start) + _end);
|
||||
}
|
||||
}
|
||||
|
||||
public override int Read ()
|
||||
{
|
||||
if (_oneBuffer == null)
|
||||
_oneBuffer = new byte[1];
|
||||
if (Read (_oneBuffer, 0, 1) == -1)
|
||||
return -1;
|
||||
return _oneBuffer[0];
|
||||
}
|
||||
|
||||
public override int Read (byte[] b, int offset, int len)
|
||||
{
|
||||
int length = 0;
|
||||
do {
|
||||
_dataEvent.WaitOne ();
|
||||
lock (_thisLock) {
|
||||
if (_closed && Available () == 0) {
|
||||
return -1;
|
||||
}
|
||||
if (_start < _end) {
|
||||
length = Math.Min (len, _end - _start);
|
||||
Array.Copy (Buffer, _start, b, offset, length);
|
||||
_start += length;
|
||||
} else if (_start > _end) {
|
||||
length = Math.Min (len, Buffer.Length - _start);
|
||||
Array.Copy (Buffer, _start, b, offset, length);
|
||||
len -= length;
|
||||
_start = (_start + length) % Buffer.Length;
|
||||
if (len > 0) {
|
||||
int i = Math.Min (len, _end);
|
||||
Array.Copy (Buffer, 0, b, offset + length, i);
|
||||
_start += i;
|
||||
length += i;
|
||||
}
|
||||
}
|
||||
if (_start == _end && !_closed) {
|
||||
_dataEvent.Reset ();
|
||||
}
|
||||
Monitor.PulseAll (_thisLock);
|
||||
}
|
||||
} while (length == 0);
|
||||
return length;
|
||||
}
|
||||
|
||||
private int Allocate (int len)
|
||||
{
|
||||
int alen;
|
||||
while ((alen = TryAllocate (len)) == 0) {
|
||||
// Wait until somebody reads data
|
||||
try {
|
||||
Monitor.Wait (_thisLock);
|
||||
} catch {
|
||||
_closed = true;
|
||||
_dataEvent.Set ();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
return alen;
|
||||
}
|
||||
|
||||
int TryAllocate (int len)
|
||||
{
|
||||
int free;
|
||||
if (_start <= _end) {
|
||||
free = (Buffer.Length - _end) + _start;
|
||||
} else {
|
||||
free = _start - _end;
|
||||
}
|
||||
if (free <= len) {
|
||||
if (!_allowGrow)
|
||||
return free > 0 ? free - 1 : 0;
|
||||
int sizeInc = (len - free) + 1;
|
||||
byte[] destinationArray = new byte[Buffer.Length + sizeInc];
|
||||
if (_start <= _end) {
|
||||
Array.Copy (Buffer, _start, destinationArray, _start, _end - _start);
|
||||
} else {
|
||||
Array.Copy (Buffer, 0, destinationArray, 0, _end);
|
||||
Array.Copy (Buffer, _start, destinationArray, _start + sizeInc, Buffer.Length - _start);
|
||||
_start += sizeInc;
|
||||
}
|
||||
Buffer = destinationArray;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
internal void Write (int b)
|
||||
{
|
||||
lock (_thisLock) {
|
||||
Allocate (1);
|
||||
Buffer[_end] = (byte)b;
|
||||
_end = (_end + 1) % Buffer.Length;
|
||||
_dataEvent.Set ();
|
||||
}
|
||||
}
|
||||
|
||||
internal void Write (byte[] b, int offset, int len)
|
||||
{
|
||||
do {
|
||||
lock (_thisLock) {
|
||||
int alen = Allocate (len);
|
||||
int length = Math.Min (Buffer.Length - _end, alen);
|
||||
Array.Copy (b, offset, Buffer, _end, length);
|
||||
_end = (_end + length) % Buffer.Length;
|
||||
if (length < alen) {
|
||||
Array.Copy (b, offset + length, Buffer, 0, alen - length);
|
||||
_end += alen - length;
|
||||
}
|
||||
_dataEvent.Set ();
|
||||
len -= alen;
|
||||
offset += alen;
|
||||
}
|
||||
} while (len > 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class PipedOutputStream : OutputStream
|
||||
{
|
||||
PipedInputStream _ips;
|
||||
|
||||
public PipedOutputStream ()
|
||||
{
|
||||
}
|
||||
|
||||
public PipedOutputStream (PipedInputStream iss) : this()
|
||||
{
|
||||
Attach (iss);
|
||||
}
|
||||
|
||||
public override void Close ()
|
||||
{
|
||||
_ips.Close ();
|
||||
base.Close ();
|
||||
}
|
||||
|
||||
internal void Attach (PipedInputStream iss)
|
||||
{
|
||||
_ips = iss;
|
||||
}
|
||||
|
||||
public override void Write (int b)
|
||||
{
|
||||
_ips.Write (b);
|
||||
}
|
||||
|
||||
public override void Write (byte[] b, int offset, int len)
|
||||
{
|
||||
_ips.Write (b, offset, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class PrintWriter : TextWriter
|
||||
{
|
||||
TextWriter _writer;
|
||||
private FileStream _stream;
|
||||
|
||||
public PrintWriter (FilePath path)
|
||||
{
|
||||
//Stream(string path) constructor deleted
|
||||
_stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
|
||||
_writer = new StreamWriter (_stream);
|
||||
}
|
||||
|
||||
public PrintWriter (TextWriter other)
|
||||
{
|
||||
_writer = other;
|
||||
}
|
||||
|
||||
public override Encoding Encoding {
|
||||
get { return _writer.Encoding; }
|
||||
}
|
||||
|
||||
public void Close() // remove `override`
|
||||
{
|
||||
//Stream.`Close` method deleted
|
||||
//_writer.Close ();
|
||||
_writer.Dispose();
|
||||
_stream.Dispose();
|
||||
}
|
||||
|
||||
public override void Flush ()
|
||||
{
|
||||
_writer.Flush ();
|
||||
}
|
||||
|
||||
public override IFormatProvider FormatProvider {
|
||||
get {
|
||||
return _writer.FormatProvider;
|
||||
}
|
||||
}
|
||||
|
||||
public override string NewLine {
|
||||
get {
|
||||
return _writer.NewLine;
|
||||
}
|
||||
set {
|
||||
_writer.NewLine = value;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write (char[] buffer, int index, int count)
|
||||
{
|
||||
_writer.Write (buffer, index, count);
|
||||
}
|
||||
|
||||
public override void Write (char[] buffer)
|
||||
{
|
||||
_writer.Write (buffer);
|
||||
}
|
||||
|
||||
public void Write (string format, object arg0, object arg1, object arg2)
|
||||
{
|
||||
_writer.Write (format, arg0, arg1, arg2);
|
||||
}
|
||||
|
||||
public override void Write (string format, object arg0, object arg1)
|
||||
{
|
||||
_writer.Write (format, arg0, arg1);
|
||||
}
|
||||
|
||||
public override void Write (string format, object arg0)
|
||||
{
|
||||
_writer.Write (format, arg0);
|
||||
}
|
||||
|
||||
public override void Write (string format, params object[] arg)
|
||||
{
|
||||
_writer.Write (format, arg);
|
||||
}
|
||||
|
||||
public override void WriteLine (char[] buffer, int index, int count)
|
||||
{
|
||||
_writer.WriteLine (buffer, index, count);
|
||||
}
|
||||
|
||||
public override void WriteLine (char[] buffer)
|
||||
{
|
||||
_writer.WriteLine (buffer);
|
||||
}
|
||||
|
||||
public void WriteLine (string format, object arg0, object arg1, object arg2)
|
||||
{
|
||||
_writer.WriteLine (format, arg0, arg1, arg2);
|
||||
}
|
||||
|
||||
public override void WriteLine (string format, object arg0, object arg1)
|
||||
{
|
||||
_writer.WriteLine (format, arg0, arg1);
|
||||
}
|
||||
|
||||
public override void WriteLine (string format, object arg0)
|
||||
{
|
||||
_writer.WriteLine (format, arg0);
|
||||
}
|
||||
|
||||
public override void WriteLine (string format, params object[] arg)
|
||||
{
|
||||
_writer.WriteLine (format, arg);
|
||||
}
|
||||
|
||||
public override void WriteLine (ulong value)
|
||||
{
|
||||
_writer.WriteLine (value);
|
||||
}
|
||||
|
||||
public override void WriteLine (uint value)
|
||||
{
|
||||
_writer.WriteLine (value);
|
||||
}
|
||||
|
||||
public override void WriteLine (string value)
|
||||
{
|
||||
_writer.WriteLine (value);
|
||||
}
|
||||
|
||||
public override void WriteLine (float value)
|
||||
{
|
||||
_writer.WriteLine (value);
|
||||
}
|
||||
|
||||
public override void WriteLine (object value)
|
||||
{
|
||||
_writer.WriteLine (value);
|
||||
}
|
||||
|
||||
public override void WriteLine (long value)
|
||||
{
|
||||
_writer.WriteLine (value);
|
||||
}
|
||||
|
||||
public override void WriteLine (int value)
|
||||
{
|
||||
_writer.WriteLine (value);
|
||||
}
|
||||
|
||||
public override void WriteLine (double value)
|
||||
{
|
||||
_writer.WriteLine (value);
|
||||
}
|
||||
|
||||
public override void WriteLine (decimal value)
|
||||
{
|
||||
_writer.WriteLine (value);
|
||||
}
|
||||
|
||||
public override void WriteLine (char value)
|
||||
{
|
||||
_writer.WriteLine (value);
|
||||
}
|
||||
|
||||
public override void WriteLine (bool value)
|
||||
{
|
||||
_writer.WriteLine (value);
|
||||
}
|
||||
|
||||
public override void WriteLine ()
|
||||
{
|
||||
_writer.WriteLine ();
|
||||
}
|
||||
|
||||
public override void Write (bool value)
|
||||
{
|
||||
_writer.Write (value);
|
||||
}
|
||||
|
||||
public override void Write (char value)
|
||||
{
|
||||
_writer.Write (value);
|
||||
}
|
||||
|
||||
public override void Write (decimal value)
|
||||
{
|
||||
_writer.Write (value);
|
||||
}
|
||||
|
||||
public override void Write (double value)
|
||||
{
|
||||
_writer.Write (value);
|
||||
}
|
||||
|
||||
public override void Write (int value)
|
||||
{
|
||||
_writer.Write (value);
|
||||
}
|
||||
|
||||
public override void Write (long value)
|
||||
{
|
||||
_writer.Write (value);
|
||||
}
|
||||
|
||||
public override void Write (object value)
|
||||
{
|
||||
_writer.Write (value);
|
||||
}
|
||||
|
||||
public override void Write (float value)
|
||||
{
|
||||
_writer.Write (value);
|
||||
}
|
||||
|
||||
public override void Write (string value)
|
||||
{
|
||||
_writer.Write (value);
|
||||
}
|
||||
|
||||
public override void Write (uint value)
|
||||
{
|
||||
_writer.Write (value);
|
||||
}
|
||||
|
||||
public override void Write (ulong value)
|
||||
{
|
||||
_writer.Write (value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class Properties
|
||||
{
|
||||
protected Hashtable _properties;
|
||||
|
||||
public Properties()
|
||||
{
|
||||
_properties = new Hashtable();
|
||||
}
|
||||
|
||||
public Properties(Properties defaultProp): this()
|
||||
{
|
||||
PutAll(defaultProp._properties);
|
||||
}
|
||||
|
||||
public void PutAll(Hashtable properties)
|
||||
{
|
||||
foreach (var key in properties.Keys)
|
||||
{
|
||||
//_properties.Add(key, properties[key]);
|
||||
_properties.Put(key, properties[key]);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetProperty(object key, object value)
|
||||
{
|
||||
//_properties.Add(key, value);
|
||||
_properties.Put(key, value);
|
||||
}
|
||||
|
||||
public object GetProperty(object key)
|
||||
{
|
||||
return _properties.Keys.Contains(key) ? _properties[key] : null;
|
||||
}
|
||||
|
||||
public object GetProperty(object key, object def)
|
||||
{
|
||||
/*if (_properties.ContainsKey(key))
|
||||
{
|
||||
return _properties[key];
|
||||
}
|
||||
return def;*/
|
||||
object value = _properties.Get(key);
|
||||
|
||||
return value ?? def;
|
||||
}
|
||||
|
||||
public void Load(InputStream input)
|
||||
{
|
||||
StreamReader sr = new StreamReader(input);
|
||||
while (!sr.EndOfStream)
|
||||
{
|
||||
string line = sr.ReadLine();
|
||||
|
||||
if (!string.IsNullOrEmpty(line))
|
||||
{
|
||||
string[] tokens = line.Split('=');
|
||||
//_properties.Add(tokens[0], tokens[1]);
|
||||
_properties.Put(tokens[0], tokens[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Store(OutputStream output)
|
||||
{
|
||||
StreamWriter sw = new StreamWriter(output);
|
||||
foreach (var key in _properties.Keys)
|
||||
{
|
||||
string line = string.Format("{0}={1}", key, _properties[key]);
|
||||
sw.WriteLine(line);
|
||||
}
|
||||
}
|
||||
|
||||
public void Store(TextWriter output)
|
||||
{
|
||||
foreach (var key in _properties.Keys)
|
||||
{
|
||||
string line = string.Format("{0}={1}", key, _properties[key]);
|
||||
output.WriteLine(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class RandomAccessFile
|
||||
{
|
||||
private FileStream _stream;
|
||||
|
||||
public RandomAccessFile (FilePath file, string mode) : this(file.GetPath (), mode)
|
||||
{
|
||||
}
|
||||
|
||||
public RandomAccessFile (string file, string mode)
|
||||
{
|
||||
if (mode.IndexOf ('w') != -1)
|
||||
_stream = new FileStream (file, FileMode.OpenOrCreate, FileAccess.ReadWrite);
|
||||
else
|
||||
_stream = new FileStream (file, FileMode.Open, FileAccess.Read);
|
||||
}
|
||||
|
||||
public void Close ()
|
||||
{
|
||||
//Stream.`Close` method deleted
|
||||
//_stream.Close ();
|
||||
_stream.Dispose();
|
||||
}
|
||||
|
||||
public long GetFilePointer ()
|
||||
{
|
||||
return _stream.Position;
|
||||
}
|
||||
|
||||
public long Length ()
|
||||
{
|
||||
return _stream.Length;
|
||||
}
|
||||
|
||||
public int Read (byte[] buffer)
|
||||
{
|
||||
int r = _stream.Read (buffer, 0, buffer.Length);
|
||||
return r > 0 ? r : -1;
|
||||
}
|
||||
|
||||
public int Read (byte[] buffer, int start, int size)
|
||||
{
|
||||
return _stream.Read (buffer, start, size);
|
||||
}
|
||||
|
||||
public void ReadFully (byte[] buffer, int start, int size)
|
||||
{
|
||||
while (size > 0) {
|
||||
int num = _stream.Read (buffer, start, size);
|
||||
if (num == 0) {
|
||||
throw new EofException ();
|
||||
}
|
||||
size -= num;
|
||||
start += num;
|
||||
}
|
||||
}
|
||||
|
||||
public void Seek (long pos)
|
||||
{
|
||||
_stream.Position = pos;
|
||||
}
|
||||
|
||||
public void SetLength (long len)
|
||||
{
|
||||
_stream.SetLength (len);
|
||||
}
|
||||
|
||||
public void Write (int value)
|
||||
{
|
||||
_stream.Write (BitConverter.GetBytes (value), 0, 4);
|
||||
}
|
||||
|
||||
public void Write (byte[] buffer)
|
||||
{
|
||||
_stream.Write (buffer, 0, buffer.Length);
|
||||
}
|
||||
|
||||
public void Write (byte[] buffer, int start, int size)
|
||||
{
|
||||
_stream.Write (buffer, start, size);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using System.Threading;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class ReentrantLock
|
||||
{
|
||||
public void Lock ()
|
||||
{
|
||||
Monitor.Enter (this);
|
||||
}
|
||||
|
||||
public bool TryLock ()
|
||||
{
|
||||
return Monitor.TryEnter (this);
|
||||
}
|
||||
|
||||
public void Unlock ()
|
||||
{
|
||||
Monitor.Exit (this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal abstract class Reference<T>
|
||||
{
|
||||
public abstract T Get ();
|
||||
}
|
||||
}
|
||||
212
Emby.Server.Implementations/IO/SharpCifs/Util/Sharpen/Runtime.cs
Normal file
212
Emby.Server.Implementations/IO/SharpCifs/Util/Sharpen/Runtime.cs
Normal file
@@ -0,0 +1,212 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class Runtime
|
||||
{
|
||||
private static Runtime _instance;
|
||||
private List<ShutdownHook> _shutdownHooks = new List<ShutdownHook> ();
|
||||
|
||||
internal void AddShutdownHook (IRunnable r)
|
||||
{
|
||||
ShutdownHook item = new ShutdownHook ();
|
||||
item.Runnable = r;
|
||||
_shutdownHooks.Add (item);
|
||||
}
|
||||
|
||||
internal int AvailableProcessors ()
|
||||
{
|
||||
return Environment.ProcessorCount;
|
||||
}
|
||||
|
||||
public static long CurrentTimeMillis ()
|
||||
{
|
||||
return DateTime.UtcNow.ToMillisecondsSinceEpoch ();
|
||||
}
|
||||
|
||||
static Hashtable _properties;
|
||||
|
||||
public static Hashtable GetProperties ()
|
||||
{
|
||||
if (_properties == null) {
|
||||
_properties = new Hashtable ();
|
||||
_properties ["jgit.fs.debug"] = "false";
|
||||
_properties["file.encoding"] = "UTF-8";
|
||||
if (Path.DirectorySeparatorChar != '\\')
|
||||
_properties ["os.name"] = "Unix";
|
||||
else
|
||||
_properties ["os.name"] = "Windows";
|
||||
}
|
||||
return _properties;
|
||||
}
|
||||
|
||||
public static string GetProperty (string key)
|
||||
{
|
||||
if (GetProperties().Keys.Contains(key))
|
||||
{
|
||||
return ((string)GetProperties()[key]);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void SetProperty (string key, string value)
|
||||
{
|
||||
GetProperties () [key] = value;
|
||||
}
|
||||
|
||||
public static Runtime GetRuntime ()
|
||||
{
|
||||
if (_instance == null) {
|
||||
_instance = new Runtime ();
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
|
||||
public static int IdentityHashCode (object ob)
|
||||
{
|
||||
return RuntimeHelpers.GetHashCode (ob);
|
||||
}
|
||||
|
||||
internal long MaxMemory ()
|
||||
{
|
||||
return int.MaxValue;
|
||||
}
|
||||
|
||||
private class ShutdownHook
|
||||
{
|
||||
public IRunnable Runnable;
|
||||
|
||||
~ShutdownHook ()
|
||||
{
|
||||
Runnable.Run ();
|
||||
}
|
||||
}
|
||||
|
||||
public static void DeleteCharAt (StringBuilder sb, int index)
|
||||
{
|
||||
sb.Remove (index, 1);
|
||||
}
|
||||
|
||||
public static byte[] GetBytesForString (string str)
|
||||
{
|
||||
return Encoding.UTF8.GetBytes (str);
|
||||
}
|
||||
|
||||
public static byte[] GetBytesForString (string str, string encoding)
|
||||
{
|
||||
return Encoding.GetEncoding (encoding).GetBytes (str);
|
||||
}
|
||||
|
||||
public static FieldInfo[] GetDeclaredFields (Type t)
|
||||
{
|
||||
throw new NotImplementedException("Type.GetFields not found on .NetStandard");
|
||||
//return t.GetFields (BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
|
||||
}
|
||||
|
||||
public static void NotifyAll (object ob)
|
||||
{
|
||||
Monitor.PulseAll (ob);
|
||||
}
|
||||
|
||||
public static void Notify(object obj)
|
||||
{
|
||||
Monitor.Pulse(obj);
|
||||
}
|
||||
|
||||
public static void PrintStackTrace (Exception ex)
|
||||
{
|
||||
Console.WriteLine (ex);
|
||||
}
|
||||
|
||||
public static void PrintStackTrace (Exception ex, TextWriter tw)
|
||||
{
|
||||
tw.WriteLine (ex);
|
||||
}
|
||||
|
||||
public static string Substring (string str, int index)
|
||||
{
|
||||
return str.Substring (index);
|
||||
}
|
||||
|
||||
public static string Substring (string str, int index, int endIndex)
|
||||
{
|
||||
return str.Substring (index, endIndex - index);
|
||||
}
|
||||
|
||||
public static void Wait (object ob)
|
||||
{
|
||||
Monitor.Wait (ob);
|
||||
}
|
||||
|
||||
public static bool Wait (object ob, long milis)
|
||||
{
|
||||
return Monitor.Wait (ob, (int)milis);
|
||||
}
|
||||
|
||||
public static Type GetType (string name)
|
||||
{
|
||||
throw new NotImplementedException("AppDomain.CurrentDomain.GetAssemblies not found on .NetStandard");
|
||||
//foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies ()) {
|
||||
// Type t = a.GetType (name);
|
||||
// if (t != null)
|
||||
// return t;
|
||||
//}
|
||||
//never used
|
||||
//throw new InvalidOperationException ("Type not found: " + name);
|
||||
}
|
||||
|
||||
public static void SetCharAt (StringBuilder sb, int index, char c)
|
||||
{
|
||||
sb [index] = c;
|
||||
}
|
||||
|
||||
public static bool EqualsIgnoreCase (string s1, string s2)
|
||||
{
|
||||
return s1.Equals (s2, StringComparison.CurrentCultureIgnoreCase);
|
||||
}
|
||||
|
||||
internal static long NanoTime ()
|
||||
{
|
||||
return Environment.TickCount * 1000 * 1000;
|
||||
}
|
||||
|
||||
internal static int CompareOrdinal (string s1, string s2)
|
||||
{
|
||||
return string.CompareOrdinal (s1, s2);
|
||||
}
|
||||
|
||||
public static string GetStringForBytes (byte[] chars)
|
||||
{
|
||||
return Encoding.UTF8.GetString (chars, 0, chars.Length);
|
||||
}
|
||||
|
||||
public static string GetStringForBytes (byte[] chars, string encoding)
|
||||
{
|
||||
return GetEncoding (encoding).GetString (chars, 0, chars.Length);
|
||||
}
|
||||
|
||||
public static string GetStringForBytes (byte[] chars, int start, int len)
|
||||
{
|
||||
return Encoding.UTF8.GetString (chars, start, len);
|
||||
}
|
||||
|
||||
public static string GetStringForBytes (byte[] chars, int start, int len, string encoding)
|
||||
{
|
||||
return GetEncoding (encoding).Decode (chars, start, len);
|
||||
}
|
||||
|
||||
public static Encoding GetEncoding (string name)
|
||||
{
|
||||
Encoding e = Encoding.GetEncoding (name.Replace ('_','-'));
|
||||
if (e is UTF8Encoding)
|
||||
return new UTF8Encoding (false, true);
|
||||
return e;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class SimpleDateFormat : DateFormat
|
||||
{
|
||||
string _format;
|
||||
|
||||
CultureInfo Culture {
|
||||
get; set;
|
||||
}
|
||||
|
||||
bool Lenient {
|
||||
get; set;
|
||||
}
|
||||
|
||||
public SimpleDateFormat (): this ("g")
|
||||
{
|
||||
}
|
||||
|
||||
public SimpleDateFormat (string format): this (format, CultureInfo.CurrentCulture)
|
||||
{
|
||||
}
|
||||
|
||||
public SimpleDateFormat (string format, CultureInfo c)
|
||||
{
|
||||
Culture = c;
|
||||
this._format = format.Replace ("EEE", "ddd");
|
||||
this._format = this._format.Replace ("Z", "zzz");
|
||||
SetTimeZone (TimeZoneInfo.Local);
|
||||
}
|
||||
|
||||
public bool IsLenient ()
|
||||
{
|
||||
return Lenient;
|
||||
}
|
||||
|
||||
public void SetLenient (bool lenient)
|
||||
{
|
||||
Lenient = lenient;
|
||||
}
|
||||
|
||||
public override DateTime Parse (string value)
|
||||
{
|
||||
if (IsLenient ())
|
||||
return DateTime.Parse (value);
|
||||
return DateTime.ParseExact (value, _format, Culture);
|
||||
}
|
||||
|
||||
public override string Format (DateTime date)
|
||||
{
|
||||
date += GetTimeZone().BaseUtcOffset;
|
||||
return date.ToString (_format);
|
||||
}
|
||||
|
||||
public string Format (long date)
|
||||
{
|
||||
return Extensions.MillisToDateTimeOffset (date, (int)GetTimeZone ().BaseUtcOffset.TotalMinutes).DateTime.ToString (_format);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
// SocketEx.cs implementation by J. Arturo <webmaster at komodosoft dot net>
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class SocketEx : Socket
|
||||
{
|
||||
private int _soTimeOut = -1;
|
||||
|
||||
public int SoTimeOut
|
||||
{
|
||||
get
|
||||
{
|
||||
return _soTimeOut;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
if (value > 0)
|
||||
{
|
||||
_soTimeOut = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
_soTimeOut = -1;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public SocketEx(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
|
||||
: base(addressFamily, socketType, protocolType)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Connect(IPEndPoint endPoint, int timeOut)
|
||||
{
|
||||
using (var evt = new ManualResetEventSlim(false))
|
||||
{
|
||||
using (var args = new SocketAsyncEventArgs
|
||||
{
|
||||
RemoteEndPoint = endPoint
|
||||
})
|
||||
{
|
||||
args.Completed += delegate
|
||||
{
|
||||
evt.Set();
|
||||
};
|
||||
|
||||
ConnectAsync(args);
|
||||
|
||||
if (!evt.Wait(timeOut))
|
||||
{
|
||||
CancelConnectAsync(args);
|
||||
throw new ConnectException("Can't connect to end point.");
|
||||
}
|
||||
if (args.SocketError != SocketError.Success)
|
||||
{
|
||||
throw new ConnectException("Can't connect to end point.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Bind2(EndPoint ep)
|
||||
{
|
||||
if (ep == null)
|
||||
Bind(new IPEndPoint(IPAddress.Any, 0));
|
||||
else
|
||||
Bind(ep);
|
||||
}
|
||||
|
||||
|
||||
public int Receive(byte[] buffer, int offset, int count)
|
||||
{
|
||||
using (var evt = new ManualResetEventSlim(false))
|
||||
{
|
||||
using (var args = new SocketAsyncEventArgs
|
||||
{
|
||||
UserToken = this
|
||||
})
|
||||
{
|
||||
args.SetBuffer(buffer, offset, count);
|
||||
|
||||
args.Completed += delegate
|
||||
{
|
||||
evt.Set();
|
||||
};
|
||||
|
||||
if (ReceiveAsync(args))
|
||||
{
|
||||
if (!evt.Wait(_soTimeOut))
|
||||
{
|
||||
throw new TimeoutException("No data received.");
|
||||
}
|
||||
}
|
||||
|
||||
return args.BytesTransferred;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Send(byte[] buffer, int offset, int length, EndPoint destination = null)
|
||||
{
|
||||
using (var evt = new ManualResetEventSlim(false))
|
||||
{
|
||||
using (SocketAsyncEventArgs args = new SocketAsyncEventArgs
|
||||
{
|
||||
UserToken = this
|
||||
})
|
||||
{
|
||||
args.SetBuffer(buffer, offset, length);
|
||||
|
||||
args.Completed += delegate
|
||||
{
|
||||
evt.Set();
|
||||
};
|
||||
|
||||
args.RemoteEndPoint = destination ?? RemoteEndPoint;
|
||||
|
||||
|
||||
SendToAsync(args);
|
||||
if (!evt.Wait(_soTimeOut))
|
||||
{
|
||||
throw new TimeoutException("No data sent.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public InputStream GetInputStream()
|
||||
{
|
||||
return new NetworkStream(this);
|
||||
}
|
||||
|
||||
public OutputStream GetOutputStream()
|
||||
{
|
||||
return new NetworkStream(this);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class StringTokenizer
|
||||
{
|
||||
private string[] _tokens;
|
||||
private int _pos;
|
||||
|
||||
public StringTokenizer(string text, string delim)
|
||||
{
|
||||
_tokens = text.Split(delim);
|
||||
}
|
||||
|
||||
public int CountTokens()
|
||||
{
|
||||
return _tokens.Length;
|
||||
}
|
||||
|
||||
public string NextToken()
|
||||
{
|
||||
string value = _tokens[_pos];
|
||||
|
||||
_pos++;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public bool HasMoreTokens()
|
||||
{
|
||||
return _pos < _tokens.Length;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class SynchronizedList<T> : IList<T>
|
||||
{
|
||||
private IList<T> _list;
|
||||
|
||||
public SynchronizedList (IList<T> list)
|
||||
{
|
||||
this._list = list;
|
||||
}
|
||||
|
||||
public int IndexOf (T item)
|
||||
{
|
||||
lock (_list) {
|
||||
return _list.IndexOf (item);
|
||||
}
|
||||
}
|
||||
|
||||
public void Insert (int index, T item)
|
||||
{
|
||||
lock (_list) {
|
||||
_list.Insert (index, item);
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveAt (int index)
|
||||
{
|
||||
lock (_list) {
|
||||
_list.RemoveAt (index);
|
||||
}
|
||||
}
|
||||
|
||||
void ICollection<T>.Add (T item)
|
||||
{
|
||||
lock (_list) {
|
||||
_list.Add (item);
|
||||
}
|
||||
}
|
||||
|
||||
void ICollection<T>.Clear ()
|
||||
{
|
||||
lock (_list) {
|
||||
_list.Clear ();
|
||||
}
|
||||
}
|
||||
|
||||
bool ICollection<T>.Contains (T item)
|
||||
{
|
||||
lock (_list) {
|
||||
return _list.Contains (item);
|
||||
}
|
||||
}
|
||||
|
||||
void ICollection<T>.CopyTo (T[] array, int arrayIndex)
|
||||
{
|
||||
lock (_list) {
|
||||
_list.CopyTo (array, arrayIndex);
|
||||
}
|
||||
}
|
||||
|
||||
bool ICollection<T>.Remove (T item)
|
||||
{
|
||||
lock (_list) {
|
||||
return _list.Remove (item);
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator<T> IEnumerable<T>.GetEnumerator ()
|
||||
{
|
||||
return _list.GetEnumerator ();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator ()
|
||||
{
|
||||
return _list.GetEnumerator ();
|
||||
}
|
||||
|
||||
public T this[int index] {
|
||||
get {
|
||||
lock (_list) {
|
||||
return _list[index];
|
||||
}
|
||||
}
|
||||
set {
|
||||
lock (_list) {
|
||||
_list[index] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int ICollection<T>.Count {
|
||||
get {
|
||||
lock (_list) {
|
||||
return _list.Count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool ICollection<T>.IsReadOnly {
|
||||
get { return _list.IsReadOnly; }
|
||||
}
|
||||
}
|
||||
}
|
||||
193
Emby.Server.Implementations/IO/SharpCifs/Util/Sharpen/Thread.cs
Normal file
193
Emby.Server.Implementations/IO/SharpCifs/Util/Sharpen/Thread.cs
Normal file
@@ -0,0 +1,193 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class Thread : IRunnable
|
||||
{
|
||||
private static ThreadGroup _defaultGroup = new ThreadGroup ();
|
||||
private bool _interrupted;
|
||||
private IRunnable _runnable;
|
||||
private ThreadGroup _tgroup;
|
||||
private System.Threading.Thread _thread;
|
||||
|
||||
[ThreadStatic]
|
||||
private static Thread _wrapperThread;
|
||||
|
||||
public Thread () : this(null, null, null)
|
||||
{
|
||||
}
|
||||
|
||||
public Thread (string name) : this (null, null, name)
|
||||
{
|
||||
}
|
||||
|
||||
public Thread (ThreadGroup grp, string name) : this (null, grp, name)
|
||||
{
|
||||
}
|
||||
|
||||
public Thread (IRunnable runnable): this (runnable, null, null)
|
||||
{
|
||||
}
|
||||
|
||||
Thread (IRunnable runnable, ThreadGroup grp, string name)
|
||||
{
|
||||
_thread = new System.Threading.Thread (InternalRun);
|
||||
|
||||
this._runnable = runnable ?? this;
|
||||
_tgroup = grp ?? _defaultGroup;
|
||||
_tgroup.Add (this);
|
||||
if (name != null)
|
||||
_thread.Name = name;
|
||||
}
|
||||
|
||||
private Thread (System.Threading.Thread t)
|
||||
{
|
||||
_thread = t;
|
||||
_tgroup = _defaultGroup;
|
||||
_tgroup.Add (this);
|
||||
}
|
||||
|
||||
public static Thread CurrentThread ()
|
||||
{
|
||||
if (_wrapperThread == null) {
|
||||
_wrapperThread = new Thread (System.Threading.Thread.CurrentThread);
|
||||
}
|
||||
return _wrapperThread;
|
||||
}
|
||||
|
||||
public string GetName ()
|
||||
{
|
||||
return _thread.Name;
|
||||
}
|
||||
|
||||
public ThreadGroup GetThreadGroup ()
|
||||
{
|
||||
return _tgroup;
|
||||
}
|
||||
|
||||
private void InternalRun ()
|
||||
{
|
||||
_wrapperThread = this;
|
||||
try {
|
||||
_runnable.Run ();
|
||||
} catch (Exception exception) {
|
||||
Console.WriteLine (exception);
|
||||
} finally {
|
||||
_tgroup.Remove (this);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Yield ()
|
||||
{
|
||||
}
|
||||
|
||||
public void Interrupt ()
|
||||
{
|
||||
lock (_thread) {
|
||||
_interrupted = true;
|
||||
//thread.Interrupt ();
|
||||
|
||||
//TODO: implement CancellationToken
|
||||
//_thread.Abort();
|
||||
throw new NotImplementedException("implement CancellationToken for thread");
|
||||
}
|
||||
}
|
||||
|
||||
public static bool Interrupted ()
|
||||
{
|
||||
if (Thread._wrapperThread == null) {
|
||||
return false;
|
||||
}
|
||||
Thread wrapperThread = Thread._wrapperThread;
|
||||
lock (wrapperThread) {
|
||||
bool interrupted = Thread._wrapperThread._interrupted;
|
||||
Thread._wrapperThread._interrupted = false;
|
||||
return interrupted;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsAlive ()
|
||||
{
|
||||
return _thread.IsAlive;
|
||||
}
|
||||
|
||||
public void Join ()
|
||||
{
|
||||
_thread.Join ();
|
||||
}
|
||||
|
||||
public void Join (long timeout)
|
||||
{
|
||||
_thread.Join ((int)timeout);
|
||||
}
|
||||
|
||||
public virtual void Run ()
|
||||
{
|
||||
}
|
||||
|
||||
public void SetDaemon (bool daemon)
|
||||
{
|
||||
_thread.IsBackground = daemon;
|
||||
}
|
||||
|
||||
public void SetName (string name)
|
||||
{
|
||||
_thread.Name = name;
|
||||
}
|
||||
|
||||
public static void Sleep (long milis)
|
||||
{
|
||||
System.Threading.Thread.Sleep ((int)milis);
|
||||
}
|
||||
|
||||
public void Start ()
|
||||
{
|
||||
_thread.Start ();
|
||||
}
|
||||
|
||||
public void Abort ()
|
||||
{
|
||||
//TODO: implement CancellationToken
|
||||
//_thread.Abort ();
|
||||
throw new NotImplementedException("implement CancellationToken for thread");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class ThreadGroup
|
||||
{
|
||||
private List<Thread> _threads = new List<Thread> ();
|
||||
|
||||
public ThreadGroup()
|
||||
{
|
||||
}
|
||||
|
||||
public ThreadGroup (string name)
|
||||
{
|
||||
}
|
||||
|
||||
internal void Add (Thread t)
|
||||
{
|
||||
lock (_threads) {
|
||||
_threads.Add (t);
|
||||
}
|
||||
}
|
||||
|
||||
internal void Remove (Thread t)
|
||||
{
|
||||
lock (_threads) {
|
||||
_threads.Remove (t);
|
||||
}
|
||||
}
|
||||
|
||||
public int Enumerate (Thread[] array)
|
||||
{
|
||||
lock (_threads) {
|
||||
int count = Math.Min (array.Length, _threads.Count);
|
||||
_threads.CopyTo (0, array, 0, count);
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class ThreadFactory
|
||||
{
|
||||
public Thread NewThread (IRunnable r)
|
||||
{
|
||||
Thread t = new Thread (r);
|
||||
t.SetDaemon (true);
|
||||
t.Start ();
|
||||
return t;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using ST = System.Threading;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
class ThreadPoolExecutor
|
||||
{
|
||||
ThreadFactory _tf;
|
||||
int _corePoolSize;
|
||||
int _maxPoolSize;
|
||||
List<Thread> _pool = new List<Thread> ();
|
||||
int _runningThreads;
|
||||
int _freeThreads;
|
||||
bool _shutdown;
|
||||
Queue<IRunnable> _pendingTasks = new Queue<IRunnable> ();
|
||||
|
||||
public ThreadPoolExecutor (int corePoolSize, ThreadFactory factory)
|
||||
{
|
||||
this._corePoolSize = corePoolSize;
|
||||
_maxPoolSize = corePoolSize;
|
||||
_tf = factory;
|
||||
}
|
||||
|
||||
public void SetMaximumPoolSize (int size)
|
||||
{
|
||||
_maxPoolSize = size;
|
||||
}
|
||||
|
||||
public bool IsShutdown ()
|
||||
{
|
||||
return _shutdown;
|
||||
}
|
||||
|
||||
public virtual bool IsTerminated ()
|
||||
{
|
||||
lock (_pendingTasks) {
|
||||
return _shutdown && _pendingTasks.Count == 0;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual bool IsTerminating ()
|
||||
{
|
||||
lock (_pendingTasks) {
|
||||
return _shutdown && !IsTerminated ();
|
||||
}
|
||||
}
|
||||
|
||||
public int GetCorePoolSize ()
|
||||
{
|
||||
return _corePoolSize;
|
||||
}
|
||||
|
||||
public void PrestartAllCoreThreads ()
|
||||
{
|
||||
lock (_pendingTasks) {
|
||||
while (_runningThreads < _corePoolSize)
|
||||
StartPoolThread ();
|
||||
}
|
||||
}
|
||||
|
||||
public void SetThreadFactory (ThreadFactory f)
|
||||
{
|
||||
_tf = f;
|
||||
}
|
||||
|
||||
public void Execute (IRunnable r)
|
||||
{
|
||||
InternalExecute (r, true);
|
||||
}
|
||||
|
||||
internal void InternalExecute (IRunnable r, bool checkShutdown)
|
||||
{
|
||||
lock (_pendingTasks) {
|
||||
if (_shutdown && checkShutdown)
|
||||
throw new InvalidOperationException ();
|
||||
if (_runningThreads < _corePoolSize) {
|
||||
StartPoolThread ();
|
||||
}
|
||||
else if (_freeThreads > 0) {
|
||||
_freeThreads--;
|
||||
}
|
||||
else if (_runningThreads < _maxPoolSize) {
|
||||
StartPoolThread ();
|
||||
}
|
||||
_pendingTasks.Enqueue (r);
|
||||
ST.Monitor.PulseAll (_pendingTasks);
|
||||
}
|
||||
}
|
||||
|
||||
void StartPoolThread ()
|
||||
{
|
||||
_runningThreads++;
|
||||
_pool.Add (_tf.NewThread (new RunnableAction (RunPoolThread)));
|
||||
}
|
||||
|
||||
public void RunPoolThread ()
|
||||
{
|
||||
while (!IsTerminated ()) {
|
||||
try {
|
||||
IRunnable r = null;
|
||||
lock (_pendingTasks) {
|
||||
_freeThreads++;
|
||||
while (!IsTerminated () && _pendingTasks.Count == 0)
|
||||
ST.Monitor.Wait (_pendingTasks);
|
||||
if (IsTerminated ())
|
||||
break;
|
||||
r = _pendingTasks.Dequeue ();
|
||||
}
|
||||
if (r != null)
|
||||
r.Run ();
|
||||
}
|
||||
//supress all errors, anyway
|
||||
//catch (ST.ThreadAbortException) {
|
||||
// // Do not catch a thread abort. If we've been aborted just let the thread die.
|
||||
// // Currently reseting an abort which was issued because the appdomain is being
|
||||
// // torn down results in the process living forever and consuming 100% cpu time.
|
||||
// return;
|
||||
//}
|
||||
catch {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Shutdown ()
|
||||
{
|
||||
lock (_pendingTasks) {
|
||||
_shutdown = true;
|
||||
ST.Monitor.PulseAll (_pendingTasks);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual List<IRunnable> ShutdownNow ()
|
||||
{
|
||||
lock (_pendingTasks) {
|
||||
_shutdown = true;
|
||||
foreach (var t in _pool) {
|
||||
try {
|
||||
t.Abort ();
|
||||
} catch {}
|
||||
}
|
||||
_pool.Clear ();
|
||||
_freeThreads = 0;
|
||||
_runningThreads = 0;
|
||||
var res = new List<IRunnable> (_pendingTasks);
|
||||
_pendingTasks.Clear ();
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class RunnableAction: IRunnable
|
||||
{
|
||||
Action _action;
|
||||
|
||||
public RunnableAction (Action a)
|
||||
{
|
||||
_action = a;
|
||||
}
|
||||
|
||||
public void Run ()
|
||||
{
|
||||
_action ();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class WrappedSystemStream : Stream
|
||||
{
|
||||
private InputStream _ist;
|
||||
private OutputStream _ost;
|
||||
int _position;
|
||||
int _markedPosition;
|
||||
|
||||
public WrappedSystemStream (InputStream ist)
|
||||
{
|
||||
this._ist = ist;
|
||||
}
|
||||
|
||||
public WrappedSystemStream (OutputStream ost)
|
||||
{
|
||||
this._ost = ost;
|
||||
}
|
||||
|
||||
public InputStream InputStream {
|
||||
get { return _ist; }
|
||||
}
|
||||
|
||||
public OutputStream OutputStream {
|
||||
get { return _ost; }
|
||||
}
|
||||
|
||||
public void Close() //remove `override`
|
||||
{
|
||||
if (_ist != null) {
|
||||
//Stream.`Close` method deleted
|
||||
//_ist.Close ();
|
||||
_ist.Dispose();
|
||||
}
|
||||
if (_ost != null) {
|
||||
//Stream.`Close` method deleted
|
||||
//_ost.Close ();
|
||||
_ost.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public override void Flush ()
|
||||
{
|
||||
_ost.Flush ();
|
||||
}
|
||||
|
||||
public override int Read (byte[] buffer, int offset, int count)
|
||||
{
|
||||
int res = _ist.Read (buffer, offset, count);
|
||||
if (res != -1) {
|
||||
_position += res;
|
||||
return res;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public override int ReadByte ()
|
||||
{
|
||||
int res = _ist.Read ();
|
||||
if (res != -1)
|
||||
_position++;
|
||||
return res;
|
||||
}
|
||||
|
||||
public override long Seek (long offset, SeekOrigin origin)
|
||||
{
|
||||
if (origin == SeekOrigin.Begin)
|
||||
Position = offset;
|
||||
else if (origin == SeekOrigin.Current)
|
||||
Position = Position + offset;
|
||||
else if (origin == SeekOrigin.End)
|
||||
Position = Length + offset;
|
||||
return Position;
|
||||
}
|
||||
|
||||
public override void SetLength (long value)
|
||||
{
|
||||
throw new NotSupportedException ();
|
||||
}
|
||||
|
||||
public override void Write (byte[] buffer, int offset, int count)
|
||||
{
|
||||
_ost.Write (buffer, offset, count);
|
||||
_position += count;
|
||||
}
|
||||
|
||||
public override void WriteByte (byte value)
|
||||
{
|
||||
_ost.Write (value);
|
||||
_position++;
|
||||
}
|
||||
|
||||
public override bool CanRead {
|
||||
get { return (_ist != null); }
|
||||
}
|
||||
|
||||
public override bool CanSeek {
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
public override bool CanWrite {
|
||||
get { return (_ost != null); }
|
||||
}
|
||||
|
||||
public override long Length {
|
||||
get { return _ist.Length; }
|
||||
}
|
||||
|
||||
internal void OnMark (int nb)
|
||||
{
|
||||
_markedPosition = _position;
|
||||
_ist.Mark (nb);
|
||||
}
|
||||
|
||||
public override long Position {
|
||||
get
|
||||
{
|
||||
if (_ist != null && _ist.CanSeek ())
|
||||
return _ist.Position;
|
||||
return _position;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value == _position)
|
||||
return;
|
||||
if (value == _markedPosition)
|
||||
_ist.Reset ();
|
||||
else if (_ist != null && _ist.CanSeek ()) {
|
||||
_ist.Position = value;
|
||||
}
|
||||
else
|
||||
throw new NotSupportedException ();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// This code is derived from jcifs smb client library <jcifs at samba dot org>
|
||||
// Ported by J. Arturo <webmaster at komodosoft dot net>
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
namespace SharpCifs.Util.Transport
|
||||
{
|
||||
public interface IRequest
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// This code is derived from jcifs smb client library <jcifs at samba dot org>
|
||||
// Ported by J. Arturo <webmaster at komodosoft dot net>
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
namespace SharpCifs.Util.Transport
|
||||
{
|
||||
public abstract class Response
|
||||
{
|
||||
public long Expiration;
|
||||
|
||||
public bool IsReceived;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,454 @@
|
||||
// This code is derived from jcifs smb client library <jcifs at samba dot org>
|
||||
// Ported by J. Arturo <webmaster at komodosoft dot net>
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
using System;
|
||||
using System.IO;
|
||||
using SharpCifs.Smb;
|
||||
using SharpCifs.Util.Sharpen;
|
||||
|
||||
namespace SharpCifs.Util.Transport
|
||||
{
|
||||
/// <summary>
|
||||
/// This class simplifies communication for protocols that support
|
||||
/// multiplexing requests.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This class simplifies communication for protocols that support
|
||||
/// multiplexing requests. It encapsulates a stream and some protocol
|
||||
/// knowledge (provided by a concrete subclass) so that connecting,
|
||||
/// disconnecting, sending, and receiving can be syncronized
|
||||
/// properly. Apparatus is provided to send and receive requests
|
||||
/// concurrently.
|
||||
/// </remarks>
|
||||
public abstract class Transport : IRunnable
|
||||
{
|
||||
internal static int Id;
|
||||
|
||||
//internal static LogStream log = LogStream.GetInstance();
|
||||
|
||||
public LogStream Log
|
||||
{
|
||||
get
|
||||
{
|
||||
return LogStream.GetInstance();
|
||||
}
|
||||
}
|
||||
|
||||
/// <exception cref="System.IO.IOException"></exception>
|
||||
public static int Readn(InputStream @in, byte[] b, int off, int len)
|
||||
{
|
||||
int i = 0;
|
||||
int n = -5;
|
||||
while (i < len)
|
||||
{
|
||||
n = @in.Read(b, off + i, len - i);
|
||||
if (n <= 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
i += n;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
internal int State;
|
||||
|
||||
internal string Name = "Transport" + Id++;
|
||||
|
||||
internal Thread Thread;
|
||||
|
||||
internal TransportException Te;
|
||||
|
||||
protected internal Hashtable ResponseMap = new Hashtable();
|
||||
|
||||
/// <exception cref="System.IO.IOException"></exception>
|
||||
protected internal abstract void MakeKey(ServerMessageBlock request);
|
||||
|
||||
/// <exception cref="System.IO.IOException"></exception>
|
||||
protected internal abstract ServerMessageBlock PeekKey();
|
||||
|
||||
/// <exception cref="System.IO.IOException"></exception>
|
||||
protected internal abstract void DoSend(ServerMessageBlock request);
|
||||
|
||||
/// <exception cref="System.IO.IOException"></exception>
|
||||
protected internal abstract void DoRecv(Response response);
|
||||
|
||||
/// <exception cref="System.IO.IOException"></exception>
|
||||
protected internal abstract void DoSkip();
|
||||
|
||||
/// <exception cref="System.IO.IOException"></exception>
|
||||
public virtual void Sendrecv(ServerMessageBlock request, Response response, long timeout)
|
||||
{
|
||||
lock (this)
|
||||
{
|
||||
MakeKey(request);
|
||||
response.IsReceived = false;
|
||||
try
|
||||
{
|
||||
ResponseMap.Put(request, response);
|
||||
DoSend(request);
|
||||
response.Expiration = Runtime.CurrentTimeMillis() + timeout;
|
||||
while (!response.IsReceived)
|
||||
{
|
||||
Runtime.Wait(this, timeout);
|
||||
timeout = response.Expiration - Runtime.CurrentTimeMillis();
|
||||
if (timeout <= 0)
|
||||
{
|
||||
throw new TransportException(Name + " timedout waiting for response to " + request
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
if (Log.Level > 2)
|
||||
{
|
||||
Runtime.PrintStackTrace(ioe, Log);
|
||||
}
|
||||
try
|
||||
{
|
||||
Disconnect(true);
|
||||
}
|
||||
catch (IOException ioe2)
|
||||
{
|
||||
Runtime.PrintStackTrace(ioe2, Log);
|
||||
}
|
||||
throw;
|
||||
}
|
||||
catch (Exception ie)
|
||||
{
|
||||
throw new TransportException(ie);
|
||||
}
|
||||
finally
|
||||
{
|
||||
//Sharpen.Collections.Remove(response_map, request);
|
||||
ResponseMap.Remove(request);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Loop()
|
||||
{
|
||||
while (Thread == Thread.CurrentThread())
|
||||
{
|
||||
try
|
||||
{
|
||||
ServerMessageBlock key = PeekKey();
|
||||
if (key == null)
|
||||
{
|
||||
throw new IOException("end of stream");
|
||||
}
|
||||
|
||||
|
||||
lock (this)
|
||||
{
|
||||
Response response = (Response)ResponseMap.Get(key);
|
||||
if (response == null)
|
||||
{
|
||||
if (Log.Level >= 4)
|
||||
{
|
||||
Log.WriteLine("Invalid key, skipping message");
|
||||
}
|
||||
DoSkip();
|
||||
}
|
||||
else
|
||||
{
|
||||
DoRecv(response);
|
||||
response.IsReceived = true;
|
||||
Runtime.NotifyAll(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
string msg = ex.Message;
|
||||
bool timeout = msg != null && msg.Equals("Read timed out");
|
||||
bool hard = timeout == false;
|
||||
if (!timeout && Log.Level >= 3)
|
||||
{
|
||||
Runtime.PrintStackTrace(ex, Log);
|
||||
}
|
||||
try
|
||||
{
|
||||
Disconnect(hard);
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
Runtime.PrintStackTrace(ioe, Log);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <exception cref="System.Exception"></exception>
|
||||
protected internal abstract void DoConnect();
|
||||
|
||||
/// <exception cref="System.IO.IOException"></exception>
|
||||
protected internal abstract void DoDisconnect(bool hard);
|
||||
|
||||
/// <exception cref="SharpCifs.Util.Transport.TransportException"></exception>
|
||||
public virtual void Connect(long timeout)
|
||||
{
|
||||
lock (this)
|
||||
{
|
||||
try
|
||||
{
|
||||
switch (State)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
case 3:
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
case 4:
|
||||
{
|
||||
// already connected
|
||||
State = 0;
|
||||
throw new TransportException("Connection in error", Te);
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
//TransportException te = new TransportException("Invalid state: " + state);
|
||||
State = 0;
|
||||
throw new TransportException("Invalid state: " + State);
|
||||
}
|
||||
}
|
||||
State = 1;
|
||||
Te = null;
|
||||
Thread = new Thread(this);
|
||||
Thread.SetDaemon(true);
|
||||
lock (Thread)
|
||||
{
|
||||
Thread.Start();
|
||||
Runtime.Wait(Thread, timeout);
|
||||
switch (State)
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
State = 0;
|
||||
Thread = null;
|
||||
throw new TransportException("Connection timeout");
|
||||
}
|
||||
|
||||
case 2:
|
||||
{
|
||||
if (Te != null)
|
||||
{
|
||||
State = 4;
|
||||
Thread = null;
|
||||
throw Te;
|
||||
}
|
||||
State = 3;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ie)
|
||||
{
|
||||
State = 0;
|
||||
Thread = null;
|
||||
throw new TransportException(ie);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (State != 0 && State != 3 && State != 4)
|
||||
{
|
||||
if (Log.Level >= 1)
|
||||
{
|
||||
Log.WriteLine("Invalid state: " + State);
|
||||
}
|
||||
State = 0;
|
||||
Thread = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <exception cref="System.IO.IOException"></exception>
|
||||
public virtual void Disconnect(bool hard)
|
||||
{
|
||||
|
||||
if (hard)
|
||||
{
|
||||
IOException ioe = null;
|
||||
switch (State)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
case 2:
|
||||
{
|
||||
hard = true;
|
||||
goto case 3;
|
||||
}
|
||||
|
||||
case 3:
|
||||
{
|
||||
if (ResponseMap.Count != 0 && !hard)
|
||||
{
|
||||
break;
|
||||
}
|
||||
try
|
||||
{
|
||||
DoDisconnect(hard);
|
||||
}
|
||||
catch (IOException ioe0)
|
||||
{
|
||||
ioe = ioe0;
|
||||
}
|
||||
goto case 4;
|
||||
}
|
||||
|
||||
case 4:
|
||||
{
|
||||
Thread = null;
|
||||
State = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
if (Log.Level >= 1)
|
||||
{
|
||||
Log.WriteLine("Invalid state: " + State);
|
||||
}
|
||||
Thread = null;
|
||||
State = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ioe != null)
|
||||
{
|
||||
throw ioe;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
lock (this)
|
||||
{
|
||||
IOException ioe = null;
|
||||
switch (State)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
case 2:
|
||||
{
|
||||
hard = true;
|
||||
goto case 3;
|
||||
}
|
||||
|
||||
case 3:
|
||||
{
|
||||
if (ResponseMap.Count != 0 && !hard)
|
||||
{
|
||||
break;
|
||||
}
|
||||
try
|
||||
{
|
||||
DoDisconnect(hard);
|
||||
}
|
||||
catch (IOException ioe0)
|
||||
{
|
||||
ioe = ioe0;
|
||||
}
|
||||
goto case 4;
|
||||
}
|
||||
|
||||
case 4:
|
||||
{
|
||||
Thread = null;
|
||||
State = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
if (Log.Level >= 1)
|
||||
{
|
||||
Log.WriteLine("Invalid state: " + State);
|
||||
}
|
||||
Thread = null;
|
||||
State = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ioe != null)
|
||||
{
|
||||
throw ioe;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Run()
|
||||
{
|
||||
Thread runThread = Thread.CurrentThread();
|
||||
Exception ex0 = null;
|
||||
try
|
||||
{
|
||||
DoConnect();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ex0 = ex;
|
||||
// Defer to below where we're locked
|
||||
return;
|
||||
}
|
||||
finally
|
||||
{
|
||||
lock (runThread)
|
||||
{
|
||||
if (runThread != Thread)
|
||||
{
|
||||
if (ex0 != null)
|
||||
{
|
||||
if (Log.Level >= 2)
|
||||
{
|
||||
Runtime.PrintStackTrace(ex0, Log);
|
||||
}
|
||||
}
|
||||
//return;
|
||||
}
|
||||
if (ex0 != null)
|
||||
{
|
||||
Te = new TransportException(ex0);
|
||||
}
|
||||
State = 2;
|
||||
// run connected
|
||||
Runtime.Notify(runThread);
|
||||
}
|
||||
}
|
||||
Loop();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
// This code is derived from jcifs smb client library <jcifs at samba dot org>
|
||||
// Ported by J. Arturo <webmaster at komodosoft dot net>
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
using System;
|
||||
using System.IO;
|
||||
using SharpCifs.Util.Sharpen;
|
||||
|
||||
namespace SharpCifs.Util.Transport
|
||||
{
|
||||
|
||||
public class TransportException : IOException
|
||||
{
|
||||
private Exception _rootCause;
|
||||
|
||||
public TransportException()
|
||||
{
|
||||
}
|
||||
|
||||
public TransportException(string msg) : base(msg)
|
||||
{
|
||||
}
|
||||
|
||||
public TransportException(Exception rootCause)
|
||||
{
|
||||
this._rootCause = rootCause;
|
||||
}
|
||||
|
||||
public TransportException(string msg, Exception rootCause) : base(msg)
|
||||
{
|
||||
this._rootCause = rootCause;
|
||||
}
|
||||
|
||||
public virtual Exception GetRootCause()
|
||||
{
|
||||
return _rootCause;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
if (_rootCause != null)
|
||||
{
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter pw = new PrintWriter(sw);
|
||||
Runtime.PrintStackTrace(_rootCause, pw);
|
||||
return base.ToString() + "\n" + sw;
|
||||
}
|
||||
return base.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user