/// <summary> /// Search an occurence of pathPos in list. The string pathPos should have following layout: "x,yy" where /// x = a number greater then 0 e.g. 2 /// yy = a tag name e.g. 30 /// The list is searched for the given tag (case insensitive). The list can have multiple tags with the same name; the number /// indicates which entry has to be returned /// </summary> /// <param name="list">A list TLV items</param> /// <param name="posCommaTagName">The entry to search for example "1,6F"</param> /// <returns>The index in the list that contains the entry or -1 if not found</returns> private int searchOccurence(TLVList list, string posCommaTagName) { if (list == null) { throw new ArgumentNullException("list"); } if (string.IsNullOrEmpty(posCommaTagName)) { throw new ArgumentException("pathPos should not be null or empty", "pathPos"); } string[] pathPosSplitted = posCommaTagName.Split(new char[] { ',' }); if (pathPosSplitted.Length != 2) { throw new ArgumentException(string.Format("Invalid pathPos: {0}", posCommaTagName)); } int occurence = int.Parse(pathPosSplitted[0]); if (occurence < 1) { throw new ArgumentException(string.Format("Invalid position: {0}", occurence)); } string TagName = pathPosSplitted[1]; if (string.IsNullOrEmpty(TagName)) { throw new ArgumentException(string.Format("Invalid tagname: {0}", TagName)); } bool found = false; int i = 0; int occurencesFound = 0; while (!found && i < list.Count) { if (string.Compare(list[i].TagName, TagName, true) == 0) { occurencesFound++; } if (occurencesFound == occurence) { return(i); } i++; } return(-1); }
private static byte CONSTRUCTED_DATAOBJECT_MASK = 0x20; // 0010 0000 (Object primitive or constructed) public static TLVList Parse(Stream s) { TLVList l = new TLVList(); while (s.Position < s.Length) { TLV t = new TLV(s); t.Childs = TLV.ParseTagList(t.Value); l.Add(t); } return(l); }
/// <summary> /// Retrieve the TLV structure at the given path. A path is for example "1,6F|2,30|1,31|1,04" /// </summary> /// <param name="path">The path to follow</param> /// <returns>The TLV structure at the given path if found; null otherwise</returns> public TLV getTag(string path) { if (string.IsNullOrEmpty(path)) { throw new ArgumentException("Path should not be null or empty", "path"); } string[] pathParts = path.Split(new char[] { '|' }); int depth = 0; bool found = false; TLVList currentList = this; while (!found && depth < pathParts.Length) { int pos = searchOccurence(currentList, pathParts[depth]); if (pos < 0) { // Tag not found at current level return(null); } // Tag is found, but are we at desired level? if (depth == pathParts.Length - 1) { // Yes; return this entry return(currentList[pos]); } else if (currentList[pos].Childs != null) { // Not yet at desired level => go down currentList = currentList[pos].Childs; found = false; depth++; } else { // Not at desired level AND current level does not have any childs.... return(null); } } // Not found return(null); }
private static TLVList ParseTagList(byte[] data) { TLVList tagList = new TLVList(); MemoryStream ms = new MemoryStream(data); while (ms.Position < ms.Length) { TLV tlv = new TLV(ms); if (tlv.Value != null && tlv.Value.Length > 0) { if (tlv.isConstructed) { tlv.Childs = TLV.ParseTagList(tlv.Value); } tagList.Add(tlv); } } return(tagList); }