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用户组。
快捷通道: 申请荣誉值 →