| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  |  | /*
 | 
					
						
							|  |  |  |  |  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  |  |  */ | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-18 19:23:24 +01:00
										 |  |  |  | #include <AK/LexicalPath.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  |  | #include <AK/StringBuilder.h>
 | 
					
						
							|  |  |  |  | #include <LibCore/MimeData.h>
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace Core { | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |  | Vector<DeprecatedString> MimeData::formats() const | 
					
						
							| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |  |     Vector<DeprecatedString> mime_types; | 
					
						
							| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  |  |     mime_types.ensure_capacity(m_data.size()); | 
					
						
							| 
									
										
										
										
											2021-05-14 20:53:27 +02:00
										 |  |  |  |     for (auto& it : m_data) | 
					
						
							| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  |  |         mime_types.unchecked_append(it.key); | 
					
						
							|  |  |  |  |     return mime_types; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | Vector<URL> MimeData::urls() const | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     auto it = m_data.find("text/uri-list"); | 
					
						
							|  |  |  |  |     if (it == m_data.end()) | 
					
						
							|  |  |  |  |         return {}; | 
					
						
							|  |  |  |  |     Vector<URL> urls; | 
					
						
							|  |  |  |  |     for (auto& line : StringView(it->value).split_view('\n')) { | 
					
						
							|  |  |  |  |         urls.append(URL(line)); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  |     return urls; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |  | void MimeData::set_urls(Vector<URL> const& urls) | 
					
						
							| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     StringBuilder builder; | 
					
						
							|  |  |  |  |     for (auto& url : urls) { | 
					
						
							|  |  |  |  |         builder.append(url.to_string()); | 
					
						
							|  |  |  |  |         builder.append('\n'); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  |     set_data("text/uri-list", builder.to_byte_buffer()); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |  | DeprecatedString MimeData::text() const | 
					
						
							| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |  |     return DeprecatedString::copy(m_data.get("text/plain").value_or({})); | 
					
						
							| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |  | void MimeData::set_text(DeprecatedString const& text) | 
					
						
							| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     set_data("text/plain", text.to_byte_buffer()); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |  | DeprecatedString guess_mime_type_based_on_filename(StringView path) | 
					
						
							| 
									
										
										
										
											2020-07-27 19:49:43 +02:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     if (path.ends_with(".pbm"sv, CaseSensitivity::CaseInsensitive)) | 
					
						
							| 
									
										
										
										
											2020-07-27 19:49:43 +02:00
										 |  |  |  |         return "image/x‑portable‑bitmap"; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     if (path.ends_with(".pgm"sv, CaseSensitivity::CaseInsensitive)) | 
					
						
							| 
									
										
										
										
											2020-07-27 19:49:43 +02:00
										 |  |  |  |         return "image/x‑portable‑graymap"; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     if (path.ends_with(".png"sv, CaseSensitivity::CaseInsensitive)) | 
					
						
							| 
									
										
										
										
											2020-07-27 19:49:43 +02:00
										 |  |  |  |         return "image/png"; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     if (path.ends_with(".ppm"sv, CaseSensitivity::CaseInsensitive)) | 
					
						
							| 
									
										
										
										
											2020-07-27 19:49:43 +02:00
										 |  |  |  |         return "image/x‑portable‑pixmap"; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     if (path.ends_with(".gif"sv, CaseSensitivity::CaseInsensitive)) | 
					
						
							| 
									
										
										
										
											2020-07-27 19:49:43 +02:00
										 |  |  |  |         return "image/gif"; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     if (path.ends_with(".bmp"sv, CaseSensitivity::CaseInsensitive)) | 
					
						
							| 
									
										
										
										
											2020-07-27 19:49:43 +02:00
										 |  |  |  |         return "image/bmp"; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     if (path.ends_with(".jpg"sv, CaseSensitivity::CaseInsensitive) || path.ends_with(".jpeg"sv, CaseSensitivity::CaseInsensitive)) | 
					
						
							| 
									
										
										
										
											2020-07-27 19:49:43 +02:00
										 |  |  |  |         return "image/jpeg"; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     if (path.ends_with(".qoi"sv, CaseSensitivity::CaseInsensitive)) | 
					
						
							| 
									
										
										
										
											2021-12-21 01:23:53 +01:00
										 |  |  |  |         return "image/x-qoi"; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     if (path.ends_with(".svg"sv, CaseSensitivity::CaseInsensitive)) | 
					
						
							| 
									
										
										
										
											2020-09-07 19:45:14 +02:00
										 |  |  |  |         return "image/svg+xml"; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     if (path.ends_with(".md"sv, CaseSensitivity::CaseInsensitive)) | 
					
						
							| 
									
										
										
										
											2020-07-27 19:49:43 +02:00
										 |  |  |  |         return "text/markdown"; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     if (path.ends_with(".html"sv, CaseSensitivity::CaseInsensitive) || path.ends_with(".htm"sv, CaseSensitivity::CaseInsensitive)) | 
					
						
							| 
									
										
										
										
											2020-07-27 19:49:43 +02:00
										 |  |  |  |         return "text/html"; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     if (path.ends_with(".css"sv, CaseSensitivity::CaseInsensitive)) | 
					
						
							| 
									
										
										
										
											2022-03-19 18:01:37 +01:00
										 |  |  |  |         return "text/css"; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     if (path.ends_with(".js"sv, CaseSensitivity::CaseInsensitive)) | 
					
						
							| 
									
										
										
										
											2021-05-05 19:24:06 +03:00
										 |  |  |  |         return "application/javascript"; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     if (path.ends_with(".json"sv, CaseSensitivity::CaseInsensitive)) | 
					
						
							| 
									
										
										
										
											2021-05-05 19:24:06 +03:00
										 |  |  |  |         return "application/json"; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     if (path.ends_with(".zip"sv, CaseSensitivity::CaseInsensitive)) | 
					
						
							| 
									
										
										
										
											2022-03-07 22:59:16 +01:00
										 |  |  |  |         return "application/zip"; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     if (path.ends_with(".md"sv, CaseSensitivity::CaseInsensitive)) | 
					
						
							| 
									
										
										
										
											2021-05-05 19:24:06 +03:00
										 |  |  |  |         return "text/markdown"; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     if (path.ends_with("/"sv, CaseSensitivity::CaseInsensitive)) | 
					
						
							| 
									
										
										
										
											2020-07-27 19:49:43 +02:00
										 |  |  |  |         return "text/html"; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     if (path.ends_with(".csv"sv, CaseSensitivity::CaseInsensitive)) | 
					
						
							| 
									
										
										
										
											2020-11-23 16:16:27 +03:30
										 |  |  |  |         return "text/csv"; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     if (path.ends_with(".sheets"sv, CaseSensitivity::CaseInsensitive)) | 
					
						
							| 
									
										
										
										
											2022-05-21 19:35:33 +02:00
										 |  |  |  |         return "application/x-sheets+json"; | 
					
						
							| 
									
										
										
										
											2022-03-18 19:23:24 +01:00
										 |  |  |  |     // FIXME: Share this, TextEditor and HackStudio language detection somehow.
 | 
					
						
							|  |  |  |  |     auto basename = LexicalPath::basename(path); | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     if (path.ends_with(".cpp"sv, CaseSensitivity::CaseInsensitive) | 
					
						
							|  |  |  |  |         || path.ends_with(".c"sv, CaseSensitivity::CaseInsensitive) | 
					
						
							|  |  |  |  |         || path.ends_with(".hpp"sv, CaseSensitivity::CaseInsensitive) | 
					
						
							|  |  |  |  |         || path.ends_with(".h"sv, CaseSensitivity::CaseInsensitive) | 
					
						
							|  |  |  |  |         || path.ends_with(".gml"sv, CaseSensitivity::CaseInsensitive) | 
					
						
							|  |  |  |  |         || path.ends_with(".ini"sv, CaseSensitivity::CaseInsensitive) | 
					
						
							|  |  |  |  |         || path.ends_with(".ipc"sv, CaseSensitivity::CaseInsensitive) | 
					
						
							|  |  |  |  |         || path.ends_with(".txt"sv, CaseSensitivity::CaseInsensitive) | 
					
						
							| 
									
										
										
										
											2022-03-18 19:23:24 +01:00
										 |  |  |  |         || basename == "CMakeLists.txt" | 
					
						
							|  |  |  |  |         || basename == ".history" | 
					
						
							|  |  |  |  |         || basename == ".shellrc") | 
					
						
							|  |  |  |  |         return "text/plain"; | 
					
						
							| 
									
										
										
										
											2022-03-18 19:02:25 +01:00
										 |  |  |  |     return "application/octet-stream"; | 
					
						
							| 
									
										
										
										
											2020-07-27 19:49:43 +02:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-18 20:19:27 +03:00
										 |  |  |  | #define ENUMERATE_HEADER_CONTENTS                                                                                                                \
 | 
					
						
							| 
									
										
										
											
												LibCore+Userland: Add 13 more detectable file types
This patch adds 13 new detectable file formats, which are as follows in
alphabetical order:
.blend, .isz, ext* filesystem, Lua bytecode, Matroska container, NES
ROM, .pdf, qcow image, .rtf, WebAssembly bytecode, Windows 3.1X/95
compressed archive and raw zlib stream
Some are a tad esoteric, but the more file types we detect, the more
useful this utility becomes! :^)
											
										 
											2021-05-24 20:14:11 +03:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(blend, "extra/blender", 0, 7, 'B', 'L', 'E', 'N', 'D', 'E', 'R')                                                \ | 
					
						
							| 
									
										
										
										
											2021-05-18 20:19:27 +03:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(bmp, "image/bmp", 0, 2, 'B', 'M')                                                                               \ | 
					
						
							|  |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(bzip2, "application/x-bzip2", 0, 3, 'B', 'Z', 'h')                                                              \ | 
					
						
							| 
									
										
										
											
												LibCore+Userland: Add 13 more detectable file types
This patch adds 13 new detectable file formats, which are as follows in
alphabetical order:
.blend, .isz, ext* filesystem, Lua bytecode, Matroska container, NES
ROM, .pdf, qcow image, .rtf, WebAssembly bytecode, Windows 3.1X/95
compressed archive and raw zlib stream
Some are a tad esoteric, but the more file types we detect, the more
useful this utility becomes! :^)
											
										 
											2021-05-24 20:14:11 +03:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(compressed_iso, "extra/isz", 0, 4, 'I', 's', 'Z', '!')                                                          \ | 
					
						
							| 
									
										
										
										
											2021-05-18 20:19:27 +03:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(elf, "extra/elf", 0, 4, 0x7F, 'E', 'L', 'F')                                                                    \ | 
					
						
							| 
									
										
										
											
												LibCore+Userland: Add 13 more detectable file types
This patch adds 13 new detectable file formats, which are as follows in
alphabetical order:
.blend, .isz, ext* filesystem, Lua bytecode, Matroska container, NES
ROM, .pdf, qcow image, .rtf, WebAssembly bytecode, Windows 3.1X/95
compressed archive and raw zlib stream
Some are a tad esoteric, but the more file types we detect, the more
useful this utility becomes! :^)
											
										 
											2021-05-24 20:14:11 +03:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(ext, "extra/ext", 0x438, 2, 0x53, 0xEF)                                                                         \ | 
					
						
							| 
									
										
										
										
											2022-07-22 20:59:52 +02:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(flac, "audio/flac", 0, 4, 'f', 'L', 'a', 'C')                                                                   \ | 
					
						
							| 
									
										
										
										
											2021-05-18 20:19:27 +03:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(gif_87, "image/gif", 0, 6, 'G', 'I', 'F', '8', '7', 'a')                                                        \ | 
					
						
							|  |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(gif_89, "image/gif", 0, 6, 'G', 'I', 'F', '8', '9', 'a')                                                        \ | 
					
						
							|  |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(gzip, "application/gzip", 0, 2, 0x1F, 0x8B)                                                                     \ | 
					
						
							| 
									
										
										
										
											2021-05-18 20:28:49 +03:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(iso9660_0, "extra/iso-9660", 0x8001, 5, 0x43, 0x44, 0x30, 0x30, 0x31)                                           \ | 
					
						
							|  |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(iso9660_1, "extra/iso-9660", 0x8801, 5, 0x43, 0x44, 0x30, 0x30, 0x31)                                           \ | 
					
						
							|  |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(iso9660_2, "extra/iso-9660", 0x9001, 5, 0x43, 0x44, 0x30, 0x30, 0x31)                                           \ | 
					
						
							| 
									
										
										
										
											2021-05-18 20:19:27 +03:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(jpeg, "image/jpeg", 0, 4, 0xFF, 0xD8, 0xFF, 0xDB)                                                               \ | 
					
						
							|  |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(jpeg_huh, "image/jpeg", 0, 4, 0xFF, 0xD8, 0xFF, 0xEE)                                                           \ | 
					
						
							|  |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(jpeg_jfif, "image/jpeg", 0, 12, 0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 'J', 'F', 'I', 'F', 0x00, 0x01)             \ | 
					
						
							| 
									
										
										
											
												LibCore+Userland: Add 13 more detectable file types
