filetype.es6.js 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794
  1. /*!
  2. * Library to detect file mime type of a Uint8Array.
  3. *
  4. * Modified from https://github.com/sindresorhus/file-type to be used standalone on browser based apps.
  5. *
  6. * This library requires Node "buffer" module as a pre-requisite. The "buffer" module is made available in this repo
  7. * for standalone use via the `buffer.js` script which needs to be loaded before this file on the page.
  8. *
  9. * Author: Kartik Visweswaran, Krajee.com
  10. */
  11. var KrajeeFileTypeConfig = {
  12. minimumBytes: 4100, // A fair amount of file-types are detectable within this range,
  13. defaultMessages: 'End-Of-Stream',
  14. tarHeaderChecksumMatches: function (buffer, offset = 0) {
  15. var readSum = Number.parseInt(buffer.toString('utf8', 148, 154).replace(/\0.*$/, '').trim(), 8); // Read sum in header
  16. if (Number.isNaN(readSum)) {
  17. return false;
  18. }
  19. var sum = 8 * 0x20; // Initialize signed bit sum
  20. for (let i = offset; i < offset + 148; i++) {
  21. sum += buffer[i];
  22. }
  23. for (let i = offset + 156; i < offset + 512; i++) {
  24. sum += buffer[i];
  25. }
  26. return readSum === sum;
  27. },
  28. uint32SyncSafeToken: {
  29. get: function (buffer, offset) {
  30. return (buffer[offset + 3] & 0x7F) | ((buffer[offset + 2]) << 7) | ((buffer[offset + 1]) << 14) | ((buffer[offset]) << 21);
  31. },
  32. len: 4,
  33. },
  34. dv: function (array) {
  35. return new DataView(array.buffer, array.byteOffset);
  36. },
  37. Token: {
  38. /**
  39. * 8-bit unsigned integer
  40. */
  41. UINT8: {
  42. len: 1,
  43. get: function (array, offset) {
  44. return KrajeeFileTypeConfig.dv(array).getUint8(offset);
  45. },
  46. put: function (array, offset, value) {
  47. KrajeeFileTypeConfig.dv(array).setUint8(offset, value);
  48. return offset + 1;
  49. }
  50. },
  51. /**
  52. * 16-bit unsigned integer, Little Endian byte order
  53. */
  54. UINT16_LE: {
  55. len: 2,
  56. get: function (array, offset) {
  57. return KrajeeFileTypeConfig.dv(array).getUint16(offset, true);
  58. },
  59. put: function (array, offset, value) {
  60. KrajeeFileTypeConfig.dv(array).setUint16(offset, value, true);
  61. return offset + 2;
  62. }
  63. },
  64. /**
  65. * 16-bit unsigned integer, Big Endian byte order
  66. */
  67. UINT16_BE: {
  68. len: 2,
  69. get: function (array, offset) {
  70. return KrajeeFileTypeConfig.dv(array).getUint16(offset);
  71. },
  72. put: function (array, offset, value) {
  73. KrajeeFileTypeConfig.dv(array).setUint16(offset, value);
  74. return offset + 2;
  75. }
  76. },
  77. /**
  78. * 32-bit unsigned integer, Big Endian byte order
  79. */
  80. INT32_BE: {
  81. len: 4,
  82. get: function (array, offset) {
  83. return KrajeeFileTypeConfig.dv(array).getInt32(offset);
  84. },
  85. put: function (array, offset, value) {
  86. KrajeeFileTypeConfig.dv(array).setInt32(offset, value);
  87. return offset + 4;
  88. }
  89. },
  90. /**
  91. * 32-bit unsigned integer, Little Endian byte order
  92. */
  93. UINT32_LE: {
  94. len: 4,
  95. get: function (array, offset) {
  96. return KrajeeFileTypeConfig.dv(array).getUint32(offset, true);
  97. },
  98. put: function (array, offset, value) {
  99. KrajeeFileTypeConfig.dv(array).setUint32(offset, value, true);
  100. return offset + 4;
  101. }
  102. },
  103. /**
  104. * 32-bit unsigned integer, Big Endian byte order
  105. */
  106. UINT32_BE: {
  107. len: 4,
  108. get: function (array, offset) {
  109. return KrajeeFileTypeConfig.dv(array).getUint32(offset);
  110. },
  111. put: function (array, offset, value) {
  112. KrajeeFileTypeConfig.dv(array).setUint32(offset, value);
  113. return offset + 4;
  114. }
  115. },
  116. /**
  117. * 64-bit unsigned integer, Little Endian byte order
  118. */
  119. UINT64_LE: {
  120. len: 8,
  121. get: function (array, offset) {
  122. return KrajeeFileTypeConfig.dv(array).getBigUint64(offset, true);
  123. },
  124. put: function (array, offset, value) {
  125. KrajeeFileTypeConfig.dv(array).setBigUint64(offset, value, true);
  126. return offset + 8;
  127. }
  128. },
  129. /**
  130. * 64-bit unsigned integer, Big Endian byte order
  131. */
  132. UINT64_BE: {
  133. len: 8,
  134. get: function (array, offset) {
  135. return KrajeeFileTypeConfig.dv(array).getBigUint64(offset);
  136. },
  137. put: function (array, offset, value) {
  138. KrajeeFileTypeConfig.dv(array).setBigUint64(offset, value);
  139. return offset + 8;
  140. }
  141. }
  142. }
  143. };
  144. class EndOfStreamError extends Error {
  145. constructor() {
  146. super(KrajeeFileTypeConfig.defaultMessages);
  147. }
  148. }
  149. class StringType {
  150. constructor(len, encoding) {
  151. this.len = len;
  152. this.encoding = encoding;
  153. }
  154. get(uint8Array, offset) {
  155. return Buffer.from(uint8Array).toString(this.encoding, offset, offset + this.len);
  156. }
  157. }
  158. async function fileTypeFromTokenizer(tokenizer) {
  159. try {
  160. return new FileTypeParser().parse(tokenizer);
  161. } catch (error) {
  162. if (!(error instanceof EndOfStreamError)) {
  163. throw error;
  164. }
  165. }
  166. }
  167. class BufferTokenizer {
  168. /**
  169. * Construct BufferTokenizer
  170. * @param uint8Array - Uint8Array to tokenize
  171. * @param fileInfo - Pass additional file information to the tokenizer
  172. */
  173. constructor(uint8Array, fileInfo) {
  174. /**
  175. * Tokenizer-stream position
  176. */
  177. this.position = 0;
  178. this.numBuffer = new Uint8Array(8);
  179. this.fileInfo = fileInfo ? fileInfo : {};
  180. this.uint8Array = uint8Array;
  181. this.fileInfo.size = this.fileInfo.size ? this.fileInfo.size : uint8Array.length;
  182. }
  183. /**
  184. * Read a token from the tokenizer-stream
  185. * @param token - The token to read
  186. * @param position - If provided, the desired position in the tokenizer-stream
  187. * @returns Promise with token data
  188. */
  189. async readToken(token, position = this.position) {
  190. const uint8Array = Buffer.alloc(token.len);
  191. const len = await this.readBuffer(uint8Array, {position});
  192. if (len < token.len)
  193. throw new EndOfStreamError();
  194. return token.get(uint8Array, 0);
  195. }
  196. /**
  197. * Peek a token from the tokenizer-stream.
  198. * @param token - Token to peek from the tokenizer-stream.
  199. * @param position - Offset where to begin reading within the file. If position is null, data will be read from the current file position.
  200. * @returns Promise with token data
  201. */
  202. async peekToken(token, position = this.position) {
  203. const uint8Array = Buffer.alloc(token.len);
  204. const len = await this.peekBuffer(uint8Array, {position});
  205. if (len < token.len)
  206. throw new EndOfStreamError();
  207. return token.get(uint8Array, 0);
  208. }
  209. /**
  210. * Read buffer from tokenizer
  211. * @param uint8Array - Uint8Array to tokenize
  212. * @param options - Read behaviour options
  213. * @returns {Promise<number>}
  214. */
  215. async readBuffer(uint8Array, options) {
  216. if (options && options.position) {
  217. if (options.position < this.position) {
  218. throw new Error('`options.position` must be equal or greater than `tokenizer.position`');
  219. }
  220. this.position = options.position;
  221. }
  222. const bytesRead = await this.peekBuffer(uint8Array, options);
  223. this.position += bytesRead;
  224. return bytesRead;
  225. }
  226. /**
  227. * Peek (read ahead) buffer from tokenizer
  228. * @param uint8Array
  229. * @param options - Read behaviour options
  230. * @returns {Promise<number>}
  231. */
  232. async peekBuffer(uint8Array, options) {
  233. const normOptions = this.normalizeOptions(uint8Array, options);
  234. const bytes2read = Math.min(this.uint8Array.length - normOptions.position, normOptions.length);
  235. if ((!normOptions.mayBeLess) && bytes2read < normOptions.length) {
  236. throw new EndOfStreamError();
  237. } else {
  238. uint8Array.set(this.uint8Array.subarray(normOptions.position, normOptions.position + bytes2read), normOptions.offset);
  239. return bytes2read;
  240. }
  241. }
  242. /**
  243. * Read a numeric token from the stream
  244. * @param token - Numeric token
  245. * @returns Promise with number
  246. */
  247. async readNumber(token) {
  248. const len = await this.readBuffer(this.numBuffer, {length: token.len});
  249. if (len < token.len)
  250. throw new EndOfStreamError();
  251. return token.get(this.numBuffer, 0);
  252. }
  253. /**
  254. * Read a numeric token from the stream
  255. * @param token - Numeric token
  256. * @returns Promise with number
  257. */
  258. async peekNumber(token) {
  259. const len = await this.peekBuffer(this.numBuffer, {length: token.len});
  260. if (len < token.len)
  261. throw new EndOfStreamError();
  262. return token.get(this.numBuffer, 0);
  263. }
  264. async close() {
  265. // empty
  266. }
  267. /**
  268. * Ignore number of bytes, advances the pointer in under tokenizer-stream.
  269. * @param length - Number of bytes to ignore
  270. * @return resolves the number of bytes ignored, equals length if this available, otherwise the number of bytes available
  271. */
  272. async ignore(length) {
  273. if (this.fileInfo.size !== undefined) {
  274. const bytesLeft = this.fileInfo.size - this.position;
  275. if (length > bytesLeft) {
  276. this.position += bytesLeft;
  277. return bytesLeft;
  278. }
  279. }
  280. this.position += length;
  281. return length;
  282. }
  283. normalizeOptions(uint8Array, options) {
  284. if (options && options.position !== undefined && options.position < this.position) {
  285. throw new Error('`options.position` must be equal or greater than `tokenizer.position`');
  286. }
  287. if (options) {
  288. return {
  289. mayBeLess: options.mayBeLess === true,
  290. offset: options.offset ? options.offset : 0,
  291. length: options.length ? options.length : (uint8Array.length - (options.offset ? options.offset : 0)),
  292. position: options.position ? options.position : this.position
  293. };
  294. }
  295. return {
  296. mayBeLess: false,
  297. offset: 0,
  298. length: uint8Array.length,
  299. position: this.position
  300. };
  301. }
  302. }
  303. class FileTypeParser {
  304. _check(buffer, headers, options) {
  305. options = {
  306. offset: 0,
  307. ...options,
  308. };
  309. for (const [index, header] of headers.entries()) {
  310. // If a bitmask is set
  311. if (options.mask) {
  312. // If header doesn't equal `buf` with bits masked off
  313. if (header !== (options.mask[index] & buffer[index + options.offset])) {
  314. return false;
  315. }
  316. } else if (header !== buffer[index + options.offset]) {
  317. return false;
  318. }
  319. }
  320. return true;
  321. }
  322. check(header, options) {
  323. return this._check(this.buffer, header, options);
  324. }
  325. stringToBytes(string) {
  326. return [...string].map(character => character.charCodeAt(0));
  327. }
  328. checkString(header, options) {
  329. return this.check(this.stringToBytes(header), options);
  330. }
  331. async parse(input) {
  332. if (!(input instanceof Uint8Array || input instanceof ArrayBuffer || input instanceof BufferTokenizer)) {
  333. throw new TypeError(`Expected the \`input\` argument to be of type \`Uint8Array\` or \`Buffer\` or \`ArrayBuffer\`, got \`${typeof input}\``);
  334. }
  335. let tokenizer = input;
  336. if (!(tokenizer instanceof BufferTokenizer)) {
  337. const buffer = input instanceof Uint8Array ? input : new Uint8Array(input);
  338. if (!(buffer && buffer.length > 1)) {
  339. return;
  340. }
  341. tokenizer = new BufferTokenizer(buffer);
  342. }
  343. try {
  344. return this.parseTokenizer(tokenizer);
  345. } catch (error) {
  346. if (!(error instanceof EndOfStreamError)) {
  347. throw error;
  348. }
  349. }
  350. }
  351. async parseTokenizer(tokenizer) {
  352. const Token = KrajeeFileTypeConfig.Token;
  353. this.buffer = Buffer.alloc(KrajeeFileTypeConfig.minimumBytes);
  354. // Keep reading until EOF if the file size is unknown.
  355. if (tokenizer.fileInfo.size === undefined) {
  356. tokenizer.fileInfo.size = Number.MAX_SAFE_INTEGER;
  357. }
  358. this.tokenizer = tokenizer;
  359. await tokenizer.peekBuffer(this.buffer, {length: 12, mayBeLess: true});
  360. // -- 2-byte signatures --
  361. if (this.check([0x42, 0x4D])) {
  362. return {
  363. ext: 'bmp',
  364. mime: 'image/bmp',
  365. };
  366. }
  367. if (this.check([0x0B, 0x77])) {
  368. return {
  369. ext: 'ac3',
  370. mime: 'audio/vnd.dolby.dd-raw',
  371. };
  372. }
  373. if (this.check([0x78, 0x01])) {
  374. return {
  375. ext: 'dmg',
  376. mime: 'application/x-apple-diskimage',
  377. };
  378. }
  379. if (this.check([0x4D, 0x5A])) {
  380. return {
  381. ext: 'exe',
  382. mime: 'application/x-msdownload',
  383. };
  384. }
  385. if (this.check([0x25, 0x21])) {
  386. await tokenizer.peekBuffer(this.buffer, {length: 24, mayBeLess: true});
  387. if (
  388. this.checkString('PS-Adobe-', {offset: 2})
  389. && this.checkString(' EPSF-', {offset: 14})
  390. ) {
  391. return {
  392. ext: 'eps',
  393. mime: 'application/eps',
  394. };
  395. }
  396. return {
  397. ext: 'ps',
  398. mime: 'application/postscript',
  399. };
  400. }
  401. if (this.check([0x1F, 0xA0]) || this.check([0x1F, 0x9D])) {
  402. return {
  403. ext: 'Z',
  404. mime: 'application/x-compress',
  405. };
  406. }
  407. // -- 3-byte signatures --
  408. if (this.check([0x47, 0x49, 0x46])) {
  409. return {
  410. ext: 'gif',
  411. mime: 'image/gif',
  412. };
  413. }
  414. if (this.check([0xFF, 0xD8, 0xFF])) {
  415. return {
  416. ext: 'jpg',
  417. mime: 'image/jpeg',
  418. };
  419. }
  420. if (this.check([0x49, 0x49, 0xBC])) {
  421. return {
  422. ext: 'jxr',
  423. mime: 'image/vnd.ms-photo',
  424. };
  425. }
  426. if (this.check([0x1F, 0x8B, 0x8])) {
  427. return {
  428. ext: 'gz',
  429. mime: 'application/gzip',
  430. };
  431. }
  432. if (this.check([0x42, 0x5A, 0x68])) {
  433. return {
  434. ext: 'bz2',
  435. mime: 'application/x-bzip2',
  436. };
  437. }
  438. if (this.checkString('ID3')) {
  439. await tokenizer.ignore(6); // Skip ID3 header until the header size
  440. const id3HeaderLength = await tokenizer.readToken(KrajeeFileTypeConfig.uint32SyncSafeToken);
  441. if (tokenizer.position + id3HeaderLength > tokenizer.fileInfo.size) {
  442. // Guess file type based on ID3 header for backward compatibility
  443. return {
  444. ext: 'mp3',
  445. mime: 'audio/mpeg',
  446. };
  447. }
  448. await tokenizer.ignore(id3HeaderLength);
  449. return fileTypeFromTokenizer(tokenizer); // Skip ID3 header, recursion
  450. }
  451. // Musepack, SV7
  452. if (this.checkString('MP+')) {
  453. return {
  454. ext: 'mpc',
  455. mime: 'audio/x-musepack',
  456. };
  457. }
  458. if (
  459. (this.buffer[0] === 0x43 || this.buffer[0] === 0x46)
  460. && this.check([0x57, 0x53], {offset: 1})
  461. ) {
  462. return {
  463. ext: 'swf',
  464. mime: 'application/x-shockwave-flash',
  465. };
  466. }
  467. // -- 4-byte signatures --
  468. if (this.checkString('FLIF')) {
  469. return {
  470. ext: 'flif',
  471. mime: 'image/flif',
  472. };
  473. }
  474. if (this.checkString('8BPS')) {
  475. return {
  476. ext: 'psd',
  477. mime: 'image/vnd.adobe.photoshop',
  478. };
  479. }
  480. if (this.checkString('WEBP', {offset: 8})) {
  481. return {
  482. ext: 'webp',
  483. mime: 'image/webp',
  484. };
  485. }
  486. // Musepack, SV8
  487. if (this.checkString('MPCK')) {
  488. return {
  489. ext: 'mpc',
  490. mime: 'audio/x-musepack',
  491. };
  492. }
  493. if (this.checkString('FORM')) {
  494. return {
  495. ext: 'aif',
  496. mime: 'audio/aiff',
  497. };
  498. }
  499. if (this.checkString('icns', {offset: 0})) {
  500. return {
  501. ext: 'icns',
  502. mime: 'image/icns',
  503. };
  504. }
  505. // Zip-based file formats
  506. // Need to be before the `zip` check
  507. if (this.check([0x50, 0x4B, 0x3, 0x4])) { // Local file header signature
  508. try {
  509. while (tokenizer.position + 30 < tokenizer.fileInfo.size) {
  510. await tokenizer.readBuffer(this.buffer, {length: 30});
  511. // https://en.wikipedia.org/wiki/Zip_(file_format)#File_headers
  512. const zipHeader = {
  513. compressedSize: this.buffer.readUInt32LE(18),
  514. uncompressedSize: this.buffer.readUInt32LE(22),
  515. filenameLength: this.buffer.readUInt16LE(26),
  516. extraFieldLength: this.buffer.readUInt16LE(28),
  517. };
  518. zipHeader.filename = await tokenizer.readToken(new StringType(zipHeader.filenameLength, 'utf-8'));
  519. await tokenizer.ignore(zipHeader.extraFieldLength);
  520. // Assumes signed `.xpi` from addons.mozilla.org
  521. if (zipHeader.filename === 'META-INF/mozilla.rsa') {
  522. return {
  523. ext: 'xpi',
  524. mime: 'application/x-xpinstall',
  525. };
  526. }
  527. if (zipHeader.filename.endsWith('.rels') || zipHeader.filename.endsWith('.xml')) {
  528. const type = zipHeader.filename.split('/')[0];
  529. switch (type) {
  530. case '_rels':
  531. break;
  532. case 'word':
  533. return {
  534. ext: 'docx',
  535. mime: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  536. };
  537. case 'ppt':
  538. return {
  539. ext: 'pptx',
  540. mime: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  541. };
  542. case 'xl':
  543. return {
  544. ext: 'xlsx',
  545. mime: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  546. };
  547. default:
  548. break;
  549. }
  550. }
  551. if (zipHeader.filename.startsWith('xl/')) {
  552. return {
  553. ext: 'xlsx',
  554. mime: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  555. };
  556. }
  557. if (zipHeader.filename.startsWith('3D/') && zipHeader.filename.endsWith('.model')) {
  558. return {
  559. ext: '3mf',
  560. mime: 'model/3mf',
  561. };
  562. }
  563. // The docx, xlsx and pptx file types extend the Office Open XML file format:
  564. // https://en.wikipedia.org/wiki/Office_Open_XML_file_formats
  565. // We look for:
  566. // - one entry named '[Content_Types].xml' or '_rels/.rels',
  567. // - one entry indicating specific type of file.
  568. // MS Office, OpenOffice and LibreOffice may put the parts in different order, so the check should not rely on it.
  569. if (zipHeader.filename === 'mimetype' && zipHeader.compressedSize === zipHeader.uncompressedSize) {
  570. const mimeType = (await tokenizer.readToken(new StringType(zipHeader.compressedSize, 'utf-8'))).trim();
  571. switch (mimeType) {
  572. case 'application/epub+zip':
  573. return {
  574. ext: 'epub',
  575. mime: 'application/epub+zip',
  576. };
  577. case 'application/vnd.oasis.opendocument.text':
  578. return {
  579. ext: 'odt',
  580. mime: 'application/vnd.oasis.opendocument.text',
  581. };
  582. case 'application/vnd.oasis.opendocument.spreadsheet':
  583. return {
  584. ext: 'ods',
  585. mime: 'application/vnd.oasis.opendocument.spreadsheet',
  586. };
  587. case 'application/vnd.oasis.opendocument.presentation':
  588. return {
  589. ext: 'odp',
  590. mime: 'application/vnd.oasis.opendocument.presentation',
  591. };
  592. default:
  593. }
  594. }
  595. // Try to find next header manually when current one is corrupted
  596. if (zipHeader.compressedSize === 0) {
  597. let nextHeaderIndex = -1;
  598. while (nextHeaderIndex < 0 && (tokenizer.position < tokenizer.fileInfo.size)) {
  599. await tokenizer.peekBuffer(this.buffer, {mayBeLess: true});
  600. nextHeaderIndex = this.buffer.indexOf('504B0304', 0, 'hex');
  601. // Move position to the next header if found, skip the whole buffer otherwise
  602. await tokenizer.ignore(nextHeaderIndex >= 0 ? nextHeaderIndex : this.buffer.length);
  603. }
  604. } else {
  605. await tokenizer.ignore(zipHeader.compressedSize);
  606. }
  607. }
  608. } catch (error) {
  609. if (!(error instanceof EndOfStreamError)) {
  610. throw error;
  611. }
  612. }
  613. return {
  614. ext: 'zip',
  615. mime: 'application/zip',
  616. };
  617. }
  618. if (this.checkString('OggS')) {
  619. // This is an OGG container
  620. await tokenizer.ignore(28);
  621. const type = Buffer.alloc(8);
  622. await tokenizer.readBuffer(type);
  623. // Needs to be before `ogg` check
  624. if (this._check(type, [0x4F, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64])) {
  625. return {
  626. ext: 'opus',
  627. mime: 'audio/opus',
  628. };
  629. }
  630. // If ' theora' in header.
  631. if (this._check(type, [0x80, 0x74, 0x68, 0x65, 0x6F, 0x72, 0x61])) {
  632. return {
  633. ext: 'ogv',
  634. mime: 'video/ogg',
  635. };
  636. }
  637. // If '\x01video' in header.
  638. if (this._check(type, [0x01, 0x76, 0x69, 0x64, 0x65, 0x6F, 0x00])) {
  639. return {
  640. ext: 'ogm',
  641. mime: 'video/ogg',
  642. };
  643. }
  644. // If ' FLAC' in header https://xiph.org/flac/faq.html
  645. if (this._check(type, [0x7F, 0x46, 0x4C, 0x41, 0x43])) {
  646. return {
  647. ext: 'oga',
  648. mime: 'audio/ogg',
  649. };
  650. }
  651. // 'Speex ' in header https://en.wikipedia.org/wiki/Speex
  652. if (this._check(type, [0x53, 0x70, 0x65, 0x65, 0x78, 0x20, 0x20])) {
  653. return {
  654. ext: 'spx',
  655. mime: 'audio/ogg',
  656. };
  657. }
  658. // If '\x01vorbis' in header
  659. if (this._check(type, [0x01, 0x76, 0x6F, 0x72, 0x62, 0x69, 0x73])) {
  660. return {
  661. ext: 'ogg',
  662. mime: 'audio/ogg',
  663. };
  664. }
  665. // Default OGG container https://www.iana.org/assignments/media-types/application/ogg
  666. return {
  667. ext: 'ogx',
  668. mime: 'application/ogg',
  669. };
  670. }
  671. if (
  672. this.check([0x50, 0x4B])
  673. && (this.buffer[2] === 0x3 || this.buffer[2] === 0x5 || this.buffer[2] === 0x7)
  674. && (this.buffer[3] === 0x4 || this.buffer[3] === 0x6 || this.buffer[3] === 0x8)
  675. ) {
  676. return {
  677. ext: 'zip',
  678. mime: 'application/zip',
  679. };
  680. }
  681. //
  682. // File Type Box (https://en.wikipedia.org/wiki/ISO_base_media_file_format)
  683. // It's not required to be first, but it's recommended to be. Almost all ISO base media files start with `ftyp` box.
  684. // `ftyp` box must contain a brand major identifier, which must consist of ISO 8859-1 printable characters.
  685. // Here we check for 8859-1 printable characters (for simplicity, it's a mask which also catches one non-printable character).
  686. if (
  687. this.checkString('ftyp', {offset: 4})
  688. && (this.buffer[8] & 0x60) !== 0x00 // Brand major, first character ASCII?
  689. ) {
  690. // They all can have MIME `video/mp4` except `application/mp4` special-case which is hard to detect.
  691. // For some cases, we're specific, everything else falls to `video/mp4` with `mp4` extension.
  692. const brandMajor = this.buffer.toString('binary', 8, 12).replace('\0', ' ').trim();
  693. switch (brandMajor) {
  694. case 'avif':
  695. case 'avis':
  696. return {ext: 'avif', mime: 'image/avif'};
  697. case 'mif1':
  698. return {ext: 'heic', mime: 'image/heif'};
  699. case 'msf1':
  700. return {ext: 'heic', mime: 'image/heif-sequence'};
  701. case 'heic':
  702. case 'heix':
  703. return {ext: 'heic', mime: 'image/heic'};
  704. case 'hevc':
  705. case 'hevx':
  706. return {ext: 'heic', mime: 'image/heic-sequence'};
  707. case 'qt':
  708. return {ext: 'mov', mime: 'video/quicktime'};
  709. case 'M4V':
  710. case 'M4VH':
  711. case 'M4VP':
  712. return {ext: 'm4v', mime: 'video/x-m4v'};
  713. case 'M4P':
  714. return {ext: 'm4p', mime: 'video/mp4'};
  715. case 'M4B':
  716. return {ext: 'm4b', mime: 'audio/mp4'};
  717. case 'M4A':
  718. return {ext: 'm4a', mime: 'audio/x-m4a'};
  719. case 'F4V':
  720. return {ext: 'f4v', mime: 'video/mp4'};
  721. case 'F4P':
  722. return {ext: 'f4p', mime: 'video/mp4'};
  723. case 'F4A':
  724. return {ext: 'f4a', mime: 'audio/mp4'};
  725. case 'F4B':
  726. return {ext: 'f4b', mime: 'audio/mp4'};
  727. case 'crx':
  728. return {ext: 'cr3', mime: 'image/x-canon-cr3'};
  729. default:
  730. if (brandMajor.startsWith('3g')) {
  731. if (brandMajor.startsWith('3g2')) {
  732. return {ext: '3g2', mime: 'video/3gpp2'};
  733. }
  734. return {ext: '3gp', mime: 'video/3gpp'};
  735. }
  736. return {ext: 'mp4', mime: 'video/mp4'};
  737. }
  738. }
  739. if (this.checkString('MThd')) {
  740. return {
  741. ext: 'mid',
  742. mime: 'audio/midi',
  743. };
  744. }
  745. if (
  746. this.checkString('wOFF')
  747. && (
  748. this.check([0x00, 0x01, 0x00, 0x00], {offset: 4})
  749. || this.checkString('OTTO', {offset: 4})
  750. )
  751. ) {
  752. return {
  753. ext: 'woff',
  754. mime: 'font/woff',
  755. };
  756. }
  757. if (this.checkString('wOF2') && (this.check([0x00, 0x01, 0x00, 0x00], {offset: 4}) || this.checkString('OTTO', {offset: 4}))) {
  758. return {
  759. ext: 'woff2',
  760. mime: 'font/woff2',
  761. };
  762. }
  763. if (this.check([0xD4, 0xC3, 0xB2, 0xA1]) || this.check([0xA1, 0xB2, 0xC3, 0xD4])) {
  764. return {
  765. ext: 'pcap',
  766. mime: 'application/vnd.tcpdump.pcap',
  767. };
  768. }
  769. // Sony DSD Stream File (DSF)
  770. if (this.checkString('DSD ')) {
  771. return {
  772. ext: 'dsf',
  773. mime: 'audio/x-dsf', // Non-standard
  774. };
  775. }
  776. if (this.checkString('LZIP')) {
  777. return {
  778. ext: 'lz',
  779. mime: 'application/x-lzip',
  780. };
  781. }
  782. if (this.checkString('fLaC')) {
  783. return {
  784. ext: 'flac',
  785. mime: 'audio/x-flac',
  786. };
  787. }
  788. if (this.check([0x42, 0x50, 0x47, 0xFB])) {
  789. return {
  790. ext: 'bpg',
  791. mime: 'image/bpg',
  792. };
  793. }
  794. if (this.checkString('wvpk')) {
  795. return {
  796. ext: 'wv',
  797. mime: 'audio/wavpack',
  798. };
  799. }
  800. if (this.checkString('%PDF')) {
  801. await tokenizer.ignore(1350);
  802. const maxBufferSize = 10 * 1024 * 1024;
  803. const buffer = Buffer.alloc(Math.min(maxBufferSize, tokenizer.fileInfo.size));
  804. await tokenizer.readBuffer(buffer, {mayBeLess: true});
  805. // Check if this is an Adobe Illustrator file
  806. if (buffer.includes(Buffer.from('AIPrivateData'))) {
  807. return {
  808. ext: 'ai',
  809. mime: 'application/postscript',
  810. };
  811. }
  812. // Assume this is just a normal PDF
  813. return {
  814. ext: 'pdf',
  815. mime: 'application/pdf',
  816. };
  817. }
  818. if (this.check([0x00, 0x61, 0x73, 0x6D])) {
  819. return {
  820. ext: 'wasm',
  821. mime: 'application/wasm',
  822. };
  823. }
  824. // TIFF, little-endian type
  825. if (this.check([0x49, 0x49])) {
  826. const fileType = await this.readTiffHeader(false);
  827. if (fileType) {
  828. return fileType;
  829. }
  830. }
  831. // TIFF, big-endian type
  832. if (this.check([0x4D, 0x4D])) {
  833. const fileType = await this.readTiffHeader(true);
  834. if (fileType) {
  835. return fileType;
  836. }
  837. }
  838. if (this.checkString('MAC ')) {
  839. return {
  840. ext: 'ape',
  841. mime: 'audio/ape',
  842. };
  843. }
  844. // https://github.com/threatstack/libmagic/blob/master/magic/Magdir/matroska
  845. if (this.check([0x1A, 0x45, 0xDF, 0xA3])) { // Root element: EBML
  846. async function readField() {
  847. const msb = await tokenizer.peekNumber(Token.UINT8);
  848. let mask = 0x80;
  849. let ic = 0; // 0 = A, 1 = B, 2 = C, 3
  850. // = D
  851. while ((msb & mask) === 0) {
  852. ++ic;
  853. mask >>= 1;
  854. }
  855. const id = Buffer.alloc(ic + 1);
  856. await tokenizer.readBuffer(id);
  857. return id;
  858. }
  859. async function readElement() {
  860. const id = await readField();
  861. const lengthField = await readField();
  862. lengthField[0] ^= 0x80 >> (lengthField.length - 1);
  863. const nrLength = Math.min(6, lengthField.length); // JavaScript can max read 6 bytes integer
  864. return {
  865. id: id.readUIntBE(0, id.length),
  866. len: lengthField.readUIntBE(lengthField.length - nrLength, nrLength),
  867. };
  868. }
  869. async function readChildren(level, children) {
  870. while (children > 0) {
  871. const element = await readElement();
  872. if (element.id === 0x42_82) {
  873. const rawValue = await tokenizer.readToken(new StringType(element.len, 'utf-8'));
  874. return rawValue.replace(/\00.*$/g, ''); // Return DocType
  875. }
  876. await tokenizer.ignore(element.len); // ignore payload
  877. --children;
  878. }
  879. }
  880. const re = await readElement();
  881. const docType = await readChildren(1, re.len);
  882. switch (docType) {
  883. case 'webm':
  884. return {
  885. ext: 'webm',
  886. mime: 'video/webm',
  887. };
  888. case 'matroska':
  889. return {
  890. ext: 'mkv',
  891. mime: 'video/x-matroska',
  892. };
  893. default:
  894. return;
  895. }
  896. }
  897. // RIFF file format which might be AVI, WAV, QCP, etc
  898. if (this.check([0x52, 0x49, 0x46, 0x46])) {
  899. if (this.check([0x41, 0x56, 0x49], {offset: 8})) {
  900. return {
  901. ext: 'avi',
  902. mime: 'video/vnd.avi',
  903. };
  904. }
  905. if (this.check([0x57, 0x41, 0x56, 0x45], {offset: 8})) {
  906. return {
  907. ext: 'wav',
  908. mime: 'audio/vnd.wave',
  909. };
  910. }
  911. // QLCM, QCP file
  912. if (this.check([0x51, 0x4C, 0x43, 0x4D], {offset: 8})) {
  913. return {
  914. ext: 'qcp',
  915. mime: 'audio/qcelp',
  916. };
  917. }
  918. }
  919. if (this.checkString('SQLi')) {
  920. return {
  921. ext: 'sqlite',
  922. mime: 'application/x-sqlite3',
  923. };
  924. }
  925. if (this.check([0x4E, 0x45, 0x53, 0x1A])) {
  926. return {
  927. ext: 'nes',
  928. mime: 'application/x-nintendo-nes-rom',
  929. };
  930. }
  931. if (this.checkString('Cr24')) {
  932. return {
  933. ext: 'crx',
  934. mime: 'application/x-google-chrome-extension',
  935. };
  936. }
  937. if (
  938. this.checkString('MSCF')
  939. || this.checkString('ISc(')
  940. ) {
  941. return {
  942. ext: 'cab',
  943. mime: 'application/vnd.ms-cab-compressed',
  944. };
  945. }
  946. if (this.check([0xED, 0xAB, 0xEE, 0xDB])) {
  947. return {
  948. ext: 'rpm',
  949. mime: 'application/x-rpm',
  950. };
  951. }
  952. if (this.check([0xC5, 0xD0, 0xD3, 0xC6])) {
  953. return {
  954. ext: 'eps',
  955. mime: 'application/eps',
  956. };
  957. }
  958. if (this.check([0x28, 0xB5, 0x2F, 0xFD])) {
  959. return {
  960. ext: 'zst',
  961. mime: 'application/zstd',
  962. };
  963. }
  964. if (this.check([0x7F, 0x45, 0x4C, 0x46])) {
  965. return {
  966. ext: 'elf',
  967. mime: 'application/x-elf',
  968. };
  969. }
  970. // -- 5-byte signatures --
  971. if (this.check([0x4F, 0x54, 0x54, 0x4F, 0x00])) {
  972. return {
  973. ext: 'otf',
  974. mime: 'font/otf',
  975. };
  976. }
  977. if (this.checkString('#!AMR')) {
  978. return {
  979. ext: 'amr',
  980. mime: 'audio/amr',
  981. };
  982. }
  983. if (this.checkString('{\\rtf')) {
  984. return {
  985. ext: 'rtf',
  986. mime: 'application/rtf',
  987. };
  988. }
  989. if (this.check([0x46, 0x4C, 0x56, 0x01])) {
  990. return {
  991. ext: 'flv',
  992. mime: 'video/x-flv',
  993. };
  994. }
  995. if (this.checkString('IMPM')) {
  996. return {
  997. ext: 'it',
  998. mime: 'audio/x-it',
  999. };
  1000. }
  1001. if (
  1002. this.checkString('-lh0-', {offset: 2})
  1003. || this.checkString('-lh1-', {offset: 2})
  1004. || this.checkString('-lh2-', {offset: 2})
  1005. || this.checkString('-lh3-', {offset: 2})
  1006. || this.checkString('-lh4-', {offset: 2})
  1007. || this.checkString('-lh5-', {offset: 2})
  1008. || this.checkString('-lh6-', {offset: 2})
  1009. || this.checkString('-lh7-', {offset: 2})
  1010. || this.checkString('-lzs-', {offset: 2})
  1011. || this.checkString('-lz4-', {offset: 2})
  1012. || this.checkString('-lz5-', {offset: 2})
  1013. || this.checkString('-lhd-', {offset: 2})
  1014. ) {
  1015. return {
  1016. ext: 'lzh',
  1017. mime: 'application/x-lzh-compressed',
  1018. };
  1019. }
  1020. // MPEG program stream (PS or MPEG-PS)
  1021. if (this.check([0x00, 0x00, 0x01, 0xBA])) {
  1022. // MPEG-PS, MPEG-1 Part 1
  1023. if (this.check([0x21], {offset: 4, mask: [0xF1]})) {
  1024. return {
  1025. ext: 'mpg', // May also be .ps, .mpeg
  1026. mime: 'video/MP1S',
  1027. };
  1028. }
  1029. // MPEG-PS, MPEG-2 Part 1
  1030. if (this.check([0x44], {offset: 4, mask: [0xC4]})) {
  1031. return {
  1032. ext: 'mpg', // May also be .mpg, .m2p, .vob or .sub
  1033. mime: 'video/MP2P',
  1034. };
  1035. }
  1036. }
  1037. if (this.checkString('ITSF')) {
  1038. return {
  1039. ext: 'chm',
  1040. mime: 'application/vnd.ms-htmlhelp',
  1041. };
  1042. }
  1043. // -- 6-byte signatures --
  1044. if (this.check([0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00])) {
  1045. return {
  1046. ext: 'xz',
  1047. mime: 'application/x-xz',
  1048. };
  1049. }
  1050. if (this.checkString('<?xml ')) {
  1051. return {
  1052. ext: 'xml',
  1053. mime: 'application/xml',
  1054. };
  1055. }
  1056. if (this.check([0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C])) {
  1057. return {
  1058. ext: '7z',
  1059. mime: 'application/x-7z-compressed',
  1060. };
  1061. }
  1062. if (
  1063. this.check([0x52, 0x61, 0x72, 0x21, 0x1A, 0x7])
  1064. && (this.buffer[6] === 0x0 || this.buffer[6] === 0x1)
  1065. ) {
  1066. return {
  1067. ext: 'rar',
  1068. mime: 'application/x-rar-compressed',
  1069. };
  1070. }
  1071. if (this.checkString('solid ')) {
  1072. return {
  1073. ext: 'stl',
  1074. mime: 'model/stl',
  1075. };
  1076. }
  1077. // -- 7-byte signatures --
  1078. if (this.checkString('BLENDER')) {
  1079. return {
  1080. ext: 'blend',
  1081. mime: 'application/x-blender',
  1082. };
  1083. }
  1084. if (this.checkString('!<arch>')) {
  1085. await tokenizer.ignore(8);
  1086. const string = await tokenizer.readToken(new StringType(13, 'ascii'));
  1087. if (string === 'debian-binary') {
  1088. return {
  1089. ext: 'deb',
  1090. mime: 'application/x-deb',
  1091. };
  1092. }
  1093. return {
  1094. ext: 'ar',
  1095. mime: 'application/x-unix-archive',
  1096. };
  1097. }
  1098. // -- 8-byte signatures --
  1099. if (this.check([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A])) {
  1100. // APNG format (https://wiki.mozilla.org/APNG_Specification)
  1101. // 1. Find the first IDAT (image data) chunk (49 44 41 54)
  1102. // 2. Check if there is an "acTL" chunk before the IDAT one (61 63 54 4C)
  1103. // Offset calculated as follows:
  1104. // - 8 bytes: PNG signature
  1105. // - 4 (length) + 4 (chunk type) + 13 (chunk data) + 4 (CRC): IHDR chunk
  1106. await tokenizer.ignore(8); // ignore PNG signature
  1107. async function readChunkHeader() {
  1108. return {
  1109. length: await tokenizer.readToken(Token.INT32_BE),
  1110. type: await tokenizer.readToken(new StringType(4, 'binary')),
  1111. };
  1112. }
  1113. do {
  1114. const chunk = await readChunkHeader();
  1115. if (chunk.length < 0) {
  1116. return; // Invalid chunk length
  1117. }
  1118. switch (chunk.type) {
  1119. case 'IDAT':
  1120. return {
  1121. ext: 'png',
  1122. mime: 'image/png',
  1123. };
  1124. case 'acTL':
  1125. return {
  1126. ext: 'apng',
  1127. mime: 'image/apng',
  1128. };
  1129. default:
  1130. await tokenizer.ignore(chunk.length + 4); // Ignore chunk-data + CRC
  1131. }
  1132. } while (tokenizer.position + 8 < tokenizer.fileInfo.size);
  1133. return {
  1134. ext: 'png',
  1135. mime: 'image/png',
  1136. };
  1137. }
  1138. if (this.check([0x41, 0x52, 0x52, 0x4F, 0x57, 0x31, 0x00, 0x00])) {
  1139. return {
  1140. ext: 'arrow',
  1141. mime: 'application/x-apache-arrow',
  1142. };
  1143. }
  1144. if (this.check([0x67, 0x6C, 0x54, 0x46, 0x02, 0x00, 0x00, 0x00])) {
  1145. return {
  1146. ext: 'glb',
  1147. mime: 'model/gltf-binary',
  1148. };
  1149. }
  1150. // `mov` format variants
  1151. if (
  1152. this.check([0x66, 0x72, 0x65, 0x65], {offset: 4}) // `free`
  1153. || this.check([0x6D, 0x64, 0x61, 0x74], {offset: 4}) // `mdat` MJPEG
  1154. || this.check([0x6D, 0x6F, 0x6F, 0x76], {offset: 4}) // `moov`
  1155. || this.check([0x77, 0x69, 0x64, 0x65], {offset: 4}) // `wide`
  1156. ) {
  1157. return {
  1158. ext: 'mov',
  1159. mime: 'video/quicktime',
  1160. };
  1161. }
  1162. if (this.check([0xEF, 0xBB, 0xBF]) && this.checkString('<?xml', {offset: 3})) { // UTF-8-BOM
  1163. return {
  1164. ext: 'xml',
  1165. mime: 'application/xml',
  1166. };
  1167. }
  1168. // -- 9-byte signatures --
  1169. if (this.check([0x49, 0x49, 0x52, 0x4F, 0x08, 0x00, 0x00, 0x00, 0x18])) {
  1170. return {
  1171. ext: 'orf',
  1172. mime: 'image/x-olympus-orf',
  1173. };
  1174. }
  1175. if (this.checkString('gimp xcf ')) {
  1176. return {
  1177. ext: 'xcf',
  1178. mime: 'image/x-xcf',
  1179. };
  1180. }
  1181. // -- 12-byte signatures --
  1182. if (this.check([0x49, 0x49, 0x55, 0x00, 0x18, 0x00, 0x00, 0x00, 0x88, 0xE7, 0x74, 0xD8])) {
  1183. return {
  1184. ext: 'rw2',
  1185. mime: 'image/x-panasonic-rw2',
  1186. };
  1187. }
  1188. // ASF_Header_Object first 80 bytes
  1189. if (this.check([0x30, 0x26, 0xB2, 0x75, 0x8E, 0x66, 0xCF, 0x11, 0xA6, 0xD9])) {
  1190. async function readHeader() {
  1191. const guid = Buffer.alloc(16);
  1192. await tokenizer.readBuffer(guid);
  1193. return {
  1194. id: guid,
  1195. size: Number(await tokenizer.readToken(Token.UINT64_LE)),
  1196. };
  1197. }
  1198. await tokenizer.ignore(30);
  1199. // Search for header should be in first 1KB of file.
  1200. while (tokenizer.position + 24 < tokenizer.fileInfo.size) {
  1201. const header = await readHeader();
  1202. let payload = header.size - 24;
  1203. if (this._check(header.id, [0x91, 0x07, 0xDC, 0xB7, 0xB7, 0xA9, 0xCF, 0x11, 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65])) {
  1204. // Sync on Stream-Properties-Object (B7DC0791-A9B7-11CF-8EE6-00C00C205365)
  1205. const typeId = Buffer.alloc(16);
  1206. payload -= await tokenizer.readBuffer(typeId);
  1207. if (this._check(typeId, [0x40, 0x9E, 0x69, 0xF8, 0x4D, 0x5B, 0xCF, 0x11, 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B])) {
  1208. // Found audio:
  1209. return {
  1210. ext: 'asf',
  1211. mime: 'audio/x-ms-asf',
  1212. };
  1213. }
  1214. if (this._check(typeId, [0xC0, 0xEF, 0x19, 0xBC, 0x4D, 0x5B, 0xCF, 0x11, 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B])) {
  1215. // Found video:
  1216. return {
  1217. ext: 'asf',
  1218. mime: 'video/x-ms-asf',
  1219. };
  1220. }
  1221. break;
  1222. }
  1223. await tokenizer.ignore(payload);
  1224. }
  1225. // Default to ASF generic extension
  1226. return {
  1227. ext: 'asf',
  1228. mime: 'application/vnd.ms-asf',
  1229. };
  1230. }
  1231. if (this.check([0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A])) {
  1232. return {
  1233. ext: 'ktx',
  1234. mime: 'image/ktx',
  1235. };
  1236. }
  1237. if ((this.check([0x7E, 0x10, 0x04]) || this.check([0x7E, 0x18, 0x04])) && this.check([0x30, 0x4D, 0x49, 0x45], {offset: 4})) {
  1238. return {
  1239. ext: 'mie',
  1240. mime: 'application/x-mie',
  1241. };
  1242. }
  1243. if (this.check([0x27, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], {offset: 2})) {
  1244. return {
  1245. ext: 'shp',
  1246. mime: 'application/x-esri-shape',
  1247. };
  1248. }
  1249. if (this.check([0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A])) {
  1250. // JPEG-2000 family
  1251. await tokenizer.ignore(20);
  1252. const type = await tokenizer.readToken(new StringType(4, 'ascii'));
  1253. switch (type) {
  1254. case 'jp2 ':
  1255. return {
  1256. ext: 'jp2',
  1257. mime: 'image/jp2',
  1258. };
  1259. case 'jpx ':
  1260. return {
  1261. ext: 'jpx',
  1262. mime: 'image/jpx',
  1263. };
  1264. case 'jpm ':
  1265. return {
  1266. ext: 'jpm',
  1267. mime: 'image/jpm',
  1268. };
  1269. case 'mjp2':
  1270. return {
  1271. ext: 'mj2',
  1272. mime: 'image/mj2',
  1273. };
  1274. default:
  1275. return;
  1276. }
  1277. }
  1278. if (
  1279. this.check([0xFF, 0x0A])
  1280. || this.check([0x00, 0x00, 0x00, 0x0C, 0x4A, 0x58, 0x4C, 0x20, 0x0D, 0x0A, 0x87, 0x0A])
  1281. ) {
  1282. return {
  1283. ext: 'jxl',
  1284. mime: 'image/jxl',
  1285. };
  1286. }
  1287. if (
  1288. this.check([0xFE, 0xFF, 0, 60, 0, 63, 0, 120, 0, 109, 0, 108]) // UTF-16-BOM-LE
  1289. || this.check([0xFF, 0xFE, 60, 0, 63, 0, 120, 0, 109, 0, 108, 0]) // UTF-16-BOM-LE
  1290. ) {
  1291. return {
  1292. ext: 'xml',
  1293. mime: 'application/xml',
  1294. };
  1295. }
  1296. // -- Unsafe signatures --
  1297. if (
  1298. this.check([0x0, 0x0, 0x1, 0xBA])
  1299. || this.check([0x0, 0x0, 0x1, 0xB3])
  1300. ) {
  1301. return {
  1302. ext: 'mpg',
  1303. mime: 'video/mpeg',
  1304. };
  1305. }
  1306. if (this.check([0x00, 0x01, 0x00, 0x00, 0x00])) {
  1307. return {
  1308. ext: 'ttf',
  1309. mime: 'font/ttf',
  1310. };
  1311. }
  1312. if (this.check([0x00, 0x00, 0x01, 0x00])) {
  1313. return {
  1314. ext: 'ico',
  1315. mime: 'image/x-icon',
  1316. };
  1317. }
  1318. if (this.check([0x00, 0x00, 0x02, 0x00])) {
  1319. return {
  1320. ext: 'cur',
  1321. mime: 'image/x-icon',
  1322. };
  1323. }
  1324. if (this.check([0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1])) {
  1325. // Detected Microsoft Compound File Binary File (MS-CFB) Format.
  1326. return {
  1327. ext: 'cfb',
  1328. mime: 'application/x-cfb',
  1329. };
  1330. }
  1331. // Increase sample size from 12 to 256.
  1332. await tokenizer.peekBuffer(this.buffer, {length: Math.min(256, tokenizer.fileInfo.size), mayBeLess: true});
  1333. // -- 15-byte signatures --
  1334. if (this.checkString('BEGIN:')) {
  1335. if (this.checkString('VCARD', {offset: 6})) {
  1336. return {
  1337. ext: 'vcf',
  1338. mime: 'text/vcard',
  1339. };
  1340. }
  1341. if (this.checkString('VCALENDAR', {offset: 6})) {
  1342. return {
  1343. ext: 'ics',
  1344. mime: 'text/calendar',
  1345. };
  1346. }
  1347. }
  1348. // `raf` is here just to keep all the raw image detectors together.
  1349. if (this.checkString('FUJIFILMCCD-RAW')) {
  1350. return {
  1351. ext: 'raf',
  1352. mime: 'image/x-fujifilm-raf',
  1353. };
  1354. }
  1355. if (this.checkString('Extended Module:')) {
  1356. return {
  1357. ext: 'xm',
  1358. mime: 'audio/x-xm',
  1359. };
  1360. }
  1361. if (this.checkString('Creative Voice File')) {
  1362. return {
  1363. ext: 'voc',
  1364. mime: 'audio/x-voc',
  1365. };
  1366. }
  1367. if (this.check([0x04, 0x00, 0x00, 0x00]) && this.buffer.length >= 16) { // Rough & quick check Pickle/ASAR
  1368. const jsonSize = this.buffer.readUInt32LE(12);
  1369. if (jsonSize > 12 && this.buffer.length >= jsonSize + 16) {
  1370. try {
  1371. const header = this.buffer.slice(16, jsonSize + 16).toString();
  1372. const json = JSON.parse(header);
  1373. // Check if Pickle is ASAR
  1374. if (json.files) { // Final check, assuring Pickle/ASAR format
  1375. return {
  1376. ext: 'asar',
  1377. mime: 'application/x-asar',
  1378. };
  1379. }
  1380. } catch (err) {
  1381. console.log(err);
  1382. }
  1383. }
  1384. }
  1385. if (this.check([0x06, 0x0E, 0x2B, 0x34, 0x02, 0x05, 0x01, 0x01, 0x0D, 0x01, 0x02, 0x01, 0x01, 0x02])) {
  1386. return {
  1387. ext: 'mxf',
  1388. mime: 'application/mxf',
  1389. };
  1390. }
  1391. if (this.checkString('SCRM', {offset: 44})) {
  1392. return {
  1393. ext: 's3m',
  1394. mime: 'audio/x-s3m',
  1395. };
  1396. }
  1397. // Raw MPEG-2 transport stream (188-byte packets)
  1398. if (this.check([0x47]) && this.check([0x47], {offset: 188})) {
  1399. return {
  1400. ext: 'mts',
  1401. mime: 'video/mp2t',
  1402. };
  1403. }
  1404. // Blu-ray Disc Audio-Video (BDAV) MPEG-2 transport stream has 4-byte TP_extra_header before each 188-byte packet
  1405. if (this.check([0x47], {offset: 4}) && this.check([0x47], {offset: 196})) {
  1406. return {
  1407. ext: 'mts',
  1408. mime: 'video/mp2t',
  1409. };
  1410. }
  1411. if (this.check([0x42, 0x4F, 0x4F, 0x4B, 0x4D, 0x4F, 0x42, 0x49], {offset: 60})) {
  1412. return {
  1413. ext: 'mobi',
  1414. mime: 'application/x-mobipocket-ebook',
  1415. };
  1416. }
  1417. if (this.check([0x44, 0x49, 0x43, 0x4D], {offset: 128})) {
  1418. return {
  1419. ext: 'dcm',
  1420. mime: 'application/dicom',
  1421. };
  1422. }
  1423. if (this.check([0x4C, 0x00, 0x00, 0x00, 0x01, 0x14, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46])) {
  1424. return {
  1425. ext: 'lnk',
  1426. mime: 'application/x.ms.shortcut', // Invented by us
  1427. };
  1428. }
  1429. if (this.check([0x62, 0x6F, 0x6F, 0x6B, 0x00, 0x00, 0x00, 0x00, 0x6D, 0x61, 0x72, 0x6B, 0x00, 0x00, 0x00, 0x00])) {
  1430. return {
  1431. ext: 'alias',
  1432. mime: 'application/x.apple.alias', // Invented by us
  1433. };
  1434. }
  1435. if (
  1436. this.check([0x4C, 0x50], {offset: 34})
  1437. && (
  1438. this.check([0x00, 0x00, 0x01], {offset: 8})
  1439. || this.check([0x01, 0x00, 0x02], {offset: 8})
  1440. || this.check([0x02, 0x00, 0x02], {offset: 8})
  1441. )
  1442. ) {
  1443. return {
  1444. ext: 'eot',
  1445. mime: 'application/vnd.ms-fontobject',
  1446. };
  1447. }
  1448. if (this.check([0x06, 0x06, 0xED, 0xF5, 0xD8, 0x1D, 0x46, 0xE5, 0xBD, 0x31, 0xEF, 0xE7, 0xFE, 0x74, 0xB7, 0x1D])) {
  1449. return {
  1450. ext: 'indd',
  1451. mime: 'application/x-indesign',
  1452. };
  1453. }
  1454. // Increase sample size from 256 to 512
  1455. await tokenizer.peekBuffer(this.buffer, {length: Math.min(512, tokenizer.fileInfo.size), mayBeLess: true});
  1456. // Requires a buffer size of 512 bytes
  1457. if (KrajeeFileTypeConfig.tarHeaderChecksumMatches(this.buffer)) {
  1458. return {
  1459. ext: 'tar',
  1460. mime: 'application/x-tar',
  1461. };
  1462. }
  1463. if (this.check([0xFF, 0xFE, 0xFF, 0x0E, 0x53, 0x00, 0x6B, 0x00, 0x65, 0x00, 0x74, 0x00, 0x63, 0x00, 0x68, 0x00, 0x55, 0x00, 0x70, 0x00, 0x20, 0x00, 0x4D, 0x00, 0x6F, 0x00, 0x64, 0x00, 0x65, 0x00, 0x6C, 0x00])) {
  1464. return {
  1465. ext: 'skp',
  1466. mime: 'application/vnd.sketchup.skp',
  1467. };
  1468. }
  1469. if (this.checkString('-----BEGIN PGP MESSAGE-----')) {
  1470. return {
  1471. ext: 'pgp',
  1472. mime: 'application/pgp-encrypted',
  1473. };
  1474. }
  1475. // Check MPEG 1 or 2 Layer 3 header, or 'layer 0' for ADTS (MPEG sync-word 0xFFE)
  1476. if (this.buffer.length >= 2 && this.check([0xFF, 0xE0], {offset: 0, mask: [0xFF, 0xE0]})) {
  1477. if (this.check([0x10], {offset: 1, mask: [0x16]})) {
  1478. // Check for (ADTS) MPEG-2
  1479. if (this.check([0x08], {offset: 1, mask: [0x08]})) {
  1480. return {
  1481. ext: 'aac',
  1482. mime: 'audio/aac',
  1483. };
  1484. }
  1485. // Must be (ADTS) MPEG-4
  1486. return {
  1487. ext: 'aac',
  1488. mime: 'audio/aac',
  1489. };
  1490. }
  1491. // MPEG 1 or 2 Layer 3 header
  1492. // Check for MPEG layer 3
  1493. if (this.check([0x02], {offset: 1, mask: [0x06]})) {
  1494. return {
  1495. ext: 'mp3',
  1496. mime: 'audio/mpeg',
  1497. };
  1498. }
  1499. // Check for MPEG layer 2
  1500. if (this.check([0x04], {offset: 1, mask: [0x06]})) {
  1501. return {
  1502. ext: 'mp2',
  1503. mime: 'audio/mpeg',
  1504. };
  1505. }
  1506. // Check for MPEG layer 1
  1507. if (this.check([0x06], {offset: 1, mask: [0x06]})) {
  1508. return {
  1509. ext: 'mp1',
  1510. mime: 'audio/mpeg',
  1511. };
  1512. }
  1513. }
  1514. return {};
  1515. }
  1516. async readTiffTag(bigEndian) {
  1517. const Token = KrajeeFileTypeConfig.Token;
  1518. let tagId = null;
  1519. try {
  1520. tagId = await this.tokenizer.readToken(bigEndian ? Token.UINT16_BE : Token.UINT16_LE);
  1521. } catch (error) {
  1522. if (error instanceof EndOfStreamError) {
  1523. return null;
  1524. }
  1525. throw error;
  1526. }
  1527. this.tokenizer.ignore(10);
  1528. switch (tagId) {
  1529. case 50_341:
  1530. return {
  1531. ext: 'arw',
  1532. mime: 'image/x-sony-arw',
  1533. };
  1534. case 50_706:
  1535. return {
  1536. ext: 'dng',
  1537. mime: 'image/x-adobe-dng',
  1538. };
  1539. default:
  1540. return null;
  1541. }
  1542. }
  1543. async readTiffIFD(bigEndian) {
  1544. const Token = KrajeeFileTypeConfig.Token;
  1545. const numberOfTags = await this.tokenizer.readToken(bigEndian ? Token.UINT16_BE : Token.UINT16_LE);
  1546. for (let n = 0; n < numberOfTags; ++n) {
  1547. const fileType = await this.readTiffTag(bigEndian);
  1548. if (fileType) {
  1549. return fileType;
  1550. }
  1551. }
  1552. return null;
  1553. }
  1554. async readTiffHeader(bigEndian) {
  1555. const Token = KrajeeFileTypeConfig.Token;
  1556. const version = (bigEndian ? Token.UINT16_BE : Token.UINT16_LE).get(this.buffer, 2);
  1557. const ifdOffset = (bigEndian ? Token.UINT32_BE : Token.UINT32_LE).get(this.buffer, 4);
  1558. const tiff = {ext: 'tif', mime: 'image/tiff'};
  1559. if (version === 42) {
  1560. // TIFF file header
  1561. if (ifdOffset >= 6) {
  1562. if (this.checkString('CR', {offset: 8})) {
  1563. return {
  1564. ext: 'cr2',
  1565. mime: 'image/x-canon-cr2',
  1566. };
  1567. }
  1568. if (ifdOffset >= 8 && (this.check([0x1C, 0x00, 0xFE, 0x00], {offset: 8}) || this.check([0x1F, 0x00, 0x0B, 0x00], {offset: 8}))) {
  1569. return {
  1570. ext: 'nef',
  1571. mime: 'image/x-nikon-nef',
  1572. };
  1573. }
  1574. }
  1575. await this.tokenizer.ignore(ifdOffset);
  1576. const fileType = await this.readTiffIFD(false);
  1577. return fileType ? fileType : tiff;
  1578. }
  1579. if (version === 43) { // Big TIFF file header
  1580. return tiff;
  1581. }
  1582. }
  1583. }