@@ -250,47 +250,45 @@ var TranscoderGarlic32 = NewTranscoderFromFunctions(garlic32StB, garlic32BtS, ga
250
250
var garlicBase32Encoding = base32 .NewEncoding ("abcdefghijklmnopqrstuvwxyz234567" )
251
251
252
252
func garlic32StB (s string ) ([]byte , error ) {
253
- s = strings .Replace (s , ".b32.i2p" , "" , - 1 )
253
+ // s = strings.Replace(s, ".b32.i2p", "", -1)
254
254
// 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.
255
258
if len (s ) < 55 {
256
259
if len (s ) != 52 {
260
+ // all other base32 addresses will always be exactly 52 characters
257
261
return nil , fmt .Errorf ("failed to parse garlic addr: %s not a i2p base32 address. len: %d" , s , len (s ))
258
262
}
259
263
}
260
264
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 += "="
271
267
}
272
268
273
- garlicHostBytes , err := garlicBase32Encoding .DecodeString (padout (s ))
269
+ garlicHostBytes , err := garlicBase32Encoding .DecodeString (string (s ))
274
270
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
+ )
276
276
}
277
277
return garlicHostBytes , nil
278
278
}
279
279
280
280
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
288
283
}
289
- return unpad (garlicBase32Encoding .EncodeToString (b )), nil
284
+ return strings . TrimRight (garlicBase32Encoding .EncodeToString (b ), "=" ), nil
290
285
}
291
286
292
287
func garlic32Validate (b []byte ) error {
288
+ // an i2p base64 for an Encrypted Leaseset v2 will be at least 35 bytes
289
+ // long
293
290
if len (b ) > 35 {
291
+ // other than that, they will be at least 32 bytes
294
292
if len (b ) < 32 {
295
293
return fmt .Errorf ("failed to validate garlic addr: %s not an i2p base32 address. len: %d\n " , b , len (b ))
296
294
}
0 commit comments