Skip to content

Commit

Permalink
decode integer
Browse files Browse the repository at this point in the history
  • Loading branch information
burmudar committed Oct 20, 2023
1 parent b385cf0 commit fc4885f
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions cmd/mybittorrent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,44 @@ import (
// - 5:hello -> hello
// - 10:hello12345 -> hello12345
func decodeBencode(bencodedString string) (interface{}, error) {
if unicode.IsDigit(rune(bencodedString[0])) {
switch {
case unicode.IsDigit(rune(bencodedString[0])):
{
colonIdx := strings.IndexRune(bencodedString, ':')
if colonIdx < 0 {
return "", fmt.Errorf("invalid string encoding: %v", bencodedString)
}

colonIdx := strings.IndexRune(bencodedString, ':')
encodedLen := bencodedString[:colonIdx]

encodedLen := bencodedString[:colonIdx]
length, err := strconv.Atoi(encodedLen)
if err != nil {
return "", err
}

length, err := strconv.Atoi(encodedLen)
if err != nil {
return "", err
}
strLen := len(bencodedString) - colonIdx - 1
if length > strLen {
return "", fmt.Errorf("invalid length encoded - got %d but string is %d", length, strLen)
}

strLen := len(bencodedString) - colonIdx - 1
if length > strLen {
return "", fmt.Errorf("invalid length encoded - got %d but string is %d", length, strLen)
return bencodedString[colonIdx+1 : colonIdx+1+length], nil
}
case unicode.IsLetter(rune(bencodedString[0])) && bencodedString[0] == 'i':
{
end := strings.IndexRune(bencodedString, 'e')
if end < 0 {
return "", fmt.Errorf("invalid integer encoding: %v", bencodedString)
}
num, err := strconv.Atoi(bencodedString[1:end])
if err != nil {
return "", fmt.Errorf("failed to decode integer: %v", bencodedString)
}
return num, err
}
default:
{
return "", fmt.Errorf("Only strings are supported at the moment")
}

return bencodedString[colonIdx+1 : colonIdx+1+length], nil
} else {
return "", fmt.Errorf("Only strings are supported at the moment")
}
}

Expand Down

0 comments on commit fc4885f

Please sign in to comment.