开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 147|回复: 1
收起左侧

[易语言] C语言例子转易语言求助

[复制链接]
结帖率:31% (8/26)
发表于 2026-1-15 16:15:53 | 显示全部楼层 |阅读模式   山东省日照市
10精币
public static byte[] MicrosoftDecompress(byte[] data)
{
    MemoryStream compressed = new MemoryStream(data);
    MemoryStream decompressed = new MemoryStream();
    DeflateStream deflateStream = new DeflateStream(compressed, CompressionMode.Decompress); // 注意: 这里第一个参数同样是填写压缩的数据,但是这次是作为输入的数据
    deflateStream.CopyTo(decompressed);
    byte[] result = decompressed.ToArray();
    return result;
}


public static int Encode6BitBufWithStartIndex(byte[] pSrc, int nSrcLen, byte[] pDest, int startIndex = 0)
{
    int currentIndex = startIndex;
    int nDestLen = 0;
    if (nSrcLen < 1)
    {
        nDestLen = currentIndex - startIndex;
        return nDestLen;
    }
    // 保存原始 startIndex 和 nDestLen 指针
    int savedStartIndex = startIndex;

    // 初始化循环变量
    int bitOffset = 0;      // W19 = 0
    int carryBits = 0;      // W25 = 0
    int srcIndex = 0;       // W26 = 0
    const int xorValue = 9; // W28 = 9
    const int six = 6;      // W29 = 6

    // 主循环
    while (srcIndex < nSrcLen)
    {
        if (srcIndex >= pSrc.Length)
        {
            throw new IndexOutOfRangeException();
        }
        // 获取 BaseEncryption.EncodeBitMasks
        byte[] encodeBitMasks = EncodeBitMasks;
        byte srcByte = pSrc[srcIndex];
        byte lookupValue = encodeBitMasks[srcByte];
        int newBitOffset = bitOffset + 2;
        int leftShiftAmount = six - bitOffset;
        int nextIndex = currentIndex + 1;
        int xoredValue = lookupValue ^ xorValue;
        int leftShifted = xoredValue << leftShiftAmount;
        int rightShifted = xoredValue >> newBitOffset;
        int extractedLowBits = (leftShifted >> 2) & 0x3F;
        int combined = carryBits | rightShifted;
        combined = combined & 0x3F;
        byte outputByte = (byte)(combined + 0x3C);
        pDest[currentIndex] = outputByte;

        if (newBitOffset >= 6)
        {
            if (nextIndex >= pDest.Length)
            {
                throw new IndexOutOfRangeException();
            }
            carryBits = 0;
            bitOffset = 0;
            currentIndex += 2;
            pDest[nextIndex] = (byte)(extractedLowBits + 0x3C);
        }
        else
        {
            // 不够6位,保存到 carry
            carryBits = extractedLowBits;
            currentIndex = nextIndex;
            bitOffset = newBitOffset;
        }

        srcIndex++;
    }

    // 循环结束后,检查是否还有剩余的 carry bits
    if (bitOffset >= 1)
    {
        if (currentIndex >= pDest.Length)
        {
            throw new IndexOutOfRangeException();
        }
        pDest[currentIndex] = (byte)(carryBits + 0x3C);
        currentIndex++;
    }
    nDestLen = currentIndex - savedStartIndex;
    return nDestLen;
}




