primitives.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package ndr
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "math"
  6. )
  7. // Byte sizes of primitive types
  8. const (
  9. SizeBool = 1
  10. SizeChar = 1
  11. SizeUint8 = 1
  12. SizeUint16 = 2
  13. SizeUint32 = 4
  14. SizeUint64 = 8
  15. SizeEnum = 2
  16. SizeSingle = 4
  17. SizeDouble = 8
  18. SizePtr = 4
  19. )
  20. // Bool is an NDR Boolean which is a logical quantity that assumes one of two values: TRUE or FALSE.
  21. // NDR represents a Boolean as one octet.
  22. // It represents a value of FALSE as a zero octet, an octet in which every bit is reset.
  23. // It represents a value of TRUE as a non-zero octet, an octet in which one or more bits are set.
  24. // Char is an NDR character.
  25. // NDR represents a character as one octet.
  26. // Characters have two representation formats: ASCII and EBCDIC.
  27. // USmall is an unsigned 8 bit integer
  28. // UShort is an unsigned 16 bit integer
  29. // ULong is an unsigned 32 bit integer
  30. // UHyper is an unsigned 64 bit integer
  31. // Small is an signed 8 bit integer
  32. // Short is an signed 16 bit integer
  33. // Long is an signed 32 bit integer
  34. // Hyper is an signed 64 bit integer
  35. // Enum is the NDR representation of enumerated types as signed short integers (2 octets)
  36. // Single is an NDR defined single-precision floating-point data type
  37. // Double is an NDR defined double-precision floating-point data type
  38. // readBool reads a byte representing a boolean.
  39. // NDR represents a Boolean as one octet.
  40. // It represents a value of FALSE as a zero octet, an octet in which every bit is reset.
  41. // It represents a value of TRUE as a non-zero octet, an octet in which one or more bits are set.
  42. func (dec *Decoder) readBool() (bool, error) {
  43. i, err := dec.readUint8()
  44. if err != nil {
  45. return false, err
  46. }
  47. if i != 0 {
  48. return true, nil
  49. }
  50. return false, nil
  51. }
  52. // readChar reads bytes representing a 8bit ASCII integer cast to a rune.
  53. func (dec *Decoder) readChar() (rune, error) {
  54. var r rune
  55. a, err := dec.readUint8()
  56. if err != nil {
  57. return r, err
  58. }
  59. return rune(a), nil
  60. }
  61. // readUint8 reads bytes representing a 8bit unsigned integer.
  62. func (dec *Decoder) readUint8() (uint8, error) {
  63. b, err := dec.r.ReadByte()
  64. if err != nil {
  65. return uint8(0), err
  66. }
  67. return uint8(b), nil
  68. }
  69. // readUint16 reads bytes representing a 16bit unsigned integer.
  70. func (dec *Decoder) readUint16() (uint16, error) {
  71. dec.ensureAlignment(SizeUint16)
  72. b, err := dec.readBytes(SizeUint16)
  73. if err != nil {
  74. return uint16(0), err
  75. }
  76. return dec.ch.Endianness.Uint16(b), nil
  77. }
  78. // readUint32 reads bytes representing a 32bit unsigned integer.
  79. func (dec *Decoder) readUint32() (uint32, error) {
  80. dec.ensureAlignment(SizeUint32)
  81. b, err := dec.readBytes(SizeUint32)
  82. if err != nil {
  83. return uint32(0), err
  84. }
  85. return dec.ch.Endianness.Uint32(b), nil
  86. }
  87. // readUint32 reads bytes representing a 32bit unsigned integer.
  88. func (dec *Decoder) readUint64() (uint64, error) {
  89. dec.ensureAlignment(SizeUint64)
  90. b, err := dec.readBytes(SizeUint64)
  91. if err != nil {
  92. return uint64(0), err
  93. }
  94. return dec.ch.Endianness.Uint64(b), nil
  95. }
  96. func (dec *Decoder) readInt8() (int8, error) {
  97. dec.ensureAlignment(SizeUint8)
  98. b, err := dec.readBytes(SizeUint8)
  99. if err != nil {
  100. return 0, err
  101. }
  102. var i int8
  103. buf := bytes.NewReader(b)
  104. err = binary.Read(buf, dec.ch.Endianness, &i)
  105. if err != nil {
  106. return 0, err
  107. }
  108. return i, nil
  109. }
  110. func (dec *Decoder) readInt16() (int16, error) {
  111. dec.ensureAlignment(SizeUint16)
  112. b, err := dec.readBytes(SizeUint16)
  113. if err != nil {
  114. return 0, err
  115. }
  116. var i int16
  117. buf := bytes.NewReader(b)
  118. err = binary.Read(buf, dec.ch.Endianness, &i)
  119. if err != nil {
  120. return 0, err
  121. }
  122. return i, nil
  123. }
  124. func (dec *Decoder) readInt32() (int32, error) {
  125. dec.ensureAlignment(SizeUint32)
  126. b, err := dec.readBytes(SizeUint32)
  127. if err != nil {
  128. return 0, err
  129. }
  130. var i int32
  131. buf := bytes.NewReader(b)
  132. err = binary.Read(buf, dec.ch.Endianness, &i)
  133. if err != nil {
  134. return 0, err
  135. }
  136. return i, nil
  137. }
  138. func (dec *Decoder) readInt64() (int64, error) {
  139. dec.ensureAlignment(SizeUint64)
  140. b, err := dec.readBytes(SizeUint64)
  141. if err != nil {
  142. return 0, err
  143. }
  144. var i int64
  145. buf := bytes.NewReader(b)
  146. err = binary.Read(buf, dec.ch.Endianness, &i)
  147. if err != nil {
  148. return 0, err
  149. }
  150. return i, nil
  151. }
  152. // https://en.wikipedia.org/wiki/IEEE_754-1985
  153. func (dec *Decoder) readFloat32() (f float32, err error) {
  154. dec.ensureAlignment(SizeSingle)
  155. b, err := dec.readBytes(SizeSingle)
  156. if err != nil {
  157. return
  158. }
  159. bits := dec.ch.Endianness.Uint32(b)
  160. f = math.Float32frombits(bits)
  161. return
  162. }
  163. func (dec *Decoder) readFloat64() (f float64, err error) {
  164. dec.ensureAlignment(SizeDouble)
  165. b, err := dec.readBytes(SizeDouble)
  166. if err != nil {
  167. return
  168. }
  169. bits := dec.ch.Endianness.Uint64(b)
  170. f = math.Float64frombits(bits)
  171. return
  172. }
  173. // NDR enforces NDR alignment of primitive data; that is, any primitive of size n octets is aligned at a octet stream
  174. // index that is a multiple of n. (In this version of NDR, n is one of {1, 2, 4, 8}.) An octet stream index indicates
  175. // the number of an octet in an octet stream when octets are numbered, beginning with 0, from the first octet in the
  176. // stream. Where necessary, an alignment gap, consisting of octets of unspecified value, precedes the representation
  177. // of a primitive. The gap is of the smallest size sufficient to align the primitive.
  178. func (dec *Decoder) ensureAlignment(n int) {
  179. p := dec.size - dec.r.Buffered()
  180. if s := p % n; s != 0 {
  181. dec.r.Discard(n - s)
  182. }
  183. }