public D900CdrElement DecodeRecord(Stream asnStream, bool skipFillers) { TlvObject tlv; long offset = asnStream.Position; // Verify that the next byte - Billing Record Tag (0xE1) if (!skipFillers) { int b = asnStream.ReadByte(); if (b != 0xE1) { return(null); } asnStream.Seek(-1, SeekOrigin.Current); } BerDecoderResult pr = DecodeTlv(asnStream, out tlv, ref offset, 0, byte.MaxValue); D900CdrElement record = null; if (pr == BerDecoderResult.Finished) { record = _tlvParser.ParseTlvObject(tlv);; } return(record); }
public CdrElement DecodeRecord(Stream asnStream, bool searchSignature) { TlvObject tlv; long offset = asnStream.Position; if (searchSignature && (_cdrSignatures != null)) { int b = asnStream.ReadByte(); bool res = false; foreach (byte sgn in _cdrSignatures) { if (b == sgn) { res = true; break; } } if (!res) { return(null); } asnStream.Seek(-1, SeekOrigin.Current); } BerDecoderResult pr = DecodeTlv(asnStream, out tlv, ref offset, 0, byte.MaxValue); CdrElement record = null; if ((pr == BerDecoderResult.Finished) && (_tlvParser != null)) { record = _tlvParser.ParseTlvObject(tlv);; } return(record); }
public int Dump(Stream asnStream, TextWriter dumpWriter, DumpType dumpType, byte maxLevel) { long offset = asnStream.Position; BerDecoderResult pr = BerDecoderResult.Finished; TlvObject tlv; CdrElement record; int cnt = 0; for (; ;) //(int n = 11; n > 0; n--) { pr = DecodeTlv(asnStream, out tlv, ref offset, 0, maxLevel); if (pr != BerDecoderResult.Finished) { break; } switch (dumpType) { case DumpType.TLV_TXT: tlv.DumpToTxt(dumpWriter, String.Format("{0,8} > {1}", tlv.Offset, ++cnt)); dumpWriter.WriteLine(); break; case DumpType.TLV_XML: tlv.DumpToXml(dumpWriter, 0); break; case DumpType.ELEMENT_TXT: record = _tlvParser.ParseTlvObject(tlv); record.DumpToTxt(dumpWriter, String.Format("{0,8} > {1}", record.Offset, ++cnt)); dumpWriter.WriteLine(); break; case DumpType.ELEMENT_XML: record = _tlvParser.ParseTlvObject(tlv); record.DumpToXml(dumpWriter, 0); break; } } dumpWriter.Flush(); return((int)(pr)); }
// Fetch out BER-encoded data until EOF or error protected BerDecoderResult DecodeTlv(Stream asnStream, out TlvObject tlvObj, ref long offset, byte level, byte maxLevel) { BerDecoderResult procResult = BerDecoderResult.Finished; tlvObj = null; byte[] tlvBuffer = new byte[32]; byte tlvBuffer_Length = 0; int octet; int tlvTag = 0; int tlvTag_Length = 0; // Decode the TLV tag while (tlvTag_Length == 0) { // Get the next byte from the input stream octet = asnStream.ReadByte(); if (octet == -1) { return(BerDecoderResult.EOF); } // Skip the fillers between billing records (TLV with level == 0) if ((level == 0) && ((octet == 0xFF) || (octet == 0))) { offset++; continue; } tlvBuffer[tlvBuffer_Length++] = (byte)octet; tlvTag_Length = FetchTlvTag(tlvBuffer, tlvBuffer_Length, ref tlvTag); switch (tlvTag_Length) { case -1: return(BerDecoderResult.Failed); case 0: continue; // More data expected } } long tlvOffset = offset; offset += tlvTag_Length; bool _constructed = ((tlvBuffer[0] & 0x20) == 0x20); int tlvLength = 0; int tlvLength_Length = 0; // Decode the TLV length while (tlvLength_Length == 0) { // Get the next byte from the input stream octet = asnStream.ReadByte(); if (octet == -1) { return(BerDecoderResult.EOF); } tlvBuffer[tlvBuffer_Length++] = (byte)octet; tlvLength_Length = FetchTlvLength(_constructed, tlvBuffer, tlvTag_Length, tlvBuffer_Length - tlvTag_Length, ref tlvLength); switch (tlvLength_Length) { case -1: return(BerDecoderResult.Failed); case 0: continue; // More data expected } } offset += tlvLength_Length; tlvObj = new TlvObject(tlvTag >> 2, (AsnTagClass)(tlvBuffer[0] & 0xC0), _constructed, tlvOffset); if (_constructed) { if (level < maxLevel) { long _vLength = offset + tlvLength; BerDecoderResult _pr; while (offset < _vLength) { TlvObject _childObj; _pr = this.DecodeTlv(asnStream, out _childObj, ref offset, (byte)(level + 1), maxLevel); if (_pr != BerDecoderResult.Finished) { break; } tlvObj.AddTlv(_childObj); } } else { // Stop processing for TLV with level > maxLevel, and skip content. offset += tlvLength; asnStream.Seek(tlvLength, SeekOrigin.Current); } } else { int _readOffset = tlvObj.ReadValue(asnStream, tlvLength); offset += _readOffset; if (_readOffset != tlvLength) { return(BerDecoderResult.Failed); } } return(procResult); }
public int CheckElementsDefinition(Stream asnStream, TextWriter dumpWriter) { long offset = 0; BerDecoderResult pr = BerDecoderResult.Finished; TlvObject tlv; D900CdrElement record; int cnt = 0; ElementPathComparer comparer = new ElementPathComparer(); ElementsStatTable present = new ElementsStatTable(comparer); ElementsStatTable none = new ElementsStatTable(comparer); dumpWriter.WriteLine("Check elements definition.\n=========================="); dumpWriter.WriteLine("Current configuration: {0}\n", ElementDefinitionProvider.CurrentSchema); int rem; dumpWriter.WriteLine("Please wait"); for (; ;) //(int n = 5000; n > 0; n--) { pr = DecodeTlv(asnStream, out tlv, ref offset, 0, Byte.MaxValue); if (pr != BerDecoderResult.Finished) { break; } record = _tlvParser.ParseTlvObject(tlv); CheckElement(record, present, none); cnt++; Math.DivRem(cnt, 1000, out rem); if (rem == 0) { dumpWriter.Write('.'); } } dumpWriter.WriteLine("\n"); int e_sum = 0; int max_present_value = 0; foreach (var value in present) { e_sum += value.Value; if (max_present_value < value.Value) { max_present_value = value.Value; } } int max_none_value = 0; foreach (var value in none) { e_sum += value.Value; if (max_none_value < value.Value) { max_none_value = value.Value; } } dumpWriter.WriteLine("- Total {0} records processed ({1} elements)\n", cnt, e_sum); dumpWriter.WriteLine("- The elements are present:\n==========================="); string headerFmt = " {0,-15}| {1, -30}| {2}"; dumpWriter.WriteLine(headerFmt, "Path", "Name", "Count"); dumpWriter.WriteLine(headerFmt , "---------------" , "------------------------------" , "---------------------------" ); ID900CdrElementDefinition elementDef; foreach (var value in present) { elementDef = ElementDefinitionProvider.FindDefinition(value.Key); dumpWriter.Write(headerFmt, value.Key, (elementDef != null) ? elementDef.Name : String.Empty, string.Empty); for (int n = 1; n < value.Value * 26 / max_present_value; n++) { dumpWriter.Write("#"); } dumpWriter.WriteLine(" {0}", value.Value); } dumpWriter.WriteLine(); dumpWriter.WriteLine("- Elements are not available in D900Cdr.Elements.xml:\n===================================================="); headerFmt = " {0,-15}| {1}"; dumpWriter.WriteLine(headerFmt, "Path", "Count"); dumpWriter.WriteLine(headerFmt , "---------------" , "------------------------------" ); foreach (var value in none) { dumpWriter.Write(headerFmt, value.Key, string.Empty); for (int n = 1; n < value.Value * 29 / max_none_value; n++) { dumpWriter.Write("#"); } dumpWriter.WriteLine(" {0}", value.Value); } dumpWriter.WriteLine(); dumpWriter.Flush(); return((int)(pr)); }