| 1 | /******************************************************************** |
|---|
| 2 | |
|---|
| 3 | DMW_EpgRating.c |
|---|
| 4 | |
|---|
| 5 | EPG Middleware RRT management |
|---|
| 6 | |
|---|
| 7 | Copyright 2006 Digital STREAM Technology, Inc. |
|---|
| 8 | All Rights Reserved |
|---|
| 9 | |
|---|
| 10 | $Id: DMW_EpgRating.c cafrii |
|---|
| 11 | |
|---|
| 12 | ********************************************************************/ |
|---|
| 13 | |
|---|
| 14 | /*_____ I N C L U D E __________________________________________*/ |
|---|
| 15 | #include "DHL_UTL.h" |
|---|
| 16 | |
|---|
| 17 | #include "DMW_Platform.h" |
|---|
| 18 | |
|---|
| 19 | //#include "DHL_PsiAPI.h" |
|---|
| 20 | #include "DLIB_PSIP_Monitor.h" |
|---|
| 21 | #include "DLIB_PSIP_Parser.h" |
|---|
| 22 | #include "DLIB_PSI_Monitor.h" |
|---|
| 23 | #include "DLIB_PSI_Parser.h" |
|---|
| 24 | //#include "DHL_Memchain.h" |
|---|
| 25 | |
|---|
| 26 | //#include <string.h> |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | DHL_MODULE("$epr", 0); |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | #define RRT_FLATTEN_MAP_VERSION 0x81 |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | /* |
|---|
| 36 | RRT section flattened map version |
|---|
| 37 | |
|---|
| 38 | 0x81 |
|---|
| 39 | 00 ID |
|---|
| 40 | 01 VER |
|---|
| 41 | 02 LEN_HI |
|---|
| 42 | 03 LEN_LO, from ID to end of CRC32 |
|---|
| 43 | 04 region |
|---|
| 44 | 05 rrt version |
|---|
| 45 | .. payload |
|---|
| 46 | |
|---|
| 47 | .. CRC32 (32 bits) |
|---|
| 48 | from ID to just before CRC32 |
|---|
| 49 | */ |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | STATUS Dmc_FlattenRrtSection(const rrtSectionPtr_t rrt, UINT32 *pFlattenSize, UINT8 *pSectionBuf) |
|---|
| 53 | { |
|---|
| 54 | /* reverse of ParseRrtSection |
|---|
| 55 | |
|---|
| 56 | input: |
|---|
| 57 | caller should prepare necessary memory buffer (pSectionBuf) for flattened rrt . |
|---|
| 58 | pFlattenSize is the maximum buffer size of pSectionBuf. |
|---|
| 59 | output: |
|---|
| 60 | pFlattenSize is actual byte size flattened. |
|---|
| 61 | */ |
|---|
| 62 | |
|---|
| 63 | int i, t; |
|---|
| 64 | int total_len; |
|---|
| 65 | UINT8 *p; /* running pointer */ |
|---|
| 66 | UINT8 *buf_start; |
|---|
| 67 | int max_size; |
|---|
| 68 | UINT32 crc; |
|---|
| 69 | |
|---|
| 70 | dprint(1, "Dmc_FlattenRrtSection\n"); |
|---|
| 71 | |
|---|
| 72 | if (!rrt) { |
|---|
| 73 | dprint(0, "!! NULL rrt section\n"); |
|---|
| 74 | return statusInvalidArgument; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | if (pSectionBuf == NULL || pFlattenSize == NULL) { |
|---|
| 78 | dprint(0, "!! invalid buf or bufsize ptr\n"); |
|---|
| 79 | return statusInvalidArgument; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | p = buf_start = pSectionBuf; |
|---|
| 83 | max_size = (int) *pFlattenSize; |
|---|
| 84 | |
|---|
| 85 | #define RSET1(val) \ |
|---|
| 86 | if ((UINT8 *)p+1 - (UINT8 *)buf_start > max_size) goto label_overflow; \ |
|---|
| 87 | *p++ = (val)&0xff; |
|---|
| 88 | |
|---|
| 89 | #define RSETN(buf,n) \ |
|---|
| 90 | if ((UINT8 *)p+(n) - (UINT8 *)buf_start > max_size) goto label_overflow; \ |
|---|
| 91 | memcpy(p, (buf), (n)); \ |
|---|
| 92 | p += (n); |
|---|
| 93 | |
|---|
| 94 | /* fill in PMT header info (except length) */ |
|---|
| 95 | RSET1(tid_rating_region_table); /* 0xCA */ |
|---|
| 96 | RSET1(RRT_FLATTEN_MAP_VERSION); |
|---|
| 97 | |
|---|
| 98 | /* flatten map length filled later, after it's known */ |
|---|
| 99 | /* buf[1], buf[2].. */ |
|---|
| 100 | RSET1(0); |
|---|
| 101 | RSET1(0); |
|---|
| 102 | |
|---|
| 103 | RSET1(rrt->rating_region); |
|---|
| 104 | RSET1(rrt->version_number & 0x1F); |
|---|
| 105 | |
|---|
| 106 | /* now, payload starts.. */ |
|---|
| 107 | RSET1(rrt->rating_region_name_length); |
|---|
| 108 | RSETN(rrt->rating_region_name, rrt->rating_region_name_length); |
|---|
| 109 | |
|---|
| 110 | RSET1(rrt->dimensions_defined); |
|---|
| 111 | |
|---|
| 112 | for (i=0; i<rrt->dimensions_defined; i++) |
|---|
| 113 | { |
|---|
| 114 | rrtDimensionPtr_t dim = &rrt->dimension[i]; |
|---|
| 115 | |
|---|
| 116 | RSET1(dim->dimension_name_length); |
|---|
| 117 | RSETN(dim->dimension_name, dim->dimension_name_length); |
|---|
| 118 | |
|---|
| 119 | RSET1(dim->graduated_scale); |
|---|
| 120 | RSET1(dim->first_value_empty); |
|---|
| 121 | RSET1(dim->values_defined); |
|---|
| 122 | |
|---|
| 123 | for (t=0; t<dim->values_defined; t++) |
|---|
| 124 | { |
|---|
| 125 | rrtValuePtr_t value = &dim->value[t]; |
|---|
| 126 | |
|---|
| 127 | RSET1(value->block_on); |
|---|
| 128 | |
|---|
| 129 | RSET1(value->abbrev_rating_value_length); |
|---|
| 130 | RSETN(value->abbrev_rating_value, value->abbrev_rating_value_length); |
|---|
| 131 | |
|---|
| 132 | RSET1(value->rating_value_length); |
|---|
| 133 | RSETN(value->rating_value, value->rating_value_length); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | RSET1(rrt->descriptor_length); |
|---|
| 139 | RSETN(rrt->descriptors, rrt->descriptor_length); |
|---|
| 140 | |
|---|
| 141 | total_len = (int)(p - buf_start) + 4; /* CRC¸¦ Æ÷ÇÔÇÑ Àüü Å©±â. */ |
|---|
| 142 | |
|---|
| 143 | /* update total length */ |
|---|
| 144 | buf_start[2] = (total_len >> 8) & 0xff; |
|---|
| 145 | buf_start[3] = (total_len & 0xff); |
|---|
| 146 | |
|---|
| 147 | crc = DHL_UTL_CalcCRC32(0xffffffff, buf_start, total_len-4); |
|---|
| 148 | /* crc °è»ê¿¡´Â CRCÀÚü´Â »©°í °è»ê. |
|---|
| 149 | ¹öÆÛ óÀ½ºÎÅÍ CRC ¹Ù·Î ¾Õ±îÁö. */ |
|---|
| 150 | |
|---|
| 151 | RSET1((crc >> 24) & 0xFF); |
|---|
| 152 | RSET1((crc >> 16) & 0xFF); |
|---|
| 153 | RSET1((crc >> 8) & 0xFF); |
|---|
| 154 | RSET1((crc & 0xFF)); |
|---|
| 155 | |
|---|
| 156 | #undef RSET1 |
|---|
| 157 | #undef RSETN |
|---|
| 158 | |
|---|
| 159 | if (pFlattenSize) |
|---|
| 160 | *pFlattenSize = total_len; |
|---|
| 161 | |
|---|
| 162 | return statusOK; |
|---|
| 163 | |
|---|
| 164 | label_overflow: |
|---|
| 165 | dprint(0, "!! flatten buf overflow, maxsize %d\n", max_size); |
|---|
| 166 | |
|---|
| 167 | return statusOverflow; |
|---|
| 168 | |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | /* ParseRrtSection(UINT8 * section, rrtSectionPtr_t * rrtSectionPtr) */ |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | STATUS Dmc_RestoreRrtSection(rrtSectionPtr_t *prrt, UINT8 *pSectionBuf) |
|---|
| 176 | { |
|---|
| 177 | #define RRT_MEM_LIMIT 0x1000 /* 4K */ |
|---|
| 178 | |
|---|
| 179 | #define RatingCheckMemoryError(p) \ |
|---|
| 180 | if (p == NULL) {status = statusOutOfMemory; goto label_exit;} |
|---|
| 181 | |
|---|
| 182 | STATUS status = statusOK; |
|---|
| 183 | DHL_RESULT err; |
|---|
| 184 | rrtSectionPtr_t rrt = NULL; |
|---|
| 185 | memChainSetup_t memSetup = {RRT_MEM_LIMIT,NULL,NULL}; |
|---|
| 186 | memId_t memId = NULL; |
|---|
| 187 | int i, t, total_len; |
|---|
| 188 | UINT8 *p; |
|---|
| 189 | UINT32 crc1, crc2; |
|---|
| 190 | |
|---|
| 191 | if (pSectionBuf == NULL || prrt == NULL) { |
|---|
| 192 | dprint(0, "!! NULL flat rrt ptr or NULL section buf\n"); |
|---|
| 193 | status = statusInvalidArgument; |
|---|
| 194 | goto label_exit; |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | p = pSectionBuf; |
|---|
| 198 | if (p[0] != tid_rating_region_table) { |
|---|
| 199 | dprint(0, "!! flat rrt map id 0x%x err\n", p[0]); |
|---|
| 200 | status = statusError; |
|---|
| 201 | goto label_exit; |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | if (p[1] != RRT_FLATTEN_MAP_VERSION) { |
|---|
| 205 | dprint(0, "!! RRT flatten map version 0x%x err\n", p[1]); |
|---|
| 206 | status = statusError; |
|---|
| 207 | goto label_exit; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | total_len = ((p[2] << 8UL) | p[3]); /* total length including CRC */ |
|---|
| 211 | |
|---|
| 212 | crc1 = (p[total_len-4]<<24UL) | (p[total_len-3]<<16UL) | (p[total_len-2]<<8UL) | p[total_len-1]; |
|---|
| 213 | crc2 = DHL_UTL_CalcCRC32(0xffffffff, p, total_len-4); |
|---|
| 214 | |
|---|
| 215 | if (crc1 != crc2) { |
|---|
| 216 | dprint(0, "!! crc mismatch (flat: %08x, calc: %08x)\n", crc1, crc2); |
|---|
| 217 | status = statusError; |
|---|
| 218 | goto label_exit; |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | p = pSectionBuf + 4; /* start of payload.. */ |
|---|
| 222 | |
|---|
| 223 | err = memChainCreate(&memId,&memSetup); |
|---|
| 224 | if (err) { |
|---|
| 225 | status = statusOutOfMemory; |
|---|
| 226 | goto label_exit; |
|---|
| 227 | } |
|---|
| 228 | rrt = (rrtSectionPtr_t)((memId_t *)(memChainAlloc(memId,sizeof(rrtSection_t)+sizeof(memId_t))) + 1); |
|---|
| 229 | RatingCheckMemoryError(rrt); |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | #define RGET1(val) if ((UINT8 *)p+1 - (UINT8 *)pSectionBuf > total_len) goto label_overflow; \ |
|---|
| 233 | (val) = *p++; |
|---|
| 234 | |
|---|
| 235 | #define RGET1X(val, type) if ((UINT8 *)p+1 - (UINT8 *)pSectionBuf > total_len) goto label_overflow; \ |
|---|
| 236 | (val) = (type)*p++; |
|---|
| 237 | |
|---|
| 238 | #define RGETN(buf,n) if ((UINT8 *)p+(n) - (UINT8 *)pSectionBuf > total_len) goto label_overflow; \ |
|---|
| 239 | memcpy((buf), p, (n)); p += (n); |
|---|
| 240 | |
|---|
| 241 | RGET1X(rrt->rating_region, rating_region_k); |
|---|
| 242 | RGET1(rrt->version_number); |
|---|
| 243 | |
|---|
| 244 | RGET1(rrt->rating_region_name_length); |
|---|
| 245 | if (rrt->rating_region_name_length) { |
|---|
| 246 | rrt->rating_region_name = (UINT8 *)memChainAlloc(memId, |
|---|
| 247 | rrt->rating_region_name_length*sizeof(UINT8)); |
|---|
| 248 | RatingCheckMemoryError(rrt->rating_region_name); |
|---|
| 249 | |
|---|
| 250 | RGETN(rrt->rating_region_name, rrt->rating_region_name_length); |
|---|
| 251 | } |
|---|
| 252 | else |
|---|
| 253 | rrt->rating_region_name = NULL; |
|---|
| 254 | |
|---|
| 255 | RGET1(rrt->dimensions_defined); |
|---|
| 256 | |
|---|
| 257 | if (rrt->dimensions_defined) { |
|---|
| 258 | rrt->dimension = (rrtDimensionPtr_t)memChainAlloc(memId, |
|---|
| 259 | rrt->dimensions_defined*sizeof(rrtDimension_t)); |
|---|
| 260 | RatingCheckMemoryError(rrt->dimension); |
|---|
| 261 | } |
|---|
| 262 | else |
|---|
| 263 | rrt->dimension = NULL; |
|---|
| 264 | |
|---|
| 265 | for (i=0; i<rrt->dimensions_defined; i++) |
|---|
| 266 | { |
|---|
| 267 | rrtDimensionPtr_t dim = &rrt->dimension[i]; |
|---|
| 268 | |
|---|
| 269 | RGET1(dim->dimension_name_length); |
|---|
| 270 | if (dim->dimension_name_length) { |
|---|
| 271 | dim->dimension_name = (UINT8 *)memChainAlloc(memId, |
|---|
| 272 | dim->dimension_name_length*sizeof(UINT8)); |
|---|
| 273 | RatingCheckMemoryError(dim->dimension_name); |
|---|
| 274 | |
|---|
| 275 | RGETN(dim->dimension_name, dim->dimension_name_length); |
|---|
| 276 | } |
|---|
| 277 | else |
|---|
| 278 | dim->dimension_name = NULL; |
|---|
| 279 | |
|---|
| 280 | RGET1(dim->graduated_scale); |
|---|
| 281 | RGET1(dim->first_value_empty); |
|---|
| 282 | RGET1(dim->values_defined); |
|---|
| 283 | |
|---|
| 284 | if (dim->values_defined) { |
|---|
| 285 | dim->value = (rrtValuePtr_t)memChainAlloc(memId, |
|---|
| 286 | dim->values_defined*sizeof(rrtValue_t)); |
|---|
| 287 | RatingCheckMemoryError(dim->value); |
|---|
| 288 | } |
|---|
| 289 | else |
|---|
| 290 | dim->value = NULL; |
|---|
| 291 | |
|---|
| 292 | for (t=0; t<dim->values_defined; t++) |
|---|
| 293 | { |
|---|
| 294 | rrtValuePtr_t value = &dim->value[t]; |
|---|
| 295 | |
|---|
| 296 | RGET1(value->block_on); |
|---|
| 297 | |
|---|
| 298 | RGET1(value->abbrev_rating_value_length); |
|---|
| 299 | if (value->abbrev_rating_value_length) { |
|---|
| 300 | value->abbrev_rating_value = (UINT8 *)memChainAlloc(memId, |
|---|
| 301 | value->abbrev_rating_value_length*sizeof(UINT8)); |
|---|
| 302 | RatingCheckMemoryError(value->abbrev_rating_value); |
|---|
| 303 | RGETN(value->abbrev_rating_value, value->abbrev_rating_value_length); |
|---|
| 304 | } |
|---|
| 305 | else |
|---|
| 306 | value->abbrev_rating_value = NULL; |
|---|
| 307 | |
|---|
| 308 | RGET1(value->rating_value_length); |
|---|
| 309 | if (value->rating_value_length) { |
|---|
| 310 | value->rating_value = (UINT8 *)memChainAlloc(memId, |
|---|
| 311 | value->rating_value_length*sizeof(UINT8)); |
|---|
| 312 | RatingCheckMemoryError(value->rating_value); |
|---|
| 313 | RGETN(value->rating_value, value->rating_value_length); |
|---|
| 314 | } |
|---|
| 315 | else |
|---|
| 316 | value->rating_value = NULL; |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | RGET1(rrt->descriptor_length); |
|---|
| 322 | rrt->descriptors = (UINT8 *)memChainAlloc(memId, |
|---|
| 323 | rrt->descriptor_length*sizeof(UINT8)); |
|---|
| 324 | RatingCheckMemoryError(rrt->descriptors); |
|---|
| 325 | RGETN(rrt->descriptors, rrt->descriptor_length); |
|---|
| 326 | |
|---|
| 327 | /* parsing complete */ |
|---|
| 328 | *(((memId_t *)rrt)-1) = memId; |
|---|
| 329 | memId = NULL; /* so memChain not deleted */ |
|---|
| 330 | |
|---|
| 331 | goto label_exit; |
|---|
| 332 | |
|---|
| 333 | label_overflow: |
|---|
| 334 | dprint(0, "!! %s: overflow\n", __FUNCTION__); |
|---|
| 335 | status = statusOverflow; |
|---|
| 336 | |
|---|
| 337 | label_exit: |
|---|
| 338 | |
|---|
| 339 | if (memId) { |
|---|
| 340 | /* delete the rrtSection memory */ |
|---|
| 341 | memChainDestroy(memId); |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | if (status == statusOK) |
|---|
| 345 | *prrt = rrt; |
|---|
| 346 | |
|---|
| 347 | return status; |
|---|
| 348 | } |
|---|
| 349 | |
|---|
| 350 | |
|---|
| 351 | |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | /* end of file */ |
|---|