20
0

fix the continue block parse error

This commit is contained in:
Liu Ming 2016-06-23 10:22:57 +08:00
parent 0b5fd56f62
commit 991920a697
3 changed files with 36 additions and 31 deletions

View File

@ -55,7 +55,7 @@ func (w *WorkBook) addXf(xf st_xf_data) {
} }
func (w *WorkBook) addFont(font *FontInfo, buf io.ReadSeeker) { func (w *WorkBook) addFont(font *FontInfo, buf io.ReadSeeker) {
name := w.get_string(buf, uint16(font.NameB)) name, _ := w.get_string(buf, uint16(font.NameB))
w.Fonts = append(w.Fonts, Font{Info: font, Name: name}) w.Fonts = append(w.Fonts, Font{Info: font, Name: name})
} }
@ -86,15 +86,18 @@ func (wb *WorkBook) parseBof(buf io.ReadSeeker, b *bof, pre *bof, offset_pre int
if pre.Id == 0xfc { if pre.Id == 0xfc {
var size uint16 var size uint16
var err error var err error
if wb.continue_utf16 > 1 { if wb.continue_utf16 >= 1 {
size = wb.continue_utf16 size = wb.continue_utf16
wb.continue_utf16 = 0 wb.continue_utf16 = 0
offset_pre--
} else { } else {
err = binary.Read(buf_item, binary.LittleEndian, &size) err = binary.Read(buf_item, binary.LittleEndian, &size)
} }
for err == nil && offset_pre < len(wb.sst) { for err == nil && offset_pre < len(wb.sst) {
wb.sst[offset_pre] = wb.sst[offset_pre] + wb.get_string(buf_item, size) var str string
// if size > 0 {
str, err = wb.get_string(buf_item, size)
wb.sst[offset_pre] = wb.sst[offset_pre] + str
// }
offset_pre++ offset_pre++
err = binary.Read(buf_item, binary.LittleEndian, &size) err = binary.Read(buf_item, binary.LittleEndian, &size)
} }
@ -106,16 +109,19 @@ func (wb *WorkBook) parseBof(buf io.ReadSeeker, b *bof, pre *bof, offset_pre int
info := new(SstInfo) info := new(SstInfo)
binary.Read(buf_item, binary.LittleEndian, info) binary.Read(buf_item, binary.LittleEndian, info)
wb.sst = make([]string, info.Count) wb.sst = make([]string, info.Count)
for i := 0; i < int(info.Count); i++ { var size uint16
var size uint16 var i = 0
for ; i < int(info.Count); i++ {
if err := binary.Read(buf_item, binary.LittleEndian, &size); err == nil || err == io.EOF { if err := binary.Read(buf_item, binary.LittleEndian, &size); err == nil || err == io.EOF {
wb.sst[i] = wb.sst[i] + wb.get_string(buf_item, size) var str string
str, err = wb.get_string(buf_item, size)
wb.sst[i] = wb.sst[i] + str
if err == io.EOF { if err == io.EOF {
offset = i
break break
} }
} }
} }
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)
@ -138,7 +144,7 @@ func (wb *WorkBook) parseBof(buf io.ReadSeeker, b *bof, pre *bof, offset_pre int
case 0x41E: //FORMAT case 0x41E: //FORMAT
font := new(Format) font := new(Format)
binary.Read(buf_item, binary.LittleEndian, &font.Head) binary.Read(buf_item, binary.LittleEndian, &font.Head)
font.str = wb.get_string(buf_item, font.Head.Size) font.str, _ = wb.get_string(buf_item, font.Head.Size)
wb.addFormat(font) wb.addFormat(font)
case 0x22: //DATEMODE case 0x22: //DATEMODE
binary.Read(buf_item, binary.LittleEndian, &wb.dateMode) binary.Read(buf_item, binary.LittleEndian, &wb.dateMode)
@ -146,26 +152,24 @@ func (wb *WorkBook) parseBof(buf io.ReadSeeker, b *bof, pre *bof, offset_pre int
return return
} }
func (w *WorkBook) get_string(buf io.ReadSeeker, size uint16) string { func (w *WorkBook) get_string(buf io.ReadSeeker, size uint16) (res string, err error) {
var res string
if w.Is5ver { if w.Is5ver {
var bts = make([]byte, size) var bts = make([]byte, size)
buf.Read(bts) _, err = buf.Read(bts)
return string(bts) res = string(bts)
} else { } else {
var richtext_num uint16 var richtext_num uint16
var phonetic_size uint32 var phonetic_size uint32
var flag byte var flag byte
binary.Read(buf, binary.LittleEndian, &flag) err = binary.Read(buf, binary.LittleEndian, &flag)
if flag&0x8 != 0 { if flag&0x8 != 0 {
binary.Read(buf, binary.LittleEndian, &richtext_num) err = binary.Read(buf, binary.LittleEndian, &richtext_num)
} }
if flag&0x4 != 0 { if flag&0x4 != 0 {
binary.Read(buf, binary.LittleEndian, &phonetic_size) err = binary.Read(buf, binary.LittleEndian, &phonetic_size)
} }
if flag&0x1 != 0 { if flag&0x1 != 0 {
var bts = make([]uint16, size) var bts = make([]uint16, size)
var err error
var i = uint16(0) var i = uint16(0)
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])
@ -173,13 +177,19 @@ func (w *WorkBook) get_string(buf io.ReadSeeker, size uint16) string {
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
n, err = buf.Read(bts)
if uint16(n) < size {
w.continue_utf16 = size - uint16(n)
err = io.EOF
}
var bts1 = make([]uint16, size) var bts1 = make([]uint16, size)
binary.Read(buf, binary.LittleEndian, &bts) for k, v := range bts[:n] {
for k, v := range bts {
bts1[k] = uint16(v) bts1[k] = uint16(v)
} }
runes := utf16.Decode(bts1) runes := utf16.Decode(bts1)
@ -192,24 +202,19 @@ func (w *WorkBook) get_string(buf io.ReadSeeker, size uint16) string {
} else { } else {
bts = make([]byte, 4*richtext_num) bts = make([]byte, 4*richtext_num)
} }
binary.Read(buf, binary.LittleEndian, bts) err = binary.Read(buf, binary.LittleEndian, bts)
} }
if flag&0x4 != 0 { if flag&0x4 != 0 {
var bts []byte var bts []byte
bts = make([]byte, phonetic_size) bts = make([]byte, phonetic_size)
binary.Read(buf, binary.LittleEndian, bts) err = binary.Read(buf, binary.LittleEndian, bts)
} }
} }
return res return
}
func (w *WorkBook) get_string_from_bytes(bts []byte, size uint16) string {
buf := bytes.NewReader(bts)
return w.get_string(buf, size)
} }
func (w *WorkBook) addSheet(sheet *boundsheet, buf io.ReadSeeker) { func (w *WorkBook) addSheet(sheet *boundsheet, buf io.ReadSeeker) {
name := w.get_string(buf, uint16(sheet.Name)) name, _ := w.get_string(buf, uint16(sheet.Name))
w.sheets = append(w.sheets, &WorkSheet{bs: sheet, Name: name, wb: w}) w.sheets = append(w.sheets, &WorkSheet{bs: sheet, Name: name, wb: w})
} }

View File

@ -92,7 +92,7 @@ func (w *WorkSheet) parseBof(buf io.ReadSeeker, b *bof, pre *bof) *bof {
binary.Read(buf, binary.LittleEndian, &c.BlankCol) binary.Read(buf, binary.LittleEndian, &c.BlankCol)
var count uint16 var count uint16
binary.Read(buf, binary.LittleEndian, &count) binary.Read(buf, binary.LittleEndian, &count)
c.Str = w.wb.get_string(buf, count) c.Str, _ = w.wb.get_string(buf, count)
col = c col = c
case 0x201: //BLANK case 0x201: //BLANK
col = new(BlankCol) col = new(BlankCol)

View File

@ -7,7 +7,7 @@ import (
) )
func TestOpen(t *testing.T) { func TestOpen(t *testing.T) {
if xlFile, err := Open("Preisliste.xls", "utf-8"); err == nil { if xlFile, err := Open("Book1.xls", "utf-8"); err == nil {
if sheet1 := xlFile.GetSheet(0); sheet1 != nil { if sheet1 := xlFile.GetSheet(0); sheet1 != nil {
fmt.Println("Total Lines ", sheet1.MaxRow, sheet1.Name) fmt.Println("Total Lines ", sheet1.MaxRow, sheet1.Name)
for i := 0; i < int(sheet1.MaxRow); i++ { for i := 0; i < int(sheet1.MaxRow); i++ {