Result RSS14Reader::decodeRow(int rowNumber, const BitArray& row_, std::unique_ptr<DecodingState>& state) const { RSS14DecodingState* prevState = nullptr; if (state == nullptr) { state.reset(prevState = new RSS14DecodingState); } else { #if !defined(ZX_HAVE_CONFIG) #error "You need to include ZXConfig.h" #elif !defined(ZX_NO_RTTI) prevState = dynamic_cast<RSS14DecodingState*>(state.get()); #else prevState = static_cast<RSS14DecodingState*>(state.get()); #endif } if (prevState == nullptr) { throw std::runtime_error("Invalid state"); } BitArray row = row_.copy(); AddOrTally(prevState->possibleLeftPairs, DecodePair(row, false, rowNumber)); row.reverse(); AddOrTally(prevState->possibleRightPairs, DecodePair(row, true, rowNumber)); // row.reverse(); for (const auto& left : prevState->possibleLeftPairs) { if (left.count() > 1) { for (const auto& right : prevState->possibleRightPairs) { if (right.count() > 1) { if (CheckChecksum(left, right)) { return ConstructResult(left, right); } } } } } return Result(DecodeStatus::NotFound); }
void InitVF(struct font_entry * tfontp) { struct stat stat; unsigned char* position; int length; struct vf_char *tcharptr; uint32_t c=0; struct font_num *tfontnump; /* temporary font_num pointer */ DEBUG_PRINTF((DEBUG_DVI|DEBUG_VF),"\n OPEN FONT:\t'%s'", tfontp->name); Message(BE_VERBOSE,"<%s>", tfontp->name); if ((tfontp->filedes = open(tfontp->name,O_RDONLY)) == -1) Warning("font file %s could not be opened", tfontp->name); fstat(tfontp->filedes,&stat); tfontp->mmap = mmap(NULL,stat.st_size, PROT_READ, MAP_SHARED,tfontp->filedes,0); if (tfontp->mmap == (unsigned char *)-1) Fatal("cannot mmap VF file <%s> !\n",currentfont->name); if (*(tfontp->mmap) != PRE) Fatal("unknown font format in file <%s> !\n",currentfont->name); if (*(tfontp->mmap+1) != VF_ID) Fatal( "wrong version of vf file! (%d should be 202)\n", (int)*(tfontp->mmap+1)); DEBUG_PRINTF2(DEBUG_VF,"\n VF_PRE:\t'%.*s'", (int)*(tfontp->mmap+2), tfontp->mmap+3); position = tfontp->mmap+3 + *(tfontp->mmap+2); c=UNumRead(position, 4); DEBUG_PRINTF(DEBUG_VF," %d", c); CheckChecksum (tfontp->c, c, tfontp->name); tfontp->designsize = UNumRead(position+4,4); DEBUG_PRINTF(DEBUG_VF," %d", tfontp->designsize); tfontp->type = FONT_TYPE_VF; tfontp->vffontnump=NULL; /* Read font definitions */ position += 8; while(*position >= FNT_DEF1 && *position <= FNT_DEF4) { DEBUG_PRINTF2(DEBUG_VF,"\n @%ld VF:\t%s", (long)(position - tfontp->mmap), dvi_commands[*position]); FontDef(position,tfontp); length = dvi_commandlength[*position]; position += length + *(position + length-1) + *(position+length-2); } /* Default font is the first defined */ tfontnump = tfontp->vffontnump; while (tfontnump->next != NULL) { tfontnump = tfontnump->next; } tfontp->defaultfont=tfontnump->k; /* Read char definitions */ while(*position < FNT_DEF1) { DEBUG_PRINTF(DEBUG_VF,"\n@%ld VF CHAR:\t", (long)(position - tfontp->mmap)); tcharptr=xmalloc(sizeof(struct vf_char)); switch (*position) { case LONG_CHAR: tcharptr->length = UNumRead(position+1,4); c = UNumRead(position+5,4); tcharptr->tfmw = UNumRead(position+9,4); position += 13; break; default: tcharptr->length = UNumRead(position,1); c = UNumRead(position+1,1); tcharptr->tfmw = UNumRead(position+2,3); position += 5; } DEBUG_PRINTF2(DEBUG_VF,"%d %d",tcharptr->length,c); DEBUG_PRINTF(DEBUG_VF," %d",tcharptr->tfmw); tcharptr->tfmw = (int32_t) ((int64_t) tcharptr->tfmw * tfontp->s / (1 << 20)); DEBUG_PRINTF(DEBUG_VF," (%d)",tcharptr->tfmw); if (c > NFNTCHARS) /* Only positive for now */ Fatal("vf character exceeds numbering limit"); tfontp->chr[c] = tcharptr; tcharptr->mmap=position; position += tcharptr->length; } }
void InitPK(struct font_entry * tfontp) { unsigned char* position; struct char_entry *tcharptr; /* temporary char_entry pointer */ uint32_t hppp, vppp, packet_length; uint32_t c; DEBUG_PRINT((DEBUG_DVI|DEBUG_PK),("\n OPEN FONT:\t'%s'", tfontp->name)); Message(BE_VERBOSE,"<%s>", tfontp->name); if (MmapFile(tfontp->name,&(tfontp->fmmap))) Fatal("font file %s unusable", tfontp->name); position=(unsigned char*)tfontp->fmmap.data; if (tfontp->fmmap.size < 2 || tfontp->fmmap.size < 3+*(position+2)+16) Fatal("PK file %s ends prematurely",tfontp->name); if (*position++ != PK_PRE) Fatal("unknown font format in file %s",tfontp->name); if (*position++ != PK_ID) Fatal( "wrong version %d of PK file %s (should be 89)", (int)*(position-1),tfontp->name); DEBUG_PRINT(DEBUG_PK,("\n PK_PRE:\t'%.*s'",(int)*position, position+1)); position += *position + 1; tfontp->designsize = UNumRead(position, 4); DEBUG_PRINT(DEBUG_PK,(" %d", tfontp->designsize)); tfontp->type = FONT_TYPE_PK; c = UNumRead(position+4, 4); DEBUG_PRINT(DEBUG_PK,(" %d", c)); CheckChecksum (tfontp->c, c, tfontp->name); hppp = UNumRead(position+8, 4); vppp = UNumRead(position+12, 4); DEBUG_PRINT(DEBUG_PK,(" %d %d", hppp,vppp)); if (hppp != vppp) Warning("aspect ratio is %d:%d (should be 1:1)", hppp, vppp); tfontp->magnification = (uint32_t)((uint64_t)hppp * 7227 * 5 / 65536l + 50)/100; position+=16; /* Read char definitions */ position = skip_specials(position); while (*position != PK_POST) { DEBUG_PRINT(DEBUG_PK,("\n @%ld PK CHAR:\t%d", (long)((char *)position - tfontp->fmmap.data), *position)); if ((tcharptr = malloc(sizeof(struct char_entry))) == NULL) Fatal("cannot allocate space for char_entry"); tcharptr->flag_byte = *position; tcharptr->data = NULL; tcharptr->tfmw = 0; if ((*position & 7) == 7) { packet_length = UNumRead(position+1,4); c = UNumRead(position+5, 4); position += 9; } else if (*position & 4) { packet_length = (*position & 3) * 65536l + UNumRead(position+1, 2); c = UNumRead(position+3, 1); position += 4; } else { packet_length = (*position & 3) * 256 + UNumRead(position+1, 1); c = UNumRead(position+2, 1); position += 3; } DEBUG_PRINT(DEBUG_PK,(" %d %d",packet_length,c)); if (c > (LASTFNTCHAR)) Fatal("PK font %s exceeds char numbering limit",tfontp->name); tcharptr->length = packet_length; tcharptr->pkdata = position; tfontp->chr[c]=tcharptr; position += packet_length; position = skip_specials(position); } }
bool WesterboerVW921Device::DataReceived(const void *_data, size_t length, struct NMEAInfo &info) { assert(_data != NULL); assert(length > 0); bool result = false; const char *data = (const char *)_data, *end = data + length; do { // append new data to buffer, as much as fits there auto range = buffer.Write(); if (range.IsEmpty()) { // overflow: reset buffer to recover quickly buffer.Clear(); continue; } size_t nbytes = std::min(size_t(range.length), size_t(end - data)); memcpy(range.data, data, nbytes); data += nbytes; buffer.Append(nbytes); while (true) { // read data from the buffer, to see if there's a dollar character range = buffer.Read(); if (range.IsEmpty()) break; // Search for the dollar sign (sync byte) char *dollar = (char *)memchr(range.data, '$', range.length); if (dollar == NULL) // no dollar sign here: wait for more data break; // Make sure there are at least 5 bytes in the buffer to // read the sentence header unsigned remaining_length = range.length - (dollar - range.data); if (remaining_length < 5) break; // Check this is a Westerboer sentence if (dollar[1] != 'w') { // Skip this sentence buffer.Consume(dollar - range.data + 1); continue; } // Read the length of the sentence uint8_t sentence_length = (uint8_t)dollar[2]; // Check if the sentence was completely received already if (remaining_length < sentence_length) break; if (!CheckChecksum(dollar, sentence_length)) { // Skip this sentence buffer.Consume(dollar - range.data + 1); continue; } // Read the sentence identification number uint8_t sentence_number = (uint8_t)dollar[4]; // Parse the sentence SentenceReceived(sentence_number, dollar, sentence_length, info); buffer.Consume(dollar - range.data + sentence_length); result = true; } } while (data < end); return result; }