Guest User

MPEG2TransportStreamMultiplexor4iOS.cpp

a guest
Dec 23rd, 2011
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 20.05 KB | None | 0 0
  1. /**********
  2. This library is free software; you can redistribute it and/or modify it under
  3. the terms of the GNU Lesser General Public License as published by the
  4. Free Software Foundation; either version 2.1 of the License, or (at your
  5. option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
  6.  
  7. This library is distributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
  10. more details.
  11.  
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this library; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
  15. **********/
  16. // "liveMedia"
  17. // Copyright (c) 1996-2011 Live Networks, Inc.  All rights reserved.
  18. // A class for generating MPEG-2 Transport Stream from one or more input
  19. // Elementary Stream data sources
  20. // Implementation
  21.  
  22. #include <Media/MPEG2TransportStreamMultiplexor4iOS.hh>
  23. #include <stdint.h>
  24.  
  25. #define TRANSPORT_PACKET_SIZE 188
  26.  
  27. //#define PAT_FREQUENCY 100 // # of packets between Program Association Tables
  28. //#define PMT_FREQUENCY 500 // # of packets between Program Map Tables
  29.  
  30. //#define PID_TABLE_SIZE 8192
  31.  
  32.  
  33. MPEG2TransportStreamMultiplexor4iOS
  34. ::MPEG2TransportStreamMultiplexor4iOS(UsageEnvironment& env)
  35.   : FramedSource(env),
  36.     fHaveVideoStreams(True/*by default*/),
  37.     fOutgoingPacketCounter(0), fProgramMapVersion(0),
  38.     fPreviousInputProgramMapVersion(0xFF), fCurrentInputProgramMapVersion(0xFF),
  39.     fPCR_PID(0), fCurrentPID(0),
  40.     fInputBuffer(NULL), fInputBufferSize(0), fInputBufferBytesUsed(0),
  41.     fIsFirstAdaptationField(True) {
  42.   for (unsigned i = 0; i < PID_TABLE_SIZE; ++i) {
  43.     fPIDState[i].counter = 0;
  44.     fPIDState[i].seconds = 0;
  45.     fPIDState[i].segmentState_ = SEGMENT_STATE::NEED_PAT;
  46.     fPIDState[i].streamType = 0;
  47.   }
  48. }
  49.  
  50. MPEG2TransportStreamMultiplexor4iOS::~MPEG2TransportStreamMultiplexor4iOS() {
  51. }
  52.  
  53. void MPEG2TransportStreamMultiplexor4iOS::doGetNextFrame()
  54. {
  55.     if (fInputBufferBytesUsed >= fInputBufferSize)
  56.     {
  57.         // No more bytes are available from the current buffer.
  58.         // Arrange to read a new one.
  59.         awaitNewBuffer(fInputBuffer);
  60.        
  61.         return;
  62.     }
  63.  
  64.     bool SEGMENT_TRACE = true;
  65.     do {
  66.  
  67.         if (fPIDState[fCurrentPID].segmentState_ == SEGMENT_STATE::NEED_PMT)
  68.         {
  69.             deliverPMTPacket(false);
  70.             fPIDState[fCurrentPID].segmentState_ = SEGMENT_STATE::NEED_KEYFRAME;
  71.             break;
  72.         }
  73.  
  74.         if (fInputBufferBytesUsed == 0)
  75.         {
  76.             //if keyframe bump seconds
  77.             //if seconds>segment size insert PAT and PMT before the keyframe.
  78.             const static int segmentDurationSecs_ = 6;
  79.             int offset = 3;
  80.             int streamID = fInputBuffer[offset];
  81.             offset += 3;
  82.             bool DataAlignmentIndicator = (fInputBuffer[offset] & 0x04) == 4;
  83.             if (DataAlignmentIndicator && (streamID ==  0xE0))
  84.             {
  85.                 //if (SEGMENT_TRACE) envir() << "Beggining of video payload \n";
  86.                 offset += 2;
  87.                 int PES_HeaderLength = fInputBuffer[offset];
  88.  
  89.                 offset += PES_HeaderLength + 4;
  90.                 //skip over access_unit_delimeter
  91.                 offset += 6;
  92.                 uint8_t naltypeByte = fInputBuffer[offset];
  93.                 // If key frame, start new second.
  94.                 // If newsecond is greater than duration, start a new segment
  95.                 if ((naltypeByte & 0x1f) == 7)
  96.                 {
  97.                     if (fPIDState[fCurrentPID].seconds % segmentDurationSecs_ == 0)
  98.                     {
  99.                        if (fPIDState[fCurrentPID].segmentState_ == SEGMENT_STATE::NEED_PAT)
  100.                         {
  101.                             deliverPATPacket();
  102.                             fPIDState[fCurrentPID].segmentState_ = SEGMENT_STATE::NEED_PMT;
  103.                             break;
  104.                         }else //NEED_KEYFRAME
  105.                         {
  106.                             fPIDState[fCurrentPID].segmentState_ = SEGMENT_STATE::NEED_PAT;
  107.                             fPIDState[fCurrentPID].seconds++;
  108.                             break;
  109.                         }
  110.                     }else
  111.                         fPIDState[fCurrentPID].seconds++;
  112.                 }
  113.                 else
  114.                 {
  115.                     fPIDState[fCurrentPID].segmentState_ = SEGMENT_STATE::NEED_PAT;
  116.                 }
  117.  
  118.             }
  119.  
  120.             // Periodically (or when we see a new PID) return a Program Map Table instead:
  121.             Boolean programMapHasChanged = fCurrentInputProgramMapVersion != fPreviousInputProgramMapVersion;
  122.             if (programMapHasChanged)
  123.             { // reset values for next time:
  124.                 fPIDState[fCurrentPID].counter = 1;
  125.                 fPreviousInputProgramMapVersion = fCurrentInputProgramMapVersion;
  126.                 deliverPMTPacket(true);
  127.                 break;
  128.             }
  129.         }
  130.  
  131.         // Normal case: Deliver (or continue delivering) the recently-read data:
  132.         deliverDataToClient(fCurrentPID, fInputBuffer, fInputBufferSize, fInputBufferBytesUsed);
  133.  
  134.     } while (0);
  135.  
  136.   // NEED TO SET fPresentationTime, durationInMicroseconds #####
  137.   // Complete the delivery to the client:
  138.     envir().taskScheduler().scheduleDelayedTask(0,(TaskFunc*)afterGetting,this);
  139.   //afterGetting(this);
  140. }
  141.  
  142. void MPEG2TransportStreamMultiplexor4iOS
  143. ::handleNewBuffer(unsigned char* buffer, unsigned bufferSize,
  144.           int mpegVersion, MPEG1or2Demux::SCR scr) {
  145.   if (bufferSize < 4) return;
  146.   fInputBuffer = buffer;
  147.   fInputBufferSize = bufferSize;
  148.   fInputBufferBytesUsed = 0;
  149.  
  150.   u_int8_t stream_id = fInputBuffer[3];
  151.   // Use "stream_id" directly as our PID.
  152.   // Also, figure out the Program Map 'stream type' from this.
  153.   if (stream_id == 0xBE) { // padding_stream; ignore
  154.     fInputBufferSize = 0;
  155.   } else if (stream_id == 0xBC) { // program_stream_map
  156.     setProgramStreamMap(fInputBufferSize);
  157.     fInputBufferSize = 0; // then, ignore the buffer
  158.   } else {
  159.     fCurrentPID = stream_id;
  160.  
  161.     // Set the stream's type:
  162.     u_int8_t& streamType = fPIDState[fCurrentPID].streamType; // alias
  163.  
  164.     if (streamType == 0) {
  165.       // Instead, set the stream's type to default values, based on whether
  166.       // the stream is audio or video, and whether it's MPEG-1 or MPEG-2:
  167.       if ((stream_id&0xF0) == 0xE0) { // video
  168.     streamType = mpegVersion == 1 ? 1 : mpegVersion == 2 ? 2 : mpegVersion == 4 ? 0x10 : 0x1B;
  169.       } else if ((stream_id&0xE0) == 0xC0) { // audio
  170.     streamType = mpegVersion == 1 ? 3 : mpegVersion == 2 ? 4 : 0xF;
  171.       } else if (stream_id == 0xBD) { // private_stream1 (usually AC-3)
  172.     streamType = 0x06; // for DVB; for ATSC, use 0x81
  173.       } else { // something else, e.g., AC-3 uses private_stream1 (0xBD)
  174.     streamType = 0x81; // private
  175.       }
  176.     }
  177.  
  178.     if (fPCR_PID == 0) { // set it to this stream, if it's appropriate:
  179.       if ((!fHaveVideoStreams && (streamType == 3 || streamType == 4 || streamType == 0xF))/* audio stream */ ||
  180.       (streamType == 1 || streamType == 2 || streamType == 0x10 || streamType == 0x1B)/* video stream */) {
  181.     fPCR_PID = fCurrentPID; // use this stream's SCR for PCR
  182.       }
  183.     }
  184.     if (fCurrentPID == fPCR_PID) {
  185.       // Record the input's current SCR timestamp, for use as our PCR:
  186.       fPCR = scr;
  187.     }
  188.   }
  189.  
  190.   // Now that we have new input data, retry the last delivery to the client:
  191.   doGetNextFrame();
  192. }
  193.  
  194. void MPEG2TransportStreamMultiplexor4iOS
  195. ::deliverDataToClient(u_int16_t pid, unsigned char* buffer, unsigned bufferSize,
  196.               unsigned& startPositionInBuffer) {
  197.   // Construct a new Transport packet, and deliver it to the client:
  198.   if (fMaxSize < TRANSPORT_PACKET_SIZE) {
  199.     fFrameSize = 0; // the client hasn't given us enough space; deliver nothing
  200.     fNumTruncatedBytes = TRANSPORT_PACKET_SIZE;
  201.   } else {
  202.     fFrameSize = TRANSPORT_PACKET_SIZE;
  203.     Boolean willAddPCR = pid == fPCR_PID && startPositionInBuffer == 0
  204.       && !(fPCR.highBit == 0 && fPCR.remainingBits == 0 && fPCR.extension == 0);
  205.     unsigned const numBytesAvailable = bufferSize - startPositionInBuffer;
  206.     unsigned numHeaderBytes = 4; // by default
  207.     unsigned numPCRBytes = 0; // by default
  208.     unsigned numPaddingBytes = 0; // by default
  209.     unsigned numDataBytes;
  210.     u_int8_t adaptation_field_control;
  211.     if (willAddPCR) {
  212.       adaptation_field_control = 0x30;
  213.       numHeaderBytes += 2; // for the "adaptation_field_length" and flags
  214.       numPCRBytes = 6;
  215.       if (numBytesAvailable >= TRANSPORT_PACKET_SIZE - numHeaderBytes - numPCRBytes) {
  216.     numDataBytes = TRANSPORT_PACKET_SIZE - numHeaderBytes - numPCRBytes;
  217.       } else {
  218.     numDataBytes = numBytesAvailable;
  219.     numPaddingBytes
  220.       = TRANSPORT_PACKET_SIZE - numHeaderBytes - numPCRBytes - numDataBytes;
  221.       }
  222.     } else if (numBytesAvailable >= TRANSPORT_PACKET_SIZE - numHeaderBytes) {
  223.       // This is the common case
  224.       adaptation_field_control = 0x10;
  225.       numDataBytes = TRANSPORT_PACKET_SIZE - numHeaderBytes;
  226.     } else {
  227.       adaptation_field_control = 0x30;
  228.       ++numHeaderBytes; // for the "adaptation_field_length"
  229.       // ASSERT: numBytesAvailable <= TRANSPORT_PACKET_SIZE - numHeaderBytes
  230.       numDataBytes = numBytesAvailable;
  231.       if (numDataBytes < TRANSPORT_PACKET_SIZE - numHeaderBytes) {
  232.     ++numHeaderBytes; // for the adaptation field flags
  233.     numPaddingBytes = TRANSPORT_PACKET_SIZE - numHeaderBytes - numDataBytes;
  234.       }
  235.     }
  236.     // ASSERT: numHeaderBytes+numPCRBytes+numPaddingBytes+numDataBytes
  237.     //         == TRANSPORT_PACKET_SIZE
  238.  
  239.     // Fill in the header of the Transport Stream packet:
  240.     unsigned char* header = fTo;
  241.     *header++ = 0x47; // sync_byte
  242.     *header  = (startPositionInBuffer == 0) ? 0x40 : 0x00;// transport_error_indicator, payload_unit_start_indicator, transport_priority,
  243.     *header++ |= ((pid & 0x01fff) >> 8);   // first 5 bits of PID
  244.     *header++ = (pid & 0x00ff);           // last 8 bits of PID
  245.     unsigned& continuity_counter = fPIDState[pid].counter; // alias
  246.     *header++ = adaptation_field_control|(continuity_counter&0x0F);
  247.       // transport_scrambling_control, adaptation_field_control, continuity_counter
  248.     ++continuity_counter;
  249.     if (adaptation_field_control == 0x30) {
  250.       // Add an adaptation field:
  251.       u_int8_t adaptation_field_length
  252.     = (numHeaderBytes == 5) ? 0 : 1 + numPCRBytes + numPaddingBytes;
  253.       *header++ = adaptation_field_length;
  254.       if (numHeaderBytes > 5) {
  255.     u_int8_t flags = willAddPCR ? 0x10 : 0x00;
  256.     if (fIsFirstAdaptationField) {
  257.       flags |= 0x80; // discontinuity_indicator
  258.       fIsFirstAdaptationField = False;
  259.     }
  260.     *header++ = flags;
  261.     if (willAddPCR) {
  262.       u_int32_t pcrHigh32Bits = (fPCR.highBit<<31) | (fPCR.remainingBits>>1);
  263.       u_int8_t pcrLowBit = fPCR.remainingBits&1;
  264.       u_int8_t extHighBit = (fPCR.extension&0x100)>>8;
  265.       *header++ = pcrHigh32Bits>>24;
  266.       *header++ = pcrHigh32Bits>>16;
  267.       *header++ = pcrHigh32Bits>>8;
  268.       *header++ = pcrHigh32Bits;
  269.       *header++ = (pcrLowBit<<7)|0x7E|extHighBit;
  270.       *header++ = (u_int8_t)fPCR.extension; // low 8 bits of extension
  271.     }
  272.       }
  273.     }
  274.  
  275.     // Add any padding bytes:
  276.     for (unsigned i = 0; i < numPaddingBytes; ++i) *header++ = 0xFF;
  277.  
  278.     // Finally, add the data bytes:
  279.     memmove(header, &buffer[startPositionInBuffer], numDataBytes);
  280.     startPositionInBuffer += numDataBytes;
  281.   }
  282. }
  283.  
  284. static u_int32_t calculateCRC(u_int8_t* data, unsigned dataLength); // forward
  285.  
  286. #define PAT_PID 0
  287. #define OUR_PROGRAM_NUMBER 1
  288. #define OUR_PROGRAM_MAP_PID 0x123
  289.  
  290. void MPEG2TransportStreamMultiplexor4iOS::deliverPATPacket() {
  291.   // First, create a new buffer for the PAT packet:
  292.   unsigned const patSize = TRANSPORT_PACKET_SIZE - 4; // allow for the 4-byte header
  293.   unsigned char* patBuffer = new unsigned char[patSize];
  294.  
  295.   // and fill it in:
  296.   unsigned char* pat = patBuffer;
  297.   *pat++ = 0; // pointer_field
  298.   *pat++ = 0; // table_id
  299.   *pat++ = 0xB0; // section_syntax_indicator; 0; reserved, section_length (high)
  300.   *pat++ = 13; // section_length (low)
  301.   *pat++ = 0; *pat++ = 1; // transport_stream_id
  302.   *pat++ = 0xC1; // reserved; version_number; current_next_indicator
  303.   *pat++ = 0; // section_number
  304.   *pat++ = 0; // last_section_number
  305.   *pat++ = OUR_PROGRAM_NUMBER>>8; *pat++ = OUR_PROGRAM_NUMBER; // program_number
  306.   *pat++ = 0xE0|(OUR_PROGRAM_MAP_PID>>8); // reserved; program_map_PID (high)
  307.   *pat++ = OUR_PROGRAM_MAP_PID & 0xff; // program_map_PID (low)
  308.  
  309.   // Compute the CRC from the bytes we currently have (not including "pointer_field"):
  310.   u_int32_t crc = calculateCRC(patBuffer+1, pat - (patBuffer+1));
  311.   *pat++ = crc>>24; *pat++ = crc>>16; *pat++ = crc>>8; *pat++ = crc;
  312.  
  313.   // Fill in the rest of the packet with padding bytes:
  314.   while (pat < &patBuffer[patSize]) *pat++ = 0xFF;
  315.  
  316.   // Deliver the packet:
  317.   unsigned startPosition = 0;
  318.   deliverDataToClient(PAT_PID, patBuffer, patSize, startPosition);
  319.  
  320.   // Finally, remove the new buffer:
  321.   delete[] patBuffer;
  322. }
  323.  
  324. void MPEG2TransportStreamMultiplexor4iOS::deliverPMTPacket(Boolean hasChanged) {
  325.   if (hasChanged) ++fProgramMapVersion;
  326.  
  327.   // First, create a new buffer for the PMT packet:
  328.   unsigned const pmtSize = TRANSPORT_PACKET_SIZE - 4; // allow for the 4-byte header
  329.   unsigned char* pmtBuffer = new unsigned char[pmtSize];
  330.  
  331.   // and fill it in:
  332.   unsigned char* pmt = pmtBuffer;
  333.   *pmt++ = 0; // pointer_field
  334.   *pmt++ = 2; // table_id
  335.   *pmt++ = 0xB0; // section_syntax_indicator; 0; reserved, section_length (high)
  336.   unsigned char* section_lengthPtr = pmt; // save for later
  337.   *pmt++ = 0; // section_length (low) (fill in later)
  338.   *pmt++ = OUR_PROGRAM_NUMBER>>8; *pmt++ = OUR_PROGRAM_NUMBER; // program_number
  339.   *pmt++ = 0xC1|((fProgramMapVersion&0x1F)<<1); // reserved; version_number; current_next_indicator
  340.   *pmt++ = 0; // section_number
  341.   *pmt++ = 0; // last_section_number
  342.   *pmt++ = 0xE0; // reserved; PCR_PID (high)
  343.   *pmt++ = fPCR_PID; // PCR_PID (low)
  344.   *pmt++ = 0xF0; // reserved; program_info_length (high)
  345.   *pmt++ = 0; // program_info_length (low)
  346.   for (int pid = 0; pid < PID_TABLE_SIZE; ++pid) {
  347.     if (fPIDState[pid].streamType != 0) {
  348.       // This PID gets recorded in the table
  349.       *pmt++ = fPIDState[pid].streamType;
  350.       *pmt++ = 0xE0; // reserved; elementary_pid (high)
  351.       *pmt++ = pid; // elementary_pid (low)
  352.       *pmt++ = 0xF0; // reserved; ES_info_length (high)
  353.       *pmt++ = 0; // ES_info_length (low)
  354.     }
  355.   }
  356.   unsigned section_length = pmt - (section_lengthPtr+1) + 4 /*for CRC*/;
  357.   *section_lengthPtr = section_length;
  358.  
  359.   // Compute the CRC from the bytes we currently have (not including "pointer_field"):
  360.   u_int32_t crc = calculateCRC(pmtBuffer+1, pmt - (pmtBuffer+1));
  361.   *pmt++ = crc>>24; *pmt++ = crc>>16; *pmt++ = crc>>8; *pmt++ = crc;
  362.  
  363.   // Fill in the rest of the packet with padding bytes:
  364.   while (pmt < &pmtBuffer[pmtSize]) *pmt++ = 0xFF;
  365.  
  366.   // Deliver the packet:
  367.   unsigned startPosition = 0;
  368.   deliverDataToClient(OUR_PROGRAM_MAP_PID, pmtBuffer, pmtSize, startPosition);
  369.  
  370.   // Finally, remove the new buffer:
  371.   delete[] pmtBuffer;
  372. }
  373.  
  374. void MPEG2TransportStreamMultiplexor4iOS::setProgramStreamMap(unsigned frameSize) {
  375.   if (frameSize <= 16) return; // program_stream_map is too small to be useful
  376.   if (frameSize > 0xFF) return; // program_stream_map is too large
  377.  
  378.   u_int16_t program_stream_map_length = (fInputBuffer[4]<<8) | fInputBuffer[5];
  379.   if ((u_int16_t)frameSize > 6+program_stream_map_length) {
  380.     frameSize = 6+program_stream_map_length;
  381.   }
  382.  
  383.   u_int8_t versionByte = fInputBuffer[6];
  384.   if ((versionByte&0x80) == 0) return; // "current_next_indicator" is not set
  385.   fCurrentInputProgramMapVersion = versionByte&0x1F;
  386.  
  387.   u_int16_t program_stream_info_length = (fInputBuffer[8]<<8) | fInputBuffer[9];
  388.   unsigned offset = 10 + program_stream_info_length; // skip over 'descriptors'
  389.  
  390.   u_int16_t elementary_stream_map_length
  391.     = (fInputBuffer[offset]<<8) | fInputBuffer[offset+1];
  392.   offset += 2;
  393.   frameSize -= 4; // sizeof CRC_32
  394.   if (frameSize > offset + elementary_stream_map_length) {
  395.     frameSize = offset + elementary_stream_map_length;
  396.   }
  397.  
  398.   while (offset + 4 <= frameSize) {
  399.     u_int8_t stream_type = fInputBuffer[offset];
  400.     u_int8_t elementary_stream_id = fInputBuffer[offset+1];
  401.  
  402.     fPIDState[elementary_stream_id].streamType = stream_type;
  403.  
  404.     u_int16_t elementary_stream_info_length
  405.       = (fInputBuffer[offset+2]<<8) | fInputBuffer[offset+3];
  406.     offset += 4 + elementary_stream_info_length;
  407.   }
  408. }
  409.  
  410. static u_int32_t CRC32[256] = {
  411.   0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9,
  412.   0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005,
  413.   0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61,
  414.   0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd,
  415.   0x4c11db70, 0x48d0c6c7, 0x4593e01e, 0x4152fda9,
  416.   0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75,
  417.   0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011,
  418.   0x791d4014, 0x7ddc5da3, 0x709f7b7a, 0x745e66cd,
  419.   0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039,
  420.   0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5,
  421.   0xbe2b5b58, 0xbaea46ef, 0xb7a96036, 0xb3687d81,
  422.   0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d,
  423.   0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49,
  424.   0xc7361b4c, 0xc3f706fb, 0xceb42022, 0xca753d95,
  425.   0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1,
  426.   0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d,
  427.   0x34867077, 0x30476dc0, 0x3d044b19, 0x39c556ae,
  428.   0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072,
  429.   0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16,
  430.   0x018aeb13, 0x054bf6a4, 0x0808d07d, 0x0cc9cdca,
  431.   0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde,
  432.   0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02,
  433.   0x5e9f46bf, 0x5a5e5b08, 0x571d7dd1, 0x53dc6066,
  434.   0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba,
  435.   0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e,
  436.   0xbfa1b04b, 0xbb60adfc, 0xb6238b25, 0xb2e29692,
  437.   0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6,
  438.   0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a,
  439.   0xe0b41de7, 0xe4750050, 0xe9362689, 0xedf73b3e,
  440.   0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2,
  441.   0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686,
  442.   0xd5b88683, 0xd1799b34, 0xdc3abded, 0xd8fba05a,
  443.   0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637,
  444.   0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb,
  445.   0x4f040d56, 0x4bc510e1, 0x46863638, 0x42472b8f,
  446.   0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53,
  447.   0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47,
  448.   0x36194d42, 0x32d850f5, 0x3f9b762c, 0x3b5a6b9b,
  449.   0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff,
  450.   0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623,
  451.   0xf12f560e, 0xf5ee4bb9, 0xf8ad6d60, 0xfc6c70d7,
  452.   0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b,
  453.   0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f,
  454.   0xc423cd6a, 0xc0e2d0dd, 0xcda1f604, 0xc960ebb3,
  455.   0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7,
  456.   0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b,
  457.   0x9b3660c6, 0x9ff77d71, 0x92b45ba8, 0x9675461f,
  458.   0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3,
  459.   0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640,
  460.   0x4e8ee645, 0x4a4ffbf2, 0x470cdd2b, 0x43cdc09c,
  461.   0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8,
  462.   0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24,
  463.   0x119b4be9, 0x155a565e, 0x18197087, 0x1cd86d30,
  464.   0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec,
  465.   0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088,
  466.   0x2497d08d, 0x2056cd3a, 0x2d15ebe3, 0x29d4f654,
  467.   0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0,
  468.   0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c,
  469.   0xe3a1cbc1, 0xe760d676, 0xea23f0af, 0xeee2ed18,
  470.   0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4,
  471.   0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0,
  472.   0x9abc8bd5, 0x9e7d9662, 0x933eb0bb, 0x97ffad0c,
  473.   0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668,
  474.   0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4
  475. };
  476.  
  477. static u_int32_t calculateCRC(u_int8_t* data, unsigned dataLength) {
  478.   u_int32_t crc = 0xFFFFFFFF;
  479.  
  480.   while (dataLength-- > 0) {
  481.     crc = (crc<<8) ^ CRC32[(crc>>24) ^ (u_int32_t)(*data++)];
  482.   }
  483.  
  484.   return crc;
  485. }
Add Comment
Please, Sign In to add comment