📄 ipmpx_code.c
字号:
/* * GPAC - Multimedia Framework C SDK * * Copyright (c) Jean Le Feuvre 2000-2005 * All rights reserved * * This file is part of GPAC / MPEG-4 ObjectDescriptor sub-project * * GPAC is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * GPAC is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; see the file COPYING. If not, write to * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. * */#include <gpac/internal/odf_dev.h>#define GF_IPMPX_DATA_ALLOC(__ptr, __stname, __tag) \ __ptr = (__stname*)malloc(sizeof(__stname)); \ if (!__ptr) return NULL; \ memset(__ptr, 0, sizeof(__stname)); \ ((GF_IPMPX_Data *)__ptr)->tag = __tag; \ ((GF_IPMPX_Data *)__ptr)->Version = 0x01; \#define GF_IPMPX_DELETE_ARRAY(__ar) if (__ar) { if (__ar->data) free(__ar->data); free(__ar); }u32 GFINLINE get_field_size(u32 size_desc){ if (size_desc < 0x00000080) return 1; else if (size_desc < 0x00004000) return 2; else if (size_desc < 0x00200000) return 3; else return 4;}void write_var_size(GF_BitStream *bs, u32 size){ unsigned char vals[4]; u32 length = size; vals[3] = (unsigned char) (length & 0x7f); length >>= 7; vals[2] = (unsigned char) ((length & 0x7f) | 0x80); length >>= 7; vals[1] = (unsigned char) ((length & 0x7f) | 0x80); length >>= 7; vals[0] = (unsigned char) ((length & 0x7f) | 0x80); if (size < 0x00000080) { gf_bs_write_int(bs, vals[3], 8); } else if (size < 0x00004000) { gf_bs_write_int(bs, vals[2], 8); gf_bs_write_int(bs, vals[3], 8); } else if (size < 0x00200000) { gf_bs_write_int(bs, vals[1], 8); gf_bs_write_int(bs, vals[2], 8); gf_bs_write_int(bs, vals[3], 8); } else if (size < 0x10000000) { gf_bs_write_int(bs, vals[0], 8); gf_bs_write_int(bs, vals[1], 8); gf_bs_write_int(bs, vals[2], 8); gf_bs_write_int(bs, vals[3], 8); } }GF_IPMPX_ByteArray *GF_IPMPX_GetByteArray(GF_BitStream *bs){ GF_IPMPX_ByteArray *ba; u32 val, size; size = 0; do { val = gf_bs_read_int(bs, 8); size <<= 7; size |= val & 0x7F; } while ( val & 0x80 ); if (!size) return NULL; ba = (GF_IPMPX_ByteArray*)malloc(sizeof(GF_IPMPX_ByteArray)); ba->data = (char*)malloc(sizeof(char)*size); gf_bs_read_data(bs, ba->data, size); ba->length = size; return ba;}u32 GF_IPMPX_GetByteArraySize(GF_IPMPX_ByteArray *ba){ if (!ba) return 1; return ba->length + get_field_size(ba->length);}void GF_IPMPX_WriteByteArray(GF_BitStream *bs, GF_IPMPX_ByteArray *ba){ if (!ba || !ba->data) { write_var_size(bs, 0); } else { write_var_size(bs, ba->length); gf_bs_write_data(bs, ba->data, ba->length); }}void GF_IPMPX_AUTH_Delete(GF_IPMPX_Authentication *auth){ if (!auth) return; switch (auth->tag) { case GF_IPMPX_AUTH_AlgorithmDescr_Tag: { GF_IPMPX_AUTH_AlgorithmDescriptor *p = (GF_IPMPX_AUTH_AlgorithmDescriptor *)auth; GF_IPMPX_DELETE_ARRAY(p->specAlgoID); GF_IPMPX_DELETE_ARRAY(p->OpaqueData); free(p); } break; case GF_IPMPX_AUTH_KeyDescr_Tag: { GF_IPMPX_AUTH_KeyDescriptor *p = (GF_IPMPX_AUTH_KeyDescriptor *)auth; if (p->keyBody) free(p->keyBody); free(p); } break; }}u32 GF_IPMPX_AUTH_Size(GF_IPMPX_Authentication *auth){ u32 size; if (!auth) return 0; switch (auth->tag) { case GF_IPMPX_AUTH_AlgorithmDescr_Tag: { GF_IPMPX_AUTH_AlgorithmDescriptor *p = (GF_IPMPX_AUTH_AlgorithmDescriptor *)auth; size = 1 + (p->specAlgoID ? GF_IPMPX_GetByteArraySize(p->specAlgoID) : 2); size += GF_IPMPX_GetByteArraySize(p->OpaqueData); return size; } break; case GF_IPMPX_AUTH_KeyDescr_Tag: { GF_IPMPX_AUTH_KeyDescriptor *p = (GF_IPMPX_AUTH_KeyDescriptor *)auth; size = p->keyBodyLength; return size; } break; default: return 0; }}u32 GF_IPMPX_AUTH_FullSize(GF_IPMPX_Authentication *auth){ u32 size = GF_IPMPX_AUTH_Size(auth); size += get_field_size(size); size += 1;/*tag*/ return size;}GF_Err WriteGF_IPMPX_AUTH(GF_BitStream *bs, GF_IPMPX_Authentication *auth){ u32 size; if (!auth) return GF_OK; gf_bs_write_int(bs, auth->tag, 8); size = GF_IPMPX_AUTH_Size(auth); write_var_size(bs, size); switch (auth->tag) { case GF_IPMPX_AUTH_AlgorithmDescr_Tag: { GF_IPMPX_AUTH_AlgorithmDescriptor *p = (GF_IPMPX_AUTH_AlgorithmDescriptor *)auth; gf_bs_write_int(bs, p->specAlgoID ? 0 : 1, 1); gf_bs_write_int(bs, 0, 7); if (p->specAlgoID) { GF_IPMPX_WriteByteArray(bs, p->specAlgoID); } else { gf_bs_write_int(bs, p->regAlgoID, 16); } GF_IPMPX_WriteByteArray(bs, p->OpaqueData); } break; case GF_IPMPX_AUTH_KeyDescr_Tag: { GF_IPMPX_AUTH_KeyDescriptor *p = (GF_IPMPX_AUTH_KeyDescriptor *)auth; /*tag*/ gf_bs_write_data(bs, p->keyBody, p->keyBodyLength); } break; default: break; } return GF_OK;}GF_Err GF_IPMPX_AUTH_Parse(GF_BitStream *bs, GF_IPMPX_Authentication **auth){ u32 val, size, tag; tag = gf_bs_read_int(bs, 8); size = 0; do { val = gf_bs_read_int(bs, 8); size <<= 7; size |= val & 0x7F; } while ( val & 0x80 ); /*don't know if tolerated in IPMPX*/ if (!size) return GF_OK; switch (tag) { case GF_IPMPX_AUTH_KeyDescr_Tag: { GF_IPMPX_AUTH_KeyDescriptor *p; GF_SAFEALLOC(p, GF_IPMPX_AUTH_KeyDescriptor); if (!p) return GF_OUT_OF_MEM; p->tag = tag; p->keyBodyLength = size; p->keyBody = (char*)malloc(sizeof(char)*size); gf_bs_read_data(bs, p->keyBody, size); *auth = (GF_IPMPX_Authentication *)p; return GF_OK; } break; case GF_IPMPX_AUTH_AlgorithmDescr_Tag: { Bool isReg; GF_IPMPX_AUTH_AlgorithmDescriptor *p; GF_SAFEALLOC(p, GF_IPMPX_AUTH_AlgorithmDescriptor); if (!p) return GF_OUT_OF_MEM; p->tag = tag; isReg = gf_bs_read_int(bs, 1); gf_bs_read_int(bs, 7); if (isReg) { p->regAlgoID = gf_bs_read_int(bs, 16); } else { p->specAlgoID = GF_IPMPX_GetByteArray(bs); } p->OpaqueData = GF_IPMPX_GetByteArray(bs); *auth = (GF_IPMPX_Authentication *)p; return GF_OK; } break; default: break; } return GF_NON_COMPLIANT_BITSTREAM;}GF_Err GF_IPMPX_ReadData(GF_BitStream *bs, GF_IPMPX_Data *_p, u32 size);u32 gf_ipmpx_data_full_size(GF_IPMPX_Data *p){ u32 size; if (!p) return 0; size = gf_ipmpx_data_size(p); size += 5; /*Version and dataID*/ size += get_field_size(size); size += 1;/*tag*/ return size;}GF_Err gf_ipmpx_data_parse(GF_BitStream *bs, GF_IPMPX_Data **out_data){ GF_Err e; u32 val, size; u8 tag; GF_IPMPX_Data *p; *out_data = NULL; tag = gf_bs_read_int(bs, 8); size = 0; do { val = gf_bs_read_int(bs, 8); size <<= 7; size |= val & 0x7F; } while ( val & 0x80 ); /*don't know if tolerated in IPMPX*/ if (!size) return GF_OK; p = gf_ipmpx_data_new(tag); if (!p) return GF_NON_COMPLIANT_BITSTREAM; p->Version = gf_bs_read_int(bs, 8); p->dataID = gf_bs_read_int(bs, 32); e = GF_IPMPX_ReadData(bs, p, size); if (e) { gf_ipmpx_data_del(p); return e; } *out_data = p; return GF_OK;}GF_Err GF_IPMPX_WriteBase(GF_BitStream *bs, GF_IPMPX_Data *p){ u32 size; if (!p) return GF_BAD_PARAM; size = gf_ipmpx_data_size(p); size += 5; /*Version & dataID*/ gf_bs_write_int(bs, p->tag, 8); write_var_size(bs, size); gf_bs_write_int(bs, p->Version, 8); gf_bs_write_int(bs, p->dataID, 32); return GF_OK;}static GF_IPMPX_Data *NewGF_IPMPX_InitAuthentication(){ GF_IPMPX_InitAuthentication *ptr; GF_IPMPX_DATA_ALLOC(ptr, GF_IPMPX_InitAuthentication, GF_IPMPX_INIT_AUTHENTICATION_TAG); return (GF_IPMPX_Data *) ptr;}static void DelGF_IPMPX_InitAuthentication(GF_IPMPX_Data *_p){ GF_IPMPX_InitAuthentication *p = (GF_IPMPX_InitAuthentication*)_p; free(p);}static GF_Err ReadGF_IPMPX_InitAuthentication(GF_BitStream *bs, GF_IPMPX_Data *_p, u32 size){ GF_IPMPX_InitAuthentication *p = (GF_IPMPX_InitAuthentication*)_p; p->Context = gf_bs_read_int(bs, 32); p->AuthType = gf_bs_read_int(bs, 8); return GF_OK;}static u32 SizeGF_IPMPX_InitAuthentication(GF_IPMPX_Data *_p){ return 5;}static GF_Err WriteGF_IPMPX_InitAuthentication(GF_BitStream *bs, GF_IPMPX_Data *_p){ GF_IPMPX_InitAuthentication *p = (GF_IPMPX_InitAuthentication*)_p; gf_bs_write_int(bs, p->Context, 32); gf_bs_write_int(bs, p->AuthType, 8); return GF_OK;}static GF_IPMPX_Data *NewGF_IPMPX_MutualAuthentication(){ GF_IPMPX_MutualAuthentication *ptr; GF_IPMPX_DATA_ALLOC(ptr, GF_IPMPX_MutualAuthentication, GF_IPMPX_MUTUAL_AUTHENTICATION_TAG); ptr->candidateAlgorithms = gf_list_new(); ptr->agreedAlgorithms = gf_list_new(); ptr->certificates = gf_list_new(); return (GF_IPMPX_Data *) ptr;}static void delete_algo_list(GF_List *algos){ u32 i; for (i=0;i<gf_list_count(algos); i++) { GF_IPMPX_Authentication *ip_auth = (GF_IPMPX_Authentication *)gf_list_get(algos, i); GF_IPMPX_AUTH_Delete(ip_auth); } gf_list_del(algos);}static void DelGF_IPMPX_MutualAuthentication(GF_IPMPX_Data *_p){ GF_IPMPX_MutualAuthentication *p = (GF_IPMPX_MutualAuthentication *)_p; delete_algo_list(p->candidateAlgorithms); delete_algo_list(p->agreedAlgorithms); GF_IPMPX_DELETE_ARRAY(p->AuthenticationData); GF_IPMPX_DELETE_ARRAY(p->opaque); GF_IPMPX_DELETE_ARRAY(p->authCodes); gf_ipmpx_data_del((GF_IPMPX_Data *)p->trustData); GF_IPMPX_AUTH_Delete((GF_IPMPX_Authentication*)p->publicKey); while (gf_list_count(p->certificates)) { GF_IPMPX_ByteArray *ba = (GF_IPMPX_ByteArray *)gf_list_get(p->certificates, 0); gf_list_rem(p->certificates, 0); GF_IPMPX_DELETE_ARRAY(ba); } gf_list_del(p->certificates); free(p);}static GF_Err ReadGF_IPMPX_MutualAuthentication(GF_BitStream *bs, GF_IPMPX_Data *_p, u32 size){ GF_Err e; u32 i, count; Bool requestNegotiation, successNegotiation, inclAuthenticationData, inclAuthCodes; GF_IPMPX_MutualAuthentication *p = (GF_IPMPX_MutualAuthentication *)_p; requestNegotiation = gf_bs_read_int(bs, 1); successNegotiation = gf_bs_read_int(bs, 1); p->failedNegotiation = gf_bs_read_int(bs, 1); inclAuthenticationData = gf_bs_read_int(bs, 1); inclAuthCodes = gf_bs_read_int(bs, 1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -