func decodeMap(rdTmp io.Reader, path string, nest bool) (Map, error) {
	docLen, err := readInt32(rdTmp)
	if err != nil {
		return nil, err
	}
	if docLen > maxDocLen {
		return nil, errors.New("Doc exceeded maximum size.")
	}
	rd := bufio.NewReader(io.LimitReader(rdTmp, int64(docLen-4)))
	dst := Map{}
	for {
		eType, err := rd.ReadByte()
		if err != nil {
			return nil, err
		}
		switch eType {
		case 0x00:
			return dst, nil
		case _FLOATING_POINT:
			name, val, err := decodeFloat(rd)
			if err != nil {
				return nil, err
			}
			dst[name] = val
		case _STRING:
			name, val, err := decodeString(rd)
			if err != nil {
				return nil, err
			}
			dst[name] = val
		case _EMBEDDED_DOCUMENT:
			name, err := readCstring(rd)
			if err != nil {
				return nil, err
			}
			if !nest {
				bs, err := ReadOne(rd)
				if err != nil {
					return nil, err
				}
				dst[name] = bs
			} else {
				val, err := decodeMap(rd, catpath(path, name), true)
				if err != nil {
					return nil, err
				}
				dst[name] = val
			}
		case _ARRAY:
			name, val, err := decodeArray(rd, path)
			if err != nil {
				return nil, err
			}
			dst[name] = val
		case _BINARY_DATA:
			name, val, err := decodeBinary(rd)
			if err != nil {
				return nil, err
			}
			dst[name] = val
		case _UNDEFINED:
			name, val, err := decodeUndefined(rd)
			if err != nil {
				return nil, err
			}
			dst[name] = val
		case _OBJECT_ID:
			name, val, err := decodeObjectId(rd)
			if err != nil {
				return nil, err
			}
			dst[name] = val
		case _BOOLEAN:
			name, val, err := decodeBool(rd)
			if err != nil {
				return nil, err
			}
			dst[name] = val
		case _UTC_DATETIME:
			name, val, err := decodeUTCDateTime(rd)
			if err != nil {
				return nil, err
			}
			dst[name] = val
		case _NULL_VALUE:
			name, val, err := decodeNull(rd)
			if err != nil {
				return nil, err
			}
			dst[name] = val
		case _REGEXP:
			name, val, err := decodeRegexp(rd)
			if err != nil {
				return nil, err
			}
			dst[name] = val
		case _DBPOINTER:
			name, val, err := decodeDBPointer(rd)
			if err != nil {
				return nil, err
			}
			dst[name] = val
		case _JAVASCRIPT:
			name, val, err := decodeJavascript(rd)
			if err != nil {
				return nil, err
			}
			dst[name] = val
		case _SYMBOL:
			name, val, err := decodeSymbol(rd)
			if err != nil {
				return nil, err
			}
			dst[name] = val
		case _JAVASCRIPT_SCOPE:
			name, val, err := decodeJavascriptScope(rd, path)
			if err != nil {
				return nil, err
			}
			dst[name] = val
		case _32BIT_INTEGER:
			name, val, err := decodeInt32(rd)
			if err != nil {
				return nil, err
			}
			dst[name] = val
		case _TIMESTAMP:
			name, val, err := decodeTimestamp(rd)
			if err != nil {
				return nil, err
			}
			dst[name] = val
		case _64BIT_INTEGER:
			name, val, err := decodeInt64(rd)
			if err != nil {
				return nil, err
			}
			dst[name] = val
		case _MIN_KEY:
			name, val, err := decodeMinKey(rd)
			if err != nil {
				return nil, err
			}
			dst[name] = val
		case _MAX_KEY:
			name, val, err := decodeMaxKey(rd)
			if err != nil {
				return nil, err
			}
			dst[name] = val
		default:
			return nil, fmt.Errorf("Unsupported type '%X'.", eType)
		}
	}
	return nil, nil
}
decodeMap decodes to a Map. The path is used to keep track of where we've
recursed to in the document. If nest is true then nested documents are
decoded.
package bson
import (
	"bufio"
	"encoding/binary"
	"errors"
	"fmt"
	"io"
	"math"
	"reflect"
	"sort"
)
const maxDocLen = 64 * 1024 * 1024
func ReadOne(rd io.Reader) (BSON, error) {
	docLen, err := readInt32(rd)
	if err != nil {
		return nil, err
	}
	if docLen > maxDocLen {
		return nil, errors.New("Doc exceeded maximum size.")
	}
	buf := make([]byte, int(docLen))
	binary.LittleEndian.PutUint32(buf, uint32(docLen))
	if _, err := io.ReadFull(rd, buf[4:]); err != nil {
		return nil, err
	}
	return buf, nil
}
func ReadMap(rd io.Reader) (m Map, err error) {
	defer func() {
		if r := recover(); r != nil {
			err = errors.New(fmt.Sprint(r))
		}
	}()
	return decodeMap(rd, "", true)
}
func ReadMapNoNest(rd io.Reader) (m Map, err error) {
	defer func() {
		if r := recover(); r != nil {
			err = errors.New(fmt.Sprint(r))
		}
	}()
	return decodeMap(rd, "", false)
}
func ReadSlice(rd io.Reader) (s Slice, err error) {
	defer func() {
		if r := recover(); r != nil {
			err = errors.New(fmt.Sprint(r))
		}
	}()
	return decodeSlice(rd, "", true)
}
func ReadSliceNoNest(rd io.Reader) (s Slice, err error) {
	defer func() {
		if r := recover(); r != nil {
			err = errors.New(fmt.Sprint(r))
		}
	}()
	return decodeSlice(rd, "", false)
}