Example #1
0
        public void Decode1(DataBits data, uint a, int b, byte[] ath)
        {
            int v = data.GetBit(3);

            if (v >= 6)
            {
                for (uint i = 0; i < Count; ++i)
                {
                    Value[i] = (byte)data.GetBit(6);
                }
            }
            else if (v != 0)
            {
                int v1 = data.GetBit(6);
                int v2 = (1 << v) - 1;
                int v3 = v2 >> 1;
                Value[0] = (byte)v1;
                for (uint i = 1; i < Count; ++i)
                {
                    int v4 = data.GetBit(v);
                    if (v4 != v2)
                    {
                        v1 += v4 - v3;
                    }
                    else
                    {
                        v1 = data.GetBit(6);
                    }
                    Value[i] = (byte)v1;
                }
            }
            else
            {
                Value.ZeroMem();
            }
            if (Type == 2)
            {
                v         = data.CheckBit(4);
                Value2[0] = (byte)v;
                if (v < 15)
                {
                    for (var i = 0; i < 8; ++i)
                    {
                        Value2[i] = (byte)data.GetBit(4);
                    }
                }
            }
            else
            {
                for (uint i = 0; i < a; ++i)
                {
                    //Value3[i] = (byte)data.GetBit(6);
                    SetValue3(i, (byte)data.GetBit(6));
                }
            }
            for (uint i = 0; i < Count; ++i)
            {
                v = Value[i];
                if (v != 0)
                {
                    v = (int)(ath[i] + ((b + i) >> 8) - ((v * 5) >> 1) + 1);
                    if (v < 0)
                    {
                        v = 15;
                    }
                    else if (v >= 0x39)
                    {
                        v = 1;
                    }
                    else
                    {
                        v = ChannelTables.Decode1ScaleList[v];
                    }
                }
                Scale[i] = (byte)v;
            }
            for (var i = Count; i < Scale.Length; ++i)
            {
                Scale[i] = 0;
            }
            for (uint i = 0; i < Count; ++i)
            {
                Base[i] = ChannelTables.Decode1ValueSingle[Value[i]] * ChannelTables.Decode1ScaleSingle[Scale[i]];
            }
        }
Example #2
0
        public void Decode1(int channelIndex, DataBits data, uint a, int b, byte[] ath)
        {
            var v      = data.GetBit(3);
            var pCount = GetPtrOfCount(channelIndex);
            var pValue = GetPtrOfValue(channelIndex);

            if (v >= 6)
            {
                for (var i = 0; i < *pCount; ++i)
                {
                    pValue[i] = (sbyte)data.GetBit(6);
                }
            }
            else if (v != 0)
            {
                var v1 = data.GetBit(6);
                var v2 = (1 << v) - 1;
                var v3 = v2 >> 1;

                pValue[0] = (sbyte)v1;

                for (var i = 1; i < *pCount; ++i)
                {
                    var v4 = data.GetBit(v);

                    if (v4 != v2)
                    {
                        v1 += v4 - v3;
                    }
                    else
                    {
                        v1 = data.GetBit(6);
                    }

                    pValue[i] = (sbyte)v1;
                }
            }
            else
            {
                ZeroMemory(pValue, 0x80);
            }

            var pType    = GetPtrOfType(channelIndex);
            var pValue2  = GetPtrOfValue2(channelIndex);
            var ppValue3 = GetPtrOfValue3(channelIndex);

            if (*pType == 2)
            {
                v          = data.CheckBit(4);
                pValue2[0] = (sbyte)v;

                if (v < 15)
                {
                    for (var i = 0; i < 8; ++i)
                    {
                        pValue2[i] = (sbyte)data.GetBit(4);
                    }
                }
            }
            else
            {
                for (var i = 0; i < a; ++i)
                {
                    (*ppValue3)[i] = (sbyte)data.GetBit(6);
                }
            }

            var pScale = GetPtrOfScale(channelIndex);

            for (var i = 0; i < *pCount; ++i)
            {
                v = pValue[i];

                if (v != 0)
                {
                    v = ath[i] + ((b + i) >> 8) - ((v * 5) >> 1) + 1;

                    if (v < 0)
                    {
                        v = 15;
                    }
                    else if (v >= 0x39)
                    {
                        v = 1;
                    }
                    else
                    {
                        v = ChannelTables.Decode1ScaleList[v];
                    }
                }

                pScale[i] = (sbyte)v;
            }

            ZeroMemory(&pScale[*pCount], (int)(0x80 - *pCount));

            var pBase = GetPtrOfBase(channelIndex);

            for (var i = 0; i < *pCount; ++i)
            {
                pBase[i] = ChannelTables.Decode1ValueSingle[pValue[i]] * ChannelTables.Decode1ScaleSingle[pScale[i]];
            }
        }
