biff.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package xls
  2. /*
  3. biff.go - the BIFF record stream and its strings.
  4. A workbook stream is a flat list of records: a 16-bit type, a 16-bit
  5. length and the payload. A payload longer than 8224 bytes is split into
  6. CONTINUE records, and readers must stitch them back together - with a
  7. twist for strings: when a string's characters run into a CONTINUE, the
  8. continuation starts with a fresh flags byte saying whether the rest is
  9. stored 8-bit ("compressed", Latin-1) or 16-bit (UTF-16LE). So each
  10. stitched record remembers where its CONTINUE parts begin, and the
  11. string reader re-reads the flags at those boundaries.
  12. */
  13. import (
  14. "encoding/binary"
  15. "errors"
  16. "math"
  17. "unicode/utf16"
  18. "imuslab.com/arozos/mod/office/internal/codepage"
  19. )
  20. // record types
  21. const (
  22. rtFormula = 0x0006
  23. rtEOF = 0x000A
  24. rtCalcCount = 0x000C
  25. rtCalcMode = 0x000D
  26. rtPrecision = 0x000E
  27. rtRefMode = 0x000F
  28. rtDelta = 0x0010
  29. rtIteration = 0x0011
  30. rtProtect = 0x0012
  31. rtPassword = 0x0013
  32. rtHeader = 0x0014
  33. rtFooter = 0x0015
  34. rtExternSheet = 0x0017
  35. rtName = 0x0018
  36. rtWindowProtect = 0x0019
  37. rtNote = 0x001C
  38. rtDateMode = 0x0022
  39. rtExternName = 0x0023
  40. rtFilePass = 0x002F
  41. rtFont = 0x0031
  42. rtContinue = 0x003C
  43. rtWindow1 = 0x003D
  44. rtPane = 0x0041
  45. rtCodePage = 0x0042
  46. rtDefColWidth = 0x0055
  47. rtObj = 0x005D
  48. rtColInfo = 0x007D
  49. rtBoundSheet = 0x0085
  50. rtPalette = 0x0092
  51. rtStandardWidth = 0x0099
  52. rtMulRk = 0x00BD
  53. rtMulBlank = 0x00BE
  54. rtRString = 0x00D6
  55. rtXF = 0x00E0
  56. rtMergeCells = 0x00E5
  57. rtMsoDrawingGrp = 0x00EB
  58. rtMsoDrawing = 0x00EC
  59. rtSST = 0x00FC
  60. rtLabelSST = 0x00FD
  61. rtSupBook = 0x01AE
  62. rtCondFmt = 0x01B0
  63. rtCF = 0x01B1
  64. rtHLink = 0x01B8
  65. rtTxo = 0x01B6
  66. rtDimensions = 0x0200
  67. rtBlank = 0x0201
  68. rtNumber = 0x0203
  69. rtLabel = 0x0204
  70. rtBoolErr = 0x0205
  71. rtString = 0x0207
  72. rtRow = 0x0208
  73. rtArray = 0x0221
  74. rtDefaultRowHgt = 0x0225
  75. rtTable = 0x0236
  76. rtWindow2 = 0x023E
  77. rtRK = 0x027E
  78. rtStyle = 0x0293
  79. rtFormat = 0x041E
  80. rtShrFmla = 0x04BC
  81. rtBOF = 0x0809
  82. rtSheetExt = 0x0862
  83. rtXFExt = 0x087D
  84. rtBOF2 = 0x0009 // BIFF2
  85. rtBOF3 = 0x0209 // BIFF3
  86. rtBOF4 = 0x0409 // BIFF4
  87. rtFormula4 = 0x0406 // BIFF4 FORMULA
  88. rtLabel2 = 0x0004 // BIFF2 LABEL
  89. rtFormat4 = 0x001E // BIFF4/5 FORMAT
  90. )
  91. // maxRecords bounds the records read from one stream
  92. const maxRecords = 8 << 20
  93. var errTruncated = errors.New("truncated record")
  94. // record is one record with its CONTINUE parts appended
  95. type record struct {
  96. typ uint16
  97. data []byte
  98. // conts are the offsets in data where a CONTINUE part begins
  99. conts []int
  100. // pos is the offset of the record header in the stream
  101. pos int
  102. }
  103. // readRecords splits a workbook stream into records, folding CONTINUE
  104. // records into the record before them
  105. func readRecords(stream []byte) ([]*record, error) {
  106. var out []*record
  107. pos := 0
  108. for pos+4 <= len(stream) {
  109. typ := binary.LittleEndian.Uint16(stream[pos:])
  110. size := int(binary.LittleEndian.Uint16(stream[pos+2:]))
  111. start := pos + 4
  112. end := start + size
  113. if end > len(stream) {
  114. end = len(stream)
  115. }
  116. payload := stream[start:end]
  117. if typ == rtContinue && len(out) > 0 {
  118. last := out[len(out)-1]
  119. last.conts = append(last.conts, len(last.data))
  120. last.data = append(last.data, payload...)
  121. } else {
  122. // copy so appending a CONTINUE never writes into the stream
  123. out = append(out, &record{typ: typ, data: append([]byte(nil), payload...), pos: pos})
  124. }
  125. if len(out) > maxRecords {
  126. return nil, errors.New("too many records")
  127. }
  128. pos = end
  129. }
  130. return out, nil
  131. }
  132. // reader walks a record's payload; it knows where CONTINUE parts begin so
  133. // strings can be read across them
  134. type reader struct {
  135. b []byte
  136. conts []int
  137. pos int
  138. err error
  139. }
  140. func newReader(r *record) *reader { return &reader{b: r.data, conts: r.conts} }
  141. func (r *reader) left() int { return len(r.b) - r.pos }
  142. func (r *reader) fail() {
  143. if r.err == nil {
  144. r.err = errTruncated
  145. }
  146. r.pos = len(r.b)
  147. }
  148. func (r *reader) u8() uint8 {
  149. if r.pos+1 > len(r.b) {
  150. r.fail()
  151. return 0
  152. }
  153. v := r.b[r.pos]
  154. r.pos++
  155. return v
  156. }
  157. func (r *reader) u16() uint16 {
  158. if r.pos+2 > len(r.b) {
  159. r.fail()
  160. return 0
  161. }
  162. v := binary.LittleEndian.Uint16(r.b[r.pos:])
  163. r.pos += 2
  164. return v
  165. }
  166. func (r *reader) u32() uint32 {
  167. if r.pos+4 > len(r.b) {
  168. r.fail()
  169. return 0
  170. }
  171. v := binary.LittleEndian.Uint32(r.b[r.pos:])
  172. r.pos += 4
  173. return v
  174. }
  175. func (r *reader) f64() float64 {
  176. if r.pos+8 > len(r.b) {
  177. r.fail()
  178. return 0
  179. }
  180. v := math.Float64frombits(binary.LittleEndian.Uint64(r.b[r.pos:]))
  181. r.pos += 8
  182. return v
  183. }
  184. func (r *reader) bytes(n int) []byte {
  185. // against the bytes left: r.pos+n can overflow a 32-bit int
  186. if n < 0 || n > len(r.b)-r.pos {
  187. r.fail()
  188. return nil
  189. }
  190. v := r.b[r.pos : r.pos+n]
  191. r.pos += n
  192. return v
  193. }
  194. func (r *reader) skip(n int) { r.bytes(n) }
  195. // nextCont is the next CONTINUE boundary after the current position
  196. func (r *reader) nextCont() int {
  197. for _, c := range r.conts {
  198. if c > r.pos {
  199. return c
  200. }
  201. }
  202. return len(r.b)
  203. }
  204. // atCont reports whether the reader sits exactly on a CONTINUE boundary
  205. func (r *reader) atCont() bool {
  206. for _, c := range r.conts {
  207. if c == r.pos {
  208. return true
  209. }
  210. if c > r.pos {
  211. break
  212. }
  213. }
  214. return false
  215. }
  216. // chars reads cch characters that start in the given width; at a
  217. // CONTINUE boundary the width is re-read from a new flags byte
  218. func (r *reader) chars(cch int, high bool) string {
  219. if cch <= 0 {
  220. return ""
  221. }
  222. // a character takes at least a byte: never reserve more than is left
  223. u := make([]uint16, 0, min(cch, max(len(r.b)-r.pos, 0)))
  224. for len(u) < cch && r.pos < len(r.b) {
  225. // characters running into a CONTINUE part: it starts with a new
  226. // flags byte (also when only the string's header fitted before it)
  227. if r.atCont() {
  228. high = r.u8()&1 != 0
  229. continue
  230. }
  231. limit := r.nextCont()
  232. if high {
  233. for len(u) < cch && r.pos+2 <= limit {
  234. u = append(u, binary.LittleEndian.Uint16(r.b[r.pos:]))
  235. r.pos += 2
  236. }
  237. if len(u) < cch && r.pos < limit {
  238. r.pos = limit // an odd byte before the boundary
  239. }
  240. } else {
  241. for len(u) < cch && r.pos < limit {
  242. u = append(u, uint16(r.b[r.pos]))
  243. r.pos++
  244. }
  245. }
  246. if len(u) < cch && r.pos >= len(r.b) {
  247. r.fail()
  248. }
  249. }
  250. return string(utf16.Decode(u))
  251. }
  252. // xlString reads an XLUnicodeString (16-bit count) - BIFF8
  253. func (r *reader) xlString() string {
  254. cch := int(r.u16())
  255. flags := r.u8()
  256. return r.chars(cch, flags&1 != 0)
  257. }
  258. // shortString reads a ShortXLUnicodeString (8-bit count) - BIFF8
  259. func (r *reader) shortString() string {
  260. cch := int(r.u8())
  261. flags := r.u8()
  262. return r.chars(cch, flags&1 != 0)
  263. }
  264. // noCchString reads an XLUnicodeStringNoCch of known length - BIFF8
  265. func (r *reader) noCchString(cch int) string {
  266. flags := r.u8()
  267. return r.chars(cch, flags&1 != 0)
  268. }
  269. // richString reads an XLUnicodeRichExtendedString (the SST entries):
  270. // formatting runs and phonetic data are skipped
  271. func (r *reader) richString() string {
  272. cch := int(r.u16())
  273. flags := r.u8()
  274. runs, ext := 0, 0
  275. if flags&0x08 != 0 {
  276. runs = int(r.u16())
  277. }
  278. if flags&0x04 != 0 {
  279. ext = int(int32(r.u32()))
  280. }
  281. s := r.chars(cch, flags&1 != 0)
  282. r.skip(4 * runs)
  283. if ext > 0 {
  284. r.skip(ext)
  285. }
  286. return s
  287. }
  288. // byteString reads 8-bit characters in the workbook's code page (BIFF5)
  289. func (r *reader) byteString(n int, cp codepage.Decoder) string {
  290. return cp.Decode(r.bytes(n))
  291. }