mirror of https://github.com/dahall/Vanara.git
Included RdcStreamReader implementation of IRdcFileReader in base assembly since it is very reusable.
parent
232b0e88cd
commit
912bc9a3cc
|
@ -1,4 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Security.Permissions;
|
using System.Security.Permissions;
|
||||||
using Vanara.InteropServices;
|
using Vanara.InteropServices;
|
||||||
|
@ -667,8 +668,8 @@ namespace Vanara.PInvoke
|
||||||
/// initialized on return. Callers must release the interface.
|
/// initialized on return. Callers must release the interface.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
/// <remarks>The caller must create a separate signature comparator for each level of recursion.</remarks>
|
/// <remarks>The caller must create a separate signature comparator for each level of recursion.</remarks>
|
||||||
// https://docs.microsoft.com/en-us/windows/win32/api/msrdc/nf-msrdc-irdclibrary-createcomparator
|
// https://docs.microsoft.com/en-us/windows/win32/api/msrdc/nf-msrdc-irdclibrary-createcomparator HRESULT CreateComparator( [in]
|
||||||
// HRESULT CreateComparator( [in] IRdcFileReader *iSeedSignaturesFile, [in] ULONG comparatorBufferSize, [out] IRdcComparator **iComparator );
|
// IRdcFileReader *iSeedSignaturesFile, [in] ULONG comparatorBufferSize, [out] IRdcComparator **iComparator );
|
||||||
[return: MarshalAs(UnmanagedType.Interface)]
|
[return: MarshalAs(UnmanagedType.Interface)]
|
||||||
IRdcComparator CreateComparator([In, MarshalAs(UnmanagedType.Interface)] IRdcFileReader iSeedSignaturesFile, [Optional] uint comparatorBufferSize);
|
IRdcComparator CreateComparator([In, MarshalAs(UnmanagedType.Interface)] IRdcFileReader iSeedSignaturesFile, [Optional] uint comparatorBufferSize);
|
||||||
|
|
||||||
|
@ -1595,6 +1596,53 @@ namespace Vanara.PInvoke
|
||||||
[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
|
[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
|
||||||
public class RdcSimilarityGenerator { }
|
public class RdcSimilarityGenerator { }
|
||||||
|
|
||||||
|
/// <summary>An implementation of <see cref="IRdcFileReader"/> on top of a <see cref="Stream"/>.</summary>
|
||||||
|
/// <seealso cref="IRdcFileReader"/>
|
||||||
|
[ComVisible(true), Guid("01EED492-3E92-4DF1-AC94-A7CDD0F23699"), ClassInterface(ClassInterfaceType.None)]
|
||||||
|
[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
|
||||||
|
public class RdcStreamReader : IRdcFileReader
|
||||||
|
{
|
||||||
|
private readonly Stream stream;
|
||||||
|
|
||||||
|
/// <summary>Initializes a new instance of the <see cref="RdcStreamReader"/> class with stream to read.</summary>
|
||||||
|
/// <param name="stream">The stream to read from.</param>
|
||||||
|
public RdcStreamReader(Stream stream) => this.stream = stream;
|
||||||
|
|
||||||
|
private RdcStreamReader() { }
|
||||||
|
|
||||||
|
HRESULT IRdcFileReader.GetFilePosition(out ulong offsetFromStart)
|
||||||
|
{
|
||||||
|
offsetFromStart = (uint)stream.Position;
|
||||||
|
return HRESULT.S_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
HRESULT IRdcFileReader.GetFileSize(out ulong fileSize)
|
||||||
|
{
|
||||||
|
fileSize = (ulong)stream.Length;
|
||||||
|
return HRESULT.S_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
HRESULT IRdcFileReader.Read(ulong offsetFileStart, uint bytesToRead, out uint bytesActuallyRead, byte[] buffer, out bool eof)
|
||||||
|
{
|
||||||
|
if (stream.Position != (long)offsetFileStart)
|
||||||
|
{
|
||||||
|
stream.Seek((long)offsetFileStart, SeekOrigin.Begin);
|
||||||
|
}
|
||||||
|
|
||||||
|
var intBuff = new byte[bytesToRead];
|
||||||
|
int read = 0, lastRead;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
lastRead = stream.Read(intBuff, read, ((int)bytesToRead - read));
|
||||||
|
read += lastRead;
|
||||||
|
} while (lastRead != 0 && read < bytesToRead);
|
||||||
|
bytesActuallyRead = (uint)read;
|
||||||
|
Array.Copy(intBuff, buffer, (int)bytesToRead);
|
||||||
|
eof = read < bytesToRead;
|
||||||
|
return HRESULT.S_OK;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>CLSID_Similarity</summary>
|
/// <summary>CLSID_Similarity</summary>
|
||||||
[ComImport, Guid("96236A91-9DBC-11DA-9E3F-0011114AE311"), ClassInterface(ClassInterfaceType.None)]
|
[ComImport, Guid("96236A91-9DBC-11DA-9E3F-0011114AE311"), ClassInterface(ClassInterfaceType.None)]
|
||||||
[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
|
[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
|
||||||
|
|
|
@ -36,7 +36,7 @@ public class MsRdcTests
|
||||||
var gParamsCopy = gen.GetGeneratorParameters(MSRDC_MINIMUM_DEPTH);
|
var gParamsCopy = gen.GetGeneratorParameters(MSRDC_MINIMUM_DEPTH);
|
||||||
Assert.That(gParams.GetSerializeSize(), Is.EqualTo(gParamsCopy.GetSerializeSize()));
|
Assert.That(gParams.GetSerializeSize(), Is.EqualTo(gParamsCopy.GetSerializeSize()));
|
||||||
|
|
||||||
IRdcFileReader reader = new RdcFileReader(File.OpenRead(TestCaseSources.WordDoc));
|
IRdcFileReader reader = new RdcStreamReader(File.OpenRead(TestCaseSources.WordDoc));
|
||||||
//var comp = lib.CreateComparator(reader, maxdepth);
|
//var comp = lib.CreateComparator(reader, maxdepth);
|
||||||
var sigRead = lib.CreateSignatureReader(reader);
|
var sigRead = lib.CreateSignatureReader(reader);
|
||||||
//TestContext.WriteLine($"SigHdrRes={sigRead.ReadHeader()}");
|
//TestContext.WriteLine($"SigHdrRes={sigRead.ReadHeader()}");
|
||||||
|
@ -53,48 +53,3 @@ public class MsRdcTests
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[ClassInterface(ClassInterfaceType.None)]
|
|
||||||
[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
|
|
||||||
[ComVisible(true)]
|
|
||||||
[Guid("01EED492-3E92-4DF1-AC94-A7CDD0F23699")]
|
|
||||||
public class RdcFileReader : IRdcFileReader
|
|
||||||
{
|
|
||||||
private readonly Stream stream;
|
|
||||||
|
|
||||||
private RdcFileReader() { }
|
|
||||||
|
|
||||||
public RdcFileReader(Stream stream) => this.stream = stream;
|
|
||||||
|
|
||||||
HRESULT IRdcFileReader.GetFileSize(out ulong fileSize)
|
|
||||||
{
|
|
||||||
fileSize = (ulong)stream.Length;
|
|
||||||
return HRESULT.S_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
HRESULT IRdcFileReader.Read(ulong offsetFileStart, uint bytesToRead, out uint bytesActuallyRead, byte[] buffer, out bool eof)
|
|
||||||
{
|
|
||||||
if (stream.Position != (long)offsetFileStart)
|
|
||||||
{
|
|
||||||
stream.Seek((long)offsetFileStart, SeekOrigin.Begin);
|
|
||||||
}
|
|
||||||
|
|
||||||
var intBuff = new byte[bytesToRead];
|
|
||||||
int read = 0, lastRead;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
lastRead = stream.Read(intBuff, read, ((int)bytesToRead - read));
|
|
||||||
read += lastRead;
|
|
||||||
} while (lastRead != 0 && read < bytesToRead);
|
|
||||||
bytesActuallyRead = (uint)read;
|
|
||||||
Array.Copy(intBuff, buffer, (int)bytesToRead);
|
|
||||||
eof = read < bytesToRead;
|
|
||||||
return HRESULT.S_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
HRESULT IRdcFileReader.GetFilePosition(out ulong offsetFromStart)
|
|
||||||
{
|
|
||||||
offsetFromStart = (uint)stream.Position;
|
|
||||||
return HRESULT.S_OK;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue