private void Parse(byte[] contents) { if (contents.Length < 10) { throw new ArgumentOutOfRangeException(string.Format("length mismatch - expected at least 10, got {0} - {1}", contents.Length.ToString(), BitConverter.ToString(contents))); } byte messageId = contents[0]; int messageLength = 0; messageLength |= contents[1] << 8; messageLength |= contents[2]; int messageBodyLength = messageLength - 7; byte[] messageBody = new byte[messageBodyLength]; Array.Copy(contents, 10, messageBody, 0, messageBodyLength); if ((MessageId)messageId == MessageId.InventoryCommand) { if (messageBody.Length > 0) { InventoryType inventoryType = (InventoryType)messageBody[0]; if (inventoryType == InventoryType.ListActiveKsetIds) { KmmBody kmmBody = new InventoryCommandListActiveKsetIds(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else if (inventoryType == InventoryType.ListRsiItems) { KmmBody kmmBody = new InventoryCommandListRsiItems(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else if (inventoryType == InventoryType.ListActiveKeys) { KmmBody kmmBody = new InventoryCommandListActiveKeys(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else { throw new Exception(string.Format("unknown inventory command type: 0x{0:X2}", (byte)inventoryType)); } } else { throw new Exception("inventory command length zero"); } } else if ((MessageId)messageId == MessageId.InventoryResponse) { if (messageBody.Length > 0) { InventoryType inventoryType = (InventoryType)messageBody[0]; if (inventoryType == InventoryType.ListActiveKsetIds) { KmmBody kmmBody = new InventoryResponseListActiveKsetIds(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else if (inventoryType == InventoryType.ListActiveKeys) { KmmBody kmmBody = new InventoryResponseListActiveKeys(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else { throw new Exception(string.Format("unknown inventory response type: 0x{0:X2}", (byte)inventoryType)); } } else { throw new Exception("inventory response length zero"); } } else if ((MessageId)messageId == MessageId.ModifyKeyCommand) { KmmBody kmmBody = new ModifyKeyCommand(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else if ((MessageId)messageId == MessageId.NegativeAcknowledgment) { KmmBody kmmBody = new NegativeAcknowledgment(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else if ((MessageId)messageId == MessageId.RekeyAcknowledgment) { KmmBody kmmBody = new RekeyAcknowledgment(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else if ((MessageId)messageId == MessageId.ZeroizeResponse) { KmmBody kmmBody = new ZeroizeResponse(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else { throw new Exception(string.Format("unknown kmm - message id: 0x{0:X2}", messageId)); } }
private void Parse(byte mfid, byte[] contents) { if (contents.Length < 10) { throw new ArgumentOutOfRangeException(string.Format("length mismatch - expected at least 10, got {0} - {1}", contents.Length.ToString(), BitConverter.ToString(contents))); } byte messageId = contents[0]; int messageLength = 0; messageLength |= contents[1] << 8; messageLength |= contents[2]; int messageBodyLength = messageLength - 7; byte[] messageBody = new byte[messageBodyLength]; Array.Copy(contents, 10, messageBody, 0, messageBodyLength); if ((MessageId)messageId == MessageId.InventoryCommand) { if (messageBody.Length > 0) { InventoryType inventoryType = (InventoryType)messageBody[0]; if (inventoryType == InventoryType.ListActiveKsetIds) { KmmBody kmmBody = new InventoryCommandListActiveKsetIds(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else if (inventoryType == InventoryType.ListRsiItems) { KmmBody kmmBody = new InventoryCommandListRsiItems(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else if (inventoryType == InventoryType.ListActiveKeys) { KmmBody kmmBody = new InventoryCommandListActiveKeys(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else { throw new Exception(string.Format("unknown inventory command type: 0x{0:X2}", (byte)inventoryType)); } } else { throw new Exception("inventory command length zero"); } } else if ((MessageId)messageId == MessageId.InventoryResponse) { if (messageBody.Length > 0) { InventoryType inventoryType = (InventoryType)messageBody[0]; if (inventoryType == InventoryType.ListActiveKsetIds) { KmmBody kmmBody = new InventoryResponseListActiveKsetIds(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else if (inventoryType == InventoryType.ListActiveKeys) { KmmBody kmmBody = new InventoryResponseListActiveKeys(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else if (inventoryType == InventoryType.ListRsiItems) { //cg KmmBody kmmBody = new InventoryResponseListRsiItems(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else if (inventoryType == InventoryType.ListMnp) { //cg KmmBody kmmBody = new InventoryResponseListMnp(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else if (inventoryType == InventoryType.ListKmfRsi) { //cg KmmBody kmmBody = new InventoryResponseListKmfRsi(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else if (inventoryType == InventoryType.ListKeysetTaggingInfo) { //cg KmmBody kmmBody = new InventoryResponseListKeysetTaggingInfo(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else { throw new Exception(string.Format("unknown inventory response type: 0x{0:X2}", (byte)inventoryType)); } } else { throw new Exception("inventory response length zero"); } } else if ((MessageId)messageId == MessageId.ModifyKeyCommand) { KmmBody kmmBody = new ModifyKeyCommand(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else if ((MessageId)messageId == MessageId.NegativeAcknowledgment) { KmmBody kmmBody = new NegativeAcknowledgment(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else if ((MessageId)messageId == MessageId.RekeyAcknowledgment) { KmmBody kmmBody = new RekeyAcknowledgment(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else if ((MessageId)messageId == MessageId.ZeroizeResponse) { KmmBody kmmBody = new ZeroizeResponse(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else if ((MessageId)messageId == MessageId.LoadConfigResponse) { //cg KmmBody kmmBody = new LoadConfigResponse(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else if ((MessageId)messageId == MessageId.ChangeRsiResponse) { //cg KmmBody kmmBody = new ChangeRsiResponse(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else if ((MessageId)messageId == MessageId.ChangeoverResponse) { //cg KmmBody kmmBody = new ChangeoverResponse(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else if ((MessageId)messageId == MessageId.SessionControl) { if (messageBody.Length > 0) { byte version = messageBody[0]; if (mfid == 0x00 && version == 0x00) { KmmBody kmmBody = new Mfid90SessionControlVer1(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else if (mfid == 0x90 && version == 0x01) { KmmBody kmmBody = new Mfid90SessionControlVer1(); kmmBody.Parse(messageBody); KmmBody = kmmBody; } else { throw new Exception(string.Format("unknown session control - mfid: 0x{0:X2}, version: 0x{1:X2}", mfid, version)); } } else { throw new Exception("session control body length zero"); } } else { throw new Exception(string.Format("unknown kmm - message id: 0x{0:X2}", messageId)); } }