This patch adds 13 new detectable file formats, which are as follows in
alphabetical order:
.blend, .isz, ext* filesystem, Lua bytecode, Matroska container, NES
ROM, .pdf, qcow image, .rtf, WebAssembly bytecode, Windows 3.1X/95
compressed archive and raw zlib stream
Some are a tad esoteric, but the more file types we detect, the more
useful this utility becomes! :^)
											
										 
											2021-05-24 20:14:11 +03:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(lua_bytecode, "extra/lua-bytecode", 0, 4, 0x1B, 'L', 'u', 'a')                                                  \ | 
					
						
							| 
									
										
										
										
											2021-05-18 20:19:27 +03:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(midi, "audio/midi", 0, 4, 0x4D, 0x54, 0x68, 0x64)                                                               \ | 
					
						
							| 
									
										
										
											
												LibCore+Userland: Add 13 more detectable file types
This patch adds 13 new detectable file formats, which are as follows in
alphabetical order:
.blend, .isz, ext* filesystem, Lua bytecode, Matroska container, NES
ROM, .pdf, qcow image, .rtf, WebAssembly bytecode, Windows 3.1X/95
compressed archive and raw zlib stream
Some are a tad esoteric, but the more file types we detect, the more
useful this utility becomes! :^)
											
										 
											2021-05-24 20:14:11 +03:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(mkv, "extra/matroska", 0, 4, 0x1A, 0x45, 0xDF, 0xA3)                                                            \ | 
					
						
							| 
									
										
										
										
											2022-07-22 21:00:23 +02:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(mp3, "audio/mpeg", 0, 2, 0xFF, 0xFB)                                                                            \ | 
					
						
							| 
									
										
										
											
												LibCore+Userland: Add 13 more detectable file types
