Skip to content

Commit

Permalink
Added JSON encoder (#21)
Browse files Browse the repository at this point in the history
* Add new lines to printed error messages

* Added JSON encoder and decoder

* Fixed error handling in jsondecode.go

* Changed from json 'encode' to json 'escape'

* README.md changes - added json escaper info
  • Loading branch information
denandz authored Apr 27, 2021
1 parent 937072d commit 51d5197
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Available encoders:
b64encode - Base64 encoder
hexdecode - Hex string decoder
hexencode - Hex string encoder
jsonescape - JSON escape
jsonunescape - JSON unescape
unicodedecode - Unicode escape string decode
unicodeencodeall - Unicode escape string encode (all characters)
urldecode - URL decode
Expand All @@ -47,6 +49,8 @@ $ echo 'what%ever'|pencode urlencode b64encode hexencode
- Base64 decoder
- Hex string encoder
- Hex string decoder
- JSON escape encoder (reserved characters)
- JSON escape decoder
- Unicode escape string encoder
- Unicode escape string decoder
- URL encoder (reserved characters)
Expand Down
4 changes: 2 additions & 2 deletions cmd/pencode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ func main() {
err := chain.Initialize(os.Args[1:])
if err != nil {
flag.Usage()
fmt.Printf("\n[!] %s", err)
fmt.Printf("\n[!] %s\n", err)
os.Exit(1)
}

input := readInput()
output, err := chain.Encode([]byte(input))
if err != nil {
fmt.Printf(" [!] %s", err)
fmt.Printf(" [!] %s\n", err)
}
fmt.Print(string(output))
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/pencode/encoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ var availableEncoders = map[string]Encoder{
"b64decode": Base64Decoder{},
"hexencode": HexEncoder{},
"hexdecode": HexDecoder{},
"jsonescape": JSONEscaper{},
"jsonunescape": JSONUnescaper{},
"unicodedecode": UnicodeDecode{},
"unicodeencodeall": UnicodeEncodeAll{},
"urlencode": URLEncoder{},
Expand Down
24 changes: 24 additions & 0 deletions pkg/pencode/jsondecode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package pencode

import "encoding/json"

type JSONUnescaper struct{}

type JSONInput struct {
Input string `json:"input"`
}

func (u JSONUnescaper) Encode(input []byte) ([]byte, error) {
inputJson := `{"input":"` + string(input) + `"}`

var out JSONInput
if err := json.Unmarshal([]byte(inputJson), &out); err != nil {
return []byte{}, err
}

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

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

import (
"bytes"
"encoding/json"
)

type JSONEscaper struct{}

func (u JSONEscaper) Encode(input []byte) ([]byte, error) {
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false) // prevents encoding of < and > characters
err := enc.Encode(string(input))
if err != nil {
return []byte{}, err
}

output := buf.Bytes()
return output[1 : len(output)-2], nil
}

func (u JSONEscaper) HelpText() string {
return "JSON escape"
}

0 comments on commit 51d5197

Please sign in to comment.