...

Source file src/github.com/gabriel-vasile/mimetype/internal/magic/ftyp.go

Documentation: github.com/gabriel-vasile/mimetype/internal/magic

     1  package magic
     2  
     3  import "bytes"
     4  
     5  var (
     6  	// AVIF matches an AV1 Image File Format still or animated.
     7  	// Wikipedia page seems outdated listing image/avif-sequence for animations.
     8  	// https://github.com/AOMediaCodec/av1-avif/issues/59
     9  	AVIF = ftyp([]byte("avif"), []byte("avis"))
    10  	// Mp4 matches an MP4 file.
    11  	Mp4 = ftyp(
    12  		[]byte("avc1"), []byte("dash"), []byte("iso2"), []byte("iso3"),
    13  		[]byte("iso4"), []byte("iso5"), []byte("iso6"), []byte("isom"),
    14  		[]byte("mmp4"), []byte("mp41"), []byte("mp42"), []byte("mp4v"),
    15  		[]byte("mp71"), []byte("MSNV"), []byte("NDAS"), []byte("NDSC"),
    16  		[]byte("NSDC"), []byte("NSDH"), []byte("NDSM"), []byte("NDSP"),
    17  		[]byte("NDSS"), []byte("NDXC"), []byte("NDXH"), []byte("NDXM"),
    18  		[]byte("NDXP"), []byte("NDXS"), []byte("F4V "), []byte("F4P "),
    19  	)
    20  	// ThreeGP matches a 3GPP file.
    21  	ThreeGP = ftyp(
    22  		[]byte("3gp1"), []byte("3gp2"), []byte("3gp3"), []byte("3gp4"),
    23  		[]byte("3gp5"), []byte("3gp6"), []byte("3gp7"), []byte("3gs7"),
    24  		[]byte("3ge6"), []byte("3ge7"), []byte("3gg6"),
    25  	)
    26  	// ThreeG2 matches a 3GPP2 file.
    27  	ThreeG2 = ftyp(
    28  		[]byte("3g24"), []byte("3g25"), []byte("3g26"), []byte("3g2a"),
    29  		[]byte("3g2b"), []byte("3g2c"), []byte("KDDI"),
    30  	)
    31  	// AMp4 matches an audio MP4 file.
    32  	AMp4 = ftyp(
    33  		// audio for Adobe Flash Player 9+
    34  		[]byte("F4A "), []byte("F4B "),
    35  		// Apple iTunes AAC-LC (.M4A) Audio
    36  		[]byte("M4B "), []byte("M4P "),
    37  		// MPEG-4 (.MP4) for SonyPSP
    38  		[]byte("MSNV"),
    39  		// Nero Digital AAC Audio
    40  		[]byte("NDAS"),
    41  	)
    42  	// Mqv matches a Sony / Mobile QuickTime  file.
    43  	Mqv = ftyp([]byte("mqt "))
    44  	// M4a matches an audio M4A file.
    45  	M4a = ftyp([]byte("M4A "))
    46  	// M4v matches an Appl4 M4V video file.
    47  	M4v = ftyp([]byte("M4V "), []byte("M4VH"), []byte("M4VP"))
    48  	// Heic matches a High Efficiency Image Coding (HEIC) file.
    49  	Heic = ftyp([]byte("heic"), []byte("heix"))
    50  	// HeicSequence matches a High Efficiency Image Coding (HEIC) file sequence.
    51  	HeicSequence = ftyp([]byte("hevc"), []byte("hevx"))
    52  	// Heif matches a High Efficiency Image File Format (HEIF) file.
    53  	Heif = ftyp([]byte("mif1"), []byte("heim"), []byte("heis"), []byte("avic"))
    54  	// HeifSequence matches a High Efficiency Image File Format (HEIF) file sequence.
    55  	HeifSequence = ftyp([]byte("msf1"), []byte("hevm"), []byte("hevs"), []byte("avcs"))
    56  	// TODO: add support for remaining video formats at ftyps.com.
    57  )
    58  
    59  // QuickTime matches a QuickTime File Format file.
    60  // https://www.loc.gov/preservation/digital/formats/fdd/fdd000052.shtml
    61  // https://developer.apple.com/library/archive/documentation/QuickTime/QTFF/QTFFChap1/qtff1.html#//apple_ref/doc/uid/TP40000939-CH203-38190
    62  // https://github.com/apache/tika/blob/0f5570691133c75ac4472c3340354a6c4080b104/tika-core/src/main/resources/org/apache/tika/mime/tika-mimetypes.xml#L7758-L7777
    63  func QuickTime(raw []byte, _ uint32) bool {
    64  	if len(raw) < 12 {
    65  		return false
    66  	}
    67  	// First 4 bytes represent the size of the atom as unsigned int.
    68  	// Next 4 bytes are the type of the atom.
    69  	// For `ftyp` atoms check if first byte in size is 0, otherwise, a text file
    70  	// which happens to contain 'ftypqt  ' at index 4 will trigger a false positive.
    71  	if bytes.Equal(raw[4:12], []byte("ftypqt  ")) ||
    72  		bytes.Equal(raw[4:12], []byte("ftypmoov")) {
    73  		return raw[0] == 0x00
    74  	}
    75  	basicAtomTypes := [][]byte{
    76  		[]byte("moov\x00"),
    77  		[]byte("mdat\x00"),
    78  		[]byte("free\x00"),
    79  		[]byte("skip\x00"),
    80  		[]byte("pnot\x00"),
    81  	}
    82  	for _, a := range basicAtomTypes {
    83  		if bytes.Equal(raw[4:9], a) {
    84  			return true
    85  		}
    86  	}
    87  	return bytes.Equal(raw[:8], []byte("\x00\x00\x00\x08wide"))
    88  }
    89  

View as plain text