summaryrefslogtreecommitdiff
path: root/mime.c
diff options
context:
space:
mode:
Diffstat (limited to 'mime.c')
-rw-r--r--mime.c139
1 files changed, 139 insertions, 0 deletions
diff --git a/mime.c b/mime.c
new file mode 100644
index 0000000..7136a99
--- /dev/null
+++ b/mime.c
@@ -0,0 +1,139 @@
+/*
+ * Copyright 2023, Vincent Douillet <vincent@vdouillet.fr>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of the copyright holder nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stddef.h>
+#include <strings.h>
+
+#include "mime.h"
+
+struct mime_map {
+ char* str;
+ enum mime mime;
+};
+
+struct mime_map mime_ext_list[] = {
+ { "aac", MIME_AAC },
+ { "avi", MIME_AVI },
+ { "bin", MIME_BIN },
+ { "bz2", MIME_BZ2 },
+ { "csv", MIME_CSV },
+ { "doc", MIME_DOC },
+ { "docx", MIME_DOCX },
+ { "epub", MIME_EPUB },
+ { "gz", MIME_GZ },
+ { "gif", MIME_GIF },
+ { "html", MIME_HTML },
+ { "jar", MIME_JAR },
+ { "jpg", MIME_JPG },
+ { "jpeg", MIME_JPG },
+ { "log", MIME_TXT },
+ { "mkv", MIME_MKV },
+ { "mp3", MIME_MP3 },
+ { "mp4", MIME_MP4 },
+ { "odp", MIME_ODP },
+ { "ods", MIME_ODS },
+ { "odt", MIME_ODT },
+ { "png", MIME_PNG },
+ { "pdf", MIME_PDF },
+ { "ppt", MIME_PPT },
+ { "pptx", MIME_PPTX },
+ { "tar", MIME_TAR },
+ { "rar", MIME_RAR },
+ { "sh", MIME_SH },
+ { "svg", MIME_SVG },
+ { "svg", MIME_SVG },
+ { "txt", MIME_TXT },
+ { "webm", MIME_WEBM },
+ { "webp", MIME_WEBP },
+ { "xls", MIME_XLS },
+ { "xlsx", MIME_XLSX },
+ { "xml", MIME_XML },
+ { "zip", MIME_ZIP },
+ { "7z", MIME_7Z },
+ { NULL, MIME__MAX }
+};
+
+char* mime_desc_list[MIME__MAX] = {
+ "audio/aac", /* MIME_AAC */
+ "video/x-msvideo", /* MIME_AVI */
+ "application/octet-stream", /* MIME_BIN */
+ "application/x-bzip2", /* MIME_BZ2 */
+ "text/csv", /* MIME_CSV */
+ "application/msword", /* MIME_DOC */
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document", /* MIME_DOCX */
+ "application/epub+zip", /* MIME_EPUB */
+ "application/gzip", /* MIME_GZ */
+ "image/gif", /* MIME_GIF */
+ "text/html", /* MIME_HTML */
+ "application/java-archive", /* MIME_JAR */
+ "image/jpeg", /* MIME_JPG */
+ "video/x-matroska", /* MIME_MKV */
+ "audio/mpeg", /* MIME_MP3 */
+ "video/mp4", /* MIME_MP4 */
+ "application/vnd.oasis.opendocument.presentation", /* MIME_ODP */
+ "application/vnd.oasis.opendocument.spreadsheet", /* MIME_ODS */
+ "application/vnd.oasis.opendocument.text", /* MIME_ODT */
+ "image/png", /* MIME_PNG */
+ "application/pdf", /* MIME_PDF */
+ "application/vnd.ms-powerpoint", /* MIME_PPT */
+ "application/vnd.openxmlformats-officedocument.presentationml.presentation", /* MIME_PPTX */
+ "application/vnd.rar", /* MIME_RAR */
+ "application/x-sh", /* MIME_SH */
+ "image/svg+xml", /* MIME_SVG */
+ "application/x-tar", /* MIME_TAR */
+ "text/plain", /* MIME_TXT */
+ "video/webm", /* MIME_WEBM */
+ "image/webp", /* MIME_WEBP */
+ "application/vnd.ms-excel", /* MIME_XLS */
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", /* MIME_XLSX */
+ "application/xml", /* MIME_XML */
+ "application/zip", /* MIME_ZIP */
+ "application/x-7z-compressed", /* MIME_7Z */
+};
+
+enum mime
+mime_from_ext(char * ext)
+{
+ struct mime_map *mime;
+
+ for(mime = mime_ext_list; mime->str != NULL; mime++) {
+ if (strcasecmp(ext, mime->str) == 0) {
+ return mime->mime;
+ }
+ }
+ return MIME_BIN;
+}
+
+
+char *
+mime_str(enum mime mime)
+{
+ return mime_desc_list[mime];
+}