#include "stdafx.h" #include "mp4info.h" static unsigned int big_to_small_endian_32bit(unsigned int const big) { unsigned int data = big; unsigned int uret = 0; uret += (data >> 0 & 0xff) << 24; uret += (data >> 8 & 0xff) << 16; uret += (data >> 16 & 0xff) << 8; uret += (data >> 24 & 0xff) << 0; return uret; } //ftyp, free, mdat box not encryt unsigned int get_noencrypt_boxs_size(FILE* pSrcFile, const filecryption_callback_t* pcallback, bool bgetmoov) { unsigned int uret = 0; if (NULL == pSrcFile) { return uret; } int ipostion = ftell(pSrcFile); //ftyp box box_head_t box_head; fread(&box_head, sizeof(box_head_t), 1, pSrcFile); unsigned int uboxsize = big_to_small_endian_32bit(box_head.ibox_size); if (BOX_FTYP != big_to_small_endian_32bit(box_head.box_type)) { safe_log(pcallback, FILECRYPT_LOG_ERROR, "get ftyp box failed."); return 0; } safe_log(pcallback, FILECRYPT_LOG_DEBUG, "get ftyp box success, box size is %d.", uboxsize); uret += uboxsize; fseek(pSrcFile, uboxsize - sizeof(box_head_t), SEEK_CUR); //free box fread(&box_head, sizeof(box_head_t), 1, pSrcFile); uboxsize = big_to_small_endian_32bit(box_head.ibox_size); if (FREE_BOX != big_to_small_endian_32bit(box_head.box_type)) { safe_log(pcallback, FILECRYPT_LOG_ERROR, "get free box failed."); return 0; } safe_log(pcallback, FILECRYPT_LOG_DEBUG, "get free box success, box size is %d.", uboxsize); uret += uboxsize; fseek(pSrcFile, uboxsize - sizeof(box_head_t), SEEK_CUR); //mdat box fread(&box_head, sizeof(box_head_t), 1, pSrcFile); uboxsize = big_to_small_endian_32bit(box_head.ibox_size); if (MDAT_BOX != big_to_small_endian_32bit(box_head.box_type)) { safe_log(pcallback, FILECRYPT_LOG_ERROR, "get mdat box failed."); return 0; } safe_log(pcallback, FILECRYPT_LOG_DEBUG, "get mdat box success, box size is %d.", uboxsize); uret += uboxsize; safe_log(pcallback, FILECRYPT_LOG_DEBUG, "ftyp box, free box and mdat box total size is %u.", uret); //moov box if (bgetmoov) { fseek(pSrcFile, uboxsize - sizeof(box_head_t), SEEK_CUR); fread(&box_head, sizeof(box_head_t), 1, pSrcFile); uboxsize = big_to_small_endian_32bit(box_head.ibox_size); if (MOOV_BOX != big_to_small_endian_32bit(box_head.box_type)) { safe_log(pcallback, FILECRYPT_LOG_ERROR, "get moov box failed."); return 0; } safe_log(pcallback, FILECRYPT_LOG_INFO, "get moov box success, box size is %d.", uboxsize); } fseek(pSrcFile, ipostion, SEEK_SET); return uret; } bool is_mp4file_completed(const char* pSrcName, const filecryption_callback_t* pcallback) { bool bret = false; if (NULL == pSrcName) { return bret; } FILE* pSrcFile = fopen(pSrcName, "rb"); if (NULL == pSrcFile) { return bret; } unsigned int unoencryptlen = get_noencrypt_boxs_size(pSrcFile, pcallback, true); fclose(pSrcFile); if (0 == unoencryptlen) { return bret; } bret = true; return bret; }