1 /*****************************************************************************
2  * dr_0f.c
3  * Copyright (C) 2001-2011 VideoLAN
4  * $Id: dr_0f.c,v 1.3 2003/07/25 20:20:40 fenrir Exp $
5  *
6  * Authors: Arnaud de Bossoreille de Ribou <bozo@via.ecp.fr>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  *****************************************************************************/
23 
24 #include "config.h"
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdbool.h>
29 #include <string.h>
30 
31 #if defined(HAVE_INTTYPES_H)
32 #include <inttypes.h>
33 #elif defined(HAVE_STDINT_H)
34 #include <stdint.h>
35 #endif
36 
37 #include "../dvbpsi.h"
38 #include "../dvbpsi_private.h"
39 #include "../descriptor.h"
40 
41 #include "dr_0f.h"
42 
43 
44 /*****************************************************************************
45  * dvbpsi_DecodePrivateDataDr
46  *****************************************************************************/
dvbpsi_DecodePrivateDataDr(dvbpsi_descriptor_t * p_descriptor)47 dvbpsi_private_data_dr_t * dvbpsi_DecodePrivateDataDr(
48                                         dvbpsi_descriptor_t * p_descriptor)
49 {
50     dvbpsi_private_data_dr_t * p_decoded;
51 
52     /* Check the tag */
53     if (!dvbpsi_CanDecodeAsDescriptor(p_descriptor, 0x0f))
54         return NULL;
55 
56     /* Don't decode twice */
57     if (dvbpsi_IsDescriptorDecoded(p_descriptor))
58         return p_descriptor->p_decoded;
59 
60     if (p_descriptor->i_length != 4)
61         return NULL;
62 
63     /* Allocate memory */
64     p_decoded = (dvbpsi_private_data_dr_t*)malloc(sizeof(dvbpsi_private_data_dr_t));
65     if (!p_decoded)
66         return NULL;
67 
68     p_decoded->i_private_data =   ((uint32_t)(p_descriptor->p_data[0]) << 24)
69             | ((uint32_t)(p_descriptor->p_data[1]) << 16)
70             | ((uint32_t)(p_descriptor->p_data[2]) << 8)
71             | p_descriptor->p_data[3];
72 
73     p_descriptor->p_decoded = (void*)p_decoded;
74 
75     return p_decoded;
76 }
77 
78 
79 /*****************************************************************************
80  * dvbpsi_GenPrivateDataDr
81  *****************************************************************************/
dvbpsi_GenPrivateDataDr(dvbpsi_private_data_dr_t * p_decoded,bool b_duplicate)82 dvbpsi_descriptor_t * dvbpsi_GenPrivateDataDr(
83                                         dvbpsi_private_data_dr_t * p_decoded,
84                                         bool b_duplicate)
85 {
86     /* Create the descriptor */
87     dvbpsi_descriptor_t * p_descriptor = dvbpsi_NewDescriptor(0x0f, 4, NULL);
88     if (!p_descriptor)
89         return NULL;
90 
91     /* Encode data */
92     p_descriptor->p_data[0] = p_decoded->i_private_data >> 24;
93     p_descriptor->p_data[1] = p_decoded->i_private_data >> 16;
94     p_descriptor->p_data[2] = p_decoded->i_private_data >> 8;
95     p_descriptor->p_data[3] = p_decoded->i_private_data;
96 
97     if (b_duplicate)
98     {
99         /* Duplicate decoded data */
100         p_descriptor->p_decoded =
101                 dvbpsi_DuplicateDecodedDescriptor(p_decoded,
102                                                   sizeof(dvbpsi_private_data_dr_t));
103     }
104 
105     return p_descriptor;
106 }
107