private void DecodeWBXML(byte[] bytes) { currentTagCodePage = 0; currentAttributeCodePage = 0; bool isAttribute = false; bool tagHasAttributes = false; bool tagHasContent = false; XmlNode activeNode = this; XmlAttribute activeAttribute = null; Queue<byte> byteQueue = new Queue<byte>(bytes); //Get the WBXML version number; versionNumber = ((double)byteQueue.Dequeue() / 10.0) + 1.0; //Get the value of the public identifier publicIdentifier = GetInt(byteQueue); if (publicIdentifier == 0) { //TODO the publicIdentifier is defined as string inside the stringtable int publicIdentifierStringTableIndex = GetInt(byteQueue); } //Get the charset for text encoding textEncoding = IANACharacterSets.GetEncoding(GetInt(byteQueue)); XmlDeclaration declaration = CreateXmlDeclaration("1.0", textEncoding.WebName, null); activeNode.AppendChild(declaration); //Get the string table (use of the string table is not implemented) int stringTableLength = GetInt(byteQueue); if (stringTableLength > 0) { Queue<byte> byteStringTableQueue = new Queue<byte>(); for (int i = 0; i < stringTableLength; i++) { byteStringTableQueue.Enqueue(byteQueue.Dequeue()); } List<string> stringTableList = new List<string>(); while (byteStringTableQueue.Count > 0) { string stringTableItem = GetString(byteStringTableQueue); stringTableList.Add(stringTableItem); } stringTable = new StringTable(stringTableList.ToArray()); } //WBXML body while (byteQueue.Count > 0) { byte byteItem = byteQueue.Dequeue(); if (globalTokens.Contains(byteItem)) { GlobalTokens.Names globalToken = globalTokens.GetToken(byteItem); switch (globalToken) { case GlobalTokens.Names.SWITCH_PAGE: currentTagCodePage = (int)byteQueue.Dequeue(); break; case GlobalTokens.Names.END: if (isAttribute) { isAttribute = false; activeAttribute = null; if (!tagHasContent) { activeNode = activeNode.ParentNode; } } else { activeNode = activeNode.ParentNode; } break; case GlobalTokens.Names.ENTITY: //TODO ENTITY is not implemented yet int entityLength = GetInt(byteQueue); break; case GlobalTokens.Names.STR_I: if (isAttribute) { activeAttribute.InnerText += GetString(byteQueue); } else { activeNode.AppendChild(CreateTextNode(GetString(byteQueue))); } break; case GlobalTokens.Names.LITERAL: case GlobalTokens.Names.LITERAL_A: case GlobalTokens.Names.LITERAL_AC: case GlobalTokens.Names.LITERAL_C: //TODO LITERALs are not implemented yet int literalReference = GetInt(byteQueue); break; case GlobalTokens.Names.EXT_I_0: case GlobalTokens.Names.EXT_I_1: case GlobalTokens.Names.EXT_I_2: //TODO EXT_I_x are not implemented yet string extiString = GetString(byteQueue); break; case GlobalTokens.Names.PI: //TODO PI is not implemented yet List<byte> piByteList = new List<byte>(); while (byteQueue.Peek() != (byte)GlobalTokens.Names.END) { piByteList.Add(byteQueue.Dequeue()); } byteQueue.Dequeue(); break; case GlobalTokens.Names.EXT_T_0: case GlobalTokens.Names.EXT_T_1: case GlobalTokens.Names.EXT_T_2: //TODO EXT_T_x are not implemented yet int exttReference = GetInt(byteQueue); break; case GlobalTokens.Names.STR_T: int strtReference = GetInt(byteQueue); if (stringTable.ContainsString(strtReference)) { StringTableItem stringTableItem = stringTable.GetString(strtReference); if (isAttribute) { activeAttribute.InnerText += stringTableItem.Value; } else { activeNode.AppendChild(CreateTextNode(stringTableItem.Value)); } } break; case GlobalTokens.Names.EXT_0: case GlobalTokens.Names.EXT_1: case GlobalTokens.Names.EXT_2: //TODO EXT_x are not implemented yet break; case GlobalTokens.Names.OPAQUE: int opaqueLength = GetInt(byteQueue); byte[] opaqueByteArray = GetByteArray(byteQueue, opaqueLength); List<string> opaqueHexList = new List<string>(); foreach (byte opaqueByteItem in opaqueByteArray) { opaqueHexList.Add(opaqueByteItem.ToString("X2")); } activeNode.InnerText = String.Join("", opaqueHexList.ToArray()); break; } } else if (!isAttribute) { tagHasAttributes = IsBitSet(byteItem, 7); tagHasContent = IsBitSet(byteItem, 6); byteItem &= 127; byteItem &= 63; XmlElement xmlElement; if (tagCodeSpace.GetCodePage(currentTagCodePage).ContainsTag(byteItem)) { var codePage = tagCodeSpace.GetCodePage(currentTagCodePage); string tagValue = codePage.GetTag(byteItem).Name; xmlElement = CreateElement(tagValue); } else { string tagValue = "Tag_" + byteItem.ToString("X2"); xmlElement = CreateElement(tagValue); } activeNode.AppendChild(xmlElement); if (tagHasContent || tagHasAttributes) { activeNode = xmlElement; if (tagHasAttributes) { isAttribute = true; } } } else { if (byteItem < 128) { if (attributeCodeSpace.GetCodePage(currentAttributeCodePage).ContainsAttributeStart(byteItem)) { AttributeStart attributeStart = attributeCodeSpace.GetCodePage(currentAttributeCodePage).GetAttributeStart(byteItem); XmlAttribute xmlAttribute = CreateAttribute(attributeStart.Name); xmlAttribute.InnerText = attributeStart.Prefix; activeNode.Attributes.Append(xmlAttribute); activeAttribute = xmlAttribute; } else { XmlAttribute xmlAttribute = CreateAttribute("attribute_" + byteItem.ToString("X2")); activeNode.Attributes.Append(xmlAttribute); activeAttribute = xmlAttribute; } } else { if (attributeCodeSpace.GetCodePage(currentAttributeCodePage).ContainsAttributeValue(byteItem)) { AttributeValue attributeValue = attributeCodeSpace.GetCodePage(currentAttributeCodePage).GetAttributeValue(byteItem); activeAttribute.InnerText += attributeValue.Value; } } } } }
private void DecodeWBXML(byte[] bytes) { currentTagCodePage = 0; currentAttributeCodePage = 0; bool isAttribute = false; bool tagHasAttributes = false; bool tagHasContent = false; XmlNode activeNode = this; XmlAttribute activeAttribute = null; Queue <byte> byteQueue = new Queue <byte>(bytes); //Get the WBXML version number; versionNumber = ((double)byteQueue.Dequeue() / 10.0) + 1.0; //Get the value of the public identifier publicIdentifier = GetInt(byteQueue); if (publicIdentifier == 0) { //TODO the publicIdentifier is defined as string inside the stringtable int publicIdentifierStringTableIndex = GetInt(byteQueue); } //Get the charset for text encoding textEncoding = IANACharacterSets.GetEncoding(GetInt(byteQueue)); XmlDeclaration declaration = CreateXmlDeclaration("1.0", textEncoding.WebName, null); activeNode.AppendChild(declaration); //Get the string table (use of the string table is not implemented) int stringTableLength = GetInt(byteQueue); if (stringTableLength > 0) { Queue <byte> byteStringTableQueue = new Queue <byte>(); for (int i = 0; i < stringTableLength; i++) { byteStringTableQueue.Enqueue(byteQueue.Dequeue()); } List <string> stringTableList = new List <string>(); while (byteStringTableQueue.Count > 0) { string stringTableItem = GetString(byteStringTableQueue); stringTableList.Add(stringTableItem); } stringTable = new StringTable(stringTableList.ToArray()); } //WBXML body while (byteQueue.Count > 0) { byte byteItem = byteQueue.Dequeue(); if (globalTokens.Contains(byteItem)) { GlobalTokens.Names globalToken = globalTokens.GetToken(byteItem); switch (globalToken) { case GlobalTokens.Names.SWITCH_PAGE: currentTagCodePage = (int)byteQueue.Dequeue(); break; case GlobalTokens.Names.END: if (isAttribute) { isAttribute = false; activeAttribute = null; if (!tagHasContent) { activeNode = activeNode.ParentNode; } } else { activeNode = activeNode.ParentNode; } break; case GlobalTokens.Names.ENTITY: //TODO ENTITY is not implemented yet int entityLength = GetInt(byteQueue); break; case GlobalTokens.Names.STR_I: if (isAttribute) { activeAttribute.InnerText += GetString(byteQueue); } else { activeNode.AppendChild(CreateTextNode(GetString(byteQueue))); } break; case GlobalTokens.Names.LITERAL: case GlobalTokens.Names.LITERAL_A: case GlobalTokens.Names.LITERAL_AC: case GlobalTokens.Names.LITERAL_C: //TODO LITERALs are not implemented yet int literalReference = GetInt(byteQueue); break; case GlobalTokens.Names.EXT_I_0: case GlobalTokens.Names.EXT_I_1: case GlobalTokens.Names.EXT_I_2: //TODO EXT_I_x are not implemented yet string extiString = GetString(byteQueue); break; case GlobalTokens.Names.PI: //TODO PI is not implemented yet List <byte> piByteList = new List <byte>(); while (byteQueue.Peek() != (byte)GlobalTokens.Names.END) { piByteList.Add(byteQueue.Dequeue()); } byteQueue.Dequeue(); break; case GlobalTokens.Names.EXT_T_0: case GlobalTokens.Names.EXT_T_1: case GlobalTokens.Names.EXT_T_2: //TODO EXT_T_x are not implemented yet int exttReference = GetInt(byteQueue); break; case GlobalTokens.Names.STR_T: int strtReference = GetInt(byteQueue); if (stringTable.ContainsString(strtReference)) { StringTableItem stringTableItem = stringTable.GetString(strtReference); if (isAttribute) { activeAttribute.InnerText += stringTableItem.Value; } else { activeNode.AppendChild(CreateTextNode(stringTableItem.Value)); } } break; case GlobalTokens.Names.EXT_0: case GlobalTokens.Names.EXT_1: case GlobalTokens.Names.EXT_2: //TODO EXT_x are not implemented yet break; case GlobalTokens.Names.OPAQUE: int opaqueLength = GetInt(byteQueue); byte[] opaqueByteArray = GetByteArray(byteQueue, opaqueLength); List <string> opaqueHexList = new List <string>(); foreach (byte opaqueByteItem in opaqueByteArray) { opaqueHexList.Add(opaqueByteItem.ToString("X2")); } activeNode.InnerText = String.Join("", opaqueHexList.ToArray()); break; } } else if (!isAttribute) { tagHasAttributes = IsBitSet(byteItem, 7); tagHasContent = IsBitSet(byteItem, 6); byteItem &= 127; byteItem &= 63; XmlElement xmlElement; if (tagCodeSpace.GetCodePage(currentTagCodePage).ContainsTag(byteItem)) { var codePage = tagCodeSpace.GetCodePage(currentTagCodePage); string tagValue = codePage.GetTag(byteItem).Name; xmlElement = CreateElement(tagValue); } else { string tagValue = "Tag_" + byteItem.ToString("X2"); xmlElement = CreateElement(tagValue); } activeNode.AppendChild(xmlElement); if (tagHasContent || tagHasAttributes) { activeNode = xmlElement; if (tagHasAttributes) { isAttribute = true; } } } else { if (byteItem < 128) { if (attributeCodeSpace.GetCodePage(currentAttributeCodePage).ContainsAttributeStart(byteItem)) { AttributeStart attributeStart = attributeCodeSpace.GetCodePage(currentAttributeCodePage).GetAttributeStart(byteItem); XmlAttribute xmlAttribute = CreateAttribute(attributeStart.Name); xmlAttribute.InnerText = attributeStart.Prefix; activeNode.Attributes.Append(xmlAttribute); activeAttribute = xmlAttribute; } else { XmlAttribute xmlAttribute = CreateAttribute("attribute_" + byteItem.ToString("X2")); activeNode.Attributes.Append(xmlAttribute); activeAttribute = xmlAttribute; } } else { if (attributeCodeSpace.GetCodePage(currentAttributeCodePage).ContainsAttributeValue(byteItem)) { AttributeValue attributeValue = attributeCodeSpace.GetCodePage(currentAttributeCodePage).GetAttributeValue(byteItem); activeAttribute.InnerText += attributeValue.Value; } } } } }