kb_dc_section.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:10k
源码类别:

DVD

开发平台:

C/C++

  1. //-----------------------------------------------------------------------------
  2. // Project include files:
  3. //-----------------------------------------------------------------------------
  4. #include "kb_dc_pub.h"
  5. DSMCCMessage dsmcMsg;
  6. DIIHead diiHead;
  7. ModuleInfo modInfo;
  8. DSIHead dsiHead;
  9. GroupInfo giInfo;
  10. DSMCCDownloadData dsmcDownloadData;
  11. DDBHead ddbHead;
  12. int getModInfoLength(unsigned char *section,int index);
  13. int GetGroupInfoLength(unsigned char *section,int index);
  14. unsigned short GetSectionLength(unsigned char *section)
  15. {
  16. unsigned short  sectionLength=(section[1] & 0x0f)<<8|section[2];
  17. return sectionLength;
  18. }
  19. unsigned char  GetSectionVersion(unsigned char *section)
  20. {
  21. unsigned char version=(section[5]& 0x3e)>>1;
  22. return version;
  23. }
  24. unsigned short GetExtTableID(unsigned char *section)
  25. {
  26. unsigned short extTableId=section[3]*256+section[4];
  27. return extTableId;
  28. }
  29. unsigned char GetTableID(unsigned char *section)
  30. {
  31. unsigned char tableId=section[0];
  32. return tableId;
  33. }
  34. unsigned char GetSectionNo(unsigned char *section)
  35. {
  36. unsigned char sectionNo=section[6];
  37. return sectionNo;
  38. }
  39. unsigned char GetLastSectionNo(unsigned char *section)
  40. {
  41. unsigned char lastSectionNo=section[7];
  42. return lastSectionNo;
  43. }
  44. int readSection(FILE *fp,unsigned char *section)
  45. {
  46. int readBytes;
  47. unsigned short sectionLength;
  48. readBytes=fread(section,1,3,fp);
  49. if(readBytes<0 || readBytes==0)
  50. return -1;
  51. sectionLength = GetSectionLength(section);  
  52. readBytes=fread(section+3,1,sectionLength,fp);
  53.     //DCDebug4("sectionLength=%dn",sectionLength);
  54. return sectionLength+3;
  55. }
  56. //=======================for DSMCC (DC)==============================
  57. int GetMsgHeaderAdapLength(unsigned char *section)
  58. {
  59. int adapLen;
  60. unsigned char *p=section;
  61. p=section+8+9;
  62. adapLen=*p;
  63. return adapLen;
  64. }
  65. int GetMsgHeaderLength(unsigned char *section)
  66. {
  67. int adapLen=GetMsgHeaderAdapLength(section);
  68. return 12+adapLen;
  69. }
  70. int GetDSMCCMessage(unsigned char *section,DSMCCMessage *msg)
  71. {
  72. unsigned char *p=section+8;
  73. msg->protocalDisminator=*p++;
  74. msg->dsmccType=*p++;
  75. msg->messageId=(p[0]<<8)|p[1];
  76. p+=2;
  77. msg->transactionId=(p[0]<<24 )|( p[1]<<16) |(p[2]<<8 )|p[3];
  78. p+=4;
  79. msg->reserved=*p++;
  80. msg->adaptationLength=*p++;
  81. msg->messageLength=(p[0]<<8)|p[1];
  82. /* for dubug
  83. DCDebug4("msg->protocalDisminator=%xn",msg->protocalDisminator);
  84. DCDebug4("msg->dsmccType=%xn",msg->dsmccType);
  85. DCDebug4("msg->messageId=%xn",msg->messageId);
  86. DCDebug4("msg->transactionId=%xn",msg->transactionId);
  87. DCDebug4("msg->reserved=%xn",msg->reserved);
  88. DCDebug4("msg->messageLength=%xn",msg->messageLength);
  89. */
  90. return 0;
  91. }
  92. int GetDIIHead(unsigned char *section,DIIHead *dii)
  93. {
  94. dii->downloadId=section[20]*(unsigned char)(16*16*16*16)+section[21]*(unsigned char)(16*16*16)+section[22]*256+section[23];
  95. dii->blockSize=section[24]*256+section[25];
  96. dii->windowSize=section[26];
  97. dii->ackperiod=section[27];
  98. dii->tCDownloadWindow=section[28]*(unsigned char)(16*16*16*16)+section[29]*(unsigned char)(16*16*16)+section[30]*256+section[31];
  99. dii->tCDownloadScenario=section[32]*(unsigned char)(16*16*16*16)+section[33]*(unsigned char)(16*16*16)+section[34]*256+section[35];
  100. dii->numberOfModules=section[38]*256+section[39];
  101.        /*
  102. DCDebug3("dii->downloadId=%xn",dii->downloadId);
  103. DCDebug3("dii->blockSize=%xn",dii->blockSize);
  104. DCDebug3("dii->windowSize=%xn",dii->windowSize);
  105. DCDebug3("dii->ackperiod=%xn",dii->ackperiod);
  106. DCDebug3("dii->tCDownloadWindow=%xn",dii->tCDownloadWindow);
  107. DCDebug3("dii->tCDownloadScenario=%xn",dii->tCDownloadScenario);
  108. DCDebug3("dii->numberOfModules=%xn",dii->numberOfModules);
  109. DCDebug3("n");
  110. */
  111. return 0;
  112. }
  113. //======================for DII==========================
  114. int GetModuleInfo(unsigned char *section,int index,ModuleInfo *mi)
  115. {
  116. unsigned char *p;
  117. int i;
  118. p=section+8+GetMsgHeaderLength(section)+20;
  119.  
  120. // DCDebug4("GetMsgHeaderLength=%dn",GetMsgHeaderLength(section));
  121. for(i=0;i<index;i++)
  122. {
  123. p=p+(8+p[7]);
  124. }
  125. mi->moduleId=(p[0]<<8)|p[1];
  126. //DCDebug4("mid[%d]n", mi->moduleId);
  127. p+=2;    //(*p++)<<8|(*++p);
  128. //DCDebug4("[%02x][%02x][%02x][%02x]n", p[0], p[1], p[2], p[3]);
  129. mi->moduleSize=((unsigned int)(p[0]<<24))|((unsigned int)(p[1]<<16))|((unsigned int)(p[2]<<8))|p[3];
  130. p+=4;    //(*p++)<<24|((*++p))<<16|(*++p)<<8|(*++p);
  131. mi->moduleVersion=*p++;
  132. mi->moduleInfoLength=*p++;
  133. mi->moduleInfoBytes=p;
  134. #if 1
  135. DCDebug3("n mi->moduleId=%xn",mi->moduleId);
  136. DCDebug3("mi->moduleSize=%xn",mi->moduleSize);
  137. DCDebug3("mi->moduleVersion=%xn",mi->moduleVersion);
  138. DCDebug3("mi->moduleInfoLength=%xn",mi->moduleInfoLength);
  139. DCDebug3("mi->moduleInfoBytes= %sn",mi->moduleInfoBytes+2);
  140. #endif
  141. return 8+mi->moduleInfoLength;
  142. }
  143. unsigned char*FindDescriptor(unsigned char*des,unsigned short dslen,unsigned char tag)
  144. {
  145. int i;
  146. unsigned char *p=des;
  147. for(i=0;i<dslen;i++)
  148. {
  149. unsigned char c=p[i];
  150. if(c==tag)
  151. {
  152. return p+i;
  153. }
  154. }
  155. return NULL;
  156. }
  157. int GetModuleName(ModuleInfo *mi,unsigned char *name)
  158. {
  159. int nameLen;
  160. unsigned char *modInfoByte=mi->moduleInfoBytes;
  161. unsigned char *nameDescriptor=FindDescriptor(modInfoByte,mi->moduleInfoLength,DSMCC_NAME_DESCRIPTOR_TAG);
  162. if(nameDescriptor==NULL) return 0;
  163. nameLen=nameDescriptor[1];
  164. KBDcMemcpy(name,nameDescriptor+2,nameLen);
  165. name[nameLen]=0;
  166. return nameLen;
  167. }
  168. //===========================for DSI==============================
  169. #if 0
  170. int GetGroupInfo(unsigned char*section,int index,GroupInfo *gi)
  171. {
  172. unsigned char *p;
  173. int i=0;
  174. p=section+8+GetMsgHeaderLength(section)+20+2+2;
  175. for(i=0;i<index;i++)
  176. {
  177. p=p+(12+p[10]<<8|p[11]);
  178. }
  179. gi->groupId=(*p++)<<24 | (*++p)<<16 | (*++p)<<8 |(*++p);
  180. gi->groupSize=(*p++)<<24 | (*++p)<<16 | (*++p)<<8 |(*++p);
  181. gi->GroupCompatibility=(char*)((*p++)<<8|(*++p));
  182. gi->GroupInfoLength=(*p++)<<8|(*++p);
  183. gi->GroupInfoBytes=p++;
  184. return 12+gi->GroupInfoLength;
  185. }
  186. #endif
  187. int GetDSIHead(unsigned char*section,DSIHead *dsi)
  188. {
  189. unsigned char cTemp[21];
  190. Descriptor dec;
  191. int i;
  192. KBDcMemset(cTemp,'',21);
  193. for(i=0;i<21;i++)
  194. {
  195. cTemp[i]=section[20+i];
  196. }
  197. dsi->serverid=&section[20];
  198. dec.tag=section[40];
  199. dec.len=section[41];
  200. if(dec.len==0)
  201. dec.descriptor=NULL;
  202. else
  203. {
  204. dec.descriptor=&section[42];
  205. }
  206. dsi->compatibilityDescriptor=dec.descriptor;
  207. dsi->privateDataLength=section[42]<<8 | section[43];
  208. dsi->privateDataBytes=&section[41+dec.len+2+1];
  209. DCDebug4("serverid:n");
  210. for(i=0;i<20;i++)
  211. {
  212. DCDebug4("%x ",*(dsi->serverid+i));
  213. }
  214. DCDebug4("n");
  215. /*printf("dsi->compatibilityDescriptor=%sn",dsi->compatibilityDescriptor);
  216. printf("dsi->privateDataLength=%xn",dsi->privateDataLength);
  217. printf("dsi->privateDataBytes:n");*/
  218. for(i=0;i<dsi->privateDataLength;i++)
  219. {
  220. DCDebug4("%x ",*(dsi->privateDataBytes+i));
  221. }
  222. DCDebug4("n");
  223. return 0;
  224. }
  225. int GetGroupCount(unsigned char*section)
  226. {
  227. int groupCount;
  228. DSIHead dsiHead;
  229. DSIHead *dsi;
  230. dsi=&dsiHead;
  231. dsi->privateDataBytes=&section[44];
  232. groupCount=*(dsi->privateDataBytes)<<8 | *(dsi->privateDataBytes+1);
  233. DCDebug4("groupCount=%xn ",groupCount);
  234. return groupCount;
  235. }
  236. int getModInfoLength(unsigned char *section,int index)
  237. {
  238. int length=0;
  239. int sum=0;
  240. int i=0;
  241. int base=39;
  242. int step=8;
  243. if(index==0)
  244. length=section[base+step];
  245. else
  246. {
  247. for(i=1;i<=index;i++)
  248. {
  249. sum=sum+8+getModInfoLength(section,i-1);
  250. }
  251. length=section[base+sum+8];
  252. }
  253. return length;
  254. }
  255. int GetGroupInfoLength(unsigned char *section,int index)
  256. {
  257. int length=0;
  258. int sum=0;
  259. int i=0;
  260. int base=45;
  261. int step=11;
  262. if(index==0)
  263. length=section[base+step]<<8 |section[base+step+1];
  264. else
  265. {
  266. for(i=1;i<=index;i++)
  267. {
  268. sum=sum+12+GetGroupInfoLength(section,i-1);
  269. }
  270. length=section[base+sum+11]<<8 | section[base+sum+12];
  271. }
  272. return length;
  273. }
  274. //=========================for DDB================================
  275. int GetDSMCCDownloadData(unsigned char *section,DSMCCDownloadData *dsmcDownData)
  276. {
  277. unsigned short data1;
  278. unsigned long data2,data3;
  279. dsmcDownData->protocolDiscriminator=section[8];
  280. dsmcDownData->dsmccType=section[9];
  281. dsmcDownData->reserved=section[16];
  282. dsmcDownData->adaptationLength=section[17];
  283. data1=section[10];
  284. dsmcDownData->messageId=(data1<<8)|section[11];
  285. data3=section[12];
  286. data2=data3<<24;
  287. data3=section[13];
  288. data2=data2|(data3<<16);
  289. data3=section[14];
  290. data2=data2|(data3<<8);
  291. dsmcDownData->downloadId=data2|section[15];
  292. data1=section[18];
  293. dsmcDownData->messageLength=(data1<<8)|section[19];
  294. return 0;
  295. }
  296. int GetDDBHead(unsigned char *section,DDBHead *ddbHead)
  297. {
  298. unsigned char *p;
  299. int msgHeaderLen;
  300. if(section==NULL)
  301. {
  302. DCDebug1("n GetDDBHead section is NULL");
  303. return -1;
  304. }
  305. p=section;
  306. msgHeaderLen=GetMsgHeaderLength(section);
  307. p+=msgHeaderLen+8;
  308. ddbHead->moduleID=(p[0]<<8)|p[1];
  309. p+=2;
  310. ddbHead->moduleVersion=(*p++);
  311. ddbHead->reserved=(*p++);
  312. ddbHead->blockNumber=(p[0]<<8)|p[1];
  313. return 0;
  314. }
  315. unsigned int GetModuleId(unsigned char *sec)
  316. {
  317.     DDBHead head;
  318.     GetDDBHead(sec,&head);   
  319.     return head.moduleID;
  320. }
  321. //GetDDBData
  322. unsigned char* GetDDBData(unsigned char *section,int* pDataLen)
  323. {
  324. int msgLen;
  325. int blockNumber;
  326. int msgHeadLen;
  327. unsigned char *p;
  328. DSMCCDownloadData msg;
  329. p=section;
  330. GetDSMCCDownloadData(section,&msg);
  331. //GetDSMCCMessage(section,&msg);
  332. msgLen=msg.messageLength;
  333. msgHeadLen=GetMsgHeaderLength(section);
  334. p=section+msgHeadLen+8+4;
  335. blockNumber=(p[0]<<8)|p[1];
  336. p+=2;
  337. *pDataLen=msg.messageLength-6;//msgLen-6;
  338.  /*for test
  339.  p=section+8+msgHeadLen+6;
  340.  if(*pDataLen!=4066)
  341.  DCDebug4("the section length is %d,the datais %srn",*pDataLen,p);
  342.  // for test end*/
  343. return p;
  344. }