mirror of
https://github.com/lionsoul2014/ip2region.git
synced 2025-12-08 19:25:22 +00:00
C# 版
This commit is contained in:
parent
791e81efe4
commit
0bd2c15c56
337
binding/csharp/.gitignore
vendored
Normal file
337
binding/csharp/.gitignore
vendored
Normal file
@ -0,0 +1,337 @@
|
|||||||
|
## Ignore Visual Studio temporary files, build results, and
|
||||||
|
## files generated by popular Visual Studio add-ons.
|
||||||
|
##
|
||||||
|
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
|
||||||
|
|
||||||
|
# User-specific files
|
||||||
|
*.rsuser
|
||||||
|
*.suo
|
||||||
|
*.user
|
||||||
|
*.userosscache
|
||||||
|
*.sln.docstates
|
||||||
|
|
||||||
|
# User-specific files (MonoDevelop/Xamarin Studio)
|
||||||
|
*.userprefs
|
||||||
|
|
||||||
|
# Build results
|
||||||
|
[Dd]ebug/
|
||||||
|
[Dd]ebugPublic/
|
||||||
|
[Rr]elease/
|
||||||
|
[Rr]eleases/
|
||||||
|
x64/
|
||||||
|
x86/
|
||||||
|
[Aa][Rr][Mm]/
|
||||||
|
[Aa][Rr][Mm]64/
|
||||||
|
bld/
|
||||||
|
[Bb]in/
|
||||||
|
[Oo]bj/
|
||||||
|
[Ll]og/
|
||||||
|
|
||||||
|
# Visual Studio 2015/2017 cache/options directory
|
||||||
|
.vs/
|
||||||
|
# Uncomment if you have tasks that create the project's static files in wwwroot
|
||||||
|
#wwwroot/
|
||||||
|
|
||||||
|
# Visual Studio 2017 auto generated files
|
||||||
|
Generated\ Files/
|
||||||
|
|
||||||
|
# MSTest test Results
|
||||||
|
[Tt]est[Rr]esult*/
|
||||||
|
[Bb]uild[Ll]og.*
|
||||||
|
|
||||||
|
# NUNIT
|
||||||
|
*.VisualState.xml
|
||||||
|
TestResult.xml
|
||||||
|
|
||||||
|
# Build Results of an ATL Project
|
||||||
|
[Dd]ebugPS/
|
||||||
|
[Rr]eleasePS/
|
||||||
|
dlldata.c
|
||||||
|
|
||||||
|
# Benchmark Results
|
||||||
|
BenchmarkDotNet.Artifacts/
|
||||||
|
|
||||||
|
# .NET Core
|
||||||
|
project.lock.json
|
||||||
|
project.fragment.lock.json
|
||||||
|
artifacts/
|
||||||
|
|
||||||
|
# StyleCop
|
||||||
|
StyleCopReport.xml
|
||||||
|
|
||||||
|
# Files built by Visual Studio
|
||||||
|
*_i.c
|
||||||
|
*_p.c
|
||||||
|
*_h.h
|
||||||
|
*.ilk
|
||||||
|
*.meta
|
||||||
|
*.obj
|
||||||
|
*.iobj
|
||||||
|
*.pch
|
||||||
|
*.pdb
|
||||||
|
*.ipdb
|
||||||
|
*.pgc
|
||||||
|
*.pgd
|
||||||
|
*.rsp
|
||||||
|
*.sbr
|
||||||
|
*.tlb
|
||||||
|
*.tli
|
||||||
|
*.tlh
|
||||||
|
*.tmp
|
||||||
|
*.tmp_proj
|
||||||
|
*_wpftmp.csproj
|
||||||
|
*.log
|
||||||
|
*.vspscc
|
||||||
|
*.vssscc
|
||||||
|
.builds
|
||||||
|
*.pidb
|
||||||
|
*.svclog
|
||||||
|
*.scc
|
||||||
|
|
||||||
|
# Chutzpah Test files
|
||||||
|
_Chutzpah*
|
||||||
|
|
||||||
|
# Visual C++ cache files
|
||||||
|
ipch/
|
||||||
|
*.aps
|
||||||
|
*.ncb
|
||||||
|
*.opendb
|
||||||
|
*.opensdf
|
||||||
|
*.sdf
|
||||||
|
*.cachefile
|
||||||
|
*.VC.db
|
||||||
|
*.VC.VC.opendb
|
||||||
|
|
||||||
|
# Visual Studio profiler
|
||||||
|
*.psess
|
||||||
|
*.vsp
|
||||||
|
*.vspx
|
||||||
|
*.sap
|
||||||
|
|
||||||
|
# Visual Studio Trace Files
|
||||||
|
*.e2e
|
||||||
|
|
||||||
|
# TFS 2012 Local Workspace
|
||||||
|
$tf/
|
||||||
|
|
||||||
|
# Guidance Automation Toolkit
|
||||||
|
*.gpState
|
||||||
|
|
||||||
|
# ReSharper is a .NET coding add-in
|
||||||
|
_ReSharper*/
|
||||||
|
*.[Rr]e[Ss]harper
|
||||||
|
*.DotSettings.user
|
||||||
|
|
||||||
|
# JustCode is a .NET coding add-in
|
||||||
|
.JustCode
|
||||||
|
|
||||||
|
# TeamCity is a build add-in
|
||||||
|
_TeamCity*
|
||||||
|
|
||||||
|
# DotCover is a Code Coverage Tool
|
||||||
|
*.dotCover
|
||||||
|
|
||||||
|
# AxoCover is a Code Coverage Tool
|
||||||
|
.axoCover/*
|
||||||
|
!.axoCover/settings.json
|
||||||
|
|
||||||
|
# Visual Studio code coverage results
|
||||||
|
*.coverage
|
||||||
|
*.coveragexml
|
||||||
|
|
||||||
|
# NCrunch
|
||||||
|
_NCrunch_*
|
||||||
|
.*crunch*.local.xml
|
||||||
|
nCrunchTemp_*
|
||||||
|
|
||||||
|
# MightyMoose
|
||||||
|
*.mm.*
|
||||||
|
AutoTest.Net/
|
||||||
|
|
||||||
|
# Web workbench (sass)
|
||||||
|
.sass-cache/
|
||||||
|
|
||||||
|
# Installshield output folder
|
||||||
|
[Ee]xpress/
|
||||||
|
|
||||||
|
# DocProject is a documentation generator add-in
|
||||||
|
DocProject/buildhelp/
|
||||||
|
DocProject/Help/*.HxT
|
||||||
|
DocProject/Help/*.HxC
|
||||||
|
DocProject/Help/*.hhc
|
||||||
|
DocProject/Help/*.hhk
|
||||||
|
DocProject/Help/*.hhp
|
||||||
|
DocProject/Help/Html2
|
||||||
|
DocProject/Help/html
|
||||||
|
|
||||||
|
# Click-Once directory
|
||||||
|
publish/
|
||||||
|
|
||||||
|
# Publish Web Output
|
||||||
|
*.[Pp]ublish.xml
|
||||||
|
*.azurePubxml
|
||||||
|
# Note: Comment the next line if you want to checkin your web deploy settings,
|
||||||
|
# but database connection strings (with potential passwords) will be unencrypted
|
||||||
|
*.pubxml
|
||||||
|
*.publishproj
|
||||||
|
|
||||||
|
# Microsoft Azure Web App publish settings. Comment the next line if you want to
|
||||||
|
# checkin your Azure Web App publish settings, but sensitive information contained
|
||||||
|
# in these scripts will be unencrypted
|
||||||
|
PublishScripts/
|
||||||
|
|
||||||
|
# NuGet Packages
|
||||||
|
*.nupkg
|
||||||
|
# The packages folder can be ignored because of Package Restore
|
||||||
|
**/[Pp]ackages/*
|
||||||
|
# except build/, which is used as an MSBuild target.
|
||||||
|
!**/[Pp]ackages/build/
|
||||||
|
# Uncomment if necessary however generally it will be regenerated when needed
|
||||||
|
#!**/[Pp]ackages/repositories.config
|
||||||
|
# NuGet v3's project.json files produces more ignorable files
|
||||||
|
*.nuget.props
|
||||||
|
*.nuget.targets
|
||||||
|
|
||||||
|
# Microsoft Azure Build Output
|
||||||
|
csx/
|
||||||
|
*.build.csdef
|
||||||
|
|
||||||
|
# Microsoft Azure Emulator
|
||||||
|
ecf/
|
||||||
|
rcf/
|
||||||
|
|
||||||
|
# Windows Store app package directories and files
|
||||||
|
AppPackages/
|
||||||
|
BundleArtifacts/
|
||||||
|
Package.StoreAssociation.xml
|
||||||
|
_pkginfo.txt
|
||||||
|
*.appx
|
||||||
|
|
||||||
|
# Visual Studio cache files
|
||||||
|
# files ending in .cache can be ignored
|
||||||
|
*.[Cc]ache
|
||||||
|
# but keep track of directories ending in .cache
|
||||||
|
!?*.[Cc]ache/
|
||||||
|
|
||||||
|
# Others
|
||||||
|
ClientBin/
|
||||||
|
~$*
|
||||||
|
*~
|
||||||
|
*.dbmdl
|
||||||
|
*.dbproj.schemaview
|
||||||
|
*.jfm
|
||||||
|
*.pfx
|
||||||
|
*.publishsettings
|
||||||
|
orleans.codegen.cs
|
||||||
|
|
||||||
|
# Including strong name files can present a security risk
|
||||||
|
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
|
||||||
|
#*.snk
|
||||||
|
|
||||||
|
# Since there are multiple workflows, uncomment next line to ignore bower_components
|
||||||
|
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
|
||||||
|
#bower_components/
|
||||||
|
|
||||||
|
# RIA/Silverlight projects
|
||||||
|
Generated_Code/
|
||||||
|
|
||||||
|
# Backup & report files from converting an old project file
|
||||||
|
# to a newer Visual Studio version. Backup files are not needed,
|
||||||
|
# because we have git ;-)
|
||||||
|
_UpgradeReport_Files/
|
||||||
|
Backup*/
|
||||||
|
UpgradeLog*.XML
|
||||||
|
UpgradeLog*.htm
|
||||||
|
ServiceFabricBackup/
|
||||||
|
*.rptproj.bak
|
||||||
|
|
||||||
|
# SQL Server files
|
||||||
|
*.mdf
|
||||||
|
*.ldf
|
||||||
|
*.ndf
|
||||||
|
|
||||||
|
# Business Intelligence projects
|
||||||
|
*.rdl.data
|
||||||
|
*.bim.layout
|
||||||
|
*.bim_*.settings
|
||||||
|
*.rptproj.rsuser
|
||||||
|
*- Backup*.rdl
|
||||||
|
|
||||||
|
# Microsoft Fakes
|
||||||
|
FakesAssemblies/
|
||||||
|
|
||||||
|
# GhostDoc plugin setting file
|
||||||
|
*.GhostDoc.xml
|
||||||
|
|
||||||
|
# Node.js Tools for Visual Studio
|
||||||
|
.ntvs_analysis.dat
|
||||||
|
node_modules/
|
||||||
|
|
||||||
|
# Visual Studio 6 build log
|
||||||
|
*.plg
|
||||||
|
|
||||||
|
# Visual Studio 6 workspace options file
|
||||||
|
*.opt
|
||||||
|
|
||||||
|
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
|
||||||
|
*.vbw
|
||||||
|
|
||||||
|
# Visual Studio LightSwitch build output
|
||||||
|
**/*.HTMLClient/GeneratedArtifacts
|
||||||
|
**/*.DesktopClient/GeneratedArtifacts
|
||||||
|
**/*.DesktopClient/ModelManifest.xml
|
||||||
|
**/*.Server/GeneratedArtifacts
|
||||||
|
**/*.Server/ModelManifest.xml
|
||||||
|
_Pvt_Extensions
|
||||||
|
|
||||||
|
# Paket dependency manager
|
||||||
|
.paket/paket.exe
|
||||||
|
paket-files/
|
||||||
|
|
||||||
|
# FAKE - F# Make
|
||||||
|
.fake/
|
||||||
|
|
||||||
|
# JetBrains Rider
|
||||||
|
.idea/
|
||||||
|
*.sln.iml
|
||||||
|
|
||||||
|
# CodeRush personal settings
|
||||||
|
.cr/personal
|
||||||
|
|
||||||
|
# Python Tools for Visual Studio (PTVS)
|
||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
|
|
||||||
|
# Cake - Uncomment if you are using it
|
||||||
|
# tools/**
|
||||||
|
# !tools/packages.config
|
||||||
|
|
||||||
|
# Tabs Studio
|
||||||
|
*.tss
|
||||||
|
|
||||||
|
# Telerik's JustMock configuration file
|
||||||
|
*.jmconfig
|
||||||
|
|
||||||
|
# BizTalk build output
|
||||||
|
*.btp.cs
|
||||||
|
*.btm.cs
|
||||||
|
*.odx.cs
|
||||||
|
*.xsd.cs
|
||||||
|
|
||||||
|
# OpenCover UI analysis results
|
||||||
|
OpenCover/
|
||||||
|
|
||||||
|
# Azure Stream Analytics local run output
|
||||||
|
ASALocalRun/
|
||||||
|
|
||||||
|
# MSBuild Binary and Structured Log
|
||||||
|
*.binlog
|
||||||
|
|
||||||
|
# NVidia Nsight GPU debugger configuration file
|
||||||
|
*.nvuser
|
||||||
|
|
||||||
|
# MFractors (Xamarin productivity tool) working folder
|
||||||
|
.mfractor/
|
||||||
|
|
||||||
|
# Local History for Visual Studio
|
||||||
|
.localhistory/
|
||||||
7
binding/csharp/IP2Region/IP2Region.csproj
Normal file
7
binding/csharp/IP2Region/IP2Region.csproj
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
40
binding/csharp/IP2Region/xdb/Header.cs
Normal file
40
binding/csharp/IP2Region/xdb/Header.cs
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace IP2Region.xdb
|
||||||
|
{
|
||||||
|
public class Header
|
||||||
|
{
|
||||||
|
public int Version { get; }
|
||||||
|
public int IndexPolicy { get; }
|
||||||
|
public int CreatedAt { get; }
|
||||||
|
public int StartIndexPtr { get; }
|
||||||
|
public int EndIndexPtr { get; }
|
||||||
|
public byte[] Buffer { get; }
|
||||||
|
public Header(byte[] buff)
|
||||||
|
{
|
||||||
|
if (buff == null) throw new ArgumentNullException(nameof(buff));
|
||||||
|
if (buff.Length < 16) throw new ArgumentOutOfRangeException(nameof(buff));
|
||||||
|
Version = Searcher.GetInt2(buff, 0);
|
||||||
|
IndexPolicy = Searcher.GetInt2(buff, 2);
|
||||||
|
CreatedAt = Searcher.GetInt(buff, 4);
|
||||||
|
StartIndexPtr = Searcher.GetInt(buff, 8);
|
||||||
|
EndIndexPtr = Searcher.GetInt(buff, 12);
|
||||||
|
Buffer = buff;
|
||||||
|
|
||||||
|
}
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return "{" +
|
||||||
|
"Version: " + Version + ',' +
|
||||||
|
"IndexPolicy: " + IndexPolicy + ',' +
|
||||||
|
"CreatedAt: " + CreatedAt + ',' +
|
||||||
|
"StartIndexPtr: " + StartIndexPtr + ',' +
|
||||||
|
"EndIndexPtr: " + EndIndexPtr +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
268
binding/csharp/IP2Region/xdb/Searcher.cs
Normal file
268
binding/csharp/IP2Region/xdb/Searcher.cs
Normal file
@ -0,0 +1,268 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace IP2Region.xdb
|
||||||
|
{
|
||||||
|
public class Searcher
|
||||||
|
{
|
||||||
|
|
||||||
|
// constant defined copied from the xdb maker
|
||||||
|
public static int HeaderInfoLength = 256;
|
||||||
|
public static int VectorIndexRows = 256;
|
||||||
|
public static int VectorIndexCols = 256;
|
||||||
|
public static int VectorIndexSize = 8;
|
||||||
|
public static int SegmentIndexSize = 14;
|
||||||
|
|
||||||
|
// random access file handle for file based search
|
||||||
|
private readonly Stream handle;
|
||||||
|
|
||||||
|
private int ioCount = 0;
|
||||||
|
|
||||||
|
public int IOCount => this.ioCount;
|
||||||
|
|
||||||
|
|
||||||
|
// vector index.
|
||||||
|
// use the byte[] instead of VectorIndex entry array to keep
|
||||||
|
// the minimal memory allocation.
|
||||||
|
private readonly byte[] vectorIndex;
|
||||||
|
|
||||||
|
// xdb content buffer, used for in-memory search
|
||||||
|
private readonly byte[] contentBuff;
|
||||||
|
|
||||||
|
|
||||||
|
// --- static method to create searchers
|
||||||
|
|
||||||
|
|
||||||
|
public static Searcher NewWithFileOnly(String dbPath)
|
||||||
|
{
|
||||||
|
return new Searcher(dbPath, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Searcher NewWithVectorIndex(String dbPath, byte[] vectorIndex)
|
||||||
|
{
|
||||||
|
return new Searcher(dbPath, vectorIndex, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Searcher NewWithBuffer(byte[] cBuff)
|
||||||
|
{
|
||||||
|
return new Searcher(null, null, cBuff);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- End of creator
|
||||||
|
public Searcher(string dbFile, byte[] vectorIndex, byte[] cBuff)
|
||||||
|
{
|
||||||
|
if (cBuff != null)
|
||||||
|
{
|
||||||
|
this.handle = null;
|
||||||
|
this.vectorIndex = null;
|
||||||
|
this.contentBuff = cBuff;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.handle = File.OpenRead(dbFile);
|
||||||
|
this.vectorIndex = vectorIndex;
|
||||||
|
this.contentBuff = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Close()
|
||||||
|
{
|
||||||
|
if (this.handle != null) this.handle.Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Search(string ipStr)
|
||||||
|
{
|
||||||
|
var ip = checkIP(ipStr);
|
||||||
|
return Search(ip);
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Search(long ip)
|
||||||
|
{
|
||||||
|
// reset the global counter
|
||||||
|
this.ioCount = 0;
|
||||||
|
// locate the segment index block based on the vector index
|
||||||
|
int sPtr = 0, ePtr = 0;
|
||||||
|
int il0 = (int)((ip >> 24) & 0xFF);
|
||||||
|
int il1 = (int)((ip >> 16) & 0xFF);
|
||||||
|
int idx = il0 * VectorIndexCols * VectorIndexSize + il1 * VectorIndexSize;
|
||||||
|
if (vectorIndex != null)
|
||||||
|
{
|
||||||
|
sPtr = GetInt(vectorIndex, idx);
|
||||||
|
ePtr = GetInt(vectorIndex, idx + 4);
|
||||||
|
}
|
||||||
|
else if (contentBuff != null)
|
||||||
|
{
|
||||||
|
sPtr = GetInt(contentBuff, HeaderInfoLength + idx);
|
||||||
|
ePtr = GetInt(contentBuff, HeaderInfoLength + idx + 4);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
byte[] vectorBuff = new byte[VectorIndexSize];
|
||||||
|
Read(HeaderInfoLength + idx, vectorBuff);
|
||||||
|
sPtr = GetInt(vectorBuff, 0);
|
||||||
|
ePtr = GetInt(vectorBuff, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
// binary search the segment index block to get the region info
|
||||||
|
byte[] buff = new byte[SegmentIndexSize];
|
||||||
|
int dataLen = -1, dataPtr = -1;
|
||||||
|
int l = 0, h = (ePtr - sPtr) / SegmentIndexSize;
|
||||||
|
while (l <= h)
|
||||||
|
{
|
||||||
|
int m = (l + h) >> 1;
|
||||||
|
int p = sPtr + m * SegmentIndexSize;
|
||||||
|
|
||||||
|
// read the segment index
|
||||||
|
Read(p, buff);
|
||||||
|
long sip = GetIntLong(buff, 0);
|
||||||
|
if (ip < sip)
|
||||||
|
{
|
||||||
|
h = m - 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
long eip = GetIntLong(buff, 4);
|
||||||
|
if (ip > eip)
|
||||||
|
{
|
||||||
|
l = m + 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
dataLen = GetInt2(buff, 8);
|
||||||
|
dataPtr = GetInt(buff, 10);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// empty match interception
|
||||||
|
// System.out.printf("dataLen: %d, dataPtr: %d\n", dataLen, dataPtr);
|
||||||
|
if (dataPtr < 0) return null;
|
||||||
|
|
||||||
|
// load and return the region data
|
||||||
|
byte[] regionBuff = new byte[dataLen];
|
||||||
|
Read(dataPtr, regionBuff);
|
||||||
|
//return new String(regionBuff, "utf-8");
|
||||||
|
return Encoding.UTF8.GetString(regionBuff);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void Read(int offset, byte[] buffer)
|
||||||
|
{
|
||||||
|
// check the in-memory buffer first
|
||||||
|
if (contentBuff != null)
|
||||||
|
{
|
||||||
|
// @TODO: reduce data copying, directly decode the data ?
|
||||||
|
//System.arraycopy(contentBuff, offset, buffer, 0, buffer.length);
|
||||||
|
Array.Copy(contentBuff, offset, buffer, 0, buffer.Length);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// read from the file handle
|
||||||
|
if (handle == null) throw new ArgumentNullException(nameof(handle));
|
||||||
|
handle.Seek(offset, SeekOrigin.Begin);
|
||||||
|
|
||||||
|
this.ioCount++;
|
||||||
|
var rLen = handle.Read(buffer, 0, buffer.Length);
|
||||||
|
if (rLen != buffer.Length) throw new IOException("incomplete read: read bytes should be " + buffer.Length);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- static cache util function
|
||||||
|
public static Header LoadHeader(Stream stream)
|
||||||
|
{
|
||||||
|
stream.Seek(0, SeekOrigin.Begin);
|
||||||
|
var buffer = new byte[HeaderInfoLength];
|
||||||
|
stream.Read(buffer, 0, HeaderInfoLength);
|
||||||
|
return new Header(buffer);
|
||||||
|
}
|
||||||
|
public static Header LoadHeaderFromFile(string dbPath)
|
||||||
|
{
|
||||||
|
using (var fs = File.OpenRead(dbPath)) return LoadHeader(fs);
|
||||||
|
}
|
||||||
|
public static byte[] LoadVectorIndex(Stream stream)
|
||||||
|
{
|
||||||
|
stream.Seek(HeaderInfoLength, SeekOrigin.Begin);
|
||||||
|
int len = VectorIndexRows * VectorIndexCols * SegmentIndexSize;
|
||||||
|
var buff = new byte[len];
|
||||||
|
var rLen = stream.Read(buff, 0, buff.Length);
|
||||||
|
if (rLen != len) throw new IOException("incomplete read: read bytes should be " + len);
|
||||||
|
return buff;
|
||||||
|
}
|
||||||
|
public static byte[] LoadVectorIndexFromFile(string dbPath)
|
||||||
|
{
|
||||||
|
using (var fs = File.OpenRead(dbPath)) return LoadVectorIndex(fs);
|
||||||
|
}
|
||||||
|
public static byte[] LoadContent(Stream stream)
|
||||||
|
{
|
||||||
|
stream.Seek(0, SeekOrigin.Begin);
|
||||||
|
using (var ms = new MemoryStream())
|
||||||
|
{
|
||||||
|
stream.CopyTo(ms);
|
||||||
|
return ms.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] LoadContentFromFile(string dbPath)
|
||||||
|
{
|
||||||
|
using (var fs = File.OpenRead(dbPath)) return LoadContent(fs);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int GetInt2(byte[] b, int offset)
|
||||||
|
{
|
||||||
|
return (
|
||||||
|
(b[offset++] & 0x000000FF) |
|
||||||
|
(b[offset] & 0x0000FF00)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
public static int GetInt(byte[] b, int offset)
|
||||||
|
{
|
||||||
|
return (
|
||||||
|
((b[offset++]) & 0x000000FF) |
|
||||||
|
((b[offset++] << 8) & 0x0000FF00) |
|
||||||
|
((b[offset++] << 16) & 0x00FF0000) |
|
||||||
|
(int)((b[offset] << 24) & 0xFF000000)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
public static long GetIntLong(byte[] b, int offset)
|
||||||
|
{
|
||||||
|
return (
|
||||||
|
((b[offset++] & 0x000000FFL)) |
|
||||||
|
((b[offset++] << 8) & 0x0000FF00L) |
|
||||||
|
((b[offset++] << 16) & 0x00FF0000L) |
|
||||||
|
((b[offset] << 24) & 0xFF000000L)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* long int to ip string */
|
||||||
|
public static string Long2ip(long ip)
|
||||||
|
{
|
||||||
|
return string.Join(".", (ip >> 24) & 0xFF, (ip >> 16) & 0xFF, (ip >> 8) & 0xFF, (ip) & 0xFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] shiftIndex = { 24, 16, 8, 0 };
|
||||||
|
|
||||||
|
/* check the specified ip address */
|
||||||
|
public static long checkIP(String ip)
|
||||||
|
{
|
||||||
|
String[]
|
||||||
|
ps = ip.Split(".");
|
||||||
|
if (ps.Length != 4) throw new Exception("invalid ip address `" + ip + "`");
|
||||||
|
|
||||||
|
long ipDst = 0;
|
||||||
|
for (int i = 0; i < ps.Length; i++)
|
||||||
|
{
|
||||||
|
int val = Convert.ToInt32(ps[i]);
|
||||||
|
if (val > 255)
|
||||||
|
{
|
||||||
|
throw new Exception("ip part `" + ps[i] + "` should be less then 256");
|
||||||
|
}
|
||||||
|
ipDst |= ((long)val << shiftIndex[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ipDst & 0xFFFFFFFFL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user