filetype.js 57 KB

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