Skip to content

Commit

Permalink
Added XML escaper/unescaper (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
denandz authored May 6, 2021
1 parent 51d5197 commit 2dc0961
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Available encoders:
urlencodeall - URL encode all characters
utf16 - UTF-16 encoder (Little Endian)
utf16be - UTF-16 encoder (Big Endian)
xmlescape - XML escape
xmlunescape - XML unescape
```

To urlencode, base64encode and hex encode a string:
Expand All @@ -58,6 +60,8 @@ $ echo 'what%ever'|pencode urlencode b64encode hexencode
- URL decoder
- UTF16 encoder (Little Endian)
- UTF16 encoder (Big Endian)
- XML escape encoder (reserved characters)
- XML escape decoder


### Shell completion
Expand Down
2 changes: 2 additions & 0 deletions pkg/pencode/encoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ var availableEncoders = map[string]Encoder{
"urlencodeall": URLEncoderAll{},
"utf16": UTF16LEEncode{},
"utf16be": UTF16BEEncode{},
"xmlescape": XMLEscaper{},
"xmlunescape": XMLUnescaper{},
}

type Chain struct {
Expand Down
24 changes: 24 additions & 0 deletions pkg/pencode/xmldecode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package pencode

import "encoding/xml"

type XMLUnescaper struct{}

type XMLInput struct {
Input string `xml:"input"`
}

func (u XMLUnescaper) Encode(input []byte) ([]byte, error) {
inputXML := `<xml><input>` + string(input) + `</input></xml>`

var out XMLInput
if err := xml.Unmarshal([]byte(inputXML), &out); err != nil {
return []byte{}, err
}

return []byte(out.Input), nil
}

func (u XMLUnescaper) HelpText() string {
return "XML unescape"
}
23 changes: 23 additions & 0 deletions pkg/pencode/xmlencode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package pencode

import (
"bytes"
"encoding/xml"
)

type XMLEscaper struct{}

func (u XMLEscaper) Encode(input []byte) ([]byte, error) {
buf := &bytes.Buffer{}

if err := xml.EscapeText(buf, input); err != nil {
return []byte{}, err
}

output := buf.Bytes()
return output, nil
}

func (u XMLEscaper) HelpText() string {
return "XML escape"
}

0 comments on commit 2dc0961

Please sign in to comment.