static uint64_t Decode(BitReader<TReader> & reader) { uint8_t n = 0; while (reader.Read(1) == 0) ++n; ASSERT_LESS_OR_EQUAL(n, 63, ()); uint64_t const msb = static_cast<uint64_t>(1) << n; return msb | reader.ReadAtMost64Bits(n); }
void HuffDecompressor::bitsDecompress(BitReader bits, size_t depth) { if (depth > 32) { myErrorCode = ERROR_CORRUPTED_FILE; return; } while (bits.left()) { const unsigned long dw = (unsigned long)bits.peek(32); const unsigned long v = myCacheTable[dw >> 24]; unsigned long codelen = v & 0x1F; //if ((codelen == 0) || (codelen > 32)) { // return false; //} unsigned long code = dw >> (32 - codelen); unsigned long r = (v >> 8); if ((v & 0x80) == 0) { while (code < myBaseTable[(codelen - 1) * 2]) { codelen += 1; code = dw >> (32 - codelen); } r = myBaseTable[(codelen - 1) * 2 + 1]; } r -= code; //if (codelen == 0) { // return false; //} if (!bits.eat(codelen)) { return; } const unsigned long dicno = r >> myEntryBits; const unsigned long off1 = 16 + (r - (dicno << myEntryBits)) * 2; const unsigned char* dict = myDicts[dicno]; //TODO need index check const unsigned long off2 = 16 + dict[off1] * 256 + dict[off1 + 1]; //TODO need index check const unsigned long blen = dict[off2] * 256 + dict[off2 + 1]; //TODO need index check const unsigned char* slice = dict + off2 + 2; const unsigned long sliceSize = blen & 0x7fff; if (blen & 0x8000) { if (myTargetBufferPtr + sliceSize < myTargetBufferEnd) { memcpy(myTargetBufferPtr, slice, sliceSize); myTargetBufferPtr += sliceSize; } else { return; } } else { bitsDecompress(BitReader(slice, sliceSize), depth + 1); } } }
HuffmanTree::HuffmanTree(uint64_t symbols, uint64_t *bits, uint64_t len) { _symbols = symbols; BitReader reader = BitReader(bits, len); uint64_t w = 0; uint64_t s_size = _symbols - 1; while (s_size) { w++; s_size >>= 1; } uint64_t pos = 0; vector<HuffmanTreeNode *> stack; stack.reserve(symbols); while (pos < len) { uint64_t bit; reader.read(&bit, 1); pos++; HuffmanTreeNode *node; if (bit) { // leaf node uint64_t symbol; reader.read(&symbol, w); pos += w; node = new HuffmanTreeNode(symbol, 0); _leaves[symbol] = node; } else { // internal node node = new HuffmanTreeNode(); } if (!stack.empty()) { HuffmanTreeNode *&back = stack.back(); if (!back->_left) { back->setLeft(node); } else if (!back->_right) { back->setRight(node); stack.pop_back(); } } else { _root = node; } if (!bit) { stack.push_back(node); } } }
bool init ( BitReader br ) { br.reset(); if ( br.getByte() != 0x64 || br.getByte() != 0x58 || br.getByte() != 0x20 || br.getByte() != 0x25 ) { return false; } // * dca.c dca_exss_parse_header br.skip(8); // user data _ssIndex = br.getInt(2); bool blownUp(br.getBool()); _headerSize = br.getInt(blownUp ? 12 : 8); //hdLen = ((hdHdr[6] & 0xf) << 11) + (hdHdr[7] << 3) + ((hdHdr[8] >> 5) & 7) + 1; _hdSize = br.getInt(blownUp ? 20: 16) + 1; return true; }
QuantMatrixExtension::QuantMatrixExtension( BitReader &hdr ) { init(); hdr.reset(); ahabassert( hdr.readbits( 32 ) == 0x000001b5 ); ahabassert( hdr.readbits( 4 ) == 3 ); load_intra_quantiser_matrix = hdr.readbits( 1 ); for ( int i = 0; i < 64; i++ ) { intra_quantiser_matrix[ mpeg2_normal_scan[ i ] ] = load_intra_quantiser_matrix ? hdr.readbits( 8 ) : default_intra_quantiser_matrix[ i ]; mpegassert( intra_quantiser_matrix[ mpeg2_normal_scan[ i ] ] != 0 ); } load_non_intra_quantiser_matrix = hdr.readbits( 1 ); for ( int i = 0; i < 64; i++ ) { non_intra_quantiser_matrix[ mpeg2_normal_scan[ i ] ] = load_non_intra_quantiser_matrix ? hdr.readbits( 8 ) : 16; mpegassert( intra_quantiser_matrix[ mpeg2_normal_scan[ i ] ] != 0 ); } }
PictureCodingExtension::PictureCodingExtension( BitReader &hdr ) { init(); hdr.reset(); ahabassert( hdr.readbits( 32 ) == 0x000001b5 ); ahabassert( hdr.readbits( 4 ) == 8 ); f_code_fh = hdr.readbits( 4 ); f_code_fv = hdr.readbits( 4 ); f_code_bh = hdr.readbits( 4 ); f_code_bv = hdr.readbits( 4 ); intra_dc_precision = hdr.readbits( 2 ); picture_structure = hdr.readbits( 2 ); top_field_first = hdr.readbits( 1 ); frame_pred_frame_dct = hdr.readbits( 1 ); concealment_motion_vectors = hdr.readbits( 1 ); q_scale_type = hdr.readbits( 1 ); intra_vlc_format = hdr.readbits( 1 ); alternate_scan = hdr.readbits( 1 ); repeat_first_field = hdr.readbits( 1 ); chroma_420_type = hdr.readbits( 1 ); progressive_frame = hdr.readbits( 1 ); if ( picture_structure != 3 ) { fprintf( stderr, "Ahab does not support field pictures.\n" ); throw ConformanceLimitExceeded(); } mpegassert( ((f_code_fh >= 1) && (f_code_fh <= 9)) || (f_code_fh == 15) ); mpegassert( ((f_code_fv >= 1) && (f_code_fv <= 9)) || (f_code_fv == 15) ); mpegassert( ((f_code_bh >= 1) && (f_code_bh <= 9)) || (f_code_bh == 15) ); mpegassert( ((f_code_bv >= 1) && (f_code_bv <= 9)) || (f_code_bv == 15) ); }
SequenceExtension::SequenceExtension( BitReader &hdr ) { init(); hdr.reset(); ahabassert( hdr.readbits( 32 ) == 0x000001b5 ); ahabassert( hdr.readbits( 4 ) == 1 ); escape_bit = hdr.readbits( 1 ); profile = hdr.readbits( 3 ); level = hdr.readbits( 4 ); progressive_sequence = hdr.readbits( 1 ); chroma_format = hdr.readbits( 2 ); horizontal_size_extension = hdr.readbits( 2 ); vertical_size_extension = hdr.readbits( 2 ); bit_rate_extension = hdr.readbits( 12 ); mpegassert( hdr.readbits( 1 ) == 1 ); vbv_buffer_size_extension = hdr.readbits( 8 ); low_delay = hdr.readbits( 1 ); frame_rate_extension_n = hdr.readbits( 2 ); frame_rate_extension_d = hdr.readbits( 5 ); /* Check conformance limits */ if ( escape_bit ) { throw ConformanceLimitExceeded(); } /* Simple profile or Main profile */ if ( (profile != 5) && (profile != 4) ) { throw ConformanceLimitExceeded(); } /* 4:2:0 only */ if ( chroma_format != 1 ) { throw ConformanceLimitExceeded(); } }
UInt64 WebSocketFramer::readFrame(BitReader& frame, char*& payload) { assert(handshakeComplete()); UInt64 limit = frame.limit(); size_t offset = frame.position(); //assert(offset == 0); // Read the frame header char header[MAX_HEADER_LENGTH]; BitReader headerReader(header, MAX_HEADER_LENGTH); frame.get(header, 2); UInt8 lengthByte = static_cast<UInt8>(header[1]); int maskOffset = 0; if (lengthByte & FRAME_FLAG_MASK) maskOffset += 4; lengthByte &= 0x7f; if (lengthByte + 2 + maskOffset < MAX_HEADER_LENGTH) frame.get(header + 2, lengthByte + maskOffset); else frame.get(header + 2, MAX_HEADER_LENGTH - 2); // Reserved fields frame.skip(2); // Parse frame header UInt8 flags; char mask[4]; headerReader.getU8(flags); headerReader.getU8(lengthByte); _frameFlags = flags; UInt64 payloadLength = 0; int payloadOffset = 2; if ((lengthByte & 0x7f) == 127) { UInt64 l; headerReader.getU64(l); if (l > limit) throw std::runtime_error(util::format("WebSocket error: Insufficient buffer for payload size %" I64_FMT "u", l)); //, ws::ErrorPayloadTooBig payloadLength = l; payloadOffset += 8; } else if ((lengthByte & 0x7f) == 126) { UInt16 l; headerReader.getU16(l); if (l > limit) throw std::runtime_error(util::format("WebSocket error: Insufficient buffer for payload size %u", unsigned(l))); //, ws::ErrorPayloadTooBig payloadLength = l; payloadOffset += 2; } else { UInt8 l = lengthByte & 0x7f; if (l > limit) throw std::runtime_error(util::format("WebSocket error: Insufficient buffer for payload size %u", unsigned(l))); //, ws::ErrorPayloadTooBig payloadLength = l; } if (lengthByte & FRAME_FLAG_MASK) { headerReader.get(mask, 4); payloadOffset += 4; } if (payloadLength > limit) //length) throw std::runtime_error("WebSocket error: Incomplete frame received"); //, ws::ErrorIncompleteFrame // Get a reference to the start of the payload payload = reinterpret_cast<char*>(const_cast<char*>(frame.begin() + (offset + payloadOffset))); // Unmask the payload if required if (lengthByte & FRAME_FLAG_MASK) { auto p = reinterpret_cast<char*>(payload); //frame.data()); for (UInt64 i = 0; i < payloadLength; i++) { p[i] ^= mask[i % 4]; } } // Update frame length to include payload plus header frame.seek(std::size_t(offset + payloadOffset + payloadLength)); //frame.limit(offset + payloadOffset + payloadLength); //int frameLength = (offset + payloadOffset); //assert(frame.position() == (offset + payloadOffset)); return payloadLength; }
bool ProcessStat(unsigned int stat, BitReader &reader, ItemProperty &itemProp) { if (stat > STAT_MAX) { return false; } StatProperties *bits = GetStatProperties(stat); unsigned int saveBits = bits->saveBits; unsigned int saveParamBits = bits->saveParamBits; unsigned int saveAdd = bits->saveAdd; itemProp.stat = stat; if (saveParamBits > 0) { switch (stat) { case STAT_CLASSSKILLS: { itemProp.characterClass = reader.read(saveParamBits); itemProp.value = reader.read(saveBits); return true; } case STAT_NONCLASSSKILL: case STAT_SINGLESKILL: { itemProp.skill = reader.read(saveParamBits); itemProp.value = reader.read(saveBits); return true; } case STAT_ELEMENTALSKILLS: { ulong element = reader.read(saveParamBits); itemProp.value = reader.read(saveBits); return true; } case STAT_AURA: { itemProp.skill = reader.read(saveParamBits); itemProp.value = reader.read(saveBits); return true; } case STAT_REANIMATE: { itemProp.monster = reader.read(saveParamBits); itemProp.value = reader.read(saveBits); return true; } case STAT_SKILLTAB: { itemProp.tab = reader.read(3); itemProp.characterClass = reader.read(3); ulong unknown = reader.read(10); itemProp.value = reader.read(saveBits); return true; } case STAT_SKILLONDEATH: case STAT_SKILLONHIT: case STAT_SKILLONKILL: case STAT_SKILLONLEVELUP: case STAT_SKILLONSTRIKING: case STAT_SKILLWHENSTRUCK: { itemProp.level = reader.read(6); itemProp.skill = reader.read(10); itemProp.skillChance = reader.read(saveBits); return true; } case STAT_CHARGED: { itemProp.level = reader.read(6); itemProp.skill = reader.read(10); itemProp.charges = reader.read(8); itemProp.maximumCharges = reader.read(8); return true; } case STAT_STATE: case STAT_ATTCKRTNGVSMONSTERTYPE: case STAT_DAMAGETOMONSTERTYPE: { // For some reason heroin_glands doesn't read these, even though // they have saveParamBits; maybe they don't occur in practice? itemProp.value = reader.read(saveBits) - saveAdd; return true; } default: reader.read(saveParamBits); reader.read(saveBits); return true; } } if (bits->op >= 2 && bits->op <= 5) { itemProp.perLevel = reader.read(saveBits); return true; } switch (stat) { case STAT_ENHANCEDMAXIMUMDAMAGE: case STAT_ENHANCEDMINIMUMDAMAGE: { itemProp.minimum = reader.read(saveBits); itemProp.maximum = reader.read(saveBits); return true; } case STAT_MINIMUMFIREDAMAGE: { itemProp.minimum = reader.read(saveBits); itemProp.maximum = reader.read(GetStatProperties(STAT_MAXIMUMFIREDAMAGE)->saveBits); return true; } case STAT_MINIMUMLIGHTNINGDAMAGE: { itemProp.minimum = reader.read(saveBits); itemProp.maximum = reader.read(GetStatProperties(STAT_MAXIMUMLIGHTNINGDAMAGE)->saveBits); return true; } case STAT_MINIMUMMAGICALDAMAGE: { itemProp.minimum = reader.read(saveBits); itemProp.maximum = reader.read(GetStatProperties(STAT_MAXIMUMMAGICALDAMAGE)->saveBits); return true; } case STAT_MINIMUMCOLDDAMAGE: { itemProp.minimum = reader.read(saveBits); itemProp.maximum = reader.read(GetStatProperties(STAT_MAXIMUMCOLDDAMAGE)->saveBits); itemProp.length = reader.read(GetStatProperties(STAT_COLDDAMAGELENGTH)->saveBits); return true; } case STAT_MINIMUMPOISONDAMAGE: { itemProp.minimum = reader.read(saveBits); itemProp.maximum = reader.read(GetStatProperties(STAT_MAXIMUMPOISONDAMAGE)->saveBits); itemProp.length = reader.read(GetStatProperties(STAT_POISONDAMAGELENGTH)->saveBits); return true; } case STAT_REPAIRSDURABILITY: case STAT_REPLENISHESQUANTITY: { itemProp.value = reader.read(saveBits); return true; } default: { itemProp.value = reader.read(saveBits) - saveAdd; return true; } } }
/* static */ void H264::vui_parameters(BitReader& aBr, SPSData& aDest) { aDest.aspect_ratio_info_present_flag = aBr.ReadBit(); if (aDest.aspect_ratio_info_present_flag) { aDest.aspect_ratio_idc = aBr.ReadBits(8); aDest.sar_width = aDest.sar_height = 0; // From E.2.1 VUI parameters semantics (ITU-T H.264 02/2014) switch (aDest.aspect_ratio_idc) { case 0: // Unspecified break; case 1: /* 1:1 7680x4320 16:9 frame without horizontal overscan 3840x2160 16:9 frame without horizontal overscan 1280x720 16:9 frame without horizontal overscan 1920x1080 16:9 frame without horizontal overscan (cropped from 1920x1088) 640x480 4:3 frame without horizontal overscan */ aDest.sample_ratio = 1.0f; break; case 2: /* 12:11 720x576 4:3 frame with horizontal overscan 352x288 4:3 frame without horizontal overscan */ aDest.sample_ratio = 12.0 / 11.0; break; case 3: /* 10:11 720x480 4:3 frame with horizontal overscan 352x240 4:3 frame without horizontal overscan */ aDest.sample_ratio = 10.0 / 11.0; break; case 4: /* 16:11 720x576 16:9 frame with horizontal overscan 528x576 4:3 frame without horizontal overscan */ aDest.sample_ratio = 16.0 / 11.0; break; case 5: /* 40:33 720x480 16:9 frame with horizontal overscan 528x480 4:3 frame without horizontal overscan */ aDest.sample_ratio = 40.0 / 33.0; break; case 6: /* 24:11 352x576 4:3 frame without horizontal overscan 480x576 16:9 frame with horizontal overscan */ aDest.sample_ratio = 24.0 / 11.0; break; case 7: /* 20:11 352x480 4:3 frame without horizontal overscan 480x480 16:9 frame with horizontal overscan */ aDest.sample_ratio = 20.0 / 11.0; break; case 8: /* 32:11 352x576 16:9 frame without horizontal overscan */ aDest.sample_ratio = 32.0 / 11.0; break; case 9: /* 80:33 352x480 16:9 frame without horizontal overscan */ aDest.sample_ratio = 80.0 / 33.0; break; case 10: /* 18:11 480x576 4:3 frame with horizontal overscan */ aDest.sample_ratio = 18.0 / 11.0; break; case 11: /* 15:11 480x480 4:3 frame with horizontal overscan */ aDest.sample_ratio = 15.0 / 11.0; break; case 12: /* 64:33 528x576 16:9 frame with horizontal overscan */ aDest.sample_ratio = 64.0 / 33.0; break; case 13: /* 160:99 528x480 16:9 frame without horizontal overscan */ aDest.sample_ratio = 160.0 / 99.0; break; case 14: /* 4:3 1440x1080 16:9 frame without horizontal overscan */ aDest.sample_ratio = 4.0 / 3.0; break; case 15: /* 3:2 1280x1080 16:9 frame without horizontal overscan */ aDest.sample_ratio = 3.2 / 2.0; break; case 16: /* 2:1 960x1080 16:9 frame without horizontal overscan */ aDest.sample_ratio = 2.0 / 1.0; break; case 255: /* Extended_SAR */ aDest.sar_width = aBr.ReadBits(16); aDest.sar_height = aBr.ReadBits(16); if (aDest.sar_width && aDest.sar_height) { aDest.sample_ratio = float(aDest.sar_width) / float(aDest.sar_height); } break; default: break; } } if (aBr.ReadBit()) { //overscan_info_present_flag aDest.overscan_appropriate_flag = aBr.ReadBit(); } if (aBr.ReadBit()) { // video_signal_type_present_flag aDest.video_format = aBr.ReadBits(3); aDest.video_full_range_flag = aBr.ReadBit(); aDest.colour_description_present_flag = aBr.ReadBit(); if (aDest.colour_description_present_flag) { aDest.colour_primaries = aBr.ReadBits(8); aDest.transfer_characteristics = aBr.ReadBits(8); aDest.matrix_coefficients = aBr.ReadBits(8); } } aDest.chroma_loc_info_present_flag = aBr.ReadBit(); if (aDest.chroma_loc_info_present_flag) { aDest.chroma_sample_loc_type_top_field = aBr.ReadUE(); aDest.chroma_sample_loc_type_bottom_field = aBr.ReadUE(); } aDest.timing_info_present_flag = aBr.ReadBit(); if (aDest.timing_info_present_flag ) { aDest.num_units_in_tick = aBr.ReadBits(32); aDest.time_scale = aBr.ReadBits(32); aDest.fixed_frame_rate_flag = aBr.ReadBit(); } }
bool init ( BitReader br ) { br.reset(); // 16 bit formats if ( br.getByte() == 0x7f && br.getByte() == 0xfe && br.getByte() == 0x80 && br.getByte() == 0x01 ) { _is14Bit = false; } else { // 14 bit formats br.reset(); if (br.getByte() == 0x1f && br.getByte() == 0xff && br.getByte() == 0xe8 && br.getByte() == 0x00 && br.getByte() == 0x07 && br.getByte(4) == 0xf) { _is14Bit = true; /* * The original code was this, not sure I can parse the 14bit the same as the * 16 bit, but I can look at that later if there are problems sampleBlocks = ((be2uint16(hdr16[2]) << 4) & 0x70) | ((be2uint16(hdr16[3]) >> 10) & 0x0f); ++sampleBlocks; amode = (be2uint16(hdr16[4]) >> 4) & 0x3f; sfreq = (be2uint16(hdr16[4]) >> 0) & 0x0f; lfe = (be2uint16(hdr16[6]) >> 11) & 0x03; */ } else { return false; // invalid frame data } } _isNormalFrame = br.getBool(); // 1 _shortSamples = br.getInt(5); // 6 _isCrcPresent = br.getBool(); // 7 _sampleBlocks = br.getInt(7) + 1; // 14 _frameSize = br.getInt(14) + 1; // 28 _channelLayout = br.getInt(6); _sampleIndex = br.getInt(4); _bitRate = br.getInt(5); _downMix = br.getBool(); _dynamicRange = br.getBool(); _timeStamp = br.getBool(); _auxiliaryData = br.getBool(); _hdcd = br.getBool(); _externalDescription = br.getInt(3); _externalCoding = br.getBool(); _aspf = br.getBool(); _lfe = br.getInt(2); _predictorHistory = br.getBool(); if ( _isCrcPresent ) _crc = br.getInt(16); _multirateInter = br.getBool(); _version = br.getInt(4); _copyHistory = br.getInt(2); _sourcePcmResolution = br.getInt(3); _frontSum = br.getBool(); _surroundSum = br.getBool(); _dialogNormalization = br.getInt(4); return true; }
void Tile::load(BitReader &bits) { foreground=bits.r16(); fgHueShift=bits.r8(); fgColorVariant=bits.r8(); fgMod=bits.r16(); fgModHueShift=bits.r8(); background=bits.r16(); bgHueShift=bits.r8(); bgColorVariant=bits.r8(); bgMod=bits.r16(); bgModHueShift=bits.r8(); liquid=bits.r8(); liquidPressure=bits.r8(); bits.skip(6); //collision, dungeonid,biome,envbiome,root }