Example #1
0
func (self *Converter) ReadInt64(input interface{}) int64 {
	value, err := encoding.NumberToInt64(input)
	if err != nil {
		panic(err)
	}
	return value
}
Example #2
0
func decodeToFileEntry(entryIn interface{}, numberOfEntries int) (
	entry directoryFileEntry, err error) {
	entries, ok := entryIn.([]interface{})

	if !ok {
		err = errors.New("A directory entry has to be a array")
		return
	}
	if len(entries) != 3 {
		err = errors.New(fmt.Sprintf("A directory entry has to be a array of 3 "+
			"elements, your element "+
			"has %v entries", len(entries)))
		return
	}

	mimePointer, err := encoding.NumberToInt64(entries[0])
	if err != nil {
		err = errors.New(fmt.Sprintf("Expecting the mime pointer to be a interger, "+
			"err: %v", err))
		return
	}
	if mimePointer < -1 {
		err = errors.New("mime pointer can be -1 or positive. It's negative")
		return
	}
	if mimePointer >= int64(numberOfEntries) {
		err = errors.New(fmt.Sprintf("mime pointer can be at max %v (number of elements -1) "+
			"- but it's %v", numberOfEntries-1, mimePointer))
	}
	entry.mimePointer = int16(mimePointer)

	pointer, ok := entries[1].([]byte)
	if !ok {
		err = errors.New(fmt.Sprintf("The pointer (hash to entry) has to be a []byte "+
			" - but it's a %T", entries[1]))
		return
	}
	if len(pointer) != directoryStructPointerNumBytes {
		err = errors.New(fmt.Sprintf("The pointer to the next entry should be %v bytes "+
			" - but it's a %v bytes", directoryStructPointerNumBytes, len(pointer)))
		return
	}
	entry.hashPointer = pointer

	name, ok := entries[2].(string)
	if !ok {
		err = errors.New(fmt.Sprintf("The file name has to be a string "+
			" - but it's a %T", entries[2]))
		return
	}
	if len(name) > directoryStructNameMaxBytesLen {
		err = errors.New(fmt.Sprintf("Maximum length of a filename is %v bytes "+
			" - but you supplied a name with a %v bytes",
			directoryStructNameMaxBytesLen, len(name)))
	}
	entry.name = name

	return
}
Example #3
0
func decodeToDirectoryStruct(data []byte) (
	directory directoryStruct, err error) {

	var encoded interface{}
	err = encoding.Cbor().Decode(data, &encoded)
	if err != nil {
		return
	}

	encodedArray, ok := encoded.([]interface{})
	if !ok {
		err = errors.New("Top level is not a array")
		return
	}

	topLevelLength := len(encodedArray)
	if topLevelLength != 3 {
		err = errors.New("Expecting 2 entries in the top level array")
		return
	}

	magicNumber, err := encoding.NumberToInt64(encodedArray[0])
	if err != nil {
		err = errors.New(fmt.Sprintf("Expecting magic number top level array at index 0. %v",
			err))
		return
	}

	if magicNumber != directoryStructMagicNumber {
		err = errors.New(fmt.Sprintf("Magic number should be %v but is %v",
			directoryStructMagicNumber, magicNumber))
	}

	fileEntries, highestMimePtr, err := decodeToFileEntries(encodedArray[1])
	if err != nil {
		return
	}
	directory.fileEntries = fileEntries

	mimeEntries, err := decodeMimeEntries(encodedArray[2], highestMimePtr)
	if err != nil {
		return
	}
	directory.mimeEntries = mimeEntries

	return
}