diff --git a/Windows/CS/Framework4.0/Utils/Utils.ico b/Windows/CS/Framework4.0/Utils/Utils.ico new file mode 100644 index 0000000..e90ab1c Binary files /dev/null and b/Windows/CS/Framework4.0/Utils/Utils.ico differ diff --git a/Windows/CS/Framework4.0/Utils/Utils.sln b/Windows/CS/Framework4.0/Utils/Utils.sln new file mode 100644 index 0000000..9b8e42a --- /dev/null +++ b/Windows/CS/Framework4.0/Utils/Utils.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.36617.4 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils", "Utils\Utils.csproj", "{0AAA20B8-F8B1-4C09-ADBF-E6FD650EE9D4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0AAA20B8-F8B1-4C09-ADBF-E6FD650EE9D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0AAA20B8-F8B1-4C09-ADBF-E6FD650EE9D4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0AAA20B8-F8B1-4C09-ADBF-E6FD650EE9D4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0AAA20B8-F8B1-4C09-ADBF-E6FD650EE9D4}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7019BD3D-89E7-4B0D-85B6-9E26BD8EF151} + EndGlobalSection +EndGlobal diff --git a/Windows/CS/Framework4.0/Utils/Utils/Properties/AssemblyInfo.cs b/Windows/CS/Framework4.0/Utils/Utils/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..5a6fe04 --- /dev/null +++ b/Windows/CS/Framework4.0/Utils/Utils/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("Net40通用工具库")] +[assembly: AssemblyDescription("包含Base64、CRC编码等操作")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("JoyD")] +[assembly: AssemblyProduct("com.joyd.Utils")] +[assembly: AssemblyCopyright("Copyright © 2025 JoyD")] +[assembly: AssemblyTrademark("JoyD")] +[assembly: AssemblyCulture("zh-CN")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("0aaa20b8-f8b1-4c09-adbf-e6fd650ee9d4")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 +//通过使用 "*",如下所示: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Windows/CS/Framework4.0/Utils/Utils/Util.cs b/Windows/CS/Framework4.0/Utils/Utils/Util.cs new file mode 100644 index 0000000..aadc160 --- /dev/null +++ b/Windows/CS/Framework4.0/Utils/Utils/Util.cs @@ -0,0 +1,215 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace JoyD.Windows.CS +{ + /// + /// 通用工具和方法 + /// + public static class Util + { + // CRC16/XMODEM算法实现(带起始位置和长度) + public static ushort CalculateCRC16(byte[] data, int startIndex, int length) + { + ushort crc = 0; + int endIndex = startIndex + length; + for (int i = startIndex; i < endIndex && i < data.Length; i++) + { + crc ^= (ushort)(data[i] << 8); + for (int j = 0; j < 8; j++) + { + if ((crc & 0x8000) != 0) + crc = (ushort)((crc << 1) ^ 0x1021); + else + crc <<= 1; + } + } + return crc; + } + + + + // CRC32-MPEG2算法实现 + public static uint CalculateCRC32(byte[] data) + { + return CalculateCRC32(0xFFFFFFFF, data); + } + + // 支持增量计算的CRC32方法 + public static uint CalculateCRC32(uint crc, byte[] data) + { + uint polynomial = 0x04C11DB7; + + for (int i = 0; i < data.Length; i++) + { + crc ^= (uint)data[i] << 24; + for (int j = 0; j < 8; j++) + { + if ((crc & 0x80000000) != 0) + crc = (crc << 1) ^ polynomial; + else + crc <<= 1; + } + } + return crc; + } + + // 计算文件的CRC32(通过文件路径) + public static uint CalculateFileCRC32(string filePath) + { + using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) + { + return CalculateCRC32(fs); + } + } + + // 计算文件的CRC32(通过已打开的文件流) + public static uint CalculateCRC32(FileStream fs) + { + uint crc = 0xFFFFFFFF; + uint polynomial = 0x04C11DB7; + + // 保存当前位置 + long originalPosition = fs.Position; + + try + { + // 重置到文件开头 + fs.Seek(0, SeekOrigin.Begin); + + byte[] buffer = new byte[4096]; + int bytesRead; + + while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0) + { + for (int i = 0; i < bytesRead; i++) + { + crc ^= (uint)buffer[i] << 24; + for (int j = 0; j < 8; j++) + { + if ((crc & 0x80000000) != 0) + crc = (crc << 1) ^ polynomial; + else + crc <<= 1; + } + } + } + } + finally + { + // 恢复到原始位置 + fs.Seek(originalPosition, SeekOrigin.Begin); + } + return crc; + } + + // byte[]转base64编码 + public static string ByteArrayToBase64(byte[] data) + { + return Convert.ToBase64String(data); + } + + // base64转byte[]编码 + public static byte[] Base64ToByteArray(string base64) + { + return Convert.FromBase64String(base64); + } + + // 构建不含前缀的FTPC命令(返回byte[],直接处理字节流) + public static byte[] BuildRawFTPCommand(char command, params object[] parameters) + { + using (MemoryStream ms = new MemoryStream()) + { + // 写入命令(ASCII编码的单个字符) + ms.WriteByte((byte)command); + + // 添加参数(参数之间没有间隔,直接写入字节) + foreach (var param in parameters) + { + if (param is long) + { + // filesize24或addr24: 3字节大端序 + long value = (long)param; + + // 使用位运算直接获取24位值的三个字节,大端序 + byte byte1 = (byte)((value >> 16) & 0xFF); // 最高位字节 + byte byte2 = (byte)((value >> 8) & 0xFF); // 中间字节 + byte byte3 = (byte)(value & 0xFF); // 最低位字节 + + // 直接写入三个字节 + ms.WriteByte(byte1); + ms.WriteByte(byte2); + ms.WriteByte(byte3); + } + else if (param is ushort) + { + // N16: 16位大端序数据长度 + ushort value = (ushort)param; + + // 使用位运算直接获取16位值的两个字节,大端序 + byte byte1 = (byte)((value >> 8) & 0xFF); // 高8位字节 + byte byte2 = (byte)(value & 0xFF); // 低8位字节 + + // 直接写入两个字节 + ms.WriteByte(byte1); + ms.WriteByte(byte2); + } + else if (param is uint) + { + // 处理uint类型(如CRC32值) + uint value = (uint)param; + + // 使用位运算直接获取32位值的四个字节,大端序 + byte byte1 = (byte)((value >> 24) & 0xFF); // 最高位字节 + byte byte2 = (byte)((value >> 16) & 0xFF); // 次高位字节 + byte byte3 = (byte)((value >> 8) & 0xFF); // 次低位字节 + byte byte4 = (byte)(value & 0xFF); // 最低位字节 + + // 直接写入四个字节 + ms.WriteByte(byte1); + ms.WriteByte(byte2); + ms.WriteByte(byte3); + ms.WriteByte(byte4); + } + else if (param is string) + { + // 字符串参数直接写入ASCII字节 + string strValue = (string)param; + byte[] strBytes = Encoding.ASCII.GetBytes(strValue); + ms.Write(strBytes, 0, strBytes.Length); + } + else if (param is byte[]) + { + // byte[]参数直接写入 + byte[] data = (byte[])param; + ms.Write(data, 0, data.Length); + } + } + + // 获取当前流中的所有字节,用于计算CRC16 + byte[] currentBytes = ms.ToArray(); + + // 计算CRC16 + ushort crc16 = CalculateCRC16(currentBytes, 0, currentBytes.Length); + + // 写入CRC16(大端序) + byte crcByte1 = (byte)((crc16 >> 8) & 0xFF); // 高8位 + byte crcByte2 = (byte)(crc16 & 0xFF); // 低8位 + ms.WriteByte(crcByte1); + ms.WriteByte(crcByte2); + + // 返回完整的字节数组 + return ms.ToArray(); + } + } + + // 构建完整通信命令(接受byte[]输入) + public static string BuildCommunicationCommand(byte[] rawFTPCommand) + { + string base64 = Convert.ToBase64String(rawFTPCommand); + return $"set(LFMGR: FTPC, \"{base64}\")"; + } + } +} diff --git a/Windows/CS/Framework4.0/Utils/Utils/Utils.csproj b/Windows/CS/Framework4.0/Utils/Utils/Utils.csproj new file mode 100644 index 0000000..2fcb5cf --- /dev/null +++ b/Windows/CS/Framework4.0/Utils/Utils/Utils.csproj @@ -0,0 +1,54 @@ + + + + + Debug + AnyCPU + {0AAA20B8-F8B1-4C09-ADBF-E6FD650EE9D4} + Library + Properties + JoyD.Windows.CS + Utils + v4.0 + 512 + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + bin\Debug\Utils.xml + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + Utils.ico + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Windows/CS/Framework4.0/Utils/Utils/Utils.ico b/Windows/CS/Framework4.0/Utils/Utils/Utils.ico new file mode 100644 index 0000000..e90ab1c Binary files /dev/null and b/Windows/CS/Framework4.0/Utils/Utils/Utils.ico differ