Example #3
0
        private int DecodeToWaveR32(byte[] blockData, int blockIndex)
        {
            var hcaInfo = HcaInfo;

            if (blockData == null)
            {
                throw new ArgumentNullException(nameof(blockData));
            }
            if (blockData.Length < hcaInfo.BlockSize)
            {
                throw new HcaException(ErrorMessages.GetInvalidParameter(nameof(blockData) + "." + nameof(blockData.Length)), ActionResult.InvalidParameter);
            }
            var checksum = HcaHelper.Checksum(blockData, 0);

            if (checksum != 0)
            {
                throw new HcaException(ErrorMessages.GetChecksumNotMatch(0, checksum), ActionResult.ChecksumNotMatch);
            }
            _cipher.Decrypt(blockData);
            var d     = new DataBits(blockData, hcaInfo.BlockSize);
            var magic = d.GetBit(16);

            if (magic != 0xffff)
            {
                throw new HcaException(ErrorMessages.GetMagicNotMatch(0xffff, magic), ActionResult.MagicNotMatch);
            }
            var    a        = (d.GetBit(9) << 8) - d.GetBit(7);
            var    channels = _channels;
            var    ath      = _ath;
            string site     = null;

            try {
                int i;

                for (i = 0; i < hcaInfo.ChannelCount; ++i)
                {
                    site = $"Decode1({i.ToString()})";
                    channels.Decode1(i, d, hcaInfo.CompR09, a, ath.Table);
                }

                for (i = 0; i < 8; ++i)
                {
                    for (var j = 0; j < hcaInfo.ChannelCount; ++j)
                    {
                        site = $"Decode2({i.ToString()}/{j.ToString()})";
                        channels.Decode2(j, d);
                    }

                    for (var j = 0; j < hcaInfo.ChannelCount; ++j)
                    {
                        site = $"Decode3({i.ToString()}/{j.ToString()})";
                        channels.Decode3(j, hcaInfo.CompR09, hcaInfo.CompR08, (uint)(hcaInfo.CompR07 + hcaInfo.CompR06), hcaInfo.CompR05);
                    }

                    for (var j = 0; j < hcaInfo.ChannelCount - 1; ++j)
                    {
                        site = $"Decode4({i.ToString()}/{j.ToString()})";
                        channels.Decode4(j, j + 1, i, (uint)(hcaInfo.CompR05 - hcaInfo.CompR06), hcaInfo.CompR06, hcaInfo.CompR07);
                    }

                    for (var j = 0; j < hcaInfo.ChannelCount; ++j)
                    {
                        site = $"Decode5({i.ToString()}/{j.ToString()})";
                        channels.Decode5(j, i);
                    }
                }

                return(blockData.Length);
            } catch (IndexOutOfRangeException ex) {
                const string message  = "Index access exception detected. It is probably because you are using an incorrect HCA key pair while decoding a type 56 HCA file.";
                var          siteInfo = $"Site: {site} @ block {blockIndex.ToString()}";
                var          err      = message + Environment.NewLine + siteInfo;
                throw new HcaException(err, ActionResult.DecodeFailed, ex);
            }
        }