From 4458045c7c1ea4f17c4b2282306d2196369cc7c2 Mon Sep 17 00:00:00 2001 From: Mike Nolan Date: Sat, 13 Jun 2026 13:43:26 -0500 Subject: [PATCH] first commit --- CMakeLists.txt | 8 +++ main.c | 21 ++++++++ mp4istrunc.h | 133 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 162 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 main.c create mode 100644 mp4istrunc.h diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..97bd212 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.16) + +project(mp4istrunc) + +add_executable(mp4istrunc main.c) + +install(TARGETS mp4istrunc DESTINATION bin) +install(FILES mp4istrunc.h DESTINATION include) \ No newline at end of file diff --git a/main.c b/main.c new file mode 100644 index 0000000..be8744f --- /dev/null +++ b/main.c @@ -0,0 +1,21 @@ +#define MP4ISTRUNC_IMPL +#include "mp4istrunc.h" + +int main(int argc, char** argv) +{ + int ret=0; + int count = 0; + int i; + for(i = 1; i < argc; i++) + { + if(mp4trunc_istrunc(argv[i])) { + fprintf(stderr,"File %s was truncated\n", argv[i]); + ret = 1; + count++; + } + } + + fprintf(stderr,"Files that were truncated %i\n", count); + + return ret; +} \ No newline at end of file diff --git a/mp4istrunc.h b/mp4istrunc.h new file mode 100644 index 0000000..44d9ae4 --- /dev/null +++ b/mp4istrunc.h @@ -0,0 +1,133 @@ +#pragma once + + +#if defined(__cplusplus) +extern "C" +#endif + +#if defined(MP4ISTRUNC_IMPL) +#if defined(_WIN32) || defined(WIN32) + +#else +#define _FILE_OFFSET_BITS=64 +#endif +#endif + +#include + +#define MP4TRUNC_OK 0 +#define MP4TRUNC_TRUNC 1 + +int mp4trunc_istruncf(FILE* f); +int mp4trunc_istrunc(const char* path); + + +#if defined(MP4ISTRUNC_IMPL) +#include + +int mp4trunc_istruncf(FILE* f) +{ + #if defined(_WIN32) || defined(WIN32) + + #if defined(MP4TRUNC_LEGACYWIN32) + fseek(f,0L,SEEK_END); + long long len = (long long)ftell(f); + rewind(f); + #else + _fseeki64(f,0L,SEEK_END); + long long len = (long long)_ftelli64(f); + rewind(f); + #endif + #else + fseeko(f,0L,SEEK_END); + long long len = (long long)ftello(f); + rewind(f); + #endif + + + unsigned long long fileSizeToThisPoint = 0; + + int hadWide = 0; + + while(!feof(f)) + { + unsigned char hdr[1024]; + + + if(fread(hdr,1,8,f) != 8) return MP4TRUNC_TRUNC; + + fileSizeToThisPoint += 8; + + unsigned long long atom_len = (unsigned int)hdr[0] << 24 | + (unsigned int)hdr[1] << 16 | + (unsigned int)hdr[2] << 8 | + (unsigned int)hdr[3]; + + + + if(atom_len == 0) return MP4TRUNC_OK; + + + + + if(memcmp(hdr+4,"wide",4) == 0) + { + hadWide = 1; + continue; + } + int isBig=0; + + if(hadWide && atom_len == 1) { + hadWide=0; + isBig=1; + if(fread(hdr+8,1,8,f) != 8) return MP4TRUNC_TRUNC; + fileSizeToThisPoint+=8; + atom_len = (unsigned long)hdr[8] << 56 | + (unsigned long long)hdr[9] << 48 | + (unsigned long long)hdr[10] << 40 | + (unsigned long long)hdr[11] << 32 | + (unsigned long long)hdr[12] << 24 | + (unsigned long long)hdr[13] << 16 | + (unsigned long long)hdr[14] << 8 | + (unsigned long long)hdr[15]; + } + + if(memcmp(hdr+4,"mdat",4) == 0) + { + if((unsigned long long)len < (atom_len + fileSizeToThisPoint-(isBig?16:8))) return MP4TRUNC_TRUNC; + return MP4TRUNC_OK; + } else { + size_t left = atom_len-(isBig?16:8); + size_t read = 0; + do { + size_t toRead = sizeof(hdr); + if(left < toRead) toRead = left; + + read = fread(hdr,1,toRead,f); + + left -= read; + fileSizeToThisPoint += read; + + if(feof(f) && left != 0) return MP4TRUNC_TRUNC; + + } while(read != 0); + + + } + } + + + return MP4TRUNC_OK; +} +int mp4trunc_istrunc(const char* path) +{ + FILE* f = fopen(path,"rb"); + int res = mp4trunc_istruncf(f); + fclose(f); + return res; +} +#endif + +#if defined(__cplusplus) +} +#endif \ No newline at end of file