types.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2014 Richard Lehane. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package types
  15. import (
  16. "encoding/binary"
  17. "errors"
  18. )
  19. // MakeVariant is defined in vectorArray.go. It calls Evaluate, which refers to the MakeTypes map, so must add at runtime
  20. func init() { MakeTypes[VT_VARIANT] = MakeVariant }
  21. var (
  22. ErrType = errors.New("msoleps: error coercing byte stream to type")
  23. ErrUnknownType = errors.New("msoleps: unknown type error")
  24. )
  25. type Type interface {
  26. String() string
  27. Type() string
  28. Length() int
  29. }
  30. const (
  31. vector uint16 = iota + 1
  32. array
  33. )
  34. func Evaluate(b []byte) (Type, error) {
  35. if len(b) < 4 {
  36. return I1(0), ErrType
  37. }
  38. id := TypeID(binary.LittleEndian.Uint16(b[:2]))
  39. f, ok := MakeTypes[id]
  40. if !ok {
  41. return I1(0), ErrUnknownType
  42. }
  43. switch binary.LittleEndian.Uint16(b[2:4]) {
  44. case vector:
  45. return MakeVector(f, b[4:])
  46. case array:
  47. return MakeArray(f, b[4:])
  48. }
  49. return f(b[4:])
  50. }
  51. type TypeID uint16
  52. const (
  53. VT_EMPTY TypeID = iota // 0x00
  54. VT_NULL
  55. VT_I2
  56. VT_I4
  57. VT_R4
  58. VT_R8
  59. VT_CY
  60. VT_DATE
  61. VT_BSTR
  62. _
  63. VT_ERROR
  64. VT_BOOL
  65. VT_VARIANT
  66. _
  67. VT_DECIMAL
  68. _
  69. VT_I1
  70. VT_U1
  71. VT_UI2
  72. VT_UI4
  73. VT_I8
  74. VT_UI8
  75. VT_INT
  76. VT_UINT //0x17
  77. _ = iota + 5
  78. VT_LPSTR //0x1E
  79. VT_LPWSTR
  80. VT_FILETIME = iota + 0x25 // 0x40
  81. VT_BLOB
  82. VT_STREAM
  83. VT_STORAGE
  84. VT_STREAMED_OBJECT
  85. VT_STORED_OBJECT
  86. VT_BLOB_OBJECT
  87. VT_CF
  88. VT_CLSID
  89. VT_VERSIONED_STREAM // 0x49
  90. )
  91. type MakeType func([]byte) (Type, error)
  92. var MakeTypes map[TypeID]MakeType = map[TypeID]MakeType{
  93. VT_I2: MakeI2,
  94. VT_I4: MakeI4,
  95. VT_R4: MakeR4,
  96. VT_R8: MakeR8,
  97. VT_CY: MakeCurrency,
  98. VT_DATE: MakeDate,
  99. VT_BSTR: MakeCodeString,
  100. VT_BOOL: MakeBool,
  101. VT_DECIMAL: MakeDecimal,
  102. VT_I1: MakeI1,
  103. VT_U1: MakeUI1,
  104. VT_UI2: MakeUI2,
  105. VT_UI4: MakeUI4,
  106. VT_I8: MakeI8,
  107. VT_UI8: MakeUI8,
  108. VT_INT: MakeI4,
  109. VT_UINT: MakeUI4,
  110. VT_LPSTR: MakeCodeString,
  111. VT_LPWSTR: MakeUnicode,
  112. VT_FILETIME: MakeFileTime,
  113. VT_CLSID: MakeGuid,
  114. }