From 501123a96ac448ddb8062be7b7f94d566c8942da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20Panzenb=C3=B6ck?= Date: Fri, 7 Aug 2015 21:52:27 +0200 Subject: [PATCH] forgot to add xmidi.h and xmidi.c --- src/xmidi.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/xmidi.h | 35 ++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/xmidi.c create mode 100644 src/xmidi.h diff --git a/src/xmidi.c b/src/xmidi.c new file mode 100644 index 0000000..9952102 --- /dev/null +++ b/src/xmidi.c @@ -0,0 +1,66 @@ +/* Copyright (c) 2015 Mathias Panzenböck + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include + +#include "aiff.h" +#include "xmidi.h" + +int xmidi_isfile(const uint8_t *data, size_t input_len, struct file_info *info) +{ + size_t length = 0; + + if (input_len < XMIDI_MIN_SIZE) + return 0; + + if (MAGIC(data) == FORM_MAGIC) { + length = be32toh(*(const uint32_t *)(data + 4)); + if (SIZE_MAX - 8 < length || MAGIC(data + 8) != XDIR_MAGIC) + return 0; + length += 8; + } + + if (input_len < length || input_len - length < XMIDI_MIN_SIZE) + return 0; + + if (MAGIC(data + length) != CAT_MAGIC || MAGIC(data + length + 8) != XMID_MAGIC) + return 0; + + size_t cat_length = be32toh(*(const uint32_t *)(data + length + 4)); + + if (SIZE_MAX - 8 < cat_length) + return 0; + + cat_length += 8; + + if (SIZE_MAX - length < cat_length) + return 0; + + length += cat_length; + + if (info) + { + info->length = length; + info->ext = "xmid"; + } + + return 1; +} diff --git a/src/xmidi.h b/src/xmidi.h new file mode 100644 index 0000000..1be9702 --- /dev/null +++ b/src/xmidi.h @@ -0,0 +1,35 @@ +/* Copyright (c) 2015 Mathias Panzenböck + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MEDIAEXTRACT_XMIDI_H__ +#define MEDIAEXTRACT_XMIDI_H__ + +#include "mediaextract.h" + +#define XDIR_MAGIC MAGIC("XDIR") +#define CAT_MAGIC MAGIC("CAT ") +#define XMID_MAGIC MAGIC("XMID") + +#define XMIDI_MIN_SIZE 12 + +int xmidi_isfile(const uint8_t *data, size_t input_len, struct file_info *info); + +#endif /* MEDIAEXTRACT_XMIDI_H__ */