Skip to content

Commit

Permalink
[xml] Add support for integers in bases other than 10. (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
KenjiTakahashi authored and DHowett committed Mar 21, 2017
1 parent 1f7000b commit 445e867
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
26 changes: 26 additions & 0 deletions common_data_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type TestData struct {
Expected map[int][]byte
ShouldFail bool
SkipDecode map[int]bool
SkipEncode map[int]bool
}

type SparseBundleHeader struct {
Expand Down Expand Up @@ -326,6 +327,31 @@ var tests = []TestData{
BinaryFormat: []byte{98, 112, 108, 105, 115, 116, 48, 48, 168, 1, 2, 3, 4, 5, 6, 7, 8, 16, 255, 17, 15, 255, 17, 255, 255, 18, 0, 15, 255, 255, 18, 0, 255, 255, 255, 18, 15, 255, 255, 255, 18, 255, 255, 255, 255, 19, 255, 255, 255, 255, 255, 255, 255, 255, 8, 17, 19, 22, 25, 30, 35, 40, 45, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 54},
},
},
{
Name: "Hexadecimal Integers",
Data: []int{'h', 'e', 'x', 'i', 'n', 't', -42},
Expected: map[int][]byte{
XMLFormat: []byte(xmlPreamble + `<plist version="1.0"><array><integer>0x68</integer><integer>0X65</integer><integer>0x78</integer><integer>0X69</integer><integer>0x6e</integer><integer>0X74</integer><integer>-0x2a</integer></array></plist>`),
},
SkipEncode: map[int]bool{XMLFormat: true},
},
{
Name: "Octal Integers (treated as Decimal)",
Data: []int{'o', 'c', 't', 'i', 'n', 't', -42},
Expected: map[int][]byte{
XMLFormat: []byte(xmlPreamble + `<plist version="1.0"><array><integer>0111</integer><integer>099</integer><integer>0116</integer><integer>0105</integer><integer>0110</integer><integer>0116</integer><integer>-042</integer></array></plist>`),
},
SkipEncode: map[int]bool{XMLFormat: true},
},
{
Name: "Partial hexadecimal Integer",
Data: []int{},
Expected: map[int][]byte{
XMLFormat: []byte(xmlPreamble + `<plist version="1.0"><integer>0x</integer></plist>`),
},
ShouldFail: true,
SkipEncode: map[int]bool{XMLFormat: true},
},
{
Name: "Floats of Increasing Bitness",
Data: []interface{}{float32(math.MaxFloat32), float64(math.MaxFloat64)},
Expand Down
5 changes: 5 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ func TestDecode(t *testing.T) {
}

if failed {
if test.ShouldFail {
t.Logf("Expected: Error")
return
}

t.Logf("Expected: %#v\n", d)

for fmt, dat := range results {
Expand Down
4 changes: 4 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func TestEncode(t *testing.T) {

results := make(map[int][]byte)
for fmt, dat := range test.Expected {
if test.SkipEncode[fmt] {
continue
}

results[fmt], errors[fmt] = Marshal(test.Data, fmt)
failed = failed || (test.ShouldFail && errors[fmt] == nil)
failed = failed || !bytes.Equal(dat, results[fmt])
Expand Down
7 changes: 7 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ func (w *countedWriter) Write(p []byte) (int, error) {
func (w *countedWriter) BytesWritten() int {
return w.nbytes
}

func unsignedGetBase(s string) (string, int) {
if len(s) > 1 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X') {
return s[2:], 16
}
return s, 10
}
6 changes: 4 additions & 2 deletions xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,12 @@ func (p *xmlPlistParser) parseXMLElement(element xml.StartElement) cfValue {
}

if s[0] == '-' {
n := mustParseInt(string(charData), 10, 64)
s, base := unsignedGetBase(s[1:])
n := mustParseInt("-"+s, base, 64)
return &cfNumber{signed: true, value: uint64(n)}
} else {
n := mustParseUint(string(charData), 10, 64)
s, base := unsignedGetBase(s)
n := mustParseUint(s, base, 64)
return &cfNumber{signed: false, value: n}
}
case "real":
Expand Down

0 comments on commit 445e867

Please sign in to comment.