1 /*
2 Copyright (C) 2010-2012  Adam Charrett
3 
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8 
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 Lesser General Public License for more details.
13 
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 
18 dr_13.c
19 
20 Decode Carousel Id Descriptor.
21 
22 */
23 #include "config.h"
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdbool.h>
28 #include <string.h>
29 
30 #if defined(HAVE_INTTYPES_H)
31 #include <inttypes.h>
32 #elif defined(HAVE_STDINT_H)
33 #include <stdint.h>
34 #endif
35 
36 #include "../dvbpsi.h"
37 #include "../dvbpsi_private.h"
38 #include "../descriptor.h"
39 
40 #include "dr_13.h"
41 
NewCarouselDr(const size_t i_private)42 static dvbpsi_carousel_id_dr_t *NewCarouselDr(const size_t i_private)
43 {
44     dvbpsi_carousel_id_dr_t *p_carousel;
45     if (i_private <= 0)
46         return NULL;
47     p_carousel = (dvbpsi_carousel_id_dr_t *)
48                     calloc(1, sizeof(dvbpsi_carousel_id_dr_t) + i_private);
49     if (p_carousel)
50     {
51         p_carousel->p_private_data = ((uint8_t *)p_carousel + sizeof(dvbpsi_carousel_id_dr_t));
52         p_carousel->i_private_data_len = i_private;
53     }
54     return p_carousel;
55 }
56 
57 /*****************************************************************************
58  * dvbpsi_DecodeCarouselIdDr
59  *****************************************************************************/
dvbpsi_DecodeCarouselIdDr(dvbpsi_descriptor_t * p_descriptor)60 dvbpsi_carousel_id_dr_t *dvbpsi_DecodeCarouselIdDr(dvbpsi_descriptor_t *p_descriptor)
61 {
62     dvbpsi_carousel_id_dr_t *p_decoded;
63 
64     /* Check the tag */
65     if (p_descriptor->i_tag != 0x13)
66         return NULL;
67 
68     /* Don't decode twice */
69     if (p_descriptor->p_decoded)
70         return p_descriptor->p_decoded;
71 
72     /* Check length */
73     if (p_descriptor->i_length < 4)
74         return NULL;
75 
76     p_decoded = NewCarouselDr(p_descriptor->i_length - 4);
77     if (!p_decoded)
78         return NULL;
79 
80     p_decoded->i_carousel_id = ((p_descriptor->p_data[0] & 0xff) << 24) | ((p_descriptor->p_data[1] & 0xff) << 16)|
81                                ((p_descriptor->p_data[2] & 0xff) <<  8) |  (p_descriptor->p_data[3] & 0xff);
82 
83     memcpy(p_decoded->p_private_data, &p_descriptor->p_data[4], p_decoded->i_private_data_len);
84     p_descriptor->p_decoded = (void*)p_decoded;
85 
86     return p_decoded;
87 }
88