Exemple #1
0
// TypeByBuffer looks up for a blob's mimetype by its contents.
// It uses a magic number database which is described in magic(5).
func (m *Magic) TypeByBuffer(blob []byte) (string, error) {
	bytes := unsafe.Pointer(&blob[0])
	out := C.magic_buffer(m.db, bytes, C.size_t(len(blob)))
	if out == nil {
		return "", errors.New(C.GoString(C.magic_error(m.db)))
	}
	return C.GoString(out), nil
}
Exemple #2
0
func (detector *Detector) detectMime(cinput *C.char, cinputLen C.size_t) (string, error) {
	// magic_buffer set EINVAL, even if it succeed(binary?),
	// so ignore second value.
	mimeStr, _ := C.magic_buffer(detector.magic, unsafe.Pointer(cinput), cinputLen)
	if mimeStr == nil {
		err := magicError(detector.magic)
		if err == nil {
			panic("unreachable")
		}
		return "", err
	}
	return C.GoString(mimeStr), nil
}
Exemple #3
0
// Buffer returns a textual description of the contents of the buffer argument.
func (m *Magic) Buffer(data []byte) (string, error) {
	if m.ptr == nil {
		return "", ConnectionError
	}

	ptr := unsafe.Pointer(&data)
	sz := C.size_t(len(data))
	cr := C.magic_buffer(m.ptr, ptr, sz)

	if cr == nil {
		return "", m.check()
	}

	r := C.GoString(cr)
	C.free(unsafe.Pointer(cr))
	return r, nil
}