-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Base8 Implementation #26
Open
gowthamgts
wants to merge
5
commits into
multiformats:master
Choose a base branch
from
gowthamgts:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
79c5cfb
Fixing go lint warnings
gowthamgts 77ac1cc
Added base8 implementation
gowthamgts f738a39
Bumping int8 to int16
gowthamgts 626ce16
Removed redundant println
gowthamgts e6dbfce
Merge remote-tracking branch 'upstream/master'
gowthamgts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package multibase | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
) | ||
|
||
func octalEncodeToString(src []byte) string { | ||
dst := make([]byte, len(src)*3) | ||
octalEncode(dst, src) | ||
return string(dst) | ||
} | ||
|
||
var octalTable = [8]byte{ | ||
'0', '1', '2', '3', '4', '5', '6', '7', | ||
} | ||
|
||
func octalEncode(dst, src []byte) int { | ||
for i, v := range src { | ||
dst[i*3+2] = octalTable[v&0x7] | ||
v = v >> 3 | ||
dst[i*3+1] = octalTable[v&0x7] | ||
v = v >> 3 | ||
dst[i*3] = octalTable[v&0x7] | ||
v = v >> 3 | ||
} | ||
|
||
return len(src) * 3 | ||
} | ||
|
||
// decodeOctalString takes an octal string and gives byte array of decimals | ||
func decodeOctalString(s string) ([]byte, error) { | ||
data := make([]byte, len(s)/3) | ||
if len(s)%3 != 0 { | ||
return nil, fmt.Errorf("cannot decode multibase: %s", | ||
"length should be a multiple of 3") | ||
} | ||
|
||
for i, dstIndex := 0, 0; i < len(s); i = i + 3 { | ||
value, err := strconv.ParseInt(s[i:i+3], 8, 16) | ||
if err != nil { | ||
return nil, fmt.Errorf("error while conversion: %s", err) | ||
} | ||
|
||
data[dstIndex] = byte(value) | ||
dstIndex++ | ||
} | ||
|
||
return data, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package multibase | |
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"math/rand" | ||
"testing" | ||
) | ||
|
@@ -24,6 +25,7 @@ func TestMap(t *testing.T) { | |
var sampleBytes = []byte("Decentralize everything!!!") | ||
var encodedSamples = map[Encoding]string{ | ||
Identity: string(0x00) + "Decentralize everything!!!", | ||
Base8: "7104145143145156164162141154151172145040145166145162171164150151156147041041041", | ||
Base16: "f446563656e7472616c697a652065766572797468696e67212121", | ||
Base16Upper: "F446563656E7472616C697A652065766572797468696E67212121", | ||
Base32: "birswgzloorzgc3djpjssazlwmvzhs5dinfxgoijbee", | ||
|
@@ -81,8 +83,9 @@ func TestDecode(t *testing.T) { | |
func TestRoundTrip(t *testing.T) { | ||
buf := make([]byte, 17) | ||
rand.Read(buf) | ||
fmt.Println(buf) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Best to avoid printf in tests. If you want to log this, please use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was by a mistake. Removed now. |
||
|
||
baseList := []Encoding{Identity, Base16, Base32, Base32hex, Base32pad, Base32hexPad, Base58BTC, Base58Flickr, Base64pad, Base64urlPad} | ||
baseList := []Encoding{Identity, Base8, Base16, Base32, Base32hex, Base32pad, Base32hexPad, Base58BTC, Base58Flickr, Base64pad, Base64urlPad} | ||
|
||
for _, base := range baseList { | ||
enc, err := Encode(base, buf) | ||
|
@@ -96,17 +99,17 @@ func TestRoundTrip(t *testing.T) { | |
} | ||
|
||
if e != base { | ||
t.Fatal("got wrong encoding out") | ||
t.Fatal("got wrong encoding output") | ||
} | ||
|
||
if !bytes.Equal(buf, out) { | ||
t.Fatal("input wasnt the same as output", buf, out) | ||
t.Fatal("input wasn't the same as output", buf, out) | ||
} | ||
} | ||
|
||
_, _, err := Decode("") | ||
if err == nil { | ||
t.Fatal("shouldnt be able to decode empty string") | ||
t.Fatal("shouldn't be able to decode empty string") | ||
} | ||
} | ||
|
||
|
@@ -117,6 +120,7 @@ func BenchmarkRoundTrip(b *testing.B) { | |
|
||
bases := map[string]Encoding{ | ||
"Identity": Identity, | ||
"Base8": Base8, | ||
"Base16": Base16, | ||
"Base16Upper": Base16Upper, | ||
"Base32": Base32, | ||
|
@@ -149,11 +153,11 @@ func BenchmarkRoundTrip(b *testing.B) { | |
} | ||
|
||
if e != base { | ||
b.Fatal("got wrong encoding out") | ||
b.Fatal("got wrong encoding output") | ||
} | ||
|
||
if !bytes.Equal(buf, out) { | ||
b.Fatal("input wasnt the same as output", buf, out) | ||
b.Fatal("input wasn't the same as output", buf, out) | ||
} | ||
} | ||
}) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I'd move this after the length check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done