Esempio n. 1
0
func getMetaInfo(torrent string) (metaInfo *MetaInfo, err os.Error) {
	var input io.ReadCloser
	if strings.HasPrefix(torrent, "http:") {
		// 6g compiler bug prevents us from writing r, _, err :=
		var r *http.Response
		if r, _, err = http.Get(torrent); err != nil {
			return
		}
		input = r.Body
	} else {
		if input, err = os.Open(torrent, os.O_RDONLY, 0666); err != nil {
			return
		}
	}

	// We need to calcuate the sha1 of the Info map, including every value in the
	// map. The easiest way to do this is to read the data using the Decode
	// API, and then pick through it manually.
	var m interface{}
	m, err = bencode.Decode(input)
	input.Close()
	if err != nil {
		err = os.NewError("Couldn't parse torrent file phase 1: " + err.String())
		return
	}

	topMap, ok := m.(map[string]interface{})
	if !ok {
		err = os.NewError("Couldn't parse torrent file phase 2.")
		return
	}

	infoMap, ok := topMap["info"]
	if !ok {
		err = os.NewError("Couldn't parse torrent file. info")
		return
	}
	var b bytes.Buffer
	if err = bencode.Marshal(&b, infoMap); err != nil {
		return
	}
	hash := sha1.New()
	hash.Write(b.Bytes())

	var m2 MetaInfo
	err = bencode.Unmarshal(&b, &m2.Info)
	if err != nil {
		return
	}

	m2.InfoHash = string(hash.Sum())
	m2.Announce = getString(topMap, "announce")
	m2.CreationDate = getString(topMap, "creation date")
	m2.Comment = getString(topMap, "comment")
	m2.CreatedBy = getString(topMap, "created by")
	m2.Encoding = getString(topMap, "encoding")

	metaInfo = &m2
	return
}
Esempio n. 2
0
func (c *CallManager) DecodePayload(h *Header, raw []byte, payloadpos int) (payload interface{}, err os.Error) {

	if h.Call != 0x01 && (h.Call < 0x81 || h.Call > 0x83) {
		return nil, MalusDecodingError("unkown call")
	}

	if (h.Call > 0x83 || h.Call < 0x82) && payloadpos == len(raw) {
		return nil, MalusDecodingError("no payload!")
	} else if (h.Call <= 0x83 || h.Call >= 0x82) && payloadpos == len(raw) {
		return make(map[string]interface{}), nil
	}
	payloadbuf := bytes.NewBuffer(raw[payloadpos:])

	data, err := bencode.Decode(payloadbuf)
	if err != nil {
		return nil, MalusDecodingError("bencoding error")
	}

	//fmt.Printf("decode ok data %v\n", data)
	return data, nil
}