Last active
July 20, 2020 15:59
-
-
Save BirknerAlex/b1989b43e057226b0ba0501b9775b493 to your computer and use it in GitHub Desktop.
QEMU Image Format detection (golang)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package qemu | |
import ( | |
"bytes" | |
"io" | |
) | |
type Format string | |
const ( | |
UNKNOWN Format = "UNKNOWN" | |
QCOW2 = "QCOW2" | |
RAW = "RAW" | |
) | |
var qcow2Header = []byte{0x51, 0x46, 0x49, 0xFB} | |
var rawHeader = []byte{0xeb, 0x00, 0x90} | |
func GetFormat(r io.Reader) (Format, error) { | |
header := make([]byte, 4) | |
_, err := r.Read(header) | |
if err != nil { | |
return UNKNOWN, err | |
} | |
var f Format | |
f = UNKNOWN | |
if bytes.Equal(header, qcow2Header) { | |
f = QCOW2 | |
} else if header[0] == rawHeader[0] && header[2] == rawHeader[2] { | |
f = RAW | |
} | |
return f, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment