EncodedItem.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #region License
  2. /* Copyright 2011, 2013 James F. Bellinger <http://www.zer7.com/software/hidsharp>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  13. #endregion
  14. using System;
  15. using System.Collections.Generic;
  16. namespace HidSharp.ReportDescriptors
  17. {
  18. public class EncodedItem
  19. {
  20. public EncodedItem()
  21. {
  22. Data = new List<byte>();
  23. }
  24. public void Clear()
  25. {
  26. Data.Clear(); Tag = 0; Type = ItemType.Main;
  27. }
  28. public byte DataAt(int index)
  29. {
  30. return index >= 0 && index < Data.Count ? Data[index] : (byte)0;
  31. }
  32. static byte GetByte(IList<byte> buffer, ref int offset, ref int count)
  33. {
  34. if (count <= 0) { return 0; } else { count--; }
  35. return offset >= 0 && offset < buffer.Count ? buffer[offset++] : (byte)0;
  36. }
  37. public int Decode(IList<byte> buffer, int offset, int count)
  38. {
  39. Throw.If.OutOfRange(buffer, offset, count);
  40. Clear(); int startCount = count;
  41. byte header = GetByte(buffer, ref offset, ref count);
  42. int size = header & 0x3; if (size == 3) { size = 4; }
  43. Type = (ItemType)((header >> 2) & 0x3); Tag = (byte)(header >> 4);
  44. for (int i = 0; i < size; i++) { Data.Add(GetByte(buffer, ref offset, ref count)); }
  45. return startCount - count;
  46. }
  47. public static IEnumerable<EncodedItem> DecodeRaw(IList<byte> buffer, int offset, int count)
  48. {
  49. Throw.If.OutOfRange(buffer, offset, count);
  50. while (count > 0)
  51. {
  52. EncodedItem item = new EncodedItem();
  53. int bytes = item.Decode(buffer, offset, count);
  54. offset += bytes; count -= bytes;
  55. yield return item;
  56. }
  57. }
  58. public static IEnumerable<EncodedItem> DecodeHIDDT(IList<byte> buffer, int offset, int count)
  59. {
  60. Throw.If.OutOfRange(buffer, offset, count);
  61. while (count > 34)
  62. {
  63. EncodedItem item = new EncodedItem();
  64. int bytes = item.Decode(buffer, offset + 34, count - 34);
  65. offset += 10; count -= 10;
  66. yield return item;
  67. }
  68. }
  69. public void Encode(IList<byte> buffer)
  70. {
  71. Throw.If.Null(buffer, "buffer");
  72. if (buffer == null) { throw new ArgumentNullException("buffer"); }
  73. if (!IsShortTag) { return; }
  74. byte size = DataSize;
  75. buffer.Add((byte)((size == 4 ? (byte)3 : size) | (byte)Type << 2 | Tag << 4));
  76. foreach (byte @byte in Data) { buffer.Add(@byte); }
  77. }
  78. public static void EncodeRaw(IList<byte> buffer, IEnumerable<EncodedItem> items)
  79. {
  80. Throw.If.Null(buffer, "buffer").Null(items, "items");
  81. foreach (EncodedItem item in items) { item.Encode(buffer); }
  82. }
  83. public IList<byte> Data
  84. {
  85. get;
  86. private set;
  87. }
  88. public byte DataSize
  89. {
  90. get { return (byte)(IsShortTag ? Data.Count : 0); }
  91. }
  92. public uint DataValue
  93. {
  94. get
  95. {
  96. if (!IsShortTag) { return 0; }
  97. return (uint)(DataAt(0) | DataAt(1) << 8 | DataAt(2) << 16 | DataAt(3) << 24);
  98. }
  99. set
  100. {
  101. Data.Clear();
  102. Data.Add((byte)value);
  103. if (value > 0xff) { Data.Add((byte)(value >> 8)); }
  104. if (value > 0xffff) { Data.Add((byte)(value >> 16)); Data.Add((byte)(value >> 24)); }
  105. }
  106. }
  107. public bool DataValueMayBeNegative
  108. {
  109. get { return IsShortTag && Data.Count > 0 && (Data[Data.Count - 1] & 0x80) != 0; }
  110. }
  111. public int DataValueSigned
  112. {
  113. get
  114. {
  115. if (!IsShortTag) { return 0; }
  116. return Data.Count == 4 ? (int)DataValue :
  117. Data.Count == 2 ? (short)DataValue :
  118. Data.Count == 1 ? (sbyte)DataValue : (sbyte)0;
  119. }
  120. set
  121. {
  122. if (value == 0)
  123. { DataValue = (uint)value; }
  124. else if (value >= sbyte.MinValue && value <= sbyte.MaxValue)
  125. { DataValue = (uint)(sbyte)value; if (value < 0) { Data.Add(0); } }
  126. else if (value >= short.MinValue && value <= short.MaxValue)
  127. { DataValue = (uint)(short)value; if (value < 0) { Data.Add(0); Data.Add(0); } }
  128. else
  129. { DataValue = (uint)value; }
  130. }
  131. }
  132. public int EncodedSize
  133. {
  134. get { return IsShortTag ? (DataSize + 1) : 0; }
  135. }
  136. public byte Tag
  137. {
  138. get;
  139. set;
  140. }
  141. public GlobalItemTag TagForGlobal
  142. {
  143. get { return (GlobalItemTag)Tag; }
  144. }
  145. public LocalItemTag TagForLocal
  146. {
  147. get { return (LocalItemTag)Tag; }
  148. }
  149. public MainItemTag TagForMain
  150. {
  151. get { return (MainItemTag)Tag; }
  152. }
  153. public ItemType Type
  154. {
  155. get;
  156. set;
  157. }
  158. public bool IsShortTag
  159. {
  160. get
  161. {
  162. return !IsLongTag &&
  163. (Data.Count == 0 || Data.Count == 1 || Data.Count == 2 || Data.Count == 4);
  164. }
  165. }
  166. public bool IsLongTag
  167. {
  168. get
  169. {
  170. return Tag == 15 && Type == ItemType.Reserved && Data.Count >= 2;
  171. }
  172. }
  173. }
  174. }