This patch adds 13 new detectable file formats, which are as follows in
alphabetical order:
.blend, .isz, ext* filesystem, Lua bytecode, Matroska container, NES
ROM, .pdf, qcow image, .rtf, WebAssembly bytecode, Windows 3.1X/95
compressed archive and raw zlib stream
Some are a tad esoteric, but the more file types we detect, the more
useful this utility becomes! :^)
											
										 
											2021-05-24 20:14:11 +03:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(nesrom, "extra/nes-rom", 0, 4, 'N', 'E', 'S', 0x1A)                                                             \ | 
					
						
							| 
									
										
										
										
											2021-05-18 20:19:27 +03:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(pbm, "image/x-portable-bitmap", 0, 3, 0x50, 0x31, 0x0A)                                                         \ | 
					
						
							| 
									
										
										
											
												LibCore+Userland: Add 13 more detectable file types
This patch adds 13 new detectable file formats, which are as follows in
alphabetical order:
.blend, .isz, ext* filesystem, Lua bytecode, Matroska container, NES
ROM, .pdf, qcow image, .rtf, WebAssembly bytecode, Windows 3.1X/95
compressed archive and raw zlib stream
Some are a tad esoteric, but the more file types we detect, the more
useful this utility becomes! :^)
											
										 
											2021-05-24 20:14:11 +03:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(pdf, "application/pdf", 0, 5, 0x25, 'P', 'D', 'F', 0x2D)                                                        \ | 
					
						
							| 
									
										
										
										
											2021-05-18 20:19:27 +03:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(pgm, "image/x-portable-graymap", 0, 3, 0x50, 0x32, 0x0A)                                                        \ | 
					
						
							|  |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(png, "image/png", 0, 8, 0x89, 'P', 'N', 'G', 0x0D, 0x0A, 0x1A, 0x0A)                                            \ | 
					
						
							|  |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(ppm, "image/x-portable-pixmap", 0, 3, 0x50, 0x33, 0x0A)                                                         \ | 
					
						
							| 
									
										
										
											
												LibCore+Userland: Add 13 more detectable file types
