psi.c 6.35 KB
Newer Older
1 2 3
/*****************************************************************************
 * psi.c: common PSI functions
 *----------------------------------------------------------------------------
4
 * Copyright (C) 2001-2010 VideoLAN
5
 * $Id$
6 7 8
 *
 * Authors: Arnaud de Bossoreille de Ribou <bozo@via.ecp.fr>
 *
9 10 11 12
 * This library 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.1 of the License, or (at your option) any later version.
13
 *
14
 * This library is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
18
 *
19 20 21
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 23 24 25 26 27 28 29 30 31 32
 *
 *----------------------------------------------------------------------------
 *
 *****************************************************************************/


#include "config.h"

#include <stdio.h>
#include <stdlib.h>

33 34 35 36 37 38
#if defined(HAVE_INTTYPES_H)
#include <inttypes.h>
#elif defined(HAVE_STDINT_H)
#include <stdint.h>
#endif

39 40
#include "dvbpsi.h"
#include "dvbpsi_private.h"
41
#include "psi.h"
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66


/*****************************************************************************
 * dvbpsi_NewPSISection
 *****************************************************************************
 * Creation of a new dvbpsi_psi_section_t structure.
 *****************************************************************************/
dvbpsi_psi_section_t * dvbpsi_NewPSISection(int i_max_size)
{
  /* Allocate the dvbpsi_psi_section_t structure */
  dvbpsi_psi_section_t * p_section
                = (dvbpsi_psi_section_t*)malloc(sizeof(dvbpsi_psi_section_t));

  if(p_section != NULL)
  {
    /* Allocate the p_data memory area */
    p_section->p_data = (uint8_t*)malloc(i_max_size * sizeof(uint8_t));

    if(p_section->p_data != NULL)
    {
      p_section->p_payload_end = p_section->p_data;
    }
    else
    {
      free(p_section);
67
      return NULL;
68 69 70 71 72 73 74 75 76 77
    }

    p_section->p_next = NULL;
  }

  return p_section;
}


/*****************************************************************************
78
 * dvbpsi_DeletePSISections
79 80 81
 *****************************************************************************
 * Destruction of a dvbpsi_psi_section_t structure.
 *****************************************************************************/
82
void dvbpsi_DeletePSISections(dvbpsi_psi_section_t * p_section)
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
{
  while(p_section != NULL)
  {
    dvbpsi_psi_section_t* p_next = p_section->p_next;

    if(p_section->p_data != NULL)
      free(p_section->p_data);

    free(p_section);
    p_section = p_next;
  }
}


/*****************************************************************************
 * dvbpsi_ValidPSISection
 *****************************************************************************
 * Check the CRC_32 if the section has b_syntax_indicator set.
 *****************************************************************************/
int dvbpsi_ValidPSISection(dvbpsi_psi_section_t* p_section)
{
  if(p_section->b_syntax_indicator)
  {
    /* Check the CRC_32 if b_syntax_indicator is 0 */
    uint32_t i_crc = 0xffffffff;
    uint8_t* p_byte = p_section->p_data;

    while(p_byte < p_section->p_payload_end + 4)
    {
112
      i_crc = (i_crc << 8) ^ dvbpsi_crc32_table[(i_crc >> 24) ^ (*p_byte)];
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
      p_byte++;
    }

    if(i_crc == 0)
    {
      return 1;
    }
    else
    {
      return 0;
    }
  }
  else
  {
    /* No check to do if b_syntax_indicator is 0 */
    return 1;
  }
}


/*****************************************************************************
 * dvbpsi_BuildPSISection
 *****************************************************************************
 * Build the section based on the information in the structure.
 *****************************************************************************/
void dvbpsi_BuildPSISection(dvbpsi_psi_section_t* p_section)
{
  uint8_t* p_byte = p_section->p_data;

  /* table_id */
  p_section->p_data[0] = p_section->i_table_id;
  /* setion_syntax_indicator | private_indicator |
     first 4 MSB of section_length */
  p_section->p_data[1] =   (p_section->b_syntax_indicator ? 0x80 : 0x00)
                         | (p_section->b_private_indicator ? 0x40 : 0x00)
                         | 0x30 /* reserved bits set to 1 */
                         | ((p_section->i_length >> 8) & 0x0f);
  /* 8 LSB of section_length */
  p_section->p_data[2] = p_section->i_length & 0xff;

  /* Optional part of a PSI section */
  if(p_section->b_syntax_indicator)
  {
    /* 8 MSB of table_id_extension */
    p_section->p_data[3] = (p_section->i_extension >> 8) & 0xff;
    /* 8 LSB of table_id_extension */
    p_section->p_data[4] = p_section->i_extension & 0xff;
    /* 5 bits of version_number | current_next_indicator */
    p_section->p_data[5] =   0xc0 /* reserved bits set to 1 */
                           | ((p_section->i_version & 0x1f) << 1)
                           | (p_section->b_current_next ? 0x01 : 0x00);
    /* section_number */
    p_section->p_data[6] = p_section->i_number;
    /* last_section_number */
    p_section->p_data[7] = p_section->i_last_number;

    /* CRC_32 */
    p_section->i_crc = 0xffffffff;

    while(p_byte < p_section->p_payload_end)
    {
      p_section->i_crc =   (p_section->i_crc << 8)
175
                         ^ dvbpsi_crc32_table[(p_section->i_crc >> 24) ^ (*p_byte)];
176 177 178 179 180 181 182
      p_byte++;
    }

    p_section->p_payload_end[0] = (p_section->i_crc >> 24) & 0xff;
    p_section->p_payload_end[1] = (p_section->i_crc >> 16) & 0xff;
    p_section->p_payload_end[2] = (p_section->i_crc >> 8) & 0xff;
    p_section->p_payload_end[3] = p_section->i_crc & 0xff;
183
#if 0
184 185
    if(!dvbpsi_ValidPSISection(p_section))
    {
186 187 188 189 190
      dvbpsi_error(h_dvbpsi,"misc PSI", "********************************************");
      dvbpsi_error(h_dvbpsi,"misc PSI", "* Generated PSI section has a bad CRC_32.  *");
      dvbpsi_error(h_dvbpsi,"misc PSI", "* THIS IS A BUG, PLEASE REPORT TO THE LIST *");
      dvbpsi_error(h_dvbpsi,"misc PSI", "*  ---  libdvbpsi-devel@videolan.org  ---  *");
      dvbpsi_error(h_dvbpsi,"misc PSI", "********************************************");
191 192 193 194 195
    }
#endif
  }
}