Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions libgeo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down