This patch adds 13 new detectable file formats, which are as follows in
alphabetical order:
.blend, .isz, ext* filesystem, Lua bytecode, Matroska container, NES
ROM, .pdf, qcow image, .rtf, WebAssembly bytecode, Windows 3.1X/95
compressed archive and raw zlib stream
Some are a tad esoteric, but the more file types we detect, the more
useful this utility becomes! :^)
											
										 
											2021-05-24 20:14:11 +03:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(qcow, "extra/qcow", 0, 3, 'Q', 'F', 'I')                                                                        \ | 
					
						
							| 
									
										
										
										
											2021-12-21 01:23:53 +01:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(qoi, "image/x-qoi", 0, 4, 'q', 'o', 'i', 'f')                                                                   \ | 
					
						
							| 
									
										
										
											
												LibCore+Userland: Add 13 more detectable file types
This patch adds 13 new detectable file formats, which are as follows in
alphabetical order:
.blend, .isz, ext* filesystem, Lua bytecode, Matroska container, NES
ROM, .pdf, qcow image, .rtf, WebAssembly bytecode, Windows 3.1X/95
compressed archive and raw zlib stream
Some are a tad esoteric, but the more file types we detect, the more
useful this utility becomes! :^)
											
										 
											2021-05-24 20:14:11 +03:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(rtf, "application/rtf", 0, 6, 0x7B, 0x5C, 0x72, 0x74, 0x66, 0x31)                                               \ | 
					
						
							| 
									
										
										
										
											2021-05-18 20:19:27 +03:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(sevenzip, "application/x-7z-compressed", 0, 6, 0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C)                              \ | 
					
						
							|  |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(shell, "text/x-shellscript", 0, 10, '#', '!', '/', 'b', 'i', 'n', '/', 's', 'h', '\n')                          \ | 
					
						
							|  |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(sqlite, "extra/sqlite", 0, 16, 'S', 'Q', 'L', 'i', 't', 'e', ' ', 'f', 'o', 'r', 'm', 'a', 't', ' ', '3', 0x00) \ | 
					
						
							| 
									
										
										
										
											2021-05-18 20:28:49 +03:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(tar, "application/tar", 0x101, 5, 0x75, 0x73, 0x74, 0x61, 0x72)                                                 \ | 
					
						
							| 
									
										
										
										
											2022-03-07 22:59:16 +01:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(zip, "application/zip", 0, 2, 0x50, 0x4B)                                                                       \ | 
					
						
							| 
									
										
										
										
											2021-05-18 20:19:27 +03:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(tiff, "image/tiff", 0, 4, 'I', 'I', '*', 0x00)                                                                  \ | 
					
						
							| 
									
										
										
											
												LibCore+Userland: Add 13 more detectable file types