public static int Decode6BitBufWithStartIndex(byte[] pSrc, int nSrcLen, byte[] pDest, int startIndex = 0)
{
    int destIndex = startIndex;
    int nDestLen = 0;

    if (nSrcLen < 1)
    {
        nDestLen = 0;
        return nDestLen;
    }

    if (pSrc == null || pDest == null)
    {
        throw new NullReferenceException();
    }

    if (EncodeBitMasks == null)
    {
        throw new InvalidOperationException("EncodeBitMasks must be initialized first");
    }

    int savedStartIndex = startIndex;
    int srcIndex = 0;
    int bitBuffer = 0;        // 位缓冲器
    int bitsInBuffer = 0;     // 缓冲器中的位数
    const int xorValue = 9;

    // 主循环:读取编码数据
    while (srcIndex < nSrcLen)
    {
        if (srcIndex >= pSrc.Length)
        {
            throw new IndexOutOfRangeException();
        }

        // 读取编码字节并减去偏移量 0x3C (60),得到6位数据
        int encoded6Bits = pSrc[srcIndex] - 0x3C;

        // 验证6位数据的有效性
        if (encoded6Bits < 0 || encoded6Bits > 0x3F)
        {
            throw new ArgumentException($"Invalid encoded byte at index {srcIndex}");
        }

        // 将6位数据加入缓冲器(高位在前)
        bitBuffer = (bitBuffer << 6) | encoded6Bits;
        bitsInBuffer += 6;

        // 当缓冲器中有至少8位时,提取一个字节
        while (bitsInBuffer >= 8)
        {
            if (destIndex >= pDest.Length)
            {
                throw new IndexOutOfRangeException();
            }

            // 从缓冲器中提取高8位
            int shift = bitsInBuffer - 8;
            int decoded8Bits = (bitBuffer >> shift) & 0xFF;

            // 反向操作1: XOR 9
            int afterXor = decoded8Bits ^ xorValue;

            // 反向操作2: 在EncodeBitMasks中查找这个值对应的原始字节
            // 因为加密时是: lookupValue = EncodeBitMasks[原始字节]
            // 所以解密时需要找到: EncodeBitMasks[?] = afterXor,? 就是原始字节
            byte originalByte = 0;
            bool found = false;
            for (int i = 0; i < 256; i++)
            {
                if (EncodeBitMasks[i] == afterXor)
                {
                    originalByte = (byte)i;
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                throw new ArgumentException($"Cannot find original byte for value {afterXor} at position {destIndex}");
            }

            pDest[destIndex] = originalByte;
            destIndex++;

            // 从缓冲器中移除已使用的8位
            bitBuffer &= (1 << shift) - 1;
            bitsInBuffer = shift;
        }

        srcIndex++;
    }

    nDestLen = destIndex - savedStartIndex;
    return nDestLen;
}




public static int Decode6BitBuf(byte[] pSrc, int nSrcLen, byte[] pDest)
{
    // 初始化静态字段 (对应 byte_38BB97B 检查)
    byte[] bitMaskArray = DecodeBitMasks;

    int destIndex = 0;  // v19/v13

    if (nSrcLen < 1)
    {
        return 0;
    }

    if (pSrc == null || pDest == null || bitMaskArray == null)
    {
        throw new NullReferenceException();
    }

    int bitsAvailable = 0;      // v12 - 缓冲区中可用的位数
    int srcIndex = 0;           // v15 - 源数据索引
    byte carryBits = 0;         // v14 - 进位位
    int bitPosition = 2;        // v16 - 当前位位置 (初始为2)

    // 主循环
    while (srcIndex < nSrcLen)
    {
        // 边界检查
        if (srcIndex >= pSrc.Length)
        {
            throw new IndexOutOfRangeException();
        }

        // v17 = pSrc[v15]
        uint currentByte = pSrc[srcIndex];

        // v18 = v17 - 60
        byte adjusted = (byte)(currentByte - 60);

        // if (v17 < 0x3C) - 如果小于60,跳到结束
        if (currentByte < 0x3C)
        {
            break;
        }

        // 检查是否有足够的位 (v12 < 2)
        if (bitsAvailable < 2)
        {
            // v14 = bitMaskArray[v16-2] & (v18 << v16)
            if ((bitPosition - 2) >= bitMaskArray.Length)
            {
                throw new IndexOutOfRangeException();
            }

            carryBits = (byte)(bitMaskArray[bitPosition - 2] & (adjusted << bitPosition));

            // v12 = v12 - v16 + 8
            bitsAvailable = bitsAvailable - bitPosition + 8;
        }
        else
        {
            // v12 >= 2 的情况
            if (DecodeBitMasks == null)
            {
                throw new NullReferenceException();
            }

            // 计算解码值: (v14 | ((v18 & 0x3F) >> (6 - v16))) ^ 0x9E
            int shiftAmount = 6 - bitPosition;
            int combined = carryBits | ((adjusted & 0x3F) >> shiftAmount);
            int xoredValue = combined ^ 0x9E;

            // 边界检查
            if (xoredValue >= DecodeBitMasks.Length)
            {
                throw new IndexOutOfRangeException();
            }

            if (destIndex >= pDest.Length)
            {
                throw new IndexOutOfRangeException();
            }

            // 输出解码字节: DecodeBitMasks[xoredValue] ^ 0x34
            pDest[destIndex] = (byte)(DecodeBitMasks[xoredValue] ^ 0x34);
            destIndex++;

            // 重置位计数
            bitsAvailable = 0;

            // 更新位位置
            if (bitPosition <= 5)
            {
                bitPosition += 2;

                // 处理 carry bits
                if ((bitPosition - 2) >= bitMaskArray.Length)
                {
                    throw new IndexOutOfRangeException();
                }

                carryBits = (byte)(bitMaskArray[bitPosition - 2] & (adjusted << bitPosition));
                bitsAvailable = bitsAvailable - bitPosition + 8;
            }
            else
            {
                bitPosition = 2;
            }
        }

        srcIndex++;
    }

    return destIndex;
}


回答提醒:如果本帖被关闭无法回复,您有更好的答案帮助楼主解决,请发表至 源码区 可获得加分喔。
友情提醒:本版被采纳的主题可在 申请荣誉值 页面申请荣誉值,获得 1点 荣誉值,荣誉值可兑换荣誉会员、终身vip用户组。
快捷通道:申请荣誉值

结帖率:100% (5/5)

签到天数: 2 天

发表于 2026-1-16 01:10:43 | 显示全部楼层   浙江省温州市
直接 代码编译为 DLL,易语言调用吧
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报QQ: 793400750,邮箱:wp@125.la
网站简介:精易论坛成立于2009年,是一个程序设计学习交流技术论坛,隶属于揭阳市揭东区精易科技有限公司所有。
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备2025452707号) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表