Beispiel #1
0
        private void TestCalculate(CRCModel model, bool mapping, string str, uint expected)
        {
            var crc    = CRC.Create(model, mapping);
            var data   = Encoding.ASCII.GetBytes(str);
            var actual = crc.Calculate(data);

            Assert.AreEqual(expected, actual);
        }
Beispiel #2
0
        private void TestVerify(CRCModel model, bool mapping, string str, uint crc, bool expected)
        {
            var data   = Encoding.ASCII.GetBytes(str);
            var obj    = CRC.Create(model, mapping);
            var actual = obj.Verify(data, crc);

            Assert.AreEqual(expected, actual);
        }
Beispiel #3
0
        private void UpdateCRC()
        {
            var value = Models[Key];

            if (value == mValue)
            {
                return;
            }
            CRC    = CRC.Create(value);
            mValue = value;
        }
Beispiel #4
0
        /// <summary>
        /// 创建指定校验模式的 CRC 实例
        /// </summary>
        /// <param name="model">校验模式</param>
        /// <param name="mapping">是否查表</param>
        /// <returns>指定校验模式的 CRC 实例</returns>
        public static CRC Create(CRCModel model, bool mapping = true)
        {
            switch (model)
            {
            case CRCModel.CRC_4_ITU:
                return(Create("CRC-4/ITU", 4, 0x03, 0x00, true, true, 0x00, mapping));

            case CRCModel.CRC_5_EPC:
                return(Create("CRC-5/EPC", 5, 0x09, 0x09, false, false, 0x00, mapping));

            case CRCModel.CRC_5_ITU:
                return(Create("CRC-5/ITU", 5, 0x15, 0x00, true, true, 0x00, mapping));

            case CRCModel.CRC_5_USB:
                return(Create("CRC-5/USB", 5, 0x05, 0x1F, true, true, 0x1F, mapping));

            case CRCModel.CRC_6_ITU:
                return(Create("CRC-6/ITU", 6, 0x03, 0x00, true, true, 0x00, mapping));

            case CRCModel.CRC_7_MMC:
                return(Create("CRC-7/MMC", 7, 0x09, 0x00, false, false, 0x00, mapping));

            case CRCModel.CRC_8:
                return(Create("CRC-8", 8, 0x07, 0x00, false, false, 0x00, mapping));

            case CRCModel.CRC_8_ITU:
                return(Create("CRC-8/ITU", 8, 0x07, 0x00, false, false, 0x55, mapping));

            case CRCModel.CRC_8_MAXIM:
                return(Create("CRC-8/MAXIM", 8, 0x31, 0x00, true, true, 0x00, mapping));

            case CRCModel.CRC_8_ROHC:
                return(Create("CRC-8/ROHC", 8, 0x07, 0xFF, true, true, 0x00, mapping));

            case CRCModel.CRC_16:
                return(Create("CRC-16", 16, 0x8005, 0x0000, true, true, 0x0000, mapping));

            case CRCModel.CRC_16_CCITT:
                return(Create("CRC-16/CCITT", 16, 0x1021, 0x0000, true, true, 0x0000, mapping));

            case CRCModel.CRC_16_CCITT_FALSE:
                return(Create("CRC-16/CCITT-FALSE", 16, 0x1021, 0xFFFF, false, false, 0x0000, mapping));

            case CRCModel.CRC_16_DNP:
                return(Create("CRC-16/DNP", 16, 0x3D65, 0x0000, true, true, 0xFFFF, mapping));

            case CRCModel.CRC_16_MAXIM:
                return(Create("CRC-16/MAXIM", 16, 0x8005, 0x0000, true, true, 0xFFFF, mapping));

            case CRCModel.CRC_16_MODBUS:
                return(Create("CRC-16/MODBUS", 16, 0x8005, 0xFFFF, true, true, 0x0000, mapping));

            case CRCModel.CRC_16_USB:
                return(Create("CRC-16/USB", 16, 0x8005, 0xFFFF, true, true, 0xFFFF, mapping));

            case CRCModel.CRC_16_X25:
                return(Create("CRC-16/X25", 16, 0x1021, 0xFFFF, true, true, 0xFFFF, mapping));

            case CRCModel.CRC_16_XMODEM:
                return(Create("CRC-16/XMODEM", 16, 0x1021, 0x0000, false, false, 0x0000, mapping));

            case CRCModel.CRC_32:
                return(Create("CRC-32", 32, 0x04C11DB7, 0xFFFFFFFF, true, true, 0xFFFFFFFF, mapping));

            case CRCModel.CRC_32_MPEG_2:
                return(Create("CRC-32/MPEG-2", 32, 0x04C11DB7, 0xFFFFFFFF, false, false, 0x00000000, mapping));

            default:
                throw new ArgumentException("未定义的校验模型", nameof(model));
            }
        }