From 21ab033881b537fbf06378ac7cf74fd9df8fa2ef Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Fri, 12 Dec 2014 17:29:41 -0500 Subject: [PATCH] Convert from ISO-8859-1 to UTF-8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Otherwise the returned strings will have the Unicode Replacement Character for non-ASCII characters. Example: ip: 163.9.0.127 city: "Orléans" --- libgeo.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/libgeo.go b/libgeo.go index 4794a55..3278807 100644 --- a/libgeo.go +++ b/libgeo.go @@ -31,8 +31,14 @@ package libgeo // Dependencies import ( + "bytes" "errors" + "io/ioutil" "os" + + // for decoding ISO-8859-1 + "code.google.com/p/go-charset/charset" + _ "code.google.com/p/go-charset/data" ) // Globals (const arrays that will be initialized inside init()) @@ -205,6 +211,18 @@ func (gi *GeoIP) GetLocationByIP(ip string) *Location { return gi.GetLocationByIPNum(AddrToNum(ip)) } +func getUTF8String(b []byte) string { + cr, err := charset.NewReader("ISO-8859-1", bytes.NewBuffer(b)) + if err != nil { + return string(b) + } + bb, err := ioutil.ReadAll(cr) + if err != nil { + return string(b) + } + return string(bb) +} + // Lookup by IP number and return location func (gi *GeoIP) GetLocationByIPNum(ipNum uint32) *Location { // Perform the lookup on the database to see if the record is found @@ -245,7 +263,7 @@ func (gi *GeoIP) GetLocationByIPNum(ipNum uint32) *Location { recPointer+readLen < recordEnd; readLen++ { } if readLen != 0 { - location.Region = string(gi.data[recPointer : recPointer+readLen]) + location.Region = getUTF8String(gi.data[recPointer : recPointer+readLen]) } recPointer += readLen + 1 @@ -254,7 +272,7 @@ func (gi *GeoIP) GetLocationByIPNum(ipNum uint32) *Location { recPointer+readLen < recordEnd; readLen++ { } if readLen != 0 { - location.City = string(gi.data[recPointer : recPointer+readLen]) + location.City = getUTF8String(gi.data[recPointer : recPointer+readLen]) } recPointer += readLen + 1 @@ -263,7 +281,7 @@ func (gi *GeoIP) GetLocationByIPNum(ipNum uint32) *Location { recPointer+readLen < recordEnd; readLen++ { } if readLen != 0 { - location.PostalCode = string(gi.data[recPointer : recPointer+readLen]) + location.PostalCode = getUTF8String(gi.data[recPointer : recPointer+readLen]) } recPointer += readLen + 1