MHEG5  18.9.0
MHEG5 Documentation
fpa1_rdf.c
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright © 2014 The DTVKit Open Software Foundation Ltd (www.dtvkit.org)
3  * Copyright © 2004 Ocean Blue Software Ltd
4  * Copyright © 2000 Koninklijke Philips Electronics N.V
5  *
6  * This file is part of a DTVKit Software Component
7  * You are permitted to copy, modify or distribute this file subject to the terms
8  * of the DTVKit 1.0 Licence which can be found in licence.txt or at www.dtvkit.org
9  *
10  * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
11  * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
12  * OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
13  *
14  * If you or your organisation is not a member of DTVKit then you have access
15  * to this source code outside of the terms of the licence agreement
16  * and you are expected to delete this and any associated files immediately.
17  * Further information on DTVKit, membership and terms can be found at www.dtvkit.org
18  *******************************************************************************/
26 /*---includes for this file--------------------------------------------------*/
27 #include <stdio.h>
28 #include <stdlib.h>
29 
30 #include "vpa1_rdf.h"
31 #include "vpa1_sys.h"
32 #include "vpa1_tgs.h"
33 
34 #include "fpa1_cho.h"
35 #include "fpa1_rdf.h"
36 #include "fpa1_syn.h"
37 
38 #include "pa1_mem.h"
39 
40 #include "fpa1_BlockAlloc.h"
41 #include "glue_debug.h"
42 
43 /*---local typedef structs for this file-------------------------------------*/
44 
45 /*---constant definitions for this file--------------------------------------*/
46 /* shifts */
47 #define CLASS_SHIFT 6
48 #define TAG_BASE_SHIFT 7
49 #define LONG_LENGTH_BASE_SHIFT 8
50 
51 /* masks */
52 #define SMALL_TAG_MASK 0x1F
53 #define LARGE_TAG_MASK 0x7F
54 #define LAST_TAG_OCTET_MASK 0x80
55 #define LONG_LENGTH_TEST_MASK 0x80
56 #define LONG_LENGTH_MASK 0x7F
57 
58 #define INT_OBJ_CHOICE_COMPS 2
59 
60 unsigned char *fpa1_outputBufferStart = NULL;
61 unsigned long fpa1_outputBufferOffset = 0;
62 unsigned char *fpa1_currentBufferStart = NULL;
63 unsigned long fpa1_currentBufferLength = 0;
64 unsigned long fpa1_currentBufferOffset = 0;
65 
69 
70 /*---local (static) variable declarations for this file----------------------*/
71 static int readingTagNotLength = TRUE;
72 static int intObjChoiceCompArr[ INT_OBJ_CHOICE_COMPS + 1 ] =
75  SCENE };
76 
77 
78 
79 /*---local function definitions----------------------------------------------*/
80 
81 /*---global function definitions---------------------------------------------*/
82 
83 #ifdef DEBUG_ASN
84 
85 static void fpa1_rdfTestList( fpa1_syntaxList * );
86 
91 static void fpa1_rdfTestList( fpa1_syntaxList *listPtr )
92 {
93  fpa1_syntaxList *start1Ptr;
94  fpa1_syntaxList *unknownPtr = NULL;
95 
96  while (listPtr != NULL)
97  {
98  if (listPtr->children != NULL)
99  {
100  /* move to contents */
103 
104  /* get any unknowns */
106  listPtr->nextTagPosition,
107  listPtr->children );
108  }
109 
110  if (unknownPtr != NULL)
111  {
112  printf( "ERROR: tag %d has unrecognized items\n", listPtr->tag );
113 
114  start1Ptr = unknownPtr;
115 
116  while (unknownPtr != NULL)
117  {
118  printf( " unkTag %d posn %d\n",
119  unknownPtr->tag,
120  unknownPtr->tagPosition );
121 
122  unknownPtr = unknownPtr->next;
123  }
124 
125  fpa1_delList( start1Ptr );
126  }
127 
128  fpa1_rdfTestList( listPtr->children );
129 
130  listPtr = listPtr->next;
131  }
132 } /* fpa1_rdfTestList() */
133 
134 #endif
135 
136 
137 
142 int fpa1_rdfGetTag( void )
143 {
144  int tag, vpa1_filePosTag, tagClass;
145 
146  int done = FALSE;
147 
148  /* Get next byte */
149  tag = fpa1_rdfGetNextByte();
150 
151  if (tag != -1)
152  {
153 #ifdef DEBUG_ASN
154 
155  if (readingTagNotLength == FALSE)
156  {
157  printf( "ERROR: reading tag when should be reading length.\n" );
158  }
159 
160 #endif
161 
162  /* ID octet bits 8,7 = class */
163  tagClass = tag >> CLASS_SHIFT;
164  tag &= SMALL_TAG_MASK;
165 
166  if (tag == SMALL_TAG_MASK)
167  {
168  /* tag spread over a number of octets */
169  tag = 0;
170 
171  while (!done)
172  {
173  vpa1_filePosTag = fpa1_rdfGetNextByte();
174 
175  /* multiply by tag base and add the current byte value */
176  tag = (tag << TAG_BASE_SHIFT) | (vpa1_filePosTag & LARGE_TAG_MASK);
177 
178  /* test to see if last octet */
179  if ((vpa1_filePosTag & LAST_TAG_OCTET_MASK) == 0)
180  {
181  done = TRUE;
182  }
183  else
184  {
185  done = FALSE;
186  }
187  }
188  }
189 
190  /* Context Specific tags have 2000 added */
191  tag += tagClass * 1000;
192 
193  /* next "read" will read content length */
194  readingTagNotLength = FALSE;
195  }
196 
197  return tag;
198 } /* fpa1_rdfGetTag() */
199 
205 {
206  vpa1_filePos lengthCount;
207  vpa1_filePos length;
208 
209  /* Get next byte */
210  length = fpa1_rdfGetNextByte();
211 
212  if (length != -1)
213  {
214 #ifdef DEBUG_ASN
215 
216  if (readingTagNotLength == TRUE)
217  {
218  printf( "ERROR: reading length when should be reading tag.\n" );
219  }
220 
221 #endif
222 
223  /* test for "vpa1_filePos" length */
224  if ((length & LONG_LENGTH_TEST_MASK) != 0)
225  {
226  /* vpa1_filePos form if bit 8 is '1' */
227 
228  /* first byte contains the number of octets comprising the length */
229  lengthCount = length & LONG_LENGTH_MASK;
230 
231  /* reset length */
232  length = 0;
233 
234  while (lengthCount > 0)
235  {
236  /* multiply by 128 and add next byte */
237  length = (length << LONG_LENGTH_BASE_SHIFT) + fpa1_rdfGetNextByte();
238  lengthCount--;
239  }
240  }
241 
242  /* next "read" will read a tag or content */
243  readingTagNotLength = TRUE;
244  }
245 
246  return length;
247 } /* fpa1_rdfGetContentLength() */
248 
254 {
256 } /* fpa1_rdfGetCurrentFilePosition() */
257 
263 {
264  /* if it returns -1 is the position still incremented? (No!) */
266  {
268  }
269 
270  return EOF;
271 } /* fpa1_rdfGetNextByte() */
272 
278 {
279  int content;
280 
281  int intValue;
282 
283  content = fpa1_rdfGetNextByte();
284 
285  /* !!! not sure about the following - copied directly from ASNDecoder.cpp */
286  intValue = ((content > 127) ? (~0xFF | content) : content);
287  intLength--;
288 
289  while (intLength-- > 0)
290  {
291  intValue = (intValue << 8) | fpa1_rdfGetNextByte();
292  }
293 
294  return intValue;
295 } /* fpa1_rdfGetInteger() */
296 
302 /* void fpa1_rdfGetOctetString( char* strPtr, vpa1_filePos strLength )
303  {
304  while ( strLength > 0 )
305  {
306  * strPtr = fpa1_rdfGetNextByte();
307  strPtr++;
308  strLength--;
309  }
310 
311  * strPtr = '\0';
312 
313  } */ /* fpa1_rdfGetOctetString() */
314 
320 {
321  if (readingTagNotLength == TRUE)
322  {
323  /* move to reading length */
324  fpa1_rdfGetTag();
325  }
326 
328 
329  return;
330 } /* fpa1_rdfMoveToContents() */
331 
337 {
338  /* No error checking !!!! */
339 
340  vpa1_filePos length;
341  vpa1_filePos nextTagPosn = -1;
343  int oldReadTag = readingTagNotLength;
344 
345  if (readingTagNotLength == TRUE)
346  {
347  /* move to reading length */
348  fpa1_rdfGetTag();
349  }
350 
351  /* get contents length */
352  length = fpa1_rdfGetContentLength();
353 
354  if (length != -1)
355  {
356  /* Set new position */
357  fpa1_currentBufferOffset += length;
358 
359  /* store new position */
360  nextTagPosn = fpa1_rdfGetCurrentFilePosition();
361  }
362 
363  /* reset original position and settings */
364  fpa1_rdfSetNewFilePosition( curPosn );
365  readingTagNotLength = oldReadTag;
366 
367  return nextTagPosn;
368 } /* fpa1_rdfGetNextTagPosn () */
369 
376 {
377  /* Set new position */
378  readingTagNotLength = TRUE;
379 
380  if (newPosition < 0)
381  {
382  return 1;
383  }
384  else if (fpa1_currentBufferLength < (unsigned long)newPosition)
385  {
387  return 1;
388  }
389  else
390  {
391  fpa1_currentBufferOffset = newPosition;
392  }
393 
394  return 0;
395 } /* fpa1_rdfSetNewFilePosition() */
396 
403 {
404  int retval = 0;
405  if (pos < (long)fpa1_currentBufferLength)
406  {
407  retval = fpa1_currentBufferStart[pos];
408  }
409  return retval;
410 }
411 
418 int fpa1_rdfGetIntegerAbsolute( vpa1_filePos pos, int intLength )
419 {
420  int content;
421 
422  int intValue;
423 
424  content = fpa1_rdfGetByteAbsolute(pos);
425 
426  /* sign extend the 8 bit content to 32bit */
427  intValue = ((content > 127) ? (~0xFF | content) : content);
428  intLength--;
429  pos++;
430 
431  while (intLength > 0)
432  {
433  intValue = (intValue << 8) | fpa1_rdfGetByteAbsolute(pos);
434  intLength--;
435  pos++;
436  }
437 
438  return intValue;
439 }
440 
448 {
449  return fpa1_rdfGetByteAbsolute(pos);
450 }
451 
462 fpa1_syntaxList* fpa1_parseScriptASN(unsigned char *asndata, unsigned long len)
463 {
464  fpa1_syntaxList *itemPtr;
465 
466  fpa1_currentBufferStart = asndata;
469 
473 
474  itemPtr = fpa1_choiceParse( intObjChoiceCompArr );
475 
476  return itemPtr;
477 }
478 
#define LAST_TAG_OCTET_MASK
Definition: fpa1_rdf.c:54
#define LARGE_TAG_MASK
Definition: fpa1_rdf.c:53
Contains functions/globals used to read MHEG-5 ASN.1 scripts.
#define TAG_BASE_SHIFT
Definition: fpa1_rdf.c:48
#define SCENE
Definition: vpa1_tgs.h:50
int fpa1_GenericDataCount
Definition: fpa1_rdf.c:66
Debug tracing.
fpa1_syntaxList * fpa1_parseScriptASN(unsigned char *asndata, unsigned long len)
Top level entry point to the ASN.1 script to fpa1_syntaxList converter. This function takes the origi...
Definition: fpa1_rdf.c:462
int fpa1_rdfGetInteger(vpa1_filePos intLength)
Definition: fpa1_rdf.c:277
Contains functions/structure used to do MHEG-5 ASN.1 syntax parsing.
#define INT_OBJ_CHOICE_COMPS
Definition: fpa1_rdf.c:58
Typedefs, macros used by all of parser. These may be duplicated elsewhere.
struct fpa1_syntaxItem * children
Definition: fpa1_syn.h:53
int fpa1_rdfGetNextByte(void)
Definition: fpa1_rdf.c:262
unsigned char * fpa1_currentBufferStart
Definition: fpa1_rdf.c:62
void fpa1_rdfMoveToContents(void)
Definition: fpa1_rdf.c:319
Typedefs for script reading functions.
#define APPLICATION
Definition: vpa1_tgs.h:49
int fpa1_GroupParamCount
Definition: fpa1_rdf.c:68
Contains macros for MHEG-5 ASN.1 tags and structures.
vpa1_filePos fpa1_rdfGetNextTagPosn(void)
Definition: fpa1_rdf.c:336
vpa1_filePos nextTagPosition
Definition: fpa1_syn.h:56
int fpa1_GroupActionCount
Definition: fpa1_rdf.c:67
syntax item block manager Contains functions to create a mini memory manager for allocating syntax li...
unsigned char * fpa1_outputBufferStart
Definition: fpa1_rdf.c:60
int len
Definition: mh5gate.c:57
#define LONG_LENGTH_MASK
Definition: fpa1_rdf.c:56
#define SMALL_TAG_MASK
Definition: fpa1_rdf.c:52
int fpa1_delList(fpa1_syntaxList *firstComp)
vpa1_filePos fpa1_rdfGetContentLength(void)
Definition: fpa1_rdf.c:204
vpa1_filePos tagPosition
Definition: fpa1_syn.h:55
#define LONG_LENGTH_BASE_SHIFT
Definition: fpa1_rdf.c:49
#define LONG_LENGTH_TEST_MASK
Definition: fpa1_rdf.c:55
int fpa1_rdfGetByteAbsolute(vpa1_filePos pos)
Definition: fpa1_rdf.c:402
Contains memory management functions.
#define FALSE
Definition: techtype.h:68
unsigned long fpa1_currentBufferOffset
Definition: fpa1_rdf.c:64
unsigned long fpa1_outputBufferOffset
Definition: fpa1_rdf.c:61
int fpa1_rdfGetBooleanAbsolute(vpa1_filePos pos)
Definition: fpa1_rdf.c:447
#define TRUE
Definition: techtype.h:69
int fpa1_rdfGetIntegerAbsolute(vpa1_filePos pos, int intLength)
Definition: fpa1_rdf.c:418
unsigned long fpa1_currentBufferLength
Definition: fpa1_rdf.c:63
long vpa1_filePos
Definition: vpa1_sys.h:47
struct fpa1_syntaxItem * next
Definition: fpa1_syn.h:52
vpa1_filePos fpa1_rdfGetCurrentFilePosition(void)
Definition: fpa1_rdf.c:253
int fpa1_rdfGetTag(void)
Definition: fpa1_rdf.c:142
int fpa1_rdfSetNewFilePosition(vpa1_filePos newPosition)
Definition: fpa1_rdf.c:375
fpa1_syntaxList * fpa1_choiceParse(int *)
Definition: fpa1_cho.c:67
#define CLASS_SHIFT
Definition: fpa1_rdf.c:47
fpa1_syntaxList * fpa1_synGetUnknowns(vpa1_filePos, vpa1_filePos, fpa1_syntaxList *)
Definition: fpa1_syn.c:245