Skip to content

Commit 672c39f

Browse files
author
idk
committed
add comments and don't repeat myself as much
1 parent 71e227b commit 672c39f

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

multiaddr_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func TestConstructSucceeds(t *testing.T) {
104104
"/garlic64/jT~IyXaoauTni6N4517EG8mrFUKpy0IlgZh-EY9csMAk82Odatmzr~YTZy8Hv7u~wvkg75EFNOyqb~nAPg-khyp2TS~ObUz8WlqYAM2VlEzJ7wJB91P-cUlKF18zSzVoJFmsrcQHZCirSbWoOknS6iNmsGRh5KVZsBEfp1Dg3gwTipTRIx7Vl5Vy~1OSKQVjYiGZS9q8RL0MF~7xFiKxZDLbPxk0AK9TzGGqm~wMTI2HS0Gm4Ycy8LYPVmLvGonIBYndg2bJC7WLuF6tVjVquiokSVDKFwq70BCUU5AU-EvdOD5KEOAM7mPfw-gJUG4tm1TtvcobrObqoRnmhXPTBTN5H7qDD12AvlwFGnfAlBXjuP4xOUAISL5SRLiulrsMSiT4GcugSI80mF6sdB0zWRgL1yyvoVWeTBn1TqjO27alr95DGTluuSqrNAxgpQzCKEWAyzrQkBfo2avGAmmz2NaHaAvYbOg0QSJz1PLjv2jdPW~ofiQmrGWM1cd~1cCqAAAA/tcp/8080",
105105
"/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq",
106106
"/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuqzwas",
107-
"/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuqzwasasw",
107+
"/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuqzwassw",
108108
"/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq/http",
109109
"/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq/tcp/8080",
110110
"/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq/udp/8080",

transcoders.go

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -250,47 +250,45 @@ var TranscoderGarlic32 = NewTranscoderFromFunctions(garlic32StB, garlic32BtS, ga
250250
var garlicBase32Encoding = base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567")
251251

252252
func garlic32StB(s string) ([]byte, error) {
253-
s = strings.Replace(s, ".b32.i2p", "", -1)
253+
//s = strings.Replace(s, ".b32.i2p", "", -1)
254254
// garlic address without the ".b32.i2p" substring
255+
256+
// an i2p base32 address with a length of greater than 55 characters is
257+
// using an Encrypted Leaseset v2.
255258
if len(s) < 55 {
256259
if len(s) != 52 {
260+
// all other base32 addresses will always be exactly 52 characters
257261
return nil, fmt.Errorf("failed to parse garlic addr: %s not a i2p base32 address. len: %d", s, len(s))
258262
}
259263
}
260264

261-
padout := func(s2 string) string {
262-
str := ""
263-
for x := 0; x < 56; x++ {
264-
if x < len(s) {
265-
str += string(s[x])
266-
} else {
267-
str += "="
268-
}
269-
}
270-
return str
265+
for len(s) < 56 {
266+
s += "="
271267
}
272268

273-
garlicHostBytes, err := garlicBase32Encoding.DecodeString(padout(s))
269+
garlicHostBytes, err := garlicBase32Encoding.DecodeString(string(s))
274270
if err != nil {
275-
return nil, fmt.Errorf("failed to decode base32 garlic addr: %s err %s len %v", s, err, len(s))
271+
return nil, fmt.Errorf("failed to decode base32 garlic addr: %s, err: %v len: %v",
272+
s,
273+
err,
274+
len(s),
275+
)
276276
}
277277
return garlicHostBytes, nil
278278
}
279279

280280
func garlic32BtS(b []byte) (string, error) {
281-
if len(b) > 35 {
282-
if len(b) < 32 {
283-
return "", fmt.Errorf("failed to validate garlic addr: %s not an i2p base32 address. len: %d\n", b, len(b))
284-
}
285-
}
286-
unpad := func(s2 string) string {
287-
return strings.Replace(s2, "=", "", -1)
281+
if err := garlic32Validate(b); err != nil {
282+
return "", err
288283
}
289-
return unpad(garlicBase32Encoding.EncodeToString(b)), nil
284+
return strings.TrimRight(garlicBase32Encoding.EncodeToString(b), "="), nil
290285
}
291286

292287
func garlic32Validate(b []byte) error {
288+
// an i2p base64 for an Encrypted Leaseset v2 will be at least 35 bytes
289+
// long
293290
if len(b) > 35 {
291+
// other than that, they will be at least 32 bytes
294292
if len(b) < 32 {
295293
return fmt.Errorf("failed to validate garlic addr: %s not an i2p base32 address. len: %d\n", b, len(b))
296294
}

0 commit comments

Comments
 (0)