1/* ------------------------------------------------------------------ 2 * Copyright (C) 1998-2009 PacketVideo 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 13 * express or implied. 14 * See the License for the specific language governing permissions 15 * and limitations under the License. 16 * ------------------------------------------------------------------- 17 */ 18/* 19 * File: crccheck.cpp 20 * Author: Russell Hayashida (rhayashida@packetvideo.com) 21 * Date: February 13, 2002 22 * Comment: CRC procedures 23 */ 24 25#include "crccheck_cpp.h" 26#include <limits.h> 27 28#define CRC_X8 0xe0 /* X8+X2+X+1( 0x107 <-> 0xe0 ) */ 29#define CRC_X 0x8408U /* X16+X12+X5+1 ( 0x11021 <-> 0x8408 ) */ 30 31 32//Initialize both tables. 33OSCL_EXPORT_REF CRC::CRC() 34{ 35 int i; 36 uint8 j, Crc; 37 38 for (i = 0 ; i <= (int) UCHAR_MAX ; i ++) 39 { 40 Crc = (uint8) i; 41 for (j = 0 ; j < CHAR_BIT ; j ++) 42 { 43 if (Crc & 1) 44 Crc = (uint8)((Crc >> 1) ^ CRC_X8); /* Generator Polynomial */ 45 else 46 Crc >>= 1; 47 } 48 CrcTbl8[i] = Crc; 49 } 50 51 52 uint16 n, m, Crc16; 53 54 for (n = 0 ; n <= UCHAR_MAX ; n++) 55 { 56 Crc16 = n; 57 for (m = 0 ; m < CHAR_BIT ; m++) 58 { 59 if (Crc16 & 1) 60 Crc16 = (uint16)((Crc16 >> 1) ^ CRC_X); /* Genarator Polynomial */ 61 else 62 Crc16 >>= 1; 63 } 64 CrcTbl16[ n ] = Crc16; /* CRC Infomaiton Set */ 65 } 66} 67 68 69OSCL_EXPORT_REF uint8 CRC::Crc8Check(uint8 *pAlPdu, int16 Size) 70{ 71 uint8 crc; 72 73 crc = 0; 74 while (--Size >= 0) 75 { 76 crc = (uint8)((crc >> CHAR_BIT) ^ CrcTbl8[ crc ^ *pAlPdu++ ]); 77 } 78 return (uint8)(crc & 0xffU); 79} 80 81OSCL_EXPORT_REF uint8 CRC::Crc8Check(Packet *pPkt) 82{ 83 uint8 crc; 84 uint8 *pData = NULL; 85 int32 fragIdx, curSize, dataSize; 86 BufferFragment* frag = NULL; 87 88 crc = 0; 89 fragIdx = 0; 90 dataSize = pPkt->GetMediaSize(); 91 curSize = 0; 92 while (--dataSize >= 0) 93 { 94 95 if ((--curSize) <= 0) 96 { 97 frag = pPkt->GetMediaFragment(fragIdx); 98 fragIdx++; 99 curSize = frag->len; 100 pData = (uint8 *) frag->ptr; 101 } 102 crc = (uint8)((crc >> CHAR_BIT) ^ CrcTbl8[ crc ^ *pData++ ]); 103 } 104 return (uint8)(crc & 0xffU); 105} 106 107OSCL_EXPORT_REF uint8 CRC::Crc8Check(OsclSharedPtr<PVMFMediaDataImpl>& mediaData, bool hasCRC) 108{ 109 uint8 crc; 110 uint8 *pData = NULL; 111 int32 fragIdx, curSize, dataSize; 112 OsclRefCounterMemFrag frag; 113 114 crc = 0; 115 fragIdx = 0; 116 //Don't include CRC field in calculation 117 if (hasCRC) 118 { 119 dataSize = mediaData->getFilledSize() - 1; 120 } 121 else 122 { 123 dataSize = mediaData->getFilledSize(); 124 } 125 curSize = 0; 126 while (--dataSize >= 0) 127 { 128 129 if ((--curSize) <= 0) 130 { 131 mediaData->getMediaFragment(fragIdx, frag); 132 fragIdx++; 133 curSize = frag.getMemFragSize(); 134 pData = (uint8 *) frag.getMemFragPtr(); 135 } 136 crc = (uint8)((crc >> CHAR_BIT) ^ CrcTbl8[ crc ^ *pData++ ]); 137 } 138 return (uint8)(crc & 0xffU); 139} 140 141 142OSCL_EXPORT_REF uint16 CRC::Crc16Check(uint8 *pAlPdu, int16 Size) 143{ 144 uint16 crc; 145 146 crc = 0xffffU; 147 while (--Size >= 0) 148 { 149 crc = (uint16)((crc >> CHAR_BIT) ^ CrcTbl16[(uint8)crc ^ *pAlPdu++ ]); 150 } 151 return (uint16)(crc ^ 0xffffU); 152 /* 153 crc ^= 0xffffU; 154 crc = (crc&0xff)<<8 | crc>>8; 155 return crc; 156 */ 157} 158 159OSCL_EXPORT_REF uint16 CRC::Crc16Check(Packet *pPkt) 160{ 161 uint16 crc; 162 uint8 *pData = NULL; 163 int32 fragIdx, curSize, dataSize; 164 BufferFragment* frag = NULL; 165 166 crc = 0xffffU; 167 fragIdx = 0; 168 dataSize = pPkt->GetMediaSize(); 169 curSize = 0; 170 while (--dataSize >= 0) 171 { 172 173 if ((--curSize) <= 0) 174 { 175 frag = pPkt->GetMediaFragment(fragIdx); 176 fragIdx++; 177 curSize = frag->len; 178 pData = (uint8 *) frag->ptr; 179 } 180 crc = (uint16)((crc >> CHAR_BIT) ^ CrcTbl16[(uint8)crc ^ *pData++ ]); 181 } 182 return (uint16)(crc ^ 0xffffU); 183 /* 184 crc ^= 0xffffU; 185 crc = (crc&0xff)<<8 | crc>>8; 186 return crc; 187 */ 188 189} 190 191OSCL_EXPORT_REF uint16 CRC::Crc16Check(OsclSharedPtr<PVMFMediaDataImpl>& mediaData, bool hasCRC) 192{ 193 uint16 crc; 194 uint8 *pData = NULL; 195 int32 fragIdx, curSize, dataSize; 196 OsclRefCounterMemFrag frag; 197 198 crc = 0xffffU; 199 fragIdx = 0; 200 //Don't include CRC field in calculation 201 if (hasCRC) 202 { 203 dataSize = mediaData->getFilledSize() - 2; 204 } 205 else 206 { 207 dataSize = mediaData->getFilledSize(); 208 } 209 curSize = 0; 210 211 while (--dataSize >= 0) 212 { 213 214 if ((--curSize) <= 0) 215 { 216 mediaData->getMediaFragment(fragIdx, frag); 217 fragIdx++; 218 curSize = frag.getMemFragSize(); 219 pData = (uint8 *) frag.getMemFragPtr(); 220 } 221 crc = (uint16)((crc >> CHAR_BIT) ^ CrcTbl16[(uint8)crc ^ *pData++ ]); 222 } 223 return (uint16)(crc ^ 0xffffU); 224} 225