Skip to content

Commit

Permalink
ofx 1.x supports utf-8 encoding (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
mayswind committed Feb 2, 2025
1 parent 319f97b commit a5e7c48
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions pkg/converters/ofx/ofx_data_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import (
"github.com/mayswind/ezbookkeeping/pkg/utils"
)

const ofxUnicodeEncoding = "unicode"
const ofxUSAsciiEncoding = "usascii"
const ofx1USAsciiEncoding = "usascii"
const ofx1UnicodeEncoding = "unicode"
const ofx1UTF8Encoding = "utf8" // non-standard ofx 1.x encoding, used by some banks (https://github.com/mayswind/ezbookkeeping/issues/48)
const ofx1SGMLDataFormat = "OFXSGML"

var ofx2HeaderPattern = regexp.MustCompile("<\\?OFX( +[A-Z]+=\"[^=]*\")* *\\?>")
Expand Down Expand Up @@ -231,7 +232,7 @@ func readOFX1FileHeader(ctx core.Context, data []byte) (fileHeader *ofxFileHeade
}
}

if fileEncoding == ofxUSAsciiEncoding {
if fileEncoding == ofx1USAsciiEncoding {
if utils.IsStringOnlyContainsDigits(fileCharset) {
fileCharset = "cp" + fileCharset
}
Expand All @@ -245,12 +246,18 @@ func readOFX1FileHeader(ctx core.Context, data []byte) (fileHeader *ofxFileHeade
if enc == nil {
enc = charmap.Windows1252
}
} else if fileEncoding == ofxUnicodeEncoding {
enc, _ = charset.Lookup(ofxUnicodeEncoding)
} else if fileEncoding == ofx1UnicodeEncoding {
enc, _ = charset.Lookup(ofx1UnicodeEncoding)

if enc == nil {
enc = unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM)
}
} else if fileEncoding == ofx1UTF8Encoding {
enc, _ = charset.Lookup(ofx1UTF8Encoding)

if enc == nil {
enc = unicode.UTF8
}
} else {
log.Errorf(ctx, "[ofx_data_reader.readOFX1FileHeader] cannot parse ofx 1.x file, because encoding \"%s\" is unknown", fileEncoding)
return nil, nil, "", nil, errs.ErrInvalidOFXFile
Expand Down

0 comments on commit a5e7c48

Please sign in to comment.