msoleps.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 msoleps implements a reader for Microsoft OLE Property Set Data structures,
  15. // (http://msdn.microsoft.com/en-au/library/dd942421.aspx) a generic persistence format
  16. // for simple typed metadata
  17. // Example:
  18. // file, _ := os.Open("test/test.doc")
  19. // defer file.Close()
  20. // doc, err := mscfb.NewReader(file)
  21. // if err != nil {
  22. // log.Fatal(err)
  23. // }
  24. // props := msoleps.New()
  25. // for entry, err := doc.Next(); err == nil; entry, err = doc.Next() {
  26. // if msoleps.IsMSOLEPS(entry.Initial) {
  27. // if oerr := props.Reset(doc); oerr != nil {
  28. // log.Fatal(oerr)
  29. // }
  30. // for prop := range props.Property {
  31. // fmt.Printf("Name: %s; Type: %s; Value: %v", prop.Name, prop.Type(), prop)
  32. // }
  33. // }
  34. // }
  35. package msoleps
  36. import (
  37. "bytes"
  38. "encoding/binary"
  39. "errors"
  40. "io"
  41. "github.com/richardlehane/msoleps/types"
  42. )
  43. var (
  44. ErrFormat = errors.New("msoleps: not a valid msoleps stream")
  45. ErrRead = errors.New("msoleps: error reading msoleps stream")
  46. ErrSeek = errors.New("msoleps: can't seek backwards")
  47. )
  48. // IsMSOLEPS checks the first uint16 character of an mscfb name to test if it is a MSOLEPS stream
  49. func IsMSOLEPS(i uint16) bool {
  50. if i == 0x0005 {
  51. return true
  52. }
  53. return false
  54. }
  55. // Reader is a reader for MS OLE Property Set Data structures
  56. type Reader struct {
  57. Property []*Property
  58. b *bytes.Buffer
  59. buf []byte
  60. *propertySetStream
  61. pSets [2]*propertySet
  62. }
  63. func New() *Reader {
  64. r := &Reader{}
  65. r.b = &bytes.Buffer{}
  66. return r
  67. }
  68. func (r *Reader) Reset(rdr io.Reader) error {
  69. r.b.Reset()
  70. return r.start(rdr)
  71. }
  72. func NewFrom(rdr io.Reader) (*Reader, error) {
  73. r := &Reader{}
  74. r.b = &bytes.Buffer{}
  75. return r, r.start(rdr)
  76. }
  77. func (r *Reader) start(rdr io.Reader) error {
  78. if _, err := r.b.ReadFrom(rdr); err != nil {
  79. return ErrRead
  80. }
  81. r.buf = r.b.Bytes()
  82. // read the header (property stream details)
  83. pss, err := makePropertySetStream(r.buf)
  84. if err != nil {
  85. return err
  86. }
  87. // sanity checks to find obvious errors
  88. switch {
  89. case pss.byteOrder != 0xFFFE, pss.version > 0x0001, pss.numPropertySets > 0x00000002:
  90. return ErrFormat
  91. }
  92. r.propertySetStream = pss
  93. // identify the property identifiers and offsets
  94. ps, err := r.getPropertySet(pss.offsetA)
  95. if err != nil {
  96. return err
  97. }
  98. plen := len(ps.idsOffs)
  99. r.pSets[0] = ps
  100. var psb *propertySet
  101. if pss.numPropertySets == 2 {
  102. psb, err = r.getPropertySet(pss.offsetB)
  103. if err != nil {
  104. return err
  105. }
  106. r.pSets[1] = psb
  107. plen += len(psb.idsOffs)
  108. }
  109. r.Property = make([]*Property, plen)
  110. dict, ok := propertySets[pss.fmtidA]
  111. if !ok {
  112. dict = ps.dict
  113. if dict == nil {
  114. dict = make(map[uint32]string)
  115. }
  116. }
  117. dict = addDefaults(dict)
  118. for i, v := range ps.idsOffs {
  119. r.Property[i] = &Property{}
  120. r.Property[i].Name = dict[v.id]
  121. // don't try to evaluate dictionary property
  122. if v.id == 0x00000000 {
  123. r.Property[i].T = types.Null{}
  124. continue
  125. }
  126. t, _ := types.Evaluate(r.buf[int(v.offset+pss.offsetA):])
  127. if t.Type() == "CodeString" {
  128. cs := t.(*types.CodeString)
  129. cs.SetId(ps.code)
  130. t = types.Type(cs)
  131. }
  132. r.Property[i].T = t
  133. }
  134. if pss.numPropertySets != 2 {
  135. return nil
  136. }
  137. dict, ok = propertySets[pss.fmtidB]
  138. if !ok {
  139. dict = psb.dict
  140. if dict == nil {
  141. dict = make(map[uint32]string)
  142. }
  143. }
  144. dict = addDefaults(dict)
  145. for i, v := range psb.idsOffs {
  146. i += len(ps.idsOffs)
  147. r.Property[i] = &Property{}
  148. r.Property[i].Name = dict[v.id]
  149. // don't try to evaluate dictionary property
  150. if v.id == 0x00000000 {
  151. r.Property[i].T = types.Null{}
  152. continue
  153. }
  154. t, _ := types.Evaluate(r.buf[int(v.offset+pss.offsetB):])
  155. if t.Type() == "CodeString" {
  156. cs := t.(*types.CodeString)
  157. cs.SetId(psb.code)
  158. t = types.Type(cs)
  159. }
  160. r.Property[i].T = t
  161. }
  162. return nil
  163. }
  164. func (r *Reader) getPropertySet(o uint32) (*propertySet, error) {
  165. pSet := &propertySet{}
  166. pSet.size = binary.LittleEndian.Uint32(r.buf[int(o) : int(o)+4])
  167. pSet.numProperties = binary.LittleEndian.Uint32(r.buf[int(o)+4 : int(o)+8])
  168. pSet.idsOffs = make([]propertyIDandOffset, int(pSet.numProperties))
  169. var dictOff uint32
  170. for i := range pSet.idsOffs {
  171. this := i*8 + 8 + int(o)
  172. pSet.idsOffs[i].id = binary.LittleEndian.Uint32(r.buf[this : this+4])
  173. pSet.idsOffs[i].offset = binary.LittleEndian.Uint32(r.buf[this+4 : this+8])
  174. switch pSet.idsOffs[i].id {
  175. case 0x00000000:
  176. dictOff = pSet.idsOffs[i].offset
  177. case 0x00000001:
  178. off := int(pSet.idsOffs[i].offset + o)
  179. pSet.code = types.CodePageID(binary.LittleEndian.Uint16(r.buf[off+4 : off+6]))
  180. }
  181. }
  182. if dictOff > 0 {
  183. var err error
  184. pSet.dict, err = r.getDictionary(dictOff+o, pSet.code)
  185. if err != nil {
  186. return nil, err
  187. }
  188. }
  189. return pSet, nil
  190. }
  191. func (r *Reader) getDictionary(o uint32, code types.CodePageID) (map[uint32]string, error) {
  192. b := r.buf[int(o):]
  193. e := 4
  194. if len(b) < e {
  195. return nil, ErrFormat
  196. }
  197. num := int(binary.LittleEndian.Uint32(b[:e]))
  198. if num == 0 {
  199. return nil, nil
  200. }
  201. dict := make(map[uint32]string)
  202. for i := 0; i < num; i++ {
  203. if len(b[e:]) < 8 {
  204. return nil, ErrFormat
  205. }
  206. id, l := binary.LittleEndian.Uint32(b[e:e+4]), binary.LittleEndian.Uint32(b[e+4:e+8])
  207. var s types.Type
  208. var err error
  209. if code == 0x04B0 {
  210. var pad int
  211. if l%2 != 0 {
  212. pad = 2
  213. }
  214. s, err = types.MakeUnicode(b[e+4:])
  215. if err != nil {
  216. return nil, ErrFormat
  217. }
  218. e = e + 8 + pad + int(l)*2
  219. } else {
  220. s, err = types.MakeCodeString(b[e+4:])
  221. if err != nil {
  222. return nil, ErrFormat
  223. }
  224. cs := s.(*types.CodeString)
  225. cs.SetId((code))
  226. s = cs
  227. e = e + 8 + int(l)
  228. }
  229. dict[id] = s.String()
  230. }
  231. return dict, nil
  232. }