void FileReader::handle_last_record(RecordBlock &block) { std::string &buffer = block.buffer(); append_int32(buffer, static_cast<int32_t>(buffer_size_)); buffer.append(reader_buffer_, buffer_size_); block.set_rec_num(1); buffer_size_ = 0; }
int FileReader::extract_record(RecordBlock &block, const RecordDelima &rec_delima, const RecordDelima &col_delima, int64_t max_rec_extracted) { int64_t pos = 0; int64_t rec_nr = 0; int64_t last_pos = 0; int ret = 0; bool has_partial_rec = false; std::string &buffer = block.buffer(); while (pos < buffer_size_) { char *p = reader_buffer_ + pos; last_pos = pos; if (rec_nr >= max_rec_extracted) { TBSYS_LOG(DEBUG, "extract rec count[%ld] reaches max_rec_extracted[%ld]", rec_nr, max_rec_extracted); break; } if ((has_partial_rec = is_partial_record(pos, rec_delima, col_delima)) == true) { TBSYS_LOG(DEBUG, "partial record meet, will be proceed next time"); break; } rec_nr++; col_delima.append_delima(reader_buffer_, pos, buffer_size_); /* replace rec_delima with col_delima */ rec_delima.skip_delima(pos); /* skip rec_delima */ /* include rec_delima in buffer */ int32_t psize = static_cast<int32_t>(pos - last_pos); append_int32(buffer, psize); last_pos = pos; buffer.append(p, psize); /* append length of record here */ } TBSYS_LOG(DEBUG, "record block actual used size[%ld], rec_nr[%ld]", pos, rec_nr); if ((has_partial_rec && last_pos < buffer_size_) /* partial record reside in buffer */ || (rec_nr >= max_rec_extracted && last_pos < buffer_size_)) { shrink(last_pos); } else { buffer_size_ = 0; } if (rec_nr == 0 && buffer_size_ == kReadBufferSize) { TBSYS_LOG(ERROR, "record size too big, max size=%d", kReadBufferSize); ret = -1; } if (ret == 0) { block.set_rec_num(rec_nr); } return ret; }
static size_t fmt_rrsig(struct rfc1035_reply *r, struct rfc1035_rr *rr, time_t now, char *buf) { char p[RFC1035_MAXNAMESIZE+1]; char timebuf[RFC1035_MAXTIMEBUFSIZE+1]; time_t signature_inception, signature_expiration; struct libmail_encode_info lei; struct fmt_rrsig_info fri; fri.buf=buf; fri.n=0; rfc1035_type_itostr(rr->rr.rrsig.type_covered, tostr_callback, &fri); append_str(&fri, " ", 1); append_int32(&fri, rr->rr.rrsig.algorithm); append_str(&fri, " ", 1); append_int32(&fri, rr->rr.rrsig.labels); append_str(&fri, " ", 1); rfc1035_fmttime(rr->rr.rrsig.original_ttl, timebuf); append_str(&fri, timebuf, strlen(timebuf)); append_str(&fri, " ", 1); if (sizeof(time_t) == 4) { signature_inception=rr->rr.rrsig.signature_inception; signature_expiration=rr->rr.rrsig.signature_expiration; } else { time_t now_epoch=(now & ~0xFFFFFFFFLL); time_t cur_epoch=now_epoch | rr->rr.rrsig.signature_inception; time_t prev_epoch=cur_epoch - 0xFFFFFFFF-1; time_t next_epoch=cur_epoch + 0xFFFFFFFF+1; #define time2diff(a,b) ((a) < (b) ? (b)-(a):(a)-(b)) #define closest2now(now,time1,time2) \ (time2diff((now), (time1)) < time2diff((now), (time2)) \ ? (time1):(time2)) signature_inception = closest2now(now, closest2now(now, prev_epoch, cur_epoch), next_epoch); signature_expiration = signature_inception + ((rr->rr.rrsig.signature_expiration - rr->rr.rrsig.signature_inception) & 0x7FFFFFFF); } append_time_t(&fri, signature_inception); append_str(&fri, " ", 1); append_time_t(&fri, signature_expiration); append_str(&fri, " ", 1); append_int32(&fri, rr->rr.rrsig.key_tag); append_str(&fri, " ", 1); rfc1035_replyhostname(r, rr->rr.rrsig.signer_name, p); append_str(&fri, p, strlen(p)); append_str(&fri, ". ", 2); libmail_encode_start(&lei, "base64", encode_callback_func, &fri); libmail_encode(&lei, rr->rr.rrsig.signature, rr->rr.rrsig.signature_len); libmail_encode_end(&lei); return fri.n; }