...

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

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

     1  package magic
     2  
     3  import "bytes"
     4  
     5  var (
     6  	// Pdf matches a Portable Document Format file.
     7  	// https://github.com/file/file/blob/11010cc805546a3e35597e67e1129a481aed40e8/magic/Magdir/pdf
     8  	Pdf = prefix(
     9  		// usual pdf signature
    10  		[]byte("%PDF-"),
    11  		// new-line prefixed signature
    12  		[]byte("\012%PDF-"),
    13  		// UTF-8 BOM prefixed signature
    14  		[]byte("\xef\xbb\xbf%PDF-"),
    15  	)
    16  	// Fdf matches a Forms Data Format file.
    17  	Fdf = prefix([]byte("%FDF"))
    18  	// Mobi matches a Mobi file.
    19  	Mobi = offset([]byte("BOOKMOBI"), 60)
    20  	// Lit matches a Microsoft Lit file.
    21  	Lit = prefix([]byte("ITOLITLS"))
    22  )
    23  
    24  // DjVu matches a DjVu file.
    25  func DjVu(raw []byte, limit uint32) bool {
    26  	if len(raw) < 12 {
    27  		return false
    28  	}
    29  	if !bytes.HasPrefix(raw, []byte{0x41, 0x54, 0x26, 0x54, 0x46, 0x4F, 0x52, 0x4D}) {
    30  		return false
    31  	}
    32  	return bytes.HasPrefix(raw[12:], []byte("DJVM")) ||
    33  		bytes.HasPrefix(raw[12:], []byte("DJVU")) ||
    34  		bytes.HasPrefix(raw[12:], []byte("DJVI")) ||
    35  		bytes.HasPrefix(raw[12:], []byte("THUM"))
    36  }
    37  
    38  // P7s matches an .p7s signature File (PEM, Base64).
    39  func P7s(raw []byte, limit uint32) bool {
    40  	// Check for PEM Encoding.
    41  	if bytes.HasPrefix(raw, []byte("-----BEGIN PKCS7")) {
    42  		return true
    43  	}
    44  	// Check if DER Encoding is long enough.
    45  	if len(raw) < 20 {
    46  		return false
    47  	}
    48  	// Magic Bytes for the signedData ASN.1 encoding.
    49  	startHeader := [][]byte{{0x30, 0x80}, {0x30, 0x81}, {0x30, 0x82}, {0x30, 0x83}, {0x30, 0x84}}
    50  	signedDataMatch := []byte{0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07}
    51  	// Check if Header is correct. There are multiple valid headers.
    52  	for i, match := range startHeader {
    53  		// If first bytes match, then check for ASN.1 Object Type.
    54  		if bytes.HasPrefix(raw, match) {
    55  			if bytes.HasPrefix(raw[i+2:], signedDataMatch) {
    56  				return true
    57  			}
    58  		}
    59  	}
    60  
    61  	return false
    62  }
    63  

View as plain text