|
| 1 | +/* |
| 2 | +* MIT License |
| 3 | +* |
| 4 | +* Copyright (c) 2022 Nick Krecklow |
| 5 | +* |
| 6 | +* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +* of this software and associated documentation files (the "Software"), to deal |
| 8 | +* in the Software without restriction, including without limitation the rights |
| 9 | +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +* copies of the Software, and to permit persons to whom the Software is |
| 11 | +* furnished to do so, subject to the following conditions: |
| 12 | +* |
| 13 | +* The above copyright notice and this permission notice shall be included in all |
| 14 | +* copies or substantial portions of the Software. |
| 15 | +* |
| 16 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +* SOFTWARE. |
| 23 | +*/ |
| 24 | +#include "tinyfseq.h" |
| 25 | + |
| 26 | +#include <string.h> |
| 27 | + |
| 28 | +const char *tf_err_str(enum tf_err_t err) { |
| 29 | +#ifdef TF_INCLUDE_ERR_STRINGS |
| 30 | + switch (err) { |
| 31 | + case TF_OK: |
| 32 | + return "TF_OK (ok)"; |
| 33 | + case TF_EINVALID_HEADER_SIZE: |
| 34 | + return "TF_EINVALID_HEADER_SIZE (undersized `tf_file_header_t` data decoding buffer)"; |
| 35 | + case TF_EINVALID_MAGIC: |
| 36 | + return "TF_EINVALID_MAGIC (invalid magic file signature)"; |
| 37 | + case TF_EINVALID_COMPRESSION_TYPE: |
| 38 | + return "TF_EINVALID_COMPRESSION_TYPE (compressed files are not supported)"; |
| 39 | + case TF_EINVALID_VAR_HEADER_SIZE: |
| 40 | + return "TF_EINVALID_VAR_HEADER_SIZE (undersized `tf_var_header_t` data decoding buffer)"; |
| 41 | + case TF_EINVALID_VAR_VALUE_SIZE: |
| 42 | + return "TF_EINVALID_VAR_VALUE_SIZE (undersized variable value data decoding buffer)"; |
| 43 | + case TF_EINVALID_CHANNEL_RANGE_SIZE: |
| 44 | + return "TF_EINVALID_CHANNEL_RANGE_SIZE (undersized `tf_channel_range_t` data decoding buffer)"; |
| 45 | + default: |
| 46 | + return "unknown `tf_err_t` value"; |
| 47 | + } |
| 48 | +#else |
| 49 | + return "NULL"; |
| 50 | +#endif |
| 51 | +} |
| 52 | + |
| 53 | +enum tf_err_t tf_read_file_header(const uint8_t *bd, int bs, struct tf_file_header_t *header, uint8_t **ep) { |
| 54 | + // header structure is a fixed 32 byte size according to schema |
| 55 | + // https://github.com/Cryptkeeper/fseq-file-format#header |
| 56 | + const int FILE_HEADER_SIZE = 32; |
| 57 | + |
| 58 | + if (bs < FILE_HEADER_SIZE) { |
| 59 | + return TF_EINVALID_HEADER_SIZE; |
| 60 | + } |
| 61 | + |
| 62 | + if (bd[0] != 'P' || bd[1] != 'S' || bd[2] != 'E' || bd[3] != 'Q') { |
| 63 | + return TF_EINVALID_MAGIC; |
| 64 | + } |
| 65 | + |
| 66 | + // WARNING: this is vulnerable to breaking depending on how `tf_file_header_t` is packed/aligned |
| 67 | + // 15 bytes copies from the `.channelDataOffset` field to `.frameStepTimeMillis` |
| 68 | + // two additional calls each copy an individual field |
| 69 | + memcpy((unsigned char *) &header->channelDataOffset, &bd[4], 15); |
| 70 | + |
| 71 | + // upper 4 bits contain additional compression block count data that is ignored by tinyfseq |
| 72 | + // mask to lower 4 bits to filter only the compression type field |
| 73 | + if ((bd[20] & 0xF) != 0) { |
| 74 | + return TF_EINVALID_COMPRESSION_TYPE; |
| 75 | + } |
| 76 | + |
| 77 | + memcpy((unsigned char *) &header->channelRangeCount, &bd[22], 1); |
| 78 | + memcpy((unsigned char *) &header->sequenceUid, &bd[24], 8); |
| 79 | + |
| 80 | + if (ep != NULL) { |
| 81 | + *ep = ((uint8_t *) bd) + FILE_HEADER_SIZE; |
| 82 | + } |
| 83 | + |
| 84 | + return TF_OK; |
| 85 | +} |
| 86 | + |
| 87 | +enum tf_err_t tf_read_var_header(const uint8_t *bd, int bs, struct tf_var_header_t *varHeader, uint8_t *vd, int vs, uint8_t **ep) { |
| 88 | + const int VAR_HEADER_SIZE = 4; |
| 89 | + |
| 90 | + // variable header requires 4 bytes and is NULL terminated |
| 91 | + // an empty variable should be at least 5 bytes |
| 92 | + if (bs <= VAR_HEADER_SIZE) { |
| 93 | + return TF_EINVALID_VAR_HEADER_SIZE; |
| 94 | + } else { |
| 95 | + memcpy((unsigned char *) &varHeader->size, &bd[0], 4); |
| 96 | + |
| 97 | + // only attempt to read variable value if a decoding buffer (`vd`) is provided |
| 98 | + // `.size` already includes the 4 bytes the header consumes |
| 99 | + if (vd != NULL) { |
| 100 | + const int valueSize = varHeader->size - VAR_HEADER_SIZE; |
| 101 | + |
| 102 | + if (vs < valueSize) { |
| 103 | + return TF_EINVALID_VAR_VALUE_SIZE; |
| 104 | + } else { |
| 105 | + memcpy((unsigned char *) vd, &bd[VAR_HEADER_SIZE], valueSize); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if (ep != NULL) { |
| 110 | + *ep = ((uint8_t *) bd) + varHeader->size; |
| 111 | + } |
| 112 | + |
| 113 | + return TF_OK; |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +static uint32_t tf_read_uint24(const uint8_t *bd) { |
| 118 | + // WARNING: this assumes little endian byte order |
| 119 | + return (uint32_t) (bd[0] & (bd[1] << 8) & (bd[2] << 16)); |
| 120 | +} |
| 121 | + |
| 122 | +enum tf_err_t tf_read_channel_range(const uint8_t *bd, int bs, struct tf_channel_range_t *channelRange, uint8_t **ep) { |
| 123 | + const int CHANNEL_RANGE_SIZE = 6; |
| 124 | + |
| 125 | + if (bs < CHANNEL_RANGE_SIZE) { |
| 126 | + return TF_EINVALID_CHANNEL_RANGE_SIZE; |
| 127 | + } else { |
| 128 | + channelRange->firstChannelNumber = tf_read_uint24(&bd[0]); |
| 129 | + channelRange->channelCount = tf_read_uint24(&bd[3]); |
| 130 | + |
| 131 | + if (ep != NULL) { |
| 132 | + *ep = ((uint8_t *) bd) + CHANNEL_RANGE_SIZE; |
| 133 | + } |
| 134 | + |
| 135 | + return TF_OK; |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +float tf_sequence_duration_seconds(uint32_t frameCount, uint8_t frameStepTimeMillis) { |
| 140 | + const unsigned int millis = frameCount * frameStepTimeMillis; |
| 141 | + return ((float) millis) / 1000.0F; |
| 142 | +} |
0 commit comments