void printCXForm(FILE *f, boolean hasAlpha) { int hasAdd, hasMult, nBits; byteAlign(); hasAdd = readBits(f, 1); hasMult = readBits(f, 1); nBits = readBits(f, 4); if(hasMult) { puts("x("); printf("%i,", readSBits(f, nBits)); printf("%i,", readSBits(f, nBits)); printf("%i)", readSBits(f, nBits)); if(hasAlpha) printf("%i,", readSBits(f, nBits)); } else puts("x()"); if(hasAdd) { puts("+("); printf("%i,", readSBits(f, nBits)); printf("%i,", readSBits(f, nBits)); printf("%i)", readSBits(f, nBits)); if(hasAlpha) printf("%i,", readSBits(f, nBits)); } else puts("+()"); putchar('\n'); }
int _tmain(int argc, _TCHAR* argv[]) { get_info(); //configure CBUS for bitbang mode printf("Setting cbus1 to I/O mode..\n"); setCBUSbits(0x0A); // will use only cbus1 rxled, 0x0A set to Bitbang I/O mode FT_SetBitMode(ftHandle,0,0); // try resetting //Still having trouble response is always FAILED for (int x=0;x<10;x++) //blink rxlex 10x { writebits( 0x22); // 0010 0010 //Sleep(1000); readBits(); writebits( 0x20); // 0010 0000 //Sleep(1000); readBits(); } printf("Restoring Cbus1....\n"); setCBUSbits(old_cbus1_state); // restore it back printf("Press any key to exit.... \n"); while(!_kbhit()); return 0; }
void printMatrix(FILE *f) { int nBits; float num; byteAlign(); if(readBits(f, 1)) /* has scale */ { nBits = readBits(f, 5); num = (float)readSBits(f, nBits)/0x10000; println("xScale: %f", num); num = (float)readSBits(f, nBits)/0x10000; println("yScale: %f", num); } if(readBits(f, 1)) /* has rotate */ { nBits = readBits(f, 5); num = (float)readSBits(f, nBits)/0x10000; println("rotate0: %f", num); num = (float)readSBits(f, nBits)/0x10000; println("rotate1: %f", num); } nBits = readBits(f, 5); println("x: %i", readSBits(f, nBits)); println("y: %i", readSBits(f, nBits)); }
void printShape(FILE *f, int length, SWFBlocktype type) { int start = fileOffset; int fillBits, lineBits; println("ShapeID: %i", readUInt16(f)); print("Bounds: "); printRect(f); putchar('\n'); putchar('\n'); printFillStyleArray(f, type); printLineStyleArray(f, type); putchar('\n'); byteAlign(); fillBits = readBits(f,4); lineBits = readBits(f,4); while(fileOffset < length+start && printShapeRec(f, &lineBits, &fillBits, type)) ; /* go for end tag.. if(fileOffset == length+start && bufbits > 5) printShapeRec(f, &lineBits, &fillBits, type); */ putchar('\n'); }
void readMatrix(FILE *f, struct Matrix *s) { int nBits; byteAlign(); if(readBits(f, 1)) /* has scale */ { nBits = readBits(f, 5); s->xScale = (float)readSBits(f, nBits)/0x10000; s->yScale = (float)readSBits(f, nBits)/0x10000; } else s->xScale = s->yScale = 1.0; if(readBits(f, 1)) /* has rotate */ { nBits = readBits(f, 5); s->rot0 = (float)readSBits(f, nBits)/0x10000; s->rot1 = (float)readSBits(f, nBits)/0x10000; } else s->rot0 = s->rot1 = 0; nBits = readBits(f, 5); s->x = readSBits(f, nBits); s->y = readSBits(f, nBits); }
bool Decompressor_M1::decompressDataBlock(std::vector< unsigned char >& buf) { readDecodeTables(); size_t bufSize = buf.size(); while (true) { if ((buf.size() - bufSize) > 65536) throw Ep128Emu::Exception("error in compressed data"); unsigned int matchLength = readMatchLength(); if (matchLength == 0U) { // literal byte buf.push_back(readLiteralByte()); } else if (matchLength >= 0x80000000U) { // literal sequence matchLength &= 0x7FFFFFFFU; if (matchLength < 18U) return bool(17U - matchLength); while (matchLength > 0U) { buf.push_back(readLiteralByte()); matchLength--; } } else { if (matchLength > 65535U) throw Ep128Emu::Exception("error in compressed data"); // get match offset: unsigned int offs = 0U; unsigned char d = 0; if (matchLength == 1U) { unsigned int slotNum = readBits(2); offs = readLZMatchParameter((unsigned char) slotNum, &(offs1DecodeTable[0])); } else if (matchLength == 2U) { unsigned int slotNum = readBits(3); offs = readLZMatchParameter((unsigned char) slotNum, &(offs2DecodeTable[0])); } else { unsigned int slotNum = readBits(offs3PrefixSize); if (!slotNum) { // 5-bit offset and delta value offs = 31U - readBits(5); d = readLiteralByte(); } else { slotNum--; offs = readLZMatchParameter((unsigned char) slotNum, &(offs3DecodeTable[0])); } } if (offs >= buf.size()) throw Ep128Emu::Exception("error in compressed data"); offs++; for (unsigned int j = 0U; j < matchLength; j++) buf.push_back((buf[buf.size() - offs] + d) & 0xFF); } } return true; // not reached }
void printShape(FILE *f, int length, int shapeType) { struct Shape shape; int id, start = fileOffset; memset(&shape, 0, sizeof(struct Shape)); id = readUInt16(f); shape.shapeType = shapeType; readRect(f, &(shape.bounds)); /* bounds */ readFillStyleArray(f, &shape, 0); readLineStyleArray(f, &shape, 0); byteAlign(); shape.fillBits = readBits(f,4); shape.lineBits = readBits(f,4); while(fileOffset < length+start && readShapeRec(f, &shape)) ; /* go for end tag.. if(fileOffset == length+start && bufbits > 5) printShapeRec(f, &lineBits, &fillBits, 2); */ printf("\n\t### Shape %i ###\n", id); printf("\t$s%i = new SWF::Shape();\n", id); printDefineShape(&shape, id, 0); }
void Decompressor_M1::readDecodeTables() { unsigned int tmp = 0U; unsigned int *tablePtr = &(offs1DecodeTable[0]); offs3PrefixSize = size_t(readBits(2)) + 2; size_t offs3NumSlots = (size_t(1) << offs3PrefixSize) - 1; for (size_t i = 0; i < (8 + 4 + 8 + offs3NumSlots); i++) { if (i == 4) { tmp = 0U; tablePtr = &(lengthDecodeTable[0]); } else if (i == (4 + 8)) { tmp = 0U; tablePtr = &(offs2DecodeTable[0]); } else if (i == (4 + 8 + 8)) { tmp = 0U; tablePtr = &(offs3DecodeTable[0]); } tablePtr[0] = readBits(4); tablePtr[1] = tmp; tmp = tmp + (1U << tablePtr[0]); tablePtr = tablePtr + 2; } }
int cargarArbolHuffman(struct nodo **raiz, BYTE *mem) { struct nodo *aux, *nuevo; int pos_mem=0, pos_pos=0, dch=0; BYTE b; *raiz=crearNodo(); readBits(mem, &pos_mem, &pos_pos, &b, 1); if (b == UNO) { readBits(mem, &pos_mem, &pos_pos, &b, 8); (*raiz)->c=b; return 2; } aux=*raiz; while (aux != NULL) { nuevo=crearNodo(); nuevo->padre=aux; readBits(mem, &pos_mem, &pos_pos, &b, 1); if (b == 0) { if (dch) { aux->hdch=nuevo; dch=0; } else aux->hizq=nuevo; aux=nuevo; } else { readBits(mem, &pos_mem, &pos_pos, &b, 8); nuevo->c=b; if (dch) { aux->hdch=nuevo; aux=buscarNodoSinHdch(aux); } else { aux->hizq=nuevo; dch=1; } } } return pos_mem+1; }
void MicroSequencer::SETMSB_3(INT32 immed4) { lpc12->repeat = (repeatPrefix << 4) | immed4; if (lpc12->repeat == 0) return; lpc12->amplitude = (readBits(6) << 2) | (lpc12->amplitude & 0x03); if (mode == 0x00 || mode == 0x02) { lpc12->b[5] = 0; lpc12->f[5] = 0; } switch (mode) { case 0x0: case 0x1: lpc12->f[0] = (INT8)((readBits(5) << 3) | (lpc12->f[0] & 0x07)); lpc12->f[1] = (INT8)((readBits(5) << 3) | (lpc12->f[1] & 0x07)); lpc12->f[2] = (INT8)((readBits(5) << 3) | (lpc12->f[2] & 0x07)); break; case 0x2: case 0x3: lpc12->f[0] = (INT8)((readBits(6) << 2) | (lpc12->f[0] & 0x03)); lpc12->f[1] = (INT8)((readBits(6) << 2) | (lpc12->f[1] & 0x03)); lpc12->f[2] = (INT8)((readBits(6) << 2) | (lpc12->f[2] & 0x03)); break; } lpc12->amplitudeInterpolation = (INT8) ((lpc12->amplitudeInterpolation & 0xE0) | (readBits(5))); lpc12->periodInterpolation = (INT8) ((lpc12->periodInterpolation & 0xE0) | (readBits(5))); lpc12->isIdle = FALSE; isIdle = TRUE; }
unsigned int readGolombUE(int maxbits = 32) { int lzb = -1; int bits = 0; for (int b = 0; !b; lzb++, bits++) { if (bits > maxbits) return 0; b = readBits(1); } return (1 << lzb) - 1 + readBits(lzb); }
void MicroSequencer::LOAD_E(INT32 immed4) { lpc12->repeat = (repeatPrefix << 4) | immed4; if (lpc12->repeat == 0) return; lpc12->amplitude = (readBits(6) << 2) | (lpc12->amplitude & 0x03); lpc12->period = readBits(8); //lpc12->periodCounter = (lpc12->period == 0 ? 0x100 : lpc12->period); lpc12->isIdle = FALSE; isIdle = TRUE; }
void readCXForm(FILE *f, struct CXForm *s, boolean hasAlpha) { int hasAdd, hasMult, nBits; byteAlign(); hasAdd = readBits(f, 1); hasMult = readBits(f, 1); nBits = readBits(f, 4); if(hasMult) { s->rMult = readSBits(f, nBits); s->gMult = readSBits(f, nBits); s->bMult = readSBits(f, nBits); if(hasAlpha) s->aMult = readSBits(f, nBits); else s->aMult = 0x100; } else { s->aMult = 0x100; s->rMult = 0x100; s->gMult = 0x100; s->bMult = 0x100; } if(hasAdd) { s->rAdd = readSBits(f, nBits); s->gAdd = readSBits(f, nBits); s->bAdd = readSBits(f, nBits); if(hasAlpha) s->aAdd = readSBits(f, nBits); else s->aAdd = 0; } else { s->aAdd = 0; s->rAdd = 0; s->gAdd = 0; s->bAdd = 0; } }
void printMorphShape(FILE *f, int length) { int offset, start = fileOffset, here; struct Rect r; struct Shape shape1, shape2; int id = readUInt16(f); printf("\n\t### Morph %i ###\n", id); printf("\t$s%i = new SWF::Morph();\n", id); readRect(f, &r); /* bounds 1 */ readRect(f, &r); /* bounds 2 */ offset = readUInt32(f); here = fileOffset+1; memset(&shape1, 0, sizeof(struct Shape)); memset(&shape2, 0, sizeof(struct Shape)); shape1.shapeType = shape2.shapeType = DEFINESHAPE3; readFillStyleArray(f, &shape1, 1); readLineStyleArray(f, &shape1, 1); byteAlign(); shape1.fillBits = shape2.fillBits = readBits(f,4); shape1.lineBits = shape2.lineBits = readBits(f,4); while(fileOffset < here+offset && readShapeRec(f, &shape1)) ; printf("\n\t$s%i_1 = $s%i->getShape1();\n", id, id); printDefineShape(&shape1, id, 0); byteAlign(); readUInt8(f); /* redundant fill/line bits */ while(fileOffset < start+length && readShapeRec(f, &shape2)) ; printf("\n\t$s%i_2 = $s%i->getShape2();\n", id, id); printDefineShape(&shape1, id, 1); }
std::vector<Bitset> GocadSGridReader::readRegionFlagsBinary() const { std::vector<Bitset> result; std::ifstream in(_region_flags_fname.c_str()); if (!in) { ERR("readRegionFlagsBinary(): Could not open file '%s' for input.\n", _region_flags_fname.c_str()); in.close(); return result; } std::size_t const n = _index_calculator._n_nodes; result.resize(n); std::size_t k = 0; while (in && k < n) { result[k++] = readBits(in, regions.size()); } if (k != n && !in.eof()) ERR("Read different number of values. Expected %d, got %d.\n", n, k); return result; }
unsigned int Decompressor_M1::readLZMatchParameter( unsigned char slotNum, const unsigned int *decodeTable) { unsigned int retval = decodeTable[int(slotNum) * 2 + 1]; retval += readBits(size_t(decodeTable[int(slotNum) * 2 + 0])); return retval; }
void printDefineFont(FILE *f, int length) { int here, off0, off, i, nShapes, fillBits=1, lineBits=1; int *offset; println("FontID: %i", readUInt16(f)); off0 = readUInt16(f); nShapes = off0/2; println("Number of shapes: %i", nShapes); offset = (int *)malloc(nShapes*sizeof(int)); ++gIndent; println("Offset0: 0"); for(i=1; i<nShapes; ++i) { off = readUInt16(f); offset[i-1] = off-off0; println("Offset%i: %i", i, offset[i-1]); } offset[nShapes-1] = length-2-(nShapes*2); here = fileOffset; for(i=0; i<nShapes; ++i) { putchar('\n'); byteAlign(); println("Shape %i:", i); fillBits = readBits(f, 4); lineBits = readBits(f, 4); ++gIndent; while(fileOffset < here+offset[i]) printShapeRec(f, &fillBits, &lineBits, 2); --gIndent; } --gIndent; }
unsigned int cBitstream::readGolombUE() { int lzb = -1; for(int b = 0; !b; lzb++) b = readBits1(); return (1 << lzb) - 1 + readBits(lzb); }
I2C_RESULT I2CDev::readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t bitLengh, uint8_t * buf) { uint8_t bitMask = 1<<bitStart; uint8_t i = 0; while(i < bitLengh - bitStart + 1) { bitMask |= bitStart << i++; } return readBits(devAddr, regAddr, bitMask, buf); }
void printMorphShape(FILE *f, int length) { int offset, start = fileOffset; int fillBits, lineBits, here; println("ShapeID: %i", readUInt16(f)); print("Bounds1: "); printRect(f); putchar('\n'); print("Bounds2: "); printRect(f); putchar('\n'); offset = readUInt32(f); println("(%i)\toffset = %i", fileOffset, offset); here = fileOffset; printFillStyleArray(f, SWF_DEFINEMORPHSHAPE); printLineStyleArray(f, SWF_DEFINEMORPHSHAPE); fillBits = readBits(f, 4); lineBits = readBits(f, 4); putchar('\n'); println("Shape1:"); while(fileOffset < here+offset) printShapeRec(f, &lineBits, &fillBits, SWF_DEFINESHAPE3); byteAlign(); /* ??? */ fillBits = readBits(f, 4); lineBits = readBits(f, 4); putchar('\n'); println("Shape2:"); while(fileOffset < start+length) printShapeRec(f, &lineBits, &fillBits, SWF_DEFINESHAPE3); }
void readRect(FILE *f, struct Rect *s) { int nBits; byteAlign(); nBits = readBits(f, 5); s->xMin = readSBits(f, nBits); s->xMax = readSBits(f, nBits); s->yMin = readSBits(f, nBits); s->yMax = readSBits(f, nBits); }
void printRect(FILE *f) { int nBits, xMin, xMax, yMin, yMax; byteAlign(); nBits = readBits(f, 5); xMin = readSBits(f, nBits); xMax = readSBits(f, nBits); yMin = readSBits(f, nBits); yMax = readSBits(f, nBits); printf("(%i,%i)x(%i,%i)", xMin, xMax, yMin, yMax); }
unsigned int Decompressor_M1::readMatchLength() { unsigned int slotNum = 0U; do { if (readBits(1) == 0U) break; slotNum++; } while (slotNum < 9U); if (slotNum == 0U) // literal byte return 0U; if (slotNum == 9U) // literal sequence return ((unsigned int) readLiteralByte() + 0x80000010U); return (readLZMatchParameter((unsigned char) (slotNum - 1U), &(lengthDecodeTable[0])) + 1U); }
errorCode decodeNBitUnsignedInteger(EXIStream* strm, unsigned char n, unsigned int* int_val) { DEBUG_MSG(INFO, DEBUG_STREAM_IO, (">> (%d-bits uint)", n)); if(WITH_COMPRESSION(strm->header.opts.enumOpt) == FALSE && GET_ALIGNMENT(strm->header.opts.enumOpt) == BIT_PACKED) { return readBits(strm, n, int_val); } else { unsigned int byte_number = ((unsigned int) n) / 8 + (n % 8 != 0); unsigned int tmp_byte_buf = 0; errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR; unsigned int i = 0; *int_val = 0; for(i = 0; i < byte_number; i++) { TRY(readBits(strm, 8, &tmp_byte_buf)); tmp_byte_buf = tmp_byte_buf << (i * 8); *int_val = *int_val | tmp_byte_buf; } } return EXIP_OK; }
int printTextRecord(FILE *f, int glyphBits, int advanceBits, int type) { int i, numGlyphs; int flags = readUInt8(f); if(flags == 0) return 0; if(flags & TEXTRECORD_STATECHANGE) { if(flags & TEXTRECORD_HASFONT) println("font id: %i", readUInt16(f)); if(flags & TEXTRECORD_HASCOLOR) { print("color: "); if(type == 2) printRGBA(f); else printRGB(f); putchar('\n'); } if(flags & TEXTRECORD_HASXOFF) println("X Offset: %i", readSInt16(f)); if(flags & TEXTRECORD_HASYOFF) println("Y Offset: %i", readSInt16(f)); if(flags & TEXTRECORD_HASFONT) println("font height: %i", readUInt16(f)); } else { numGlyphs = flags & TEXTRECORD_NUMGLYPHS; for(i=0; i<numGlyphs; ++i) { println("glyph index: %i", readBits(f, glyphBits)); println("glyph x advance: %i", readSBits(f, advanceBits)); } } return 1; }
uint32 *readGroups(register uint32 numBlocks, register BITSTRUCT *bitStuff) { register uint32 block; uint32 *groups; register uint32 *scanGroups; if((groups = (uint32 *)malloc((uint32)(numBlocks * sizeof(*groups)))) == NULL) { (void)fprintf(stderr, "Not enough memory for decoding of image\n"); exit(1); }; scanGroups = groups; for(block = 0; block < numBlocks; block++) { *(scanGroups++) = readBits(3, bitStuff); }; return(groups); }
uint32_t baro_get_pressure(void) { uint32_t pressure; write8(MPL3115A2_CTRL_REG1, MPL3115A2_CTRL_REG1_SBYB | MPL3115A2_CTRL_REG1_OS128 | MPL3115A2_CTRL_REG1_BAR); uint8_t sta = 0; while (! (sta & MPL3115A2_REGISTER_STATUS_PDR)) { sta = read8(MPL3115A2_REGISTER_STATUS); cph_millis_delay(125); } // Wire.beginTransmission(MPL3115A2_ADDRESS); // start transmission to device // Wire.write(MPL3115A2_REGISTER_PRESSURE_MSB); // Wire.endTransmission(false); // end transmission bool status = writeBit(MPL3115A2_ADDRESS, MPL3115A2_REGISTER_PRESSURE_MSB, MPL3115A2_OUT_P_DELTA_MSB, true); // Wire.requestFrom((uint8_t)MPL3115A2_ADDRESS, (uint8_t)3);// send data n-bytes read // pressure = Wire.read(); // receive DATA // pressure <<= 8; // pressure |= Wire.read(); // receive DATA // pressure <<= 8; // pressure |= Wire.read(); // receive DATA // pressure >>= 4; readBits(MPL3115A2_ADDRESS, 0x01, 0x00, 3, baro_buffer, I2CDEV_DEFAULT_READ_TIMEOUT); pressure = (((int32_t)baro_buffer[0]) << 8) | (((int32_t)baro_buffer[1]) << 8) | (((int32_t)baro_buffer[2]) << 4); float baro = pressure; baro /= 4.0; return baro; }
errorCode decodeBinary(EXIStream* strm, char** binary_val, Index* nbytes) { errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR; UnsignedInteger length = 0; unsigned int int_val = 0; UnsignedInteger i = 0; DEBUG_MSG(INFO, DEBUG_STREAM_IO, (">> (binary)")); TRY(decodeUnsignedInteger(strm, &length)); *nbytes = (Index) length; (*binary_val) = (char*) EXIP_MALLOC(length); // This memory should be manually freed after the content handler is invoked if((*binary_val) == NULL) return EXIP_MEMORY_ALLOCATION_ERROR; for(i = 0; i < length; i++) { TRY_CATCH(readBits(strm, 8, &int_val), EXIP_MFREE(*binary_val)); (*binary_val)[i]=(char) int_val; } return EXIP_OK; }
uint32_t BitFieldParser::readBits(uint32_t numBits) { ALOG_ASSERT(numBits <= 32); // Extract some bits from the current byte. uint32_t byteCursor = mBitCursor >> 3; // 8 bits per byte uint8_t byte = mData[byteCursor]; uint32_t bitsLeftInByte = 8 - (mBitCursor & 7); uint32_t bitsFromByte = (bitsLeftInByte < numBits) ? bitsLeftInByte : numBits; uint32_t result = byte >> (bitsLeftInByte - bitsFromByte); result &= (1 << bitsFromByte) - 1; // mask mBitCursor += bitsFromByte; uint32_t bitsRemaining = numBits - bitsFromByte; if (bitsRemaining == 0) { return result; } else { // Use recursion to get remaining bits. return (result << bitsRemaining) | readBits(bitsRemaining); } }
int readFile (char *filename, int *rows, int *cols, PIXEL** bitmap) { int fd, ret; unsigned int start; if(filename) { if((fd = open(filename, O_RDONLY)) < 0) { perror("Can't open bmp file to read"); return -1; } } else fd = STDIN_FILENO; ret = readHeader (fd, rows, cols, &start); if(ret) return ret; *bitmap = (PIXEL*)malloc(sizeof(PIXEL)*(*rows)*(*cols)); ret = readBits (fd, *bitmap, *rows, *cols, start); if(ret) return ret; if(filename) close(fd); return 0; }