| [2] | 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2004-2005, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: tspsi_validate.c $ |
|---|
| 11 | * $brcm_Revision: 2 $ |
|---|
| 12 | * $brcm_Date: 3/11/05 6:18p $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: /BSEAV/lib/tspsi/tspsi_validate.c $ |
|---|
| 19 | * |
|---|
| 20 | * 2 3/11/05 6:18p erickson |
|---|
| 21 | * PR13415: add TS_Get_Message_Size |
|---|
| 22 | * |
|---|
| 23 | * 1 2/7/05 11:33p dlwin |
|---|
| 24 | * Merge down for release 2005_REFSW_MERGETOMAIN: |
|---|
| 25 | * |
|---|
| 26 | * Irvine_BSEAVSW_Devel/3 10/20/04 2:14p marcusk |
|---|
| 27 | * PR13054: Updated to take into account hardware padding bytes. |
|---|
| 28 | * |
|---|
| 29 | * Irvine_BSEAVSW_Devel/2 8/19/04 1:14p erickson |
|---|
| 30 | * PR12101: got working with magnum |
|---|
| 31 | * |
|---|
| 32 | * Irvine_BSEAVSW_Devel/1 8/19/04 10:28a erickson |
|---|
| 33 | * PR12101: added platform-independent tspsi validation code |
|---|
| 34 | * |
|---|
| 35 | ****************************************************************/ |
|---|
| 36 | #include "tspsi_validate.h" |
|---|
| 37 | |
|---|
| 38 | /* taken from BSEAV/lib/mpeg2_ts_parse/ts_priv.h */ |
|---|
| 39 | #define TS_PSI_TABLE_ID_OFFSET 0 |
|---|
| 40 | #define TS_PSI_SECTION_LENGTH_OFFSET 1 |
|---|
| 41 | #define TS_PSI_TABLE_ID_EXT_OFFSET 3 |
|---|
| 42 | #define TS_PSI_CNI_OFFSET 5 |
|---|
| 43 | #define TS_PSI_SECTION_NUMBER_OFFSET 6 |
|---|
| 44 | #define TS_PSI_LAST_SECTION_NUMBER_OFFSET 7 |
|---|
| 45 | |
|---|
| 46 | #define TS_READ_16( buf ) ((uint16_t)((buf)[0]<<8|(buf)[1])) |
|---|
| 47 | #define TS_PSI_GET_SECTION_LENGTH( buf ) (uint16_t)(TS_READ_16( &(buf)[TS_PSI_SECTION_LENGTH_OFFSET] ) & 0x0FFF) |
|---|
| 48 | #define TS_PSI_MAX_BYTE_OFFSET( buf ) (TS_PSI_GET_SECTION_LENGTH(buf) - 1) |
|---|
| 49 | |
|---|
| 50 | bool TS_Filter_Compare(const uint8_t *msg, const uint8_t *inclMask, const uint8_t *exclMask, |
|---|
| 51 | const uint8_t *coef, size_t filterSize ) |
|---|
| 52 | { |
|---|
| 53 | bool inclResult = true; |
|---|
| 54 | bool exclResult = true; |
|---|
| 55 | bool exclEnabled = false; |
|---|
| 56 | size_t i; |
|---|
| 57 | |
|---|
| 58 | for( i = 0; i < filterSize; i++ ) |
|---|
| 59 | { |
|---|
| 60 | if( (inclMask[i] | coef[i]) != (inclMask[i] | msg[i]) ) |
|---|
| 61 | { |
|---|
| 62 | inclResult = false; |
|---|
| 63 | break; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | if( exclEnabled == false && exclMask[i] != 0xFF ) |
|---|
| 67 | { |
|---|
| 68 | exclEnabled = true; |
|---|
| 69 | exclResult = false; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | if( (~exclMask[i] & coef[i]) != (~exclMask[i] & msg[i]) ) |
|---|
| 73 | { |
|---|
| 74 | exclResult = true; |
|---|
| 75 | } |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | #if 0 |
|---|
| 79 | /* for debug to make sure the logic is right */ |
|---|
| 80 | if( !(inclResult && exclResult) ) |
|---|
| 81 | { |
|---|
| 82 | printf("Incl: "); |
|---|
| 83 | for( i = 0; i < filterSize; i++ ) |
|---|
| 84 | { |
|---|
| 85 | printf("%02x ", inclMask[i]); |
|---|
| 86 | } |
|---|
| 87 | printf("\nExcl: "); |
|---|
| 88 | for( i = 0; i < filterSize; i++ ) |
|---|
| 89 | { |
|---|
| 90 | printf("%02x ", exclMask[i]); |
|---|
| 91 | } |
|---|
| 92 | printf("\nCoef: "); |
|---|
| 93 | for( i = 0; i < filterSize; i++ ) |
|---|
| 94 | { |
|---|
| 95 | printf("%02x ", coef[i]); |
|---|
| 96 | } |
|---|
| 97 | printf("\n Msg: "); |
|---|
| 98 | for( i = 0; i < filterSize; i++ ) |
|---|
| 99 | { |
|---|
| 100 | printf("%02x ", msg[i]); |
|---|
| 101 | } |
|---|
| 102 | printf("\n"); |
|---|
| 103 | } |
|---|
| 104 | #endif |
|---|
| 105 | |
|---|
| 106 | return inclResult && exclResult; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | int TS_Get_Message_Size(const uint8_t *msg) |
|---|
| 110 | { |
|---|
| 111 | size_t message_size = TS_PSI_GET_SECTION_LENGTH(msg) + 3; |
|---|
| 112 | /* |
|---|
| 113 | The transport hardware automatically pads messages to be 32 bit aligned. |
|---|
| 114 | Therefore, the message size must be increased to take this into account. |
|---|
| 115 | */ |
|---|
| 116 | if( message_size%4 ) { |
|---|
| 117 | message_size += 4-(message_size%4); |
|---|
| 118 | } |
|---|
| 119 | return message_size; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | bool TS_Validate_Size( const uint8_t *msg, size_t size) |
|---|
| 123 | { |
|---|
| 124 | size_t current_size = 0; |
|---|
| 125 | while (current_size + TS_PSI_SECTION_LENGTH_OFFSET + 1 < size) { |
|---|
| 126 | current_size += TS_Get_Message_Size(&msg[current_size]); |
|---|
| 127 | } |
|---|
| 128 | /* In the end, it must be exactly equal */ |
|---|
| 129 | return current_size == size; |
|---|
| 130 | } |
|---|
| 131 | |
|---|