123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- #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
- uint32_t get_noencrypt_boxs_size(FILE* pSrcFile, const filecryption_callback_t* pcallback, bool bgetmoov)
- {
- uint32_t 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_LEVEL_ERROR, "get ftyp box failed.");
- return 0;
- }
- safe_log(pcallback, FILECRYPT_LEVEL_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_LEVEL_ERROR, "get free box failed.");
- return 0;
- }
- safe_log(pcallback, FILECRYPT_LEVEL_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_LEVEL_ERROR, "get mdat box failed.");
- return 0;
- }
- safe_log(pcallback, FILECRYPT_LEVEL_DEBUG, "get mdat box success, box size is %d.", uboxsize);
- uret += uboxsize;
- safe_log(pcallback, FILECRYPT_LEVEL_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_LEVEL_ERROR, "get moov box failed.");
- return 0;
- }
- safe_log(pcallback, FILECRYPT_LEVEL_DEBUG, "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;
- }
|