/// <summary> /// Get node label string. /// </summary> /// <param name="mask"> /// <code> /// SHOW_OFFSET /// SHOW_DATA /// USE_HEX_OFFSET /// SHOW_TAG_NUMBER /// SHOW_PATH</code> /// </param> /// <returns>string</returns> public string GetLabel(uint mask) { string nodeStr = ""; string dataStr = ""; string offsetStr = ""; if ((mask & TagTextMask.USE_HEX_OFFSET) != 0) { if ((mask & TagTextMask.SHOW_TAG_NUMBER) != 0) offsetStr = String.Format("(0x{0:X2},0x{1:X6},0x{2:X4})", tag, dataOffset, dataLength); else offsetStr = String.Format("(0x{0:X6},0x{1:X4})", dataOffset, dataLength); } else { if ((mask & TagTextMask.SHOW_TAG_NUMBER) != 0) offsetStr = String.Format("({0},{1},{2})", tag, dataOffset, dataLength); else offsetStr = String.Format("({0},{1})", dataOffset, dataLength); } string oid, oidName; switch (tag) { case Asn1Tag.BIT_STRING: if ((mask & TagTextMask.SHOW_OFFSET) != 0) { nodeStr += offsetStr; } nodeStr += " " + TagName + " UnusedBits: " + unusedBits.ToString(); if ((mask & TagTextMask.SHOW_DATA) != 0) { dataStr = Asn1Util.ToHexString(data); nodeStr += ((dataStr.Length>0) ? " : '" + dataStr + "'" : ""); } break; case Asn1Tag.OBJECT_IDENTIFIER: Oid xoid = new Oid(); oid = xoid.Decode(data); oidName = xoid.GetOidName(oid); if ((mask & TagTextMask.SHOW_OFFSET) != 0) { nodeStr += offsetStr; } nodeStr += " " + TagName; nodeStr += " : " + oidName; if ((mask & TagTextMask.SHOW_DATA) != 0) { nodeStr += ((oid.Length>0) ? " : '" + oid + "'" : ""); } break; case Asn1Tag.RELATIVE_OID: RelativeOid roid = new RelativeOid(); oid = roid.Decode(data); oidName = ""; if ((mask & TagTextMask.SHOW_OFFSET) != 0) { nodeStr += offsetStr; } nodeStr += " " + TagName; nodeStr += " : " + oidName; if ((mask & TagTextMask.SHOW_DATA) != 0) { nodeStr += ((oid.Length>0) ? " : '" + oid + "'" : ""); } break; case Asn1Tag.PRINTABLE_STRING: case Asn1Tag.IA5_STRING: case Asn1Tag.UNIVERSAL_STRING: case Asn1Tag.VISIBLE_STRING: case Asn1Tag.NUMERIC_STRING: case Asn1Tag.UTC_TIME: case Asn1Tag.UTF8_STRING: case Asn1Tag.BMPSTRING: case Asn1Tag.GENERAL_STRING: case Asn1Tag.GENERALIZED_TIME: if ((mask & TagTextMask.SHOW_OFFSET) != 0) { nodeStr += offsetStr; } nodeStr += " " + TagName; if ((mask & TagTextMask.SHOW_DATA) != 0) { if ( tag == Asn1Tag.UTF8_STRING ) { UTF8Encoding unicode = new UTF8Encoding(); dataStr = unicode.GetString(data); } else { dataStr = Asn1Util.BytesToString(data); } nodeStr += ((dataStr.Length>0) ? " : '" + dataStr + "'" : ""); } break; case Asn1Tag.INTEGER: if ((mask & TagTextMask.SHOW_OFFSET) != 0) { nodeStr += offsetStr; } nodeStr += " " + TagName; if ((mask & TagTextMask.SHOW_DATA) != 0) { if (data != null && dataLength < 8) { dataStr = Asn1Util.BytesToLong(data).ToString(); } else { dataStr = Asn1Util.ToHexString(data); } nodeStr += ((dataStr.Length>0) ? " : '" + dataStr + "'" : ""); } break; default: if ((mask & TagTextMask.SHOW_OFFSET) != 0) { nodeStr += offsetStr; } nodeStr += " " + TagName; if ((mask & TagTextMask.SHOW_DATA) != 0) { if ((tag & Asn1Tag.TAG_MASK) == 6) // Visible string for certificate { dataStr = Asn1Util.BytesToString(data); } else { dataStr = Asn1Util.ToHexString(data); } nodeStr += ((dataStr.Length>0) ? " : '" + dataStr + "'" : ""); } break; }; if ((mask & TagTextMask.SHOW_PATH) != 0) { nodeStr = "(" + path + ") " + nodeStr; } return nodeStr; }
public string GetLabel(uint mask) { string str1 = ""; string str2 = ((int)mask & 4) == 0 ? (((int)mask & 8) == 0 ? string.Format("({0},{1})", (object)this.dataOffset, (object)this.dataLength) : string.Format("({0},{1},{2})", (object)this.tag, (object)this.dataOffset, (object)this.dataLength)) : (((int)mask & 8) == 0 ? string.Format("(0x{0:X6},0x{1:X4})", (object)this.dataOffset, (object)this.dataLength) : string.Format("(0x{0:X2},0x{1:X6},0x{2:X4})", (object)this.tag, (object)this.dataOffset, (object)this.dataLength)); string str3; switch (this.tag) { case 2: if (((int)mask & 1) != 0) { str1 += str2; } str3 = str1 + " " + this.TagName; if (((int)mask & 2) != 0) { string str4 = this.data == null || this.dataLength >= 8L ? Asn1Util.ToHexString(this.data) : Asn1Util.BytesToLong(this.data).ToString(); str3 += str4.Length > 0 ? " : '" + str4 + "'" : ""; break; } break; case 3: if (((int)mask & 1) != 0) { str1 += str2; } str3 = str1 + " " + this.TagName + " UnusedBits: " + this.unusedBits.ToString(); if (((int)mask & 2) != 0) { string hexString = Asn1Util.ToHexString(this.data); str3 += hexString.Length > 0 ? " : '" + hexString + "'" : ""; break; } break; case 6: Oid oid = new Oid(); string inOidStr = oid.Decode(this.data); string oidName = oid.GetOidName(inOidStr); if (((int)mask & 1) != 0) { str1 += str2; } str3 = str1 + " " + this.TagName + " : " + oidName; if (((int)mask & 2) != 0) { str3 += inOidStr.Length > 0 ? " : '" + inOidStr + "'" : ""; break; } break; case 12: case 18: case 19: case 22: case 23: case 24: case 26: case 27: case 28: case 30: if (((int)mask & 1) != 0) { str1 += str2; } str3 = str1 + " " + this.TagName; if (((int)mask & 2) != 0) { string str4 = this.tag != (byte)12 ? Asn1Util.BytesToString(this.data) : new UTF8Encoding().GetString(this.data); str3 += str4.Length > 0 ? " : '" + str4 + "'" : ""; break; } break; case 13: string str5 = new RelativeOid().Decode(this.data); string str6 = ""; if (((int)mask & 1) != 0) { str1 += str2; } str3 = str1 + " " + this.TagName + " : " + str6; if (((int)mask & 2) != 0) { str3 += str5.Length > 0 ? " : '" + str5 + "'" : ""; break; } break; default: if (((int)mask & 1) != 0) { str1 += str2; } str3 = str1 + " " + this.TagName; if (((int)mask & 2) != 0) { string str4 = ((int)this.tag & 31) == 6 || Asn1Util.IsAsciiString(this.Data) ? Asn1Util.BytesToString(this.data) : Asn1Util.ToHexString(this.data); str3 += str4.Length > 0 ? " : '" + str4 + "'" : ""; break; } break; } if (((int)mask & 16) != 0) { str3 = "(" + this.path + ") " + str3; } return(str3); }
/// <summary> /// Get the node and all the descendents text description. /// </summary> /// <param name="startNode">starting node.</param> /// <param name="lineLen">line length.</param> /// <returns></returns> public string GetText(Asn1Node startNode, int lineLen) { string nodeStr = ""; string baseLine = ""; string dataStr = ""; const string lStr = " | | | "; string oid, oidName; switch (tag) { case Asn1Tag.BIT_STRING: baseLine = String.Format("{0,6}|{1,6}|{2,7}|{3} {4} UnusedBits:{5} : ", dataOffset, dataLength, lengthFieldBytes, GetIndentStr(startNode), TagName, unusedBits ); dataStr = Asn1Util.ToHexString(data); if (baseLine.Length + dataStr.Length < lineLen) { if (dataStr.Length<1) { nodeStr += baseLine + "\r\n"; } else { nodeStr += baseLine + "'" + dataStr + "'\r\n"; } } else { nodeStr += baseLine + FormatLineHexString( lStr, GetIndentStr(startNode).Length, lineLen, dataStr + "\r\n" ); } break; case Asn1Tag.OBJECT_IDENTIFIER: Oid xoid = new Oid(); oid = xoid.Decode(new MemoryStream(data)); oidName = xoid.GetOidName(oid); nodeStr += String.Format("{0,6}|{1,6}|{2,7}|{3} {4} : {5} [{6}]\r\n", dataOffset, dataLength, lengthFieldBytes, GetIndentStr(startNode), TagName, oidName, oid ); break; case Asn1Tag.RELATIVE_OID: RelativeOid xiod = new RelativeOid(); oid = xiod.Decode(new MemoryStream(data)); oidName = ""; nodeStr += String.Format("{0,6}|{1,6}|{2,7}|{3} {4} : {5} [{6}]\r\n", dataOffset, dataLength, lengthFieldBytes, GetIndentStr(startNode), TagName, oidName, oid ); break; case Asn1Tag.PRINTABLE_STRING: case Asn1Tag.IA5_STRING: case Asn1Tag.UNIVERSAL_STRING: case Asn1Tag.VISIBLE_STRING: case Asn1Tag.NUMERIC_STRING: case Asn1Tag.UTC_TIME: case Asn1Tag.UTF8_STRING: case Asn1Tag.BMPSTRING: case Asn1Tag.GENERAL_STRING: case Asn1Tag.GENERALIZED_TIME: baseLine = String.Format("{0,6}|{1,6}|{2,7}|{3} {4} : ", dataOffset, dataLength, lengthFieldBytes, GetIndentStr(startNode), TagName ); if ( tag == Asn1Tag.UTF8_STRING ) { UTF8Encoding unicode = new UTF8Encoding(); dataStr = unicode.GetString(data); } else { dataStr = Asn1Util.BytesToString(data); } if (baseLine.Length + dataStr.Length < lineLen) { nodeStr += baseLine + "'" + dataStr + "'\r\n"; } else { nodeStr += baseLine + FormatLineString( lStr, GetIndentStr(startNode).Length, lineLen, dataStr) + "\r\n"; } break; case Asn1Tag.INTEGER: if (data != null && dataLength < 8) { nodeStr += String.Format("{0,6}|{1,6}|{2,7}|{3} {4} : {5}\r\n", dataOffset, dataLength, lengthFieldBytes, GetIndentStr(startNode), TagName, Asn1Util.BytesToLong(data).ToString() ); } else { baseLine = String.Format("{0,6}|{1,6}|{2,7}|{3} {4} : ", dataOffset, dataLength, lengthFieldBytes, GetIndentStr(startNode), TagName ); nodeStr += GetHexPrintingStr(startNode, baseLine, lStr, lineLen); } break; default: if ((tag & Asn1Tag.TAG_MASK) == 6) // Visible string for certificate { baseLine = String.Format("{0,6}|{1,6}|{2,7}|{3} {4} : ", dataOffset, dataLength, lengthFieldBytes, GetIndentStr(startNode), TagName ); dataStr = Asn1Util.BytesToString(data); if (baseLine.Length + dataStr.Length < lineLen) { nodeStr += baseLine + "'" + dataStr + "'\r\n"; } else { nodeStr += baseLine + FormatLineString( lStr, GetIndentStr(startNode).Length, lineLen, dataStr) + "\r\n"; } } else { baseLine = String.Format("{0,6}|{1,6}|{2,7}|{3} {4} : ", dataOffset, dataLength, lengthFieldBytes, GetIndentStr(startNode), TagName ); nodeStr += GetHexPrintingStr(startNode, baseLine, lStr, lineLen); } break; }; if (childNodeList.Count >= 0) { nodeStr += GetListStr(startNode, lineLen); } return nodeStr; }
public string GetText(Asn1Node startNode, int lineLen) { string str1 = ""; string str2; switch (this.tag) { case 2: if (this.data != null && this.dataLength < 8L) { str2 = str1 + string.Format("{0,6}|{1,6}|{2,7}|{3} {4} : {5}\r\n", (object)this.dataOffset, (object)this.dataLength, (object)this.lengthFieldBytes, (object)this.GetIndentStr(startNode), (object)this.TagName, (object)Asn1Util.BytesToLong(this.data).ToString()); break; } string baseLine1 = string.Format("{0,6}|{1,6}|{2,7}|{3} {4} : ", (object)this.dataOffset, (object)this.dataLength, (object)this.lengthFieldBytes, (object)this.GetIndentStr(startNode), (object)this.TagName); str2 = str1 + this.GetHexPrintingStr(startNode, baseLine1, " | | | ", lineLen); break; case 3: string str3 = string.Format("{0,6}|{1,6}|{2,7}|{3} {4} UnusedBits:{5} : ", (object)this.dataOffset, (object)this.dataLength, (object)this.lengthFieldBytes, (object)this.GetIndentStr(startNode), (object)this.TagName, (object)this.unusedBits); string hexString = Asn1Util.ToHexString(this.data); if (str3.Length + hexString.Length < lineLen) { if (hexString.Length < 1) { str2 = str1 + str3 + "\r\n"; break; } str2 = str1 + str3 + "'" + hexString + "'\r\n"; break; } str2 = str1 + str3 + this.FormatLineHexString(" | | | ", this.GetIndentStr(startNode).Length, lineLen, hexString + "\r\n"); break; case 6: Oid oid = new Oid(); string inOidStr = oid.Decode((Stream) new MemoryStream(this.data)); string oidName = oid.GetOidName(inOidStr); str2 = str1 + string.Format("{0,6}|{1,6}|{2,7}|{3} {4} : {5} [{6}]\r\n", (object)this.dataOffset, (object)this.dataLength, (object)this.lengthFieldBytes, (object)this.GetIndentStr(startNode), (object)this.TagName, (object)oidName, (object)inOidStr); break; case 12: case 18: case 19: case 22: case 23: case 24: case 26: case 27: case 28: case 30: string str4 = string.Format("{0,6}|{1,6}|{2,7}|{3} {4} : ", (object)this.dataOffset, (object)this.dataLength, (object)this.lengthFieldBytes, (object)this.GetIndentStr(startNode), (object)this.TagName); string msg1 = this.tag != (byte)12 ? Asn1Util.BytesToString(this.data) : new UTF8Encoding().GetString(this.data); if (str4.Length + msg1.Length < lineLen) { str2 = str1 + str4 + "'" + msg1 + "'\r\n"; break; } str2 = str1 + str4 + this.FormatLineString(" | | | ", this.GetIndentStr(startNode).Length, lineLen, msg1) + "\r\n"; break; case 13: string str5 = new RelativeOid().Decode((Stream) new MemoryStream(this.data)); string str6 = ""; str2 = str1 + string.Format("{0,6}|{1,6}|{2,7}|{3} {4} : {5} [{6}]\r\n", (object)this.dataOffset, (object)this.dataLength, (object)this.lengthFieldBytes, (object)this.GetIndentStr(startNode), (object)this.TagName, (object)str6, (object)str5); break; default: if (((int)this.tag & 31) == 6 || Asn1Util.IsAsciiString(this.Data)) { string str7 = string.Format("{0,6}|{1,6}|{2,7}|{3} {4} : ", (object)this.dataOffset, (object)this.dataLength, (object)this.lengthFieldBytes, (object)this.GetIndentStr(startNode), (object)this.TagName); string msg2 = Asn1Util.BytesToString(this.data); if (str7.Length + msg2.Length < lineLen) { str2 = str1 + str7 + "'" + msg2 + "'\r\n"; break; } str2 = str1 + str7 + this.FormatLineString(" | | | ", this.GetIndentStr(startNode).Length, lineLen, msg2) + "\r\n"; break; } string baseLine2 = string.Format("{0,6}|{1,6}|{2,7}|{3} {4} : ", (object)this.dataOffset, (object)this.dataLength, (object)this.lengthFieldBytes, (object)this.GetIndentStr(startNode), (object)this.TagName); str2 = str1 + this.GetHexPrintingStr(startNode, baseLine2, " | | | ", lineLen); break; } if (this.childNodeList.Count >= 0) { str2 += this.GetListStr(startNode, lineLen); } return(str2); }