filetype.js 56 KB

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