vdr  2.2.0
si.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (c) 2003 by Marcel Wiesweg *
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation; either version 2 of the License, or *
7  * (at your option) any later version. *
8  * *
9  * $Id: si.h 3.4 2015/02/10 13:54:28 kls Exp $
10  * *
11  ***************************************************************************/
12 
13 #ifndef LIBSI_SI_H
14 #define LIBSI_SI_H
15 
16 #include <stdint.h>
17 
18 #include "util.h"
19 #include "headers.h"
20 
21 namespace SI {
22 
23 enum TableId { TableIdPAT = 0x00, //program association section
24  TableIdCAT = 0x01, //conditional access section
25  TableIdPMT = 0x02, //program map section
26  TableIdTSDT = 0x03,//transport stream description section
27  TableIdNIT = 0x40, //network information, actual network section
28  TableIdNIT_other = 0x41, //network information section, other network
29  TableIdSDT = 0x42, //service description section
31  TableIdBAT = 0x4A, //bouquet association section
32  TableIdEIT_presentFollowing = 0x4E, //event information section
34  //range from 0x50 to 0x5F
37  //range from 0x60 to 0x6F
40  TableIdTDT = 0x70, //time date section
41  TableIdRST = 0x71, //running status section
42  TableIdST = 0x72, //stuffing section
43  TableIdTOT = 0x73, //time offset section
44  TableIdDIT = 0x7E, //discontinuity information section
45  TableIdSIT = 0x7F, //service information section
46  TableIdAIT = 0x74, //application information section
47  TableIdPremiereCIT = 0xA0 //premiere content information section
48  };
49 
50 
52  // defined by ISO/IEC 13818-1
70  // defined by ISO-13818-6 (DSM-CC)
72  // 0x14 - 0x3F Reserved
73  // defined by ISO/IEC 13818-1 Amendment
77  // defined by ETSI (EN 300 468)
129  // defined by ETSI (EN 300 468) v 1.7.1
141  // defined by EICTA/EACEM/DIGITALEUROPE
147  // Extension descriptors
160  // defined by ETSI (EN 300 468) v 1.12.1
163  // 0x0E - 0x0F Reserved
166 
167  // Defined by ETSI TS 102 812 (MHP)
168  // They once again start with 0x00 (see page 234, MHP specification)
174  // 0x05 - 0x0A is unimplemented this library
187  // Premiere private Descriptor Tags
189 
190  //a descriptor currently unimplemented in this library
191  //the actual value 0xFF is "forbidden" according to the spec.
193 };
194 
196 
202  };
203 
215  };
216 
221  };
222 
223 /* Some principles:
224  - Objects that return references to other objects contained in their data must make sure
225  that the returned objects have been parsed.
226  (the Loop subclasses take care of that.)
227  Note that this does not apply to Loops and Strings (their are never returned by reference, BTW).
228 */
229 
230 class Object : public Parsable {
231 public:
232  Object();
233  Object(CharArray &d);
234  //can only be called once since data is immutable
235  void setData(const unsigned char*data, int size, bool doCopy=true);
236  CharArray getData() { return data; }
237  //returns the valid flag which indicates if data is all right or errors have been encountered
238  bool isValid() { return data.isValid(); }
239  virtual int getLength() = 0;
240 protected:
242  //is protected - not used for sections
243  template <class T> friend class StructureLoop;
244  void setData(CharArray &d);
245  //returns whether the given offset fits within the limits of the actual data
246  //The valid flag will be set accordingly
247  bool checkSize(int offset);
248 };
249 
250 class Section : public Object {
251 public:
252  //convenience: sets data and parses if doParse
253  Section(const unsigned char *data, bool doCopy=true);
254  Section() {}
255  TableId getTableId() const;
256  virtual int getLength();
257 
258  static int getLength(const unsigned char *d);
259  static TableId getTableId(const unsigned char *d);
260 };
261 
262 class CRCSection : public Section {
263 public:
264  //convenience: sets data and parses if doParse
265  CRCSection(const unsigned char *data, bool doCopy=true) : Section(data, doCopy) {}
267  bool isCRCValid();
268  //convenience: isValid+CheckParse
269  bool CheckCRCAndParse();
270 };
271 
272 /* A section which has the ExtendedSectionHeader
273  (section_syntax_indicator==1) */
274 class NumberedSection : public CRCSection {
275 public:
276  NumberedSection(const unsigned char *data, bool doCopy=true) : CRCSection(data, doCopy) {}
278  int getTableIdExtension() const;
279  bool getCurrentNextIndicator() const;
280  int getVersionNumber() const;
281  int getSectionNumber() const;
282  int getLastSectionNumber() const;
283  bool moreThanOneSection() const { return getLastSectionNumber()>0; }
284 
285  static int getTableIdExtension(const unsigned char *d);
286 };
287 
288 class VariableLengthPart : public Object {
289 public:
290  //never forget to call this
291  void setData(CharArray d, int l) { Object::setData(d); checkSize(l); length=l; }
292  //convenience method
293  void setDataAndOffset(CharArray d, int l, int &offset) { Object::setData(d); checkSize(l); length=l; offset+=l; }
294  virtual int getLength() { return length; }
295 private:
296  int length;
297 };
298 
299 class LoopElement : public Object {
300 };
301 
302 class Descriptor : public LoopElement {
303 public:
304  virtual int getLength();
305  DescriptorTag getDescriptorTag() const;
306 
307  static int getLength(const unsigned char *d);
308  static DescriptorTag getDescriptorTag(const unsigned char *d);
309 protected:
310  friend class DescriptorLoop;
311  //returns a subclass of descriptor according to the data given.
312  //The object is allocated with new and must be delete'd.
313  //setData() will have been called, CheckParse() not.
314  //if returnUnimplemetedDescriptor==true:
315  // Never returns null - maybe the UnimplementedDescriptor.
316  //if returnUnimplemetedDescriptor==false:
317  // Never returns the UnimplementedDescriptor - maybe null
318  static Descriptor *getDescriptor(CharArray d, DescriptorTagDomain domain, bool returnUnimplemetedDescriptor);
319 };
320 
321 class Loop : public VariableLengthPart {
322 public:
323  class Iterator {
324  public:
325  Iterator() { i=0; }
326  void reset() { i=0; }
327  private:
328  template <class T> friend class StructureLoop;
329  friend class DescriptorLoop;
330  template <class T> friend class TypeLoop;
332  int i;
333  };
334 protected:
335  virtual void Parse() {}
336 };
337 
338 //contains LoopElements of one type only
339 template <class T> class StructureLoop : public Loop {
340 public:
341  //currently you must use a while-loop testing for hasNext()
342  //i must be 0 to get the first descriptor (with the first call)
343  bool getNext(T &obj, Iterator &it)
344  {
345  if (!isValid() || it.i >= getLength())
346  return false;
347  CharArray d=data;
348  d.addOffset(it.i);
349  T ret;
350  ret.setData(d);
351  ret.CheckParse();
352  if (!checkSize(ret.getLength()))
353  return false;
354  it.i+=ret.getLength();
355  obj=ret;
356  return true;
357  }
359  {
360  if (!isValid() || it.i >= getLength())
361  return 0;
362  CharArray d=data;
363  d.addOffset(it.i);
364  T *ret=new T();
365  ret->setData(d);
366  ret->CheckParse();
367  if (!checkSize(ret->getLength())) {
368  delete ret;
369  return 0;
370  }
371  it.i+=ret->getLength();
372  return ret;
373  }
374  //bool hasNext(Iterator &it) { return getLength() > it.i; }
375 };
376 
377 //contains descriptors of different types
378 class DescriptorLoop : public Loop {
379 public:
380  DescriptorLoop() { domain=SI; }
381  //i must be 0 to get the first descriptor (with the first call)
382  //All returned descriptors must be delete'd.
383  //returns null if no more descriptors available
384  Descriptor *getNext(Iterator &it);
385  //return the next descriptor with given tag, or 0 if not available.
386  //if returnUnimplemetedDescriptor==true:
387  // an UnimplementedDescriptor may be returned if the next matching descriptor is unimplemented,
388  // 0 will be returned if and only if no matching descriptor is found.
389  //if returnUnimplemetedDescriptor==false:
390  // if 0 is returned, either no descriptor with the given tag was found,
391  // or descriptors were found, but the descriptor type is not implemented
392  //In either case, a return value of 0 indicates that no further calls to this method
393  //with the iterator shall be made.
394  Descriptor *getNext(Iterator &it, DescriptorTag tag, bool returnUnimplemetedDescriptor=false);
395  //return the next descriptor with one of the given tags, or 0 if not available.
396  //if returnUnimplemetedDescriptor==true:
397  // returns 0 if and only if no descriptor with one of the given tags was found.
398  // The UnimplementedDescriptor may be returned.
399  //if returnUnimplemetedDescriptor==false:
400  // if 0 is returned, either no descriptor with one of the given tags was found,
401  // or descriptors were found, but none of them are implemented.
402  // The UnimplementedDescriptor will never be returned.
403  //In either case, a return value of 0 indicates that no further calls to this method
404  //with the iterator shall be made.
405  Descriptor *getNext(Iterator &it, DescriptorTag *tags, int arrayLength, bool returnUnimplemetedDescriptor=false);
406  //returns the number of descriptors in this loop
407  int getNumberOfDescriptors();
408  //writes the tags of the descriptors in this loop in the array,
409  // which must at least have the size getNumberOfDescriptors().
410  //The number of descriptors, i.e. getNumberOfDescriptors(), is returned.
411  // You can specify the array type (Descriptor tags are 8 Bit,
412  // you might e.g. choose a char, short, int or DescriptorTag array)
413  template <typename T> int getDescriptorTags(T *tags)
414  {
415  const unsigned char *p=data.getData();
416  const unsigned char *end=p+getLength();
417  int count=0;
418  while (p < end) {
419  tags[count++]=(T)Descriptor::getDescriptorTag(p);
420  p+=Descriptor::getLength(p);
421  }
422  return count;
423  }
424 protected:
425  Descriptor *createDescriptor(int &i, bool returnUnimplemetedDescriptor);
427 };
428 
429 typedef uint8_t EightBit;
430 typedef uint16_t SixteenBit;
431 typedef uint32_t ThirtyTwoBit;
432 typedef uint64_t SixtyFourBit;
433 
434 template <typename T> class TypeLoop : public Loop {
435 public:
436  int getCount() { return getLength()/sizeof(T); }
437  T operator[](const int index) const
438  {
439  switch (sizeof(T)) {
440  case 1:
441  return data[index];
442  case 2:
443  return data.TwoBytes(index);
444  case 4:
445  return data.FourBytes(index);
446  case 8:
447  return (SixtyFourBit(data.FourBytes(index)) << 32) | data.FourBytes(index+4);
448  default:
449  return 0; // just to avoid a compiler warning
450  }
451  return 0; // just to avoid a compiler warning
452  }
453  T getNext(Iterator &it) const
454  {
455  T ret=operator[](it.i);
456  it.i+=sizeof(T);
457  return ret;
458  }
459  bool hasNext(Iterator &it) { return isValid() && (getLength() > it.i); }
460 };
461 
463 public:
464  MHP_DescriptorLoop() { domain=MHP; }
465 };
466 
467 //Premiere Content Information Table
469 public:
470  PCIT_DescriptorLoop() { domain=PCIT; }
471 };
472 
473 //The content of the ExtendedEventDescriptor may be split over several
474 //descriptors if the text is longer than 256 bytes.
475 //The following classes provide base functionality to handle this case.
476 class GroupDescriptor : public Descriptor {
477 public:
478  virtual int getDescriptorNumber() = 0;
479  virtual int getLastDescriptorNumber() = 0;
480 };
481 
483 public:
484  DescriptorGroup(bool deleteOnDesctruction=true);
485  ~DescriptorGroup();
486  bool Add(GroupDescriptor *d);
487  void Delete();
488  int getLength() { return length; }
489  GroupDescriptor **getDescriptors() { return array; }
490  bool isComplete(); //if all descriptors have been added
491 protected:
492  int length;
495 };
496 
497 class String : public VariableLengthPart {
498 public:
499  //A note to the length: getLength() returns the length of the raw data.
500  //The text may be shorter. Its length can be obtained with one of the
501  //getText functions and strlen.
502 
503  //returns text. Data is allocated with new and must be delete'd by the user.
504  char *getText();
505  //copies text into given buffer.
506  //a buffer of size getLength()+1 is guaranteed to be sufficiently large.
507  //In most descriptors the string length is an 8-bit field,
508  //so the maximum there is 256.
509  //returns the given buffer for convenience.
510  //The emphasis marks 0x86 and 0x87 are still available.
511  char *getText(char *buffer, int size);
512  //The same semantics as for getText(char*) apply.
513  //The short version of the text according to ETSI TR 101 211 (chapter 4.6)
514  //will be written into the shortVersion buffer (which should, therefore, have the same
515  //length as buffer). If no shortVersion is available, shortVersion will contain
516  //an empty string.
517  //The emphasis marks 0x86 and 0x87 are still available in buffer, but not in shortVersion.
518  char *getText(char *buffer, char *shortVersion, int sizeBuffer, int sizeShortVersion);
519 protected:
520  virtual void Parse() {}
521  void decodeText(char *buffer, int size);
522  void decodeText(char *buffer, char *shortVersion, int sizeBuffer, int sizeShortVersion);
523 };
524 
525 // Set the character table to use for strings that do not begin with a character
526 // table indicator. Call with NULL to turn this off.
527 void SetOverrideCharacterTable(const char *CharacterTable);
528 // Call this function to set the system character table. CharacterTable is a string
529 // like "iso8859-15" or "utf-8" (case insensitive).
530 // Returns true if the character table was recognized.
531 bool SetSystemCharacterTable(const char *CharacterTable);
532 // Determines the character table used in the given buffer and returns
533 // a string indicating that table. If no table can be determined, the
534 // default ISO6937 is returned. If a table can be determined, the buffer
535 // and length are adjusted accordingly.
536 const char *getCharacterTable(const unsigned char *&buffer, int &length, bool *isSingleByte = NULL);
537 bool convertCharacterTable(const char *from, size_t fromLength, char *to, size_t toLength, const char *fromCode);
539 
540 } //end of namespace
541 
542 #endif //LIBSI_SI_H
bool convertCharacterTable(const char *from, size_t fromLength, char *to, size_t toLength, const char *fromCode)
Definition: si.c:389
void setDataAndOffset(CharArray d, int l, int &offset)
Definition: si.h:293
Section()
Definition: si.h:254
GroupDescriptor ** array
Definition: si.h:493
T * getNextAsPointer(Iterator &it)
Definition: si.h:358
bool systemCharacterTableIsSingleByte(void)
Definition: si.c:317
Definition: si.h:195
CharArray getData()
Definition: si.h:236
Definition: si.h:497
RunningStatus
Definition: si.h:197
const char * getCharacterTable(const unsigned char *&buffer, int &length, bool *isSingleByte)
Definition: si.c:354
int getDescriptorTags(T *tags)
Definition: si.h:413
virtual int getLength()
Definition: si.h:294
CRCSection()
Definition: si.h:266
DescriptorTag
Definition: si.h:51
uint64_t SixtyFourBit
Definition: si.h:432
virtual void Parse()
Definition: si.h:335
DescriptorTag getDescriptorTag() const
Definition: si.c:100
LinkageType
Definition: si.h:204
virtual void Parse()
Definition: si.h:520
uint32_t ThirtyTwoBit
Definition: si.h:431
Definition: descriptor.c:16
NumberedSection(const unsigned char *data, bool doCopy=true)
Definition: si.h:276
T operator[](const int index) const
Definition: si.h:437
virtual int getLength()
Definition: si.c:96
bool checkSize(int offset)
Definition: si.c:37
Definition: si.h:195
uint16_t SixteenBit
Definition: si.h:430
virtual int getLength()=0
DescriptorLoop()
Definition: si.h:380
bool moreThanOneSection() const
Definition: si.h:283
uint8_t EightBit
Definition: si.h:429
Object()
Definition: si.c:23
void setData(const unsigned char *data, int size, bool doCopy=true)
Definition: si.c:29
DescriptorTagDomain domain
Definition: si.h:426
bool deleteOnDesctruction
Definition: si.h:494
void setData(CharArray d, int l)
Definition: si.h:291
Definition: si.h:230
bool SetSystemCharacterTable(const char *CharacterTable)
Definition: si.c:330
DescriptorTagDomain
Definition: si.h:195
bool hasNext(Iterator &it)
Definition: si.h:459
GroupDescriptor ** getDescriptors()
Definition: si.h:489
T getNext(Iterator &it) const
Definition: si.h:453
bool getNext(T &obj, Iterator &it)
Definition: si.h:343
int getLength()
Definition: si.h:488
bool isValid()
Definition: si.h:238
CharArray data
Definition: si.h:241
u_int16_t TwoBytes(const int index) const
Definition: util.h:59
Definition: si.h:321
CRCSection(const unsigned char *data, bool doCopy=true)
Definition: si.h:265
void addOffset(int offset)
Definition: util.h:65
Definition: si.h:195
AudioType
Definition: si.h:217
u_int32_t FourBytes(const int index) const
Definition: util.h:60
TableId
Definition: si.h:23
const unsigned char * getData() const
Definition: util.h:51
int getCount()
Definition: si.h:436
void SetOverrideCharacterTable(const char *CharacterTable)
Definition: si.c:324
void reset()
Definition: si.h:326