21
0

FIX: issue #47

This commit is contained in:
sergeilem 2018-12-04 00:16:44 +03:00
parent d1f3d0bd6a
commit 6a6d15a25c

View File

@ -3,10 +3,10 @@ package xls
import ( import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"golang.org/x/text/encoding/charmap"
"io" "io"
"os" "os"
"unicode/utf16" "unicode/utf16"
"golang.org/x/text/encoding/charmap"
) )
//xls workbook type //xls workbook type
@ -86,11 +86,6 @@ func (wb *WorkBook) parseBof(buf io.ReadSeeker, b *bof, pre *bof, offset_pre int
case 0x042: // CODEPAGE case 0x042: // CODEPAGE
binary.Read(buf_item, binary.LittleEndian, &wb.Codepage) binary.Read(buf_item, binary.LittleEndian, &wb.Codepage)
case 0x3c: // CONTINUE case 0x3c: // CONTINUE
// step back if previous element not yet completed
if wb.continue_utf16 > 0 {
offset_pre--
}
if pre.Id == 0xfc { if pre.Id == 0xfc {
var size uint16 var size uint16
var err error var err error
@ -102,10 +97,8 @@ func (wb *WorkBook) parseBof(buf io.ReadSeeker, b *bof, pre *bof, offset_pre int
} }
for err == nil && offset_pre < len(wb.sst) { for err == nil && offset_pre < len(wb.sst) {
var str string var str string
if size > 0 { str, err = wb.get_string(buf_item, size)
str, err = wb.get_string(buf_item, size) wb.sst[offset_pre] = wb.sst[offset_pre] + str
wb.sst[offset_pre] = wb.sst[offset_pre] + str
}
if err == io.EOF { if err == io.EOF {
break break
@ -128,7 +121,8 @@ func (wb *WorkBook) parseBof(buf io.ReadSeeker, b *bof, pre *bof, offset_pre int
offset = 0 offset = 0
for ; i < int(info.Count); i++ { for ; i < int(info.Count); i++ {
var err error var err error
if err = binary.Read(buf_item, binary.LittleEndian, &size); err == nil { err = binary.Read(buf_item, binary.LittleEndian, &size)
if err == nil {
var str string var str string
str, err = wb.get_string(buf_item, size) str, err = wb.get_string(buf_item, size)
wb.sst[i] = wb.sst[i] + str wb.sst[i] = wb.sst[i] + str
@ -139,7 +133,7 @@ func (wb *WorkBook) parseBof(buf io.ReadSeeker, b *bof, pre *bof, offset_pre int
} }
} }
offset = i offset = i
case 0x85: // bOUNDSHEET case 0x85: // boundsheet
var bs = new(boundsheet) var bs = new(boundsheet)
binary.Read(buf_item, binary.LittleEndian, bs) binary.Read(buf_item, binary.LittleEndian, bs)
// different for BIFF5 and BIFF8 // different for BIFF5 and BIFF8
@ -169,9 +163,9 @@ func (wb *WorkBook) parseBof(buf io.ReadSeeker, b *bof, pre *bof, offset_pre int
return return
} }
func decodeWindows1251(enc []byte) string { func decodeWindows1251(enc []byte) string {
dec := charmap.Windows1251.NewDecoder() dec := charmap.Windows1251.NewDecoder()
out, _ := dec.Bytes(enc) out, _ := dec.Bytes(enc)
return string(out) return string(out)
} }
func (w *WorkBook) get_string(buf io.ReadSeeker, size uint16) (res string, err error) { func (w *WorkBook) get_string(buf io.ReadSeeker, size uint16) (res string, err error) {
if w.Is5ver { if w.Is5ver {
@ -199,8 +193,6 @@ func (w *WorkBook) get_string(buf io.ReadSeeker, size uint16) (res string, err e
if flag&0x1 != 0 { if flag&0x1 != 0 {
var bts = make([]uint16, size) var bts = make([]uint16, size)
var i = uint16(0) var i = uint16(0)
// we need local err here
var err error
for ; i < size && err == nil; i++ { for ; i < size && err == nil; i++ {
err = binary.Read(buf, binary.LittleEndian, &bts[i]) err = binary.Read(buf, binary.LittleEndian, &bts[i])
} }
@ -208,15 +200,15 @@ func (w *WorkBook) get_string(buf io.ReadSeeker, size uint16) (res string, err e
// when eof found, we dont want to append last element // when eof found, we dont want to append last element
var runes []rune var runes []rune
if err == io.EOF { if err == io.EOF {
runes = utf16.Decode(bts[:i-1]) i = i - 1
} else {
runes = utf16.Decode(bts[:i])
} }
runes = utf16.Decode(bts[:i])
res = string(runes) res = string(runes)
if i < size { if i < size {
w.continue_utf16 = size - i + 1 w.continue_utf16 = size - i
} }
} else { } else {
var bts = make([]byte, size) var bts = make([]byte, size)
var n int var n int