int svi_encfile(const char* outfn, const char* tagfn, const char* headerfn, const char* infn, const uint8_t* gkn) { int err = 0; mmfile inmf; err = mmfile_open(&inmf, infn, O_RDONLY); if (err != 0) { E(done, "mmfile_open %s", infn); } uint64_t outlen = svi_buflen(inmf.length); uint64_t numblocks = outlen / 65536; uint64_t taglen = numblocks * 64; mmfile outmf; err = mmfile_create(&outmf, outfn, outlen); if (err != 0) { E(cleanup_inmf, "mmfile_create %s", infn); } mmfile tagsmf; err = mmfile_create(&tagsmf, tagfn, taglen); if (err != 0) { E(cleanup_outmf, "mmfile_create %s", tagfn); } mmfile headermf; err = mmfile_create(&headermf, headerfn, 4096); if (err != 0) { E(cleanup_tagsmf, "mmfile_create %s", headerfn); } memset_s(headermf.mem, headermf.length, 0, headermf.length); err = svi_encrypt(outmf.mem, tagsmf.mem, headermf.mem, inmf.mem); if (err != 0) { // Something really bad happened. We can't assess what went // wrong, so we sanitize all of the output files. memset_s(outmf.mem, outmf.length, 0, outmf.length); memset_s(tagsmf.mem, tagsmf.length, 0, tagsmf.length); memset_s(headermf.mem, headermf.length, 0, headermf.length); E(cleanup_headermf, "svi_inplace %u", err); } uint8_t headertag[64] = {0}; blake2b_state s; blake2b_init_key(&s, 64, gkn, 64); blake2b_update(&s, header, HEADERLEN); blake2b_final(&s, headertag, 64); // Clean up the blake2b state. memset_s(&s, sizeof(blake2b_state), 0, sizeof(blake2b_state)); cleanup_headermf: mmfile_close(&headermf); cleanup_tagsmf: mmfile_close(&tagsmf); cleanup_outmf: mmfile_close(&outmf); cleanup_inmf: mmfile_close(&inmf); done: return err; }
int mmfile_amrparser_open (MMFileAMRHandle *handle, const char *filenamec) { tMMFILE_AMR_HANDLE *privateData = NULL; int ret = 0; if (NULL == filenamec || NULL == handle) { debug_error ("file source is NULL\n"); return MMFILE_AMR_PARSER_FAIL; } privateData = mmfile_malloc (sizeof(tMMFILE_AMR_HANDLE)); if (NULL == privateData) { debug_error ("file source is NULL\n"); return MMFILE_AMR_PARSER_FAIL; } /* Initialize the members of handle */ _amr_init_handle(privateData); ret = mmfile_open (&privateData->hFile, filenamec, MMFILE_RDONLY); if(ret == MMFILE_UTIL_FAIL) { debug_error ("error: mmfile_open\n"); goto exception; } mmfile_seek (privateData->hFile, 0, MMFILE_SEEK_END); privateData->fileSize= mmfile_tell(privateData->hFile); mmfile_seek (privateData->hFile, 0, MMFILE_SEEK_SET); if(privateData->fileSize < MMFILE_AMR_MIN_HEADER_SIZE) { debug_error("Too small file to parse!!\n"); goto exception; } ret = _parse_amr_header(privateData); if(ret == MMFILE_AMR_PARSER_FAIL) { debug_error("Invalid AMR header\n"); goto exception; } if(privateData->amrChannelType != AMR_CHANNEL_TYPE_SINGLE) { debug_error("Unsupported channel mode\n"); /*Need to study AMR_Format.txt, Pg:36*/ goto exception; } #ifdef __MMFILE_TEST_MODE__ debug_msg("AMR Format Type: %s\n", \ privateData->amrFormat == AMR_FORMAT_NB? "AMR-NB":"AMR-WB"); #endif *handle = privateData; return MMFILE_AMR_PARSER_SUCCESS; exception: if (privateData) { mmfile_close (privateData->hFile); mmfile_free (privateData); *handle = NULL; } return MMFILE_AMR_PARSER_FAIL; }