Skip to content

Commit

Permalink
Add utf16 encoding
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Sverdlov <[email protected]>
  • Loading branch information
sverdlov93 committed Feb 4, 2024
1 parent c825c01 commit 2b17928
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
19 changes: 11 additions & 8 deletions build/utils/dotnet/dependencies/packagesconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,15 +319,18 @@ func xmlUnmarshal(content []byte, obj interface{}) (err error) {
// Failed while trying to parse xml file. Nuspec file could be an utf-16 encoded file.
// xml.Unmarshal doesn't support utf-16 encoding, so we need to decode the utf16 by ourselves.

// Calculate the number of uint16 elements needed to represent UTF-16 content.
// Subtracting 2 to exclude the Byte Order Mark (BOM) size, if present.
size := (len(content) - 2) / 2

uint16Arr := make([]uint16, size)
for i := 0; i < size; i++ {
uint16Arr[i] = binary.LittleEndian.Uint16(content[i*2:])
buf := make([]uint16, len(content)/2)
for i := 0; i < len(content); i += 2 {
buf[i/2] = binary.LittleEndian.Uint16(content[i:])
}
err = xml.Unmarshal([]byte(string(utf16.Decode(uint16Arr))), obj)
// Remove utf-16 Byte Order Mark (BOM) if exists
utf16BOM := "\uFEFF"
stringXml := strings.ReplaceAll(string(utf16.Decode(buf)), utf16BOM, "")

// xml.Unmarshal doesn't support utf-16 encoding, so we need to convert the header to utf-8.
stringXml = strings.Replace(stringXml, "utf-16", "utf-8", 1)

err = xml.Unmarshal([]byte(stringXml), obj)
}
return
}
10 changes: 5 additions & 5 deletions build/utils/dotnet/dependencies/packagesconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestLoadPackagesConfig(t *testing.T) {
<packages>
<package id="id1" version="1.0.0" targetFramework="net461" />
<package id="id2" version="2.0.0" targetFramework="net461" />
<package id="utf16" version="2.0.0" targetFramework="net461" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net461" />
</packages>`)

packagesObj := &packagesConfig{}
Expand All @@ -92,7 +92,7 @@ func TestLoadPackagesConfig(t *testing.T) {
XmlPackages: []xmlPackage{
{Id: "id1", Version: "1.0.0"},
{Id: "id2", Version: "2.0.0"},
{Id: "utf16", Version: "2.0.0"},
{Id: "Microsoft.Web.Infrastructure", Version: "1.0.0.0"},
},
}

Expand Down Expand Up @@ -151,7 +151,7 @@ func TestExtractDependencies(t *testing.T) {
extractor, err := extractDependencies(filepath.Join("testdata", "packagesproject", "localcache"), log)
assert.NoError(t, err)

expectedAllDependencies := []string{"id1", "id2", "utf16"}
expectedAllDependencies := []string{"id1", "id2", "microsoft.web.infrastructure"}
allDependencies, err := extractor.AllDependencies(log)
assert.NoError(t, err)

Expand All @@ -161,15 +161,15 @@ func TestExtractDependencies(t *testing.T) {
}
}

expectedChildrenMap := map[string][]string{"id1": {"id2"}, "id2": {"id1"}, "utf16": {}}
expectedChildrenMap := map[string][]string{"id1": {"id2"}, "id2": {"id1"}, "microsoft.web.infrastructure": {"id1"}}
childrenMap, err := extractor.ChildrenMap()
assert.NoError(t, err)

if !reflect.DeepEqual(expectedChildrenMap, childrenMap) {
t.Errorf("Expected: %s, Got: %s", expectedChildrenMap, childrenMap)
}

expectedDirectDependencies := []string{"id1", "id2", "utf16"}
expectedDirectDependencies := []string{"microsoft.web.infrastructure", "id1", "id2"}
directDependencies, err := extractor.DirectDependencies()
assert.NoError(t, err)

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
<packages>
<package id="id1" version="1.0.0" targetFramework="net461" />
<package id="id2" version="2.0.0" targetFramework="net461" />
<package id="utf16" version="2.0.0" targetFramework="net461" />
</packages>
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net461" />
</packages>

0 comments on commit 2b17928

Please sign in to comment.