支持发布到仓库
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
@@ -8,9 +9,15 @@ namespace JoyD.Windows.CS
|
||||
/// <summary>
|
||||
/// 通用工具和方法
|
||||
/// </summary>
|
||||
public static class Util
|
||||
public static class Utils
|
||||
{
|
||||
// CRC16/XMODEM算法实现(带起始位置和长度)
|
||||
/// <summary>
|
||||
/// 使用CRC16/XMODEM算法计算字节数组的CRC16校验值
|
||||
/// </summary>
|
||||
/// <param name="data">要计算CRC16的数据字节数组</param>
|
||||
/// <param name="startIndex">计算的起始位置索引</param>
|
||||
/// <param name="length">要计算的字节长度</param>
|
||||
/// <returns>计算得到的CRC16校验值</returns>
|
||||
public static ushort CalculateCRC16(byte[] data, int startIndex, int length)
|
||||
{
|
||||
ushort crc = 0;
|
||||
@@ -31,13 +38,22 @@ namespace JoyD.Windows.CS
|
||||
|
||||
|
||||
|
||||
// CRC32-MPEG2算法实现
|
||||
/// <summary>
|
||||
/// 使用CRC32-MPEG2算法计算字节数组的CRC32校验值
|
||||
/// </summary>
|
||||
/// <param name="data">要计算CRC32的数据字节数组</param>
|
||||
/// <returns>计算得到的CRC32校验值</returns>
|
||||
public static uint CalculateCRC32(byte[] data)
|
||||
{
|
||||
return CalculateCRC32(0xFFFFFFFF, data);
|
||||
}
|
||||
|
||||
// 支持增量计算的CRC32方法
|
||||
/// <summary>
|
||||
/// 支持增量计算的CRC32-MPEG2算法实现
|
||||
/// </summary>
|
||||
/// <param name="crc">初始CRC值(用于增量计算)</param>
|
||||
/// <param name="data">要计算CRC32的数据字节数组</param>
|
||||
/// <returns>计算得到的CRC32校验值</returns>
|
||||
public static uint CalculateCRC32(uint crc, byte[] data)
|
||||
{
|
||||
uint polynomial = 0x04C11DB7;
|
||||
@@ -56,7 +72,14 @@ namespace JoyD.Windows.CS
|
||||
return crc;
|
||||
}
|
||||
|
||||
// 计算文件的CRC32(通过文件路径)
|
||||
/// <summary>
|
||||
/// 通过文件路径计算文件的CRC32校验值
|
||||
/// </summary>
|
||||
/// <param name="filePath">文件的完整路径</param>
|
||||
/// <returns>计算得到的CRC32校验值</returns>
|
||||
/// <exception cref="ArgumentException">当文件路径为空或无效时抛出</exception>
|
||||
/// <exception cref="FileNotFoundException">当指定路径的文件不存在时抛出</exception>
|
||||
/// <exception cref="IOException">当文件读取失败时抛出</exception>
|
||||
public static uint CalculateFileCRC32(string filePath)
|
||||
{
|
||||
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
|
||||
@@ -65,7 +88,13 @@ namespace JoyD.Windows.CS
|
||||
}
|
||||
}
|
||||
|
||||
// 计算文件的CRC32(通过已打开的文件流)
|
||||
/// <summary>
|
||||
/// 通过已打开的文件流计算文件的CRC32校验值
|
||||
/// </summary>
|
||||
/// <param name="fs">已打开的文件流</param>
|
||||
/// <returns>计算得到的CRC32校验值</returns>
|
||||
/// <exception cref="ArgumentNullException">当文件流为null时抛出</exception>
|
||||
/// <exception cref="IOException">当文件读取失败时抛出</exception>
|
||||
public static uint CalculateCRC32(FileStream fs)
|
||||
{
|
||||
uint crc = 0xFFFFFFFF;
|
||||
@@ -105,19 +134,44 @@ namespace JoyD.Windows.CS
|
||||
return crc;
|
||||
}
|
||||
|
||||
// byte[]转base64编码
|
||||
/// <summary>
|
||||
/// 将字节数组转换为Base64编码的字符串
|
||||
/// </summary>
|
||||
/// <param name="data">要转换的字节数组</param>
|
||||
/// <returns>Base64编码的字符串</returns>
|
||||
/// <exception cref="ArgumentNullException">当输入数据为null时抛出</exception>
|
||||
public static string ByteArrayToBase64(byte[] data)
|
||||
{
|
||||
return Convert.ToBase64String(data);
|
||||
}
|
||||
|
||||
// base64转byte[]编码
|
||||
/// <summary>
|
||||
/// 将Base64编码的字符串转换为字节数组
|
||||
/// </summary>
|
||||
/// <param name="base64">Base64编码的字符串</param>
|
||||
/// <returns>转换后的字节数组</returns>
|
||||
/// <exception cref="ArgumentNullException">当输入字符串为null时抛出</exception>
|
||||
/// <exception cref="FormatException">当输入字符串不是有效的Base64格式时抛出</exception>
|
||||
public static byte[] Base64ToByteArray(string base64)
|
||||
{
|
||||
return Convert.FromBase64String(base64);
|
||||
}
|
||||
|
||||
// 构建不含前缀的FTPC命令(返回byte[],直接处理字节流)
|
||||
/// <summary>
|
||||
/// 构建不含前缀的FTPC命令字节流
|
||||
/// </summary>
|
||||
/// <param name="command">命令字符(ASCII编码)</param>
|
||||
/// <param name="parameters">命令参数,支持以下类型:
|
||||
/// <list type="bullet">
|
||||
/// <item><description>long: 24位大端序数据(如filesize24或addr24)</description></item>
|
||||
/// <item><description>ushort: 16位大端序数据长度</description></item>
|
||||
/// <item><description>uint: 32位大端序数据(如CRC32值)</description></item>
|
||||
/// <item><description>string: ASCII编码的字符串参数</description></item>
|
||||
/// <item><description>byte[]: 原始字节数组参数</description></item>
|
||||
/// </list>
|
||||
/// </param>
|
||||
/// <returns>包含命令、参数和CRC16校验的完整字节数组</returns>
|
||||
/// <exception cref="ArgumentException">当参数类型不支持时抛出</exception>
|
||||
public static byte[] BuildRawFTPCommand(char command, params object[] parameters)
|
||||
{
|
||||
using (MemoryStream ms = new MemoryStream())
|
||||
@@ -205,7 +259,12 @@ namespace JoyD.Windows.CS
|
||||
}
|
||||
}
|
||||
|
||||
// 构建完整通信命令(接受byte[]输入)
|
||||
/// <summary>
|
||||
/// 构建完整的通信命令字符串
|
||||
/// </summary>
|
||||
/// <param name="rawFTPCommand">通过BuildRawFTPCommand方法构建的原始FTPC命令字节流</param>
|
||||
/// <returns>完整的通信命令字符串,格式为:set(LFMGR: FTPC, "base64_encoded_command")</returns>
|
||||
/// <exception cref="ArgumentNullException">当输入命令字节流为null时抛出</exception>
|
||||
public static string BuildCommunicationCommand(byte[] rawFTPCommand)
|
||||
{
|
||||
string base64 = Convert.ToBase64String(rawFTPCommand);
|
||||
@@ -8,10 +8,11 @@
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>JoyD.Windows.CS</RootNamespace>
|
||||
<AssemblyName>Utils</AssemblyName>
|
||||
<AssemblyName>JoyD.Windows.CS.Utils</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<Deterministic>true</Deterministic>
|
||||
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
@@ -21,7 +22,7 @@
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<DocumentationFile>bin\Debug\Utils.xml</DocumentationFile>
|
||||
<DocumentationFile>bin\Debug\JoyD.Windows.CS.Utils.xml</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
@@ -45,7 +46,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Util.cs" />
|
||||
<Compile Include="Utils.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Utils.ico" />
|
||||
|
||||
Reference in New Issue
Block a user