This patch adds 13 new detectable file formats, which are as follows in
alphabetical order:
.blend, .isz, ext* filesystem, Lua bytecode, Matroska container, NES
ROM, .pdf, qcow image, .rtf, WebAssembly bytecode, Windows 3.1X/95
compressed archive and raw zlib stream
Some are a tad esoteric, but the more file types we detect, the more
useful this utility becomes! :^)
											
										 
											2021-05-24 20:14:11 +03:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(tiff_bigendian, "image/tiff", 0, 4, 'M', 'M', 0x00, '*')                                                        \ | 
					
						
							|  |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(wasm, "application/wasm", 0, 4, 0x00, 'a', 's', 'm')                                                            \ | 
					
						
							| 
									
										
										
										
											2022-07-22 21:00:23 +02:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(wav, "audio/wave", 8, 4, 'W', 'A', 'V', 'E')                                                                    \ | 
					
						
							| 
									
										
										
											
												LibCore+Userland: Add 13 more detectable file types
This patch adds 13 new detectable file formats, which are as follows in
alphabetical order:
.blend, .isz, ext* filesystem, Lua bytecode, Matroska container, NES
ROM, .pdf, qcow image, .rtf, WebAssembly bytecode, Windows 3.1X/95
compressed archive and raw zlib stream
Some are a tad esoteric, but the more file types we detect, the more
useful this utility becomes! :^)
											
										 
											2021-05-24 20:14:11 +03:00
										 |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(win_31x_archive, "extra/win-31x-compressed", 0, 4, 'K', 'W', 'A', 'J')                                          \ | 
					
						
							|  |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(win_95_archive, "extra/win-95-compressed", 0, 4, 'S', 'Z', 'D', 'D')                                            \ | 
					
						
							|  |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(zlib_0, "extra/raw-zlib", 0, 2, 0x78, 0x01)                                                                     \ | 
					
						
							|  |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(zlib_1, "extra/raw-zlib", 0, 2, 0x78, 0x5E)                                                                     \ | 
					
						
							|  |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(zlib_2, "extra/raw-zlib", 0, 2, 0x78, 0x9C)                                                                     \ | 
					
						
							|  |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(zlib_3, "extra/raw-zlib", 0, 2, 0x78, 0xDA)                                                                     \ | 
					
						
							|  |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(zlib_4, "extra/raw-zlib", 0, 2, 0x78, 0x20)                                                                     \ | 
					
						
							|  |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(zlib_5, "extra/raw-zlib", 0, 2, 0x78, 0x7D)                                                                     \ | 
					
						
							|  |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(zlib_6, "extra/raw-zlib", 0, 2, 0x78, 0xBB)                                                                     \ | 
					
						
							|  |  |  |  |     __ENUMERATE_MIME_TYPE_HEADER(zlib_7, "extra/raw-zlib", 0, 2, 0x78, 0xF9) | 
					
						
							| 
									
										
										
										
											2021-05-03 21:48:21 +03:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-18 20:19:27 +03:00
										 |  |  |  | #define __ENUMERATE_MIME_TYPE_HEADER(var_name, mime_type, pattern_offset, pattern_size, ...) \
 | 
					
						
							|  |  |  |  |     static const u8 var_name##_arr[pattern_size] = { __VA_ARGS__ };                          \ | 
					
						
							| 
									
										
										
										
											2021-05-03 21:48:21 +03:00
										 |  |  |  |     static constexpr ReadonlyBytes var_name = ReadonlyBytes { var_name##_arr, pattern_size }; | 
					
						
							|  |  |  |  | ENUMERATE_HEADER_CONTENTS | 
					
						
							|  |  |  |  | #undef __ENUMERATE_MIME_TYPE_HEADER
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |  | Optional<DeprecatedString> guess_mime_type_based_on_sniffed_bytes(ReadonlyBytes bytes) | 
					
						
							| 
									
										
										
										
											2021-05-03 21:48:21 +03:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-18 20:19:27 +03:00
										 |  |  |  | #define __ENUMERATE_MIME_TYPE_HEADER(var_name, mime_type, pattern_offset, pattern_size, ...)                       \
 | 
					
						
							|  |  |  |  |     if (static_cast<ssize_t>(bytes.size()) >= pattern_offset && bytes.slice(pattern_offset).starts_with(var_name)) \ | 
					
						
							| 
									
										
										
										
											2021-05-03 21:48:21 +03:00
										 |  |  |  |         return mime_type; | 
					
						
							|  |  |  |  |     ENUMERATE_HEADER_CONTENTS; | 
					
						
							|  |  |  |  | #undef __ENUMERATE_MIME_TYPE_HEADER
 | 
					
						
							|  |  |  |  |     return {}; | 
					
						
							|  |  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-02-14 13:17:26 +01:00
										 |  |  |  | } |