filetype.js 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790
  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. console.log("KV SAYS", typeof tokenizer, tokenizer);
  450. return fileTypeFromTokenizer(tokenizer); // Skip ID3 header, recursion
  451. }
  452. // Musepack, SV7
  453. if (this.checkString('MP+')) {
  454. return {
  455. ext: 'mpc',
  456. mime: 'audio/x-musepack',
  457. };
  458. }
  459. if (
  460. (this.buffer[0] === 0x43 || this.buffer[0] === 0x46)
  461. && this.check([0x57, 0x53], {offset: 1})
  462. ) {
  463. return {
  464. ext: 'swf',
  465. mime: 'application/x-shockwave-flash',
  466. };
  467. }
  468. // -- 4-byte signatures --
  469. if (this.checkString('FLIF')) {
  470. return {
  471. ext: 'flif',
  472. mime: 'image/flif',
  473. };
  474. }
  475. if (this.checkString('8BPS')) {
  476. return {
  477. ext: 'psd',
  478. mime: 'image/vnd.adobe.photoshop',
  479. };
  480. }
  481. if (this.checkString('WEBP', {offset: 8})) {
  482. return {
  483. ext: 'webp',
  484. mime: 'image/webp',
  485. };
  486. }
  487. // Musepack, SV8
  488. if (this.checkString('MPCK')) {
  489. return {
  490. ext: 'mpc',
  491. mime: 'audio/x-musepack',
  492. };
  493. }
  494. if (this.checkString('FORM')) {
  495. return {
  496. ext: 'aif',
  497. mime: 'audio/aiff',
  498. };
  499. }
  500. if (this.checkString('icns', {offset: 0})) {
  501. return {
  502. ext: 'icns',
  503. mime: 'image/icns',
  504. };
  505. }
  506. // Zip-based file formats
  507. // Need to be before the `zip` check
  508. if (this.check([0x50, 0x4B, 0x3, 0x4])) { // Local file header signature
  509. try {
  510. while (tokenizer.position + 30 < tokenizer.fileInfo.size) {
  511. await tokenizer.readBuffer(this.buffer, {length: 30});
  512. // https://en.wikipedia.org/wiki/Zip_(file_format)#File_headers
  513. const zipHeader = {
  514. compressedSize: this.buffer.readUInt32LE(18),
  515. uncompressedSize: this.buffer.readUInt32LE(22),
  516. filenameLength: this.buffer.readUInt16LE(26),
  517. extraFieldLength: this.buffer.readUInt16LE(28),
  518. };
  519. zipHeader.filename = await tokenizer.readToken(new StringType(zipHeader.filenameLength, 'utf-8'));
  520. await tokenizer.ignore(zipHeader.extraFieldLength);
  521. // Assumes signed `.xpi` from addons.mozilla.org
  522. if (zipHeader.filename === 'META-INF/mozilla.rsa') {
  523. return {
  524. ext: 'xpi',
  525. mime: 'application/x-xpinstall',
  526. };
  527. }
  528. if (zipHeader.filename.endsWith('.rels') || zipHeader.filename.endsWith('.xml')) {
  529. const type = zipHeader.filename.split('/')[0];
  530. switch (type) {
  531. case '_rels':
  532. break;
  533. case 'word':
  534. return {
  535. ext: 'docx',
  536. mime: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  537. };
  538. case 'ppt':
  539. return {
  540. ext: 'pptx',
  541. mime: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  542. };
  543. case 'xl':
  544. return {
  545. ext: 'xlsx',
  546. mime: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  547. };
  548. default:
  549. break;
  550. }
  551. }
  552. if (zipHeader.filename.startsWith('xl/')) {
  553. return {
  554. ext: 'xlsx',
  555. mime: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  556. };
  557. }
  558. if (zipHeader.filename.startsWith('3D/') && zipHeader.filename.endsWith('.model')) {
  559. return {
  560. ext: '3mf',
  561. mime: 'model/3mf',
  562. };
  563. }
  564. // The docx, xlsx and pptx file types extend the Office Open XML file format:
  565. // https://en.wikipedia.org/wiki/Office_Open_XML_file_formats
  566. // We look for:
  567. // - one entry named '[Content_Types].xml' or '_rels/.rels',
  568. // - one entry indicating specific type of file.
  569. // MS Office, OpenOffice and LibreOffice may put the parts in different order, so the check should not rely on it.
  570. if (zipHeader.filename === 'mimetype' && zipHeader.compressedSize === zipHeader.uncompressedSize) {
  571. const mimeType = (await tokenizer.readToken(new StringType(zipHeader.compressedSize, 'utf-8'))).trim();
  572. switch (mimeType) {
  573. case 'application/epub+zip':
  574. return {
  575. ext: 'epub',
  576. mime: 'application/epub+zip',
  577. };
  578. case 'application/vnd.oasis.opendocument.text':
  579. return {
  580. ext: 'odt',
  581. mime: 'application/vnd.oasis.opendocument.text',
  582. };
  583. case 'application/vnd.oasis.opendocument.spreadsheet':
  584. return {
  585. ext: 'ods',
  586. mime: 'application/vnd.oasis.opendocument.spreadsheet',
  587. };
  588. case 'application/vnd.oasis.opendocument.presentation':
  589. return {
  590. ext: 'odp',
  591. mime: 'application/vnd.oasis.opendocument.presentation',
  592. };
  593. default:
  594. }
  595. }
  596. // Try to find next header manually when current one is corrupted
  597. if (zipHeader.compressedSize === 0) {
  598. let nextHeaderIndex = -1;
  599. while (nextHeaderIndex < 0 && (tokenizer.position < tokenizer.fileInfo.size)) {
  600. await tokenizer.peekBuffer(this.buffer, {mayBeLess: true});
  601. nextHeaderIndex = this.buffer.indexOf('504B0304', 0, 'hex');
  602. // Move position to the next header if found, skip the whole buffer otherwise
  603. await tokenizer.ignore(nextHeaderIndex >= 0 ? nextHeaderIndex : this.buffer.length);
  604. }
  605. } else {
  606. await tokenizer.ignore(zipHeader.compressedSize);
  607. }
  608. }
  609. } catch (error) {
  610. if (!(error instanceof EndOfStreamError)) {
  611. throw error;
  612. }
  613. }
  614. return {
  615. ext: 'zip',
  616. mime: 'application/zip',
  617. };
  618. }
  619. if (this.checkString('OggS')) {
  620. // This is an OGG container
  621. await tokenizer.ignore(28);
  622. const type = Buffer.alloc(8);
  623. await tokenizer.readBuffer(type);
  624. // Needs to be before `ogg` check
  625. if (this._check(type, [0x4F, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64])) {
  626. return {
  627. ext: 'opus',
  628. mime: 'audio/opus',
  629. };
  630. }
  631. // If ' theora' in header.
  632. if (this._check(type, [0x80, 0x74, 0x68, 0x65, 0x6F, 0x72, 0x61])) {
  633. return {
  634. ext: 'ogv',
  635. mime: 'video/ogg',
  636. };
  637. }
  638. // If '\x01video' in header.
  639. if (this._check(type, [0x01, 0x76, 0x69, 0x64, 0x65, 0x6F, 0x00])) {
  640. return {
  641. ext: 'ogm',
  642. mime: 'video/ogg',
  643. };
  644. }
  645. // If ' FLAC' in header https://xiph.org/flac/faq.html
  646. if (this._check(type, [0x7F, 0x46, 0x4C, 0x41, 0x43])) {
  647. return {
  648. ext: 'oga',
  649. mime: 'audio/ogg',
  650. };
  651. }
  652. // 'Speex ' in header https://en.wikipedia.org/wiki/Speex
  653. if (this._check(type, [0x53, 0x70, 0x65, 0x65, 0x78, 0x20, 0x20])) {
  654. return {
  655. ext: 'spx',
  656. mime: 'audio/ogg',
  657. };
  658. }
  659. // If '\x01vorbis' in header
  660. if (this._check(type, [0x01, 0x76, 0x6F, 0x72, 0x62, 0x69, 0x73])) {
  661. return {
  662. ext: 'ogg',
  663. mime: 'audio/ogg',
  664. };
  665. }
  666. // Default OGG container https://www.iana.org/assignments/media-types/application/ogg
  667. return {
  668. ext: 'ogx',
  669. mime: 'application/ogg',
  670. };
  671. }
  672. if (
  673. this.check([0x50, 0x4B])
  674. && (this.buffer[2] === 0x3 || this.buffer[2] === 0x5 || this.buffer[2] === 0x7)
  675. && (this.buffer[3] === 0x4 || this.buffer[3] === 0x6 || this.buffer[3] === 0x8)
  676. ) {
  677. return {
  678. ext: 'zip',
  679. mime: 'application/zip',
  680. };
  681. }
  682. //
  683. // File Type Box (https://en.wikipedia.org/wiki/ISO_base_media_file_format)
  684. // It's not required to be first, but it's recommended to be. Almost all ISO base media files start with `ftyp` box.
  685. // `ftyp` box must contain a brand major identifier, which must consist of ISO 8859-1 printable characters.
  686. // Here we check for 8859-1 printable characters (for simplicity, it's a mask which also catches one non-printable character).
  687. if (
  688. this.checkString('ftyp', {offset: 4})
  689. && (this.buffer[8] & 0x60) !== 0x00 // Brand major, first character ASCII?
  690. ) {
  691. // They all can have MIME `video/mp4` except `application/mp4` special-case which is hard to detect.
  692. // For some cases, we're specific, everything else falls to `video/mp4` with `mp4` extension.
  693. const brandMajor = this.buffer.toString('binary', 8, 12).replace('\0', ' ').trim();
  694. switch (brandMajor) {
  695. case 'avif':
  696. case 'avis':
  697. return {ext: 'avif', mime: 'image/avif'};
  698. case 'mif1':
  699. return {ext: 'heic', mime: 'image/heif'};
  700. case 'msf1':
  701. return {ext: 'heic', mime: 'image/heif-sequence'};
  702. case 'heic':
  703. case 'heix':
  704. return {ext: 'heic', mime: 'image/heic'};
  705. case 'hevc':
  706. case 'hevx':
  707. return {ext: 'heic', mime: 'image/heic-sequence'};
  708. case 'qt':
  709. return {ext: 'mov', mime: 'video/quicktime'};
  710. case 'M4V':
  711. case 'M4VH':
  712. case 'M4VP':
  713. return {ext: 'm4v', mime: 'video/x-m4v'};
  714. case 'M4P':
  715. return {ext: 'm4p', mime: 'video/mp4'};
  716. case 'M4B':
  717. return {ext: 'm4b', mime: 'audio/mp4'};
  718. case 'M4A':
  719. return {ext: 'm4a', mime: 'audio/x-m4a'};
  720. case 'F4V':
  721. return {ext: 'f4v', mime: 'video/mp4'};
  722. case 'F4P':
  723. return {ext: 'f4p', mime: 'video/mp4'};
  724. case 'F4A':
  725. return {ext: 'f4a', mime: 'audio/mp4'};
  726. case 'F4B':
  727. return {ext: 'f4b', mime: 'audio/mp4'};
  728. case 'crx':
  729. return {ext: 'cr3', mime: 'image/x-canon-cr3'};
  730. default:
  731. if (brandMajor.startsWith('3g')) {
  732. if (brandMajor.startsWith('3g2')) {
  733. return {ext: '3g2', mime: 'video/3gpp2'};
  734. }
  735. return {ext: '3gp', mime: 'video/3gpp'};
  736. }
  737. return {ext: 'mp4', mime: 'video/mp4'};
  738. }
  739. }
  740. if (this.checkString('MThd')) {
  741. return {
  742. ext: 'mid',
  743. mime: 'audio/midi',
  744. };
  745. }
  746. if (
  747. this.checkString('wOFF')
  748. && (
  749. this.check([0x00, 0x01, 0x00, 0x00], {offset: 4})
  750. || this.checkString('OTTO', {offset: 4})
  751. )
  752. ) {
  753. return {
  754. ext: 'woff',
  755. mime: 'font/woff',
  756. };
  757. }
  758. if (this.checkString('wOF2') && (this.check([0x00, 0x01, 0x00, 0x00], {offset: 4}) || this.checkString('OTTO', {offset: 4}))) {
  759. return {
  760. ext: 'woff2',
  761. mime: 'font/woff2',
  762. };
  763. }
  764. if (this.check([0xD4, 0xC3, 0xB2, 0xA1]) || this.check([0xA1, 0xB2, 0xC3, 0xD4])) {
  765. return {
  766. ext: 'pcap',
  767. mime: 'application/vnd.tcpdump.pcap',
  768. };
  769. }
  770. // Sony DSD Stream File (DSF)
  771. if (this.checkString('DSD ')) {
  772. return {
  773. ext: 'dsf',
  774. mime: 'audio/x-dsf', // Non-standard
  775. };
  776. }
  777. if (this.checkString('LZIP')) {
  778. return {
  779. ext: 'lz',
  780. mime: 'application/x-lzip',
  781. };
  782. }
  783. if (this.checkString('fLaC')) {
  784. return {
  785. ext: 'flac',
  786. mime: 'audio/x-flac',
  787. };
  788. }
  789. if (this.check([0x42, 0x50, 0x47, 0xFB])) {
  790. return {
  791. ext: 'bpg',
  792. mime: 'image/bpg',
  793. };
  794. }
  795. if (this.checkString('wvpk')) {
  796. return {
  797. ext: 'wv',
  798. mime: 'audio/wavpack',
  799. };
  800. }
  801. if (this.checkString('%PDF')) {
  802. await tokenizer.ignore(1350);
  803. const maxBufferSize = 10 * 1024 * 1024;
  804. const buffer = Buffer.alloc(Math.min(maxBufferSize, tokenizer.fileInfo.size));
  805. await tokenizer.readBuffer(buffer, {mayBeLess: true});
  806. // Check if this is an Adobe Illustrator file
  807. if (buffer.includes(Buffer.from('AIPrivateData'))) {
  808. return {
  809. ext: 'ai',
  810. mime: 'application/postscript',
  811. };
  812. }
  813. // Assume this is just a normal PDF
  814. return {
  815. ext: 'pdf',
  816. mime: 'application/pdf',
  817. };
  818. }
  819. if (this.check([0x00, 0x61, 0x73, 0x6D])) {
  820. return {
  821. ext: 'wasm',
  822. mime: 'application/wasm',
  823. };
  824. }
  825. // TIFF, little-endian type
  826. if (this.check([0x49, 0x49])) {
  827. const fileType = await this.readTiffHeader(false);
  828. if (fileType) {
  829. return fileType;
  830. }
  831. }
  832. // TIFF, big-endian type
  833. if (this.check([0x4D, 0x4D])) {
  834. const fileType = await this.readTiffHeader(true);
  835. if (fileType) {
  836. return fileType;
  837. }
  838. }
  839. if (this.checkString('MAC ')) {
  840. return {
  841. ext: 'ape',
  842. mime: 'audio/ape',
  843. };
  844. }
  845. // https://github.com/threatstack/libmagic/blob/master/magic/Magdir/matroska
  846. if (this.check([0x1A, 0x45, 0xDF, 0xA3])) { // Root element: EBML
  847. async function readField() {
  848. const msb = await tokenizer.peekNumber(Token.UINT8);
  849. let mask = 0x80;
  850. let ic = 0; // 0 = A, 1 = B, 2 = C, 3
  851. // = D
  852. while ((msb & mask) === 0) {
  853. ++ic;
  854. mask >>= 1;
  855. }
  856. const id = Buffer.alloc(ic + 1);
  857. await tokenizer.readBuffer(id);
  858. return id;
  859. }
  860. async function readElement() {
  861. const id = await readField();
  862. const lengthField = await readField();
  863. lengthField[0] ^= 0x80 >> (lengthField.length - 1);
  864. const nrLength = Math.min(6, lengthField.length); // JavaScript can max read 6 bytes integer
  865. return {
  866. id: id.readUIntBE(0, id.length),
  867. len: lengthField.readUIntBE(lengthField.length - nrLength, nrLength),
  868. };
  869. }
  870. async function readChildren(level, children) {
  871. while (children > 0) {
  872. const element = await readElement();
  873. if (element.id === 0x42_82) {
  874. const rawValue = await tokenizer.readToken(new StringType(element.len, 'utf-8'));
  875. return rawValue.replace(/\00.*$/g, ''); // Return DocType
  876. }
  877. await tokenizer.ignore(element.len); // ignore payload
  878. --children;
  879. }
  880. }
  881. const re = await readElement();
  882. const docType = await readChildren(1, re.len);
  883. switch (docType) {
  884. case 'webm':
  885. return {
  886. ext: 'webm',
  887. mime: 'video/webm',
  888. };
  889. case 'matroska':
  890. return {
  891. ext: 'mkv',
  892. mime: 'video/x-matroska',
  893. };
  894. default:
  895. return;
  896. }
  897. }
  898. // RIFF file format which might be AVI, WAV, QCP, etc
  899. if (this.check([0x52, 0x49, 0x46, 0x46])) {
  900. if (this.check([0x41, 0x56, 0x49], {offset: 8})) {
  901. return {
  902. ext: 'avi',
  903. mime: 'video/vnd.avi',
  904. };
  905. }
  906. if (this.check([0x57, 0x41, 0x56, 0x45], {offset: 8})) {
  907. return {
  908. ext: 'wav',
  909. mime: 'audio/vnd.wave',
  910. };
  911. }
  912. // QLCM, QCP file
  913. if (this.check([0x51, 0x4C, 0x43, 0x4D], {offset: 8})) {
  914. return {
  915. ext: 'qcp',
  916. mime: 'audio/qcelp',
  917. };
  918. }
  919. }
  920. if (this.checkString('SQLi')) {
  921. return {
  922. ext: 'sqlite',
  923. mime: 'application/x-sqlite3',
  924. };
  925. }
  926. if (this.check([0x4E, 0x45, 0x53, 0x1A])) {
  927. return {
  928. ext: 'nes',
  929. mime: 'application/x-nintendo-nes-rom',
  930. };
  931. }
  932. if (this.checkString('Cr24')) {
  933. return {
  934. ext: 'crx',
  935. mime: 'application/x-google-chrome-extension',
  936. };
  937. }
  938. if (
  939. this.checkString('MSCF')
  940. || this.checkString('ISc(')
  941. ) {
  942. return {
  943. ext: 'cab',
  944. mime: 'application/vnd.ms-cab-compressed',
  945. };
  946. }
  947. if (this.check([0xED, 0xAB, 0xEE, 0xDB])) {
  948. return {
  949. ext: 'rpm',
  950. mime: 'application/x-rpm',
  951. };
  952. }
  953. if (this.check([0xC5, 0xD0, 0xD3, 0xC6])) {
  954. return {
  955. ext: 'eps',
  956. mime: 'application/eps',
  957. };
  958. }
  959. if (this.check([0x28, 0xB5, 0x2F, 0xFD])) {
  960. return {
  961. ext: 'zst',
  962. mime: 'application/zstd',
  963. };
  964. }
  965. if (this.check([0x7F, 0x45, 0x4C, 0x46])) {
  966. return {
  967. ext: 'elf',
  968. mime: 'application/x-elf',
  969. };
  970. }
  971. // -- 5-byte signatures --
  972. if (this.check([0x4F, 0x54, 0x54, 0x4F, 0x00])) {
  973. return {
  974. ext: 'otf',
  975. mime: 'font/otf',
  976. };
  977. }
  978. if (this.checkString('#!AMR')) {
  979. return {
  980. ext: 'amr',
  981. mime: 'audio/amr',
  982. };
  983. }
  984. if (this.checkString('{\\rtf')) {
  985. return {
  986. ext: 'rtf',
  987. mime: 'application/rtf',
  988. };
  989. }
  990. if (this.check([0x46, 0x4C, 0x56, 0x01])) {
  991. return {
  992. ext: 'flv',
  993. mime: 'video/x-flv',
  994. };
  995. }
  996. if (this.checkString('IMPM')) {
  997. return {
  998. ext: 'it',
  999. mime: 'audio/x-it',
  1000. };
  1001. }
  1002. if (
  1003. this.checkString('-lh0-', {offset: 2})
  1004. || this.checkString('-lh1-', {offset: 2})
  1005. || this.checkString('-lh2-', {offset: 2})
  1006. || this.checkString('-lh3-', {offset: 2})
  1007. || this.checkString('-lh4-', {offset: 2})
  1008. || this.checkString('-lh5-', {offset: 2})
  1009. || this.checkString('-lh6-', {offset: 2})
  1010. || this.checkString('-lh7-', {offset: 2})
  1011. || this.checkString('-lzs-', {offset: 2})
  1012. || this.checkString('-lz4-', {offset: 2})
  1013. || this.checkString('-lz5-', {offset: 2})
  1014. || this.checkString('-lhd-', {offset: 2})
  1015. ) {
  1016. return {
  1017. ext: 'lzh',
  1018. mime: 'application/x-lzh-compressed',
  1019. };
  1020. }
  1021. // MPEG program stream (PS or MPEG-PS)
  1022. if (this.check([0x00, 0x00, 0x01, 0xBA])) {
  1023. // MPEG-PS, MPEG-1 Part 1
  1024. if (this.check([0x21], {offset: 4, mask: [0xF1]})) {
  1025. return {
  1026. ext: 'mpg', // May also be .ps, .mpeg
  1027. mime: 'video/MP1S',
  1028. };
  1029. }
  1030. // MPEG-PS, MPEG-2 Part 1
  1031. if (this.check([0x44], {offset: 4, mask: [0xC4]})) {
  1032. return {
  1033. ext: 'mpg', // May also be .mpg, .m2p, .vob or .sub
  1034. mime: 'video/MP2P',
  1035. };
  1036. }
  1037. }
  1038. if (this.checkString('ITSF')) {
  1039. return {
  1040. ext: 'chm',
  1041. mime: 'application/vnd.ms-htmlhelp',
  1042. };
  1043. }
  1044. // -- 6-byte signatures --
  1045. if (this.check([0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00])) {
  1046. return {
  1047. ext: 'xz',
  1048. mime: 'application/x-xz',
  1049. };
  1050. }
  1051. if (this.checkString('<?xml ')) {
  1052. return {
  1053. ext: 'xml',
  1054. mime: 'application/xml',
  1055. };
  1056. }
  1057. if (this.check([0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C])) {
  1058. return {
  1059. ext: '7z',
  1060. mime: 'application/x-7z-compressed',
  1061. };
  1062. }
  1063. if (
  1064. this.check([0x52, 0x61, 0x72, 0x21, 0x1A, 0x7])
  1065. && (this.buffer[6] === 0x0 || this.buffer[6] === 0x1)
  1066. ) {
  1067. return {
  1068. ext: 'rar',
  1069. mime: 'application/x-rar-compressed',
  1070. };
  1071. }
  1072. if (this.checkString('solid ')) {
  1073. return {
  1074. ext: 'stl',
  1075. mime: 'model/stl',
  1076. };
  1077. }
  1078. // -- 7-byte signatures --
  1079. if (this.checkString('BLENDER')) {
  1080. return {
  1081. ext: 'blend',
  1082. mime: 'application/x-blender',
  1083. };
  1084. }
  1085. if (this.checkString('!<arch>')) {
  1086. await tokenizer.ignore(8);
  1087. const string = await tokenizer.readToken(new StringType(13, 'ascii'));
  1088. if (string === 'debian-binary') {
  1089. return {
  1090. ext: 'deb',
  1091. mime: 'application/x-deb',
  1092. };
  1093. }
  1094. return {
  1095. ext: 'ar',
  1096. mime: 'application/x-unix-archive',
  1097. };
  1098. }
  1099. // -- 8-byte signatures --
  1100. if (this.check([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A])) {
  1101. // APNG format (https://wiki.mozilla.org/APNG_Specification)
  1102. // 1. Find the first IDAT (image data) chunk (49 44 41 54)
  1103. // 2. Check if there is an "acTL" chunk before the IDAT one (61 63 54 4C)
  1104. // Offset calculated as follows:
  1105. // - 8 bytes: PNG signature
  1106. // - 4 (length) + 4 (chunk type) + 13 (chunk data) + 4 (CRC): IHDR chunk
  1107. await tokenizer.ignore(8); // ignore PNG signature
  1108. async function readChunkHeader() {
  1109. return {
  1110. length: await tokenizer.readToken(Token.INT32_BE),
  1111. type: await tokenizer.readToken(new StringType(4, 'binary')),
  1112. };
  1113. }
  1114. do {
  1115. const chunk = await readChunkHeader();
  1116. if (chunk.length < 0) {
  1117. return; // Invalid chunk length
  1118. }
  1119. switch (chunk.type) {
  1120. case 'IDAT':
  1121. return {
  1122. ext: 'png',
  1123. mime: 'image/png',
  1124. };
  1125. case 'acTL':
  1126. return {
  1127. ext: 'apng',
  1128. mime: 'image/apng',
  1129. };
  1130. default:
  1131. await tokenizer.ignore(chunk.length + 4); // Ignore chunk-data + CRC
  1132. }
  1133. } while (tokenizer.position + 8 < tokenizer.fileInfo.size);
  1134. return {
  1135. ext: 'png',
  1136. mime: 'image/png',
  1137. };
  1138. }
  1139. if (this.check([0x41, 0x52, 0x52, 0x4F, 0x57, 0x31, 0x00, 0x00])) {
  1140. return {
  1141. ext: 'arrow',
  1142. mime: 'application/x-apache-arrow',
  1143. };
  1144. }
  1145. if (this.check([0x67, 0x6C, 0x54, 0x46, 0x02, 0x00, 0x00, 0x00])) {
  1146. return {
  1147. ext: 'glb',
  1148. mime: 'model/gltf-binary',
  1149. };
  1150. }
  1151. // `mov` format variants
  1152. if (
  1153. this.check([0x66, 0x72, 0x65, 0x65], {offset: 4}) // `free`
  1154. || this.check([0x6D, 0x64, 0x61, 0x74], {offset: 4}) // `mdat` MJPEG
  1155. || this.check([0x6D, 0x6F, 0x6F, 0x76], {offset: 4}) // `moov`
  1156. || this.check([0x77, 0x69, 0x64, 0x65], {offset: 4}) // `wide`
  1157. ) {
  1158. return {
  1159. ext: 'mov',
  1160. mime: 'video/quicktime',
  1161. };
  1162. }
  1163. if (this.check([0xEF, 0xBB, 0xBF]) && this.checkString('<?xml', {offset: 3})) { // UTF-8-BOM
  1164. return {
  1165. ext: 'xml',
  1166. mime: 'application/xml',
  1167. };
  1168. }
  1169. // -- 9-byte signatures --
  1170. if (this.check([0x49, 0x49, 0x52, 0x4F, 0x08, 0x00, 0x00, 0x00, 0x18])) {
  1171. return {
  1172. ext: 'orf',
  1173. mime: 'image/x-olympus-orf',
  1174. };
  1175. }
  1176. if (this.checkString('gimp xcf ')) {
  1177. return {
  1178. ext: 'xcf',
  1179. mime: 'image/x-xcf',
  1180. };
  1181. }
  1182. // -- 12-byte signatures --
  1183. if (this.check([0x49, 0x49, 0x55, 0x00, 0x18, 0x00, 0x00, 0x00, 0x88, 0xE7, 0x74, 0xD8])) {
  1184. return {
  1185. ext: 'rw2',
  1186. mime: 'image/x-panasonic-rw2',
  1187. };
  1188. }
  1189. // ASF_Header_Object first 80 bytes
  1190. if (this.check([0x30, 0x26, 0xB2, 0x75, 0x8E, 0x66, 0xCF, 0x11, 0xA6, 0xD9])) {
  1191. async function readHeader() {
  1192. const guid = Buffer.alloc(16);
  1193. await tokenizer.readBuffer(guid);
  1194. return {
  1195. id: guid,
  1196. size: Number(await tokenizer.readToken(Token.UINT64_LE)),
  1197. };
  1198. }
  1199. await tokenizer.ignore(30);
  1200. // Search for header should be in first 1KB of file.
  1201. while (tokenizer.position + 24 < tokenizer.fileInfo.size) {
  1202. const header = await readHeader();
  1203. let payload = header.size - 24;
  1204. if (this._check(header.id, [0x91, 0x07, 0xDC, 0xB7, 0xB7, 0xA9, 0xCF, 0x11, 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65])) {
  1205. // Sync on Stream-Properties-Object (B7DC0791-A9B7-11CF-8EE6-00C00C205365)
  1206. const typeId = Buffer.alloc(16);
  1207. payload -= await tokenizer.readBuffer(typeId);
  1208. if (this._check(typeId, [0x40, 0x9E, 0x69, 0xF8, 0x4D, 0x5B, 0xCF, 0x11, 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B])) {
  1209. // Found audio:
  1210. return {
  1211. ext: 'asf',
  1212. mime: 'audio/x-ms-asf',
  1213. };
  1214. }
  1215. if (this._check(typeId, [0xC0, 0xEF, 0x19, 0xBC, 0x4D, 0x5B, 0xCF, 0x11, 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B])) {
  1216. // Found video:
  1217. return {
  1218. ext: 'asf',
  1219. mime: 'video/x-ms-asf',
  1220. };
  1221. }
  1222. break;
  1223. }
  1224. await tokenizer.ignore(payload);
  1225. }
  1226. // Default to ASF generic extension
  1227. return {
  1228. ext: 'asf',
  1229. mime: 'application/vnd.ms-asf',
  1230. };
  1231. }
  1232. if (this.check([0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A])) {
  1233. return {
  1234. ext: 'ktx',
  1235. mime: 'image/ktx',
  1236. };
  1237. }
  1238. if ((this.check([0x7E, 0x10, 0x04]) || this.check([0x7E, 0x18, 0x04])) && this.check([0x30, 0x4D, 0x49, 0x45], {offset: 4})) {
  1239. return {
  1240. ext: 'mie',
  1241. mime: 'application/x-mie',
  1242. };
  1243. }
  1244. if (this.check([0x27, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], {offset: 2})) {
  1245. return {
  1246. ext: 'shp',
  1247. mime: 'application/x-esri-shape',
  1248. };
  1249. }
  1250. if (this.check([0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A])) {
  1251. // JPEG-2000 family
  1252. await tokenizer.ignore(20);
  1253. const type = await tokenizer.readToken(new StringType(4, 'ascii'));
  1254. switch (type) {
  1255. case 'jp2 ':
  1256. return {
  1257. ext: 'jp2',
  1258. mime: 'image/jp2',
  1259. };
  1260. case 'jpx ':
  1261. return {
  1262. ext: 'jpx',
  1263. mime: 'image/jpx',
  1264. };
  1265. case 'jpm ':
  1266. return {
  1267. ext: 'jpm',
  1268. mime: 'image/jpm',
  1269. };
  1270. case 'mjp2':
  1271. return {
  1272. ext: 'mj2',
  1273. mime: 'image/mj2',
  1274. };
  1275. default:
  1276. return;
  1277. }
  1278. }
  1279. if (
  1280. this.check([0xFF, 0x0A])
  1281. || this.check([0x00, 0x00, 0x00, 0x0C, 0x4A, 0x58, 0x4C, 0x20, 0x0D, 0x0A, 0x87, 0x0A])
  1282. ) {
  1283. return {
  1284. ext: 'jxl',
  1285. mime: 'image/jxl',
  1286. };
  1287. }
  1288. if (
  1289. this.check([0xFE, 0xFF, 0, 60, 0, 63, 0, 120, 0, 109, 0, 108]) // UTF-16-BOM-LE
  1290. || this.check([0xFF, 0xFE, 60, 0, 63, 0, 120, 0, 109, 0, 108, 0]) // UTF-16-BOM-LE
  1291. ) {
  1292. return {
  1293. ext: 'xml',
  1294. mime: 'application/xml',
  1295. };
  1296. }
  1297. // -- Unsafe signatures --
  1298. if (
  1299. this.check([0x0, 0x0, 0x1, 0xBA])
  1300. || this.check([0x0, 0x0, 0x1, 0xB3])
  1301. ) {
  1302. return {
  1303. ext: 'mpg',
  1304. mime: 'video/mpeg',
  1305. };
  1306. }
  1307. if (this.check([0x00, 0x01, 0x00, 0x00, 0x00])) {
  1308. return {
  1309. ext: 'ttf',
  1310. mime: 'font/ttf',
  1311. };
  1312. }
  1313. if (this.check([0x00, 0x00, 0x01, 0x00])) {
  1314. return {
  1315. ext: 'ico',
  1316. mime: 'image/x-icon',
  1317. };
  1318. }
  1319. if (this.check([0x00, 0x00, 0x02, 0x00])) {
  1320. return {
  1321. ext: 'cur',
  1322. mime: 'image/x-icon',
  1323. };
  1324. }
  1325. if (this.check([0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1])) {
  1326. // Detected Microsoft Compound File Binary File (MS-CFB) Format.
  1327. return {
  1328. ext: 'cfb',
  1329. mime: 'application/x-cfb',
  1330. };
  1331. }
  1332. // Increase sample size from 12 to 256.
  1333. await tokenizer.peekBuffer(this.buffer, {length: Math.min(256, tokenizer.fileInfo.size), mayBeLess: true});
  1334. // -- 15-byte signatures --
  1335. if (this.checkString('BEGIN:')) {
  1336. if (this.checkString('VCARD', {offset: 6})) {
  1337. return {
  1338. ext: 'vcf',
  1339. mime: 'text/vcard',
  1340. };
  1341. }
  1342. if (this.checkString('VCALENDAR', {offset: 6})) {
  1343. return {
  1344. ext: 'ics',
  1345. mime: 'text/calendar',
  1346. };
  1347. }
  1348. }
  1349. // `raf` is here just to keep all the raw image detectors together.
  1350. if (this.checkString('FUJIFILMCCD-RAW')) {
  1351. return {
  1352. ext: 'raf',
  1353. mime: 'image/x-fujifilm-raf',
  1354. };
  1355. }
  1356. if (this.checkString('Extended Module:')) {
  1357. return {
  1358. ext: 'xm',
  1359. mime: 'audio/x-xm',
  1360. };
  1361. }
  1362. if (this.checkString('Creative Voice File')) {
  1363. return {
  1364. ext: 'voc',
  1365. mime: 'audio/x-voc',
  1366. };
  1367. }
  1368. if (this.check([0x04, 0x00, 0x00, 0x00]) && this.buffer.length >= 16) { // Rough & quick check Pickle/ASAR
  1369. const jsonSize = this.buffer.readUInt32LE(12);
  1370. if (jsonSize > 12 && this.buffer.length >= jsonSize + 16) {
  1371. try {
  1372. const header = this.buffer.slice(16, jsonSize + 16).toString();
  1373. const json = JSON.parse(header);
  1374. // Check if Pickle is ASAR
  1375. if (json.files) { // Final check, assuring Pickle/ASAR format
  1376. return {
  1377. ext: 'asar',
  1378. mime: 'application/x-asar',
  1379. };
  1380. }
  1381. } catch (err) {
  1382. console.log(err);
  1383. }
  1384. }
  1385. }
  1386. if (this.check([0x06, 0x0E, 0x2B, 0x34, 0x02, 0x05, 0x01, 0x01, 0x0D, 0x01, 0x02, 0x01, 0x01, 0x02])) {
  1387. return {
  1388. ext: 'mxf',
  1389. mime: 'application/mxf',
  1390. };
  1391. }
  1392. if (this.checkString('SCRM', {offset: 44})) {
  1393. return {
  1394. ext: 's3m',
  1395. mime: 'audio/x-s3m',
  1396. };
  1397. }
  1398. // Raw MPEG-2 transport stream (188-byte packets)
  1399. if (this.check([0x47]) && this.check([0x47], {offset: 188})) {
  1400. return {
  1401. ext: 'mts',
  1402. mime: 'video/mp2t',
  1403. };
  1404. }
  1405. // Blu-ray Disc Audio-Video (BDAV) MPEG-2 transport stream has 4-byte TP_extra_header before each 188-byte packet
  1406. if (this.check([0x47], {offset: 4}) && this.check([0x47], {offset: 196})) {
  1407. return {
  1408. ext: 'mts',
  1409. mime: 'video/mp2t',
  1410. };
  1411. }
  1412. if (this.check([0x42, 0x4F, 0x4F, 0x4B, 0x4D, 0x4F, 0x42, 0x49], {offset: 60})) {
  1413. return {
  1414. ext: 'mobi',
  1415. mime: 'application/x-mobipocket-ebook',
  1416. };
  1417. }
  1418. if (this.check([0x44, 0x49, 0x43, 0x4D], {offset: 128})) {
  1419. return {
  1420. ext: 'dcm',
  1421. mime: 'application/dicom',
  1422. };
  1423. }
  1424. if (this.check([0x4C, 0x00, 0x00, 0x00, 0x01, 0x14, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46])) {
  1425. return {
  1426. ext: 'lnk',
  1427. mime: 'application/x.ms.shortcut', // Invented by us
  1428. };
  1429. }
  1430. if (this.check([0x62, 0x6F, 0x6F, 0x6B, 0x00, 0x00, 0x00, 0x00, 0x6D, 0x61, 0x72, 0x6B, 0x00, 0x00, 0x00, 0x00])) {
  1431. return {
  1432. ext: 'alias',
  1433. mime: 'application/x.apple.alias', // Invented by us
  1434. };
  1435. }
  1436. if (
  1437. this.check([0x4C, 0x50], {offset: 34})
  1438. && (
  1439. this.check([0x00, 0x00, 0x01], {offset: 8})
  1440. || this.check([0x01, 0x00, 0x02], {offset: 8})
  1441. || this.check([0x02, 0x00, 0x02], {offset: 8})
  1442. )
  1443. ) {
  1444. return {
  1445. ext: 'eot',
  1446. mime: 'application/vnd.ms-fontobject',
  1447. };
  1448. }
  1449. if (this.check([0x06, 0x06, 0xED, 0xF5, 0xD8, 0x1D, 0x46, 0xE5, 0xBD, 0x31, 0xEF, 0xE7, 0xFE, 0x74, 0xB7, 0x1D])) {
  1450. return {
  1451. ext: 'indd',
  1452. mime: 'application/x-indesign',
  1453. };
  1454. }
  1455. // Increase sample size from 256 to 512
  1456. await tokenizer.peekBuffer(this.buffer, {length: Math.min(512, tokenizer.fileInfo.size), mayBeLess: true});
  1457. // Requires a buffer size of 512 bytes
  1458. if (KrajeeFileTypeConfig.tarHeaderChecksumMatches(this.buffer)) {
  1459. return {
  1460. ext: 'tar',
  1461. mime: 'application/x-tar',
  1462. };
  1463. }
  1464. 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])) {
  1465. return {
  1466. ext: 'skp',
  1467. mime: 'application/vnd.sketchup.skp',
  1468. };
  1469. }
  1470. if (this.checkString('-----BEGIN PGP MESSAGE-----')) {
  1471. return {
  1472. ext: 'pgp',
  1473. mime: 'application/pgp-encrypted',
  1474. };
  1475. }
  1476. // Check MPEG 1 or 2 Layer 3 header, or 'layer 0' for ADTS (MPEG sync-word 0xFFE)
  1477. if (this.buffer.length >= 2 && this.check([0xFF, 0xE0], {offset: 0, mask: [0xFF, 0xE0]})) {
  1478. if (this.check([0x10], {offset: 1, mask: [0x16]})) {
  1479. // Check for (ADTS) MPEG-2
  1480. if (this.check([0x08], {offset: 1, mask: [0x08]})) {
  1481. return {
  1482. ext: 'aac',
  1483. mime: 'audio/aac',
  1484. };
  1485. }
  1486. // Must be (ADTS) MPEG-4
  1487. return {
  1488. ext: 'aac',
  1489. mime: 'audio/aac',
  1490. };
  1491. }
  1492. // MPEG 1 or 2 Layer 3 header
  1493. // Check for MPEG layer 3
  1494. if (this.check([0x02], {offset: 1, mask: [0x06]})) {
  1495. return {
  1496. ext: 'mp3',
  1497. mime: 'audio/mpeg',
  1498. };
  1499. }
  1500. // Check for MPEG layer 2
  1501. if (this.check([0x04], {offset: 1, mask: [0x06]})) {
  1502. return {
  1503. ext: 'mp2',
  1504. mime: 'audio/mpeg',
  1505. };
  1506. }
  1507. // Check for MPEG layer 1
  1508. if (this.check([0x06], {offset: 1, mask: [0x06]})) {
  1509. return {
  1510. ext: 'mp1',
  1511. mime: 'audio/mpeg',
  1512. };
  1513. }
  1514. }
  1515. return {};
  1516. }
  1517. async readTiffTag(bigEndian) {
  1518. const Token = KrajeeFileTypeConfig.Token;
  1519. const tagId = await this.tokenizer.readToken(bigEndian ? Token.UINT16_BE : Token.UINT16_LE);
  1520. this.tokenizer.ignore(10);
  1521. switch (tagId) {
  1522. case 50_341:
  1523. return {
  1524. ext: 'arw',
  1525. mime: 'image/x-sony-arw',
  1526. };
  1527. case 50_706:
  1528. return {
  1529. ext: 'dng',
  1530. mime: 'image/x-adobe-dng',
  1531. };
  1532. default:
  1533. }
  1534. }
  1535. async readTiffIFD(bigEndian) {
  1536. const Token = KrajeeFileTypeConfig.Token;
  1537. const numberOfTags = await this.tokenizer.readToken(bigEndian ? Token.UINT16_BE : Token.UINT16_LE);
  1538. for (let n = 0; n < numberOfTags; ++n) {
  1539. const fileType = await this.readTiffTag(bigEndian);
  1540. if (fileType) {
  1541. return fileType;
  1542. }
  1543. }
  1544. }
  1545. async readTiffHeader(bigEndian) {
  1546. const Token = KrajeeFileTypeConfig.Token;
  1547. const version = (bigEndian ? Token.UINT16_BE : Token.UINT16_LE).get(this.buffer, 2);
  1548. const ifdOffset = (bigEndian ? Token.UINT32_BE : Token.UINT32_LE).get(this.buffer, 4);
  1549. if (version === 42) {
  1550. // TIFF file header
  1551. if (ifdOffset >= 6) {
  1552. if (this.checkString('CR', {offset: 8})) {
  1553. return {
  1554. ext: 'cr2',
  1555. mime: 'image/x-canon-cr2',
  1556. };
  1557. }
  1558. if (ifdOffset >= 8 && (this.check([0x1C, 0x00, 0xFE, 0x00], {offset: 8}) || this.check([0x1F, 0x00, 0x0B, 0x00], {offset: 8}))) {
  1559. return {
  1560. ext: 'nef',
  1561. mime: 'image/x-nikon-nef',
  1562. };
  1563. }
  1564. }
  1565. await this.tokenizer.ignore(ifdOffset);
  1566. const fileType = await this.readTiffIFD(false);
  1567. return fileType ? fileType : {
  1568. ext: 'tif',
  1569. mime: 'image/tiff',
  1570. };
  1571. }
  1572. if (version === 43) { // Big TIFF file header
  1573. return {
  1574. ext: 'tif',
  1575. mime: 'image/tiff',
  1576. };
  1577. }
  1578. }
  1579. }