void RIFFParser::parse_datasize64() { next(); if (chunk_id() != 'ds64') throw std::runtime_error("Invalid RF64 format"); check_eof(stream().seek_forward(8) == 8); uint32_t x, y; check_eof(stream().read32le(&x)); check_eof(stream().read32le(&y)); m_chunk_size_map['data'] = ((static_cast<uint64_t>(y) << 32) | x); check_eof(stream().seek_forward(8) == 8); uint32_t len; check_eof(stream().read32le(&len)); for (uint32_t i = 0; i < len; ++i) { char buff[4]; check_eof(stream().read(buff, 4) == 4); check_eof(stream().read32le(&x)); check_eof(stream().read32le(&y)); m_chunk_size_map[fourcc(buff)] = ((static_cast<uint64_t>(y) << 32) | x); } done_chunk(); }
// -------------------------------------------------------------------------- // // void ScalarDistributionData::read_reference(const std::string & path) { // Open the file. std::ifstream infile(path.c_str()); check_eof(infile, path, LOCATION); // Read the number of bins. infile >> nbins_; check_eof(infile, path, LOCATION); // Throw an error if the number of bins is too small. if (nbins_ <= 1) { std::string msg = "Number of bins as read from the file \"" + path + "\" is too small.\n"; error(msg, LOCATION); } // Resize member data according to the number of bins. target_.resize(nbins_, 0.0); scale_.resize(nbins_, 0.0); factor_.resize(nbins_, 0.0); distribution_.resize(nbins_, 0.0); distribution_new_.resize(nbins_, 0.0); // Read all data. for (int i = 0; i < nbins_; ++i) { // Read the three columns, scale, target distrubution and local sigma. infile >> scale_[i]; check_eof(infile, path, LOCATION); infile >> target_[i]; check_eof(infile, path, LOCATION); infile >> factor_[i]; check_eof(infile, path, LOCATION); } // Close the file after reading. infile.close(); // Normalize the target to 1. const double norm = vsum(target_); target_ = target_ / norm; // Calculate the bin related data. const double halfbinsize = (scale_[1] - scale_[0]) / 2; lowest_ = scale_[0] - halfbinsize; highest_ = scale_[nbins_-1] - halfbinsize; one_over_binsize_ = 1.0 / (2.0 * halfbinsize); }
/* A filemark is automatically written to tape if the last tape operation * before close was a write. */ int vtape::d_close(int) { check_eof(); ::close(fd); fd = -1; return 0; }
/* BSF => just before last EOF * EOF + BSF => just before EOF * file 0 + BSF => BOT + errno */ int vtape::bsf() { ASSERT(online); ASSERT(current_file >= 0); Dmsg2(dbglevel, "bsf %i:%i count=%i\n", current_file, current_block); int ret = 0; check_eof(); atBOT = atEOF = atEOT = atEOD = false; if (current_file == 0) {/* BOT + errno */ lseek(fd, 0, SEEK_SET); read_fm(VT_READ_EOF); current_file = 0; current_block = 0; atBOT = true; errno = EIO; ret = -1; } else { Dmsg1(dbglevel, "bsf last=%lli\n", last_FM); lseek(fd, cur_FM, SEEK_SET); current_file--; current_block=-1; } return ret; }
bool RIFFParser::get_chunkinfo(uint32_t *fcc, uint64_t *size) { char buff[4]; if (stream().read(buff, 4) < 4) return false; *fcc = fourcc(buff); uint32_t x; check_eof(stream().read32le(&x)); *size = x; // RF64 support chunk_map_t::const_iterator it = m_chunk_size_map.find(chunk_id()); if (it != m_chunk_size_map.end()) *size = it->second; return true; }
/* * TODO: Check fsr with EOF */ int vtape::fsr(int count) { ASSERT(online); ASSERT(current_file >= 0); ASSERT(fd >= 0); int i,nb, ret=0; // boffset_t where=0; uint32_t s; Dmsg4(dbglevel, "fsr %i:%i EOF=%i c=%i\n", current_file,current_block,atEOF,count); check_eof(); if (atEOT) { errno = EIO; current_block = -1; return -1; } if (atEOD) { errno = EIO; return -1; } atBOT = atEOF = false; /* check all block record */ for(i=0; (i < count) && !atEOF ; i++) { nb = ::read(fd, &s, sizeof(uint32_t)); /* get size of next block */ if (nb == sizeof(uint32_t) && s) { current_block++; lseek(fd, s, SEEK_CUR); /* seek after this block */ } else { Dmsg4(dbglevel, "read EOF %i:%i nb=%i s=%i\n", current_file, current_block, nb,s); errno = EIO; ret = -1; if (next_FM) { current_file++; read_fm(VT_SKIP_EOF); } atEOF = true; /* stop the loop */ } } return ret; }
int programme(struct ast_node **root, int *eof) { struct ast_node *tree; errors = 0; consume_token(); tree = stmts(NULL); *root = (struct ast_node *)ast_node_root(tree); check_eof(eof); return errors; }
/* * DEVICE virtual that we redefine. * * A filemark is automatically written to tape if the last tape operation * before close was a write. */ int vtape::d_close(int) { struct flock lock; check_eof(); if (lockfd >= 0) { lock.l_type = F_UNLCK; lock.l_start = 0; lock.l_whence = SEEK_SET; lock.l_len = 0; lock.l_pid = getpid(); ASSERT(fcntl(fd, F_SETLK, &lock) != -1); ::close(lockfd); free(lockfile); } ::close(fd); lockfd = fd = -1; return 0; }
int vtape::tape_op(struct mtop *mt_com) { int result=0; int count = mt_com->mt_count; if (!online) { errno = ENOMEDIUM; return -1; } switch (mt_com->mt_op) { case MTRESET: case MTNOP: case MTSETDRVBUFFER: break; default: case MTRAS1: case MTRAS2: case MTRAS3: case MTSETDENSITY: errno = ENOTTY; result = -1; break; case MTFSF: /* Forward space over mt_count filemarks. */ do { result = fsf(); } while (--count > 0 && result == 0); break; case MTBSF: /* Backward space over mt_count filemarks. */ do { result = bsf(); } while (--count > 0 && result == 0); break; case MTFSR: /* Forward space over mt_count records (tape blocks). */ /* file number = 1 block number = 0 file number = 1 block number = 1 mt: /dev/lto2: Erreur d'entree/sortie file number = 2 block number = 0 */ /* tester si on se trouve a la fin du fichier */ result = fsr(mt_com->mt_count); break; case MTBSR: /* Backward space over mt_count records (tape blocks). */ result = bsr(mt_com->mt_count); break; case MTWEOF: /* Write mt_count filemarks. */ do { result = weof(); } while (result == 0 && --count > 0); break; case MTREW: /* Rewind. */ Dmsg0(dbglevel, "rewind vtape\n"); check_eof(); atEOF = atEOD = false; atBOT = true; current_file = 0; current_block = 0; lseek(fd, 0, SEEK_SET); result = !read_fm(VT_READ_EOF); break; case MTOFFL: /* put tape offline */ result = offline(); break; case MTRETEN: /* Re-tension tape. */ result = 0; break; case MTBSFM: /* not used by bareos */ errno = EIO; result = -1; break; case MTFSFM: /* not used by bareos */ errno = EIO; result = -1; break; case MTEOM:/* Go to the end of the recorded media (for appending files). */ while (next_FM) { lseek(fd, next_FM, SEEK_SET); if (read_fm(VT_READ_EOF)) { current_file++; } } boffset_t l; while (::read(fd, &l, sizeof(l)) > 0) { if (l) { lseek(fd, l, SEEK_CUR); } else { ASSERT(0); } Dmsg0(dbglevel, "skip 1 block\n"); } current_block = -1; atEOF = false; atEOD = true; /* file number = 3 block number = -1 */ /* Can be at EOM */ break; case MTERASE: /* not used by bareos */ atEOD = true; atEOF = false; atEOT = false; current_file = 0; current_block = -1; lseek(fd, 0, SEEK_SET); read_fm(VT_READ_EOF); truncate_file(); break; case MTSETBLK: break; case MTSEEK: break; case MTTELL: break; case MTFSS: break; case MTBSS: break; case MTWSM: break; case MTLOCK: break; case MTUNLOCK: break; case MTLOAD: break; case MTUNLOAD: break; case MTCOMPRESSION: break; case MTSETPART: break; case MTMKPART: break; } return result == 0 ? 0 : -1; }
/* * When a filemark is encountered while reading, the following happens. If * there are data remaining in the buffer when the filemark is found, the * buffered data is returned. The next read returns zero bytes. The following * read returns data from the next file. The end of recorded data is signaled * by returning zero bytes for two consecutive read calls. The third read * returns an error. */ ssize_t vtape::d_read(int, void *buffer, size_t count) { ASSERT(online); ASSERT(current_file >= 0); ssize_t nb; uint32_t s; Dmsg2(dbglevel*2, "read %i:%i\n", current_file, current_block); if (atEOT || atEOD) { errno = EIO; return -1; } if (atEOF) { if (!next_FM) { atEOD = true; atEOF = false; current_block=-1; return 0; } atEOF=false; } check_eof(); atEOD = atBOT = false; /* reading size of data */ nb = ::read(fd, &s, sizeof(uint32_t)); if (nb <= 0) { atEOF = true; /* TODO: check this */ return 0; } if (s > count) { /* not enough buffer to read block */ Dmsg2(dbglevel, "Need more buffer to read next block %i > %i\n",s,count); lseek(fd, s, SEEK_CUR); errno = ENOMEM; return -1; } if (!s) { /* EOF */ atEOF = true; if (read_fm(VT_SKIP_EOF)) { current_file++; } return 0; } /* reading data itself */ nb = ::read(fd, buffer, s); if (nb != (ssize_t)s) { /* read error */ errno=EIO; atEOT=true; current_block = -1; Dmsg0(dbglevel, "EOT during reading\n"); return -1; } /* read ok */ if (current_block >= 0) { current_block++; } return nb; }
/* * BSR + EOF => begin of EOF + EIO * BSR + BSR + EOF => last block * current_block = -1 */ int vtape::bsr(int count) { ASSERT(online); ASSERT(current_file >= 0); ASSERT(count == 1); ASSERT(fd >= 0); check_eof(); if (!count) { return 0; } int ret=0; int last_f=0; int last_b=0; boffset_t last=-1, last2=-1; boffset_t orig = lseek(fd, 0, SEEK_CUR); int orig_f = current_file; int orig_b = current_block; Dmsg4(dbglevel, "bsr(%i) cur_blk=%i orig=%lli cur_FM=%lli\n", count, current_block, orig, cur_FM); /* begin of tape, do nothing */ if (atBOT) { errno = EIO; return -1; } /* at EOF 0:-1 BOT=0 EOD=0 EOF=0 ERR: Input/output error */ if (atEOF) { lseek(fd, cur_FM, SEEK_SET); atEOF = false; if (current_file > 0) { current_file--; } current_block=-1; errno = EIO; return -1; } /* * First, go to cur/last_FM and read all blocks to find the good one */ if (cur_FM == orig) { /* already just before EOF */ lseek(fd, last_FM, SEEK_SET); } else { lseek(fd, cur_FM, SEEK_SET); } ret = read_fm(VT_READ_EOF); do { if (!atEOF) { last2 = last; /* keep track of the 2 last blocs position */ last = lseek(fd, 0, SEEK_CUR); last_f = current_file; last_b = current_block; Dmsg6(dbglevel, "EOF=%i last2=%lli last=%lli < orig=%lli %i:%i\n", atEOF, last2, last, orig, current_file, current_block); } ret = fsr(1); } while ((lseek(fd, 0, SEEK_CUR) < orig) && (ret == 0)); if (last2 > 0 && atEOF) { /* we take the previous position */ lseek(fd, last2, SEEK_SET); current_file = last_f; current_block = last_b - 1; Dmsg3(dbglevel, "1 set offset2=%lli %i:%i\n", last, current_file, current_block); } else if (last > 0) { lseek(fd, last, SEEK_SET); current_file = last_f; current_block = last_b; Dmsg3(dbglevel, "2 set offset=%lli %i:%i\n", last, current_file, current_block); } else { lseek(fd, orig, SEEK_SET); current_file = orig_f; current_block = orig_b; return -1; } Dmsg2(dbglevel, "bsr %i:%i\n", current_file, current_block); errno=0; atEOT = atEOF = atEOD = false; atBOT = (lseek(fd, 0, SEEK_CUR) - (sizeof(uint32_t)+2*sizeof(boffset_t))) == 0; if (orig_b == -1) { current_block = orig_b; } return 0; }
static int inifile(char *section,char *item,char *value) { char *s1,*s2; int ret = 0; int len; int remove_section = (substring(item,"/remove",1) == 0); section = trim(section); item = trim(item); if (remove_section && !*section) return 0; while (find_section(section)) { if (remove_section) { filepos = pos; ret = read((unsigned long long)(unsigned int)"[*",2,GRUB_WRITE); continue; } while (len = (read_line(buff))) { if (*buff == '[') { filepos = pos; break; } s1 = check_eof(buff); if (s1) { s2 = skip_to(0x201,s1); if (!*item) { if (setenvi) { ret = envi_cmd(s1,s2,0); } else ret = printf("%s=%s\r\n",s1,s2); } else if (substring(item,s1,1) == 0) { if (value) { filepos = pos; if (substring(value,"/d",1) == 0) ret = read((unsigned long long)(unsigned int)";",1,GRUB_WRITE); else { ret = sprintf(buff,"%s=%s",item,value); if (ret > len) return 0; ret = read((unsigned long long)(unsigned int)buff,ret,GRUB_WRITE); if (ret < len) read((unsigned long long)(unsigned int)";",1,GRUB_WRITE); } } else if (setenvi) { ret = envi_cmd(s1,s2,0); } else return printf("%s",s2); } } } if (!*section) return ret; } return ret; }
int java_classdump(const char *file, int verbose) { RBinJavaClass2 cf2; unsigned short sz, sz2; int this_class; char buf[0x9999]; int i,j; FILE *fd = fopen(file, "rb"); if (fd == NULL) return -1; /* start parsing */ fread (&cf, 10, 1, fd); //sizeof(struct classfile), 1, fd); if (memcmp (cf.cafebabe, "\xCA\xFE\xBA\xBE", 4)) { eprintf ("java_classdump: Invalid header\n"); return -1; } javasm_init (); /* show class version information */ V printf ("Version: 0x%02x%02x 0x%02x%02x\n", cf.major[1],cf.major[0], cf.minor[1],cf.minor[0]); cf.cp_count = r_num_ntohs(cf.cp_count); if (cf.major[0]==cf.major[1] && cf.major[0]==0) { eprintf ("Oops. this is a Mach-O\n"); return 0; } cf.cp_count--; V printf ("ConstantPoolCount %d\n", cf.cp_count); cp_items = malloc (sizeof (struct cp_item)*(cf.cp_count+1)); for (i=0;i<cf.cp_count;i++) { struct constant_t *c; fread (buf, 1, 1, fd); c = NULL; for (j=0; r_bin_java_constants[j].name; j++) { if (r_bin_java_constants[j].tag == buf[0]) { c = &r_bin_java_constants[j]; break; } } if (c == NULL) { eprintf ("Invalid tag '%d'\n", buf[0]); return 0; } V eprintf (" %3d %s: ", i+1, c->name); /* store constant pool item */ strcpy (cp_items[i].name, c->name); cp_items[i].tag = c->tag; cp_items[i].value = NULL; // no string by default cp_items[i].off = ftell(fd)-1; /* read bytes */ switch (c->tag) { case 1: // utf 8 string fread (buf, 2, 1, fd); sz = USHORT (buf,0); //cp_items[i].len = sz; fread(buf, sz, 1, fd); buf[sz] = '\0'; break; default: fread(buf, c->len, 1, fd); } memcpy (cp_items[i].bytes, buf, 5); /* parse value */ switch(c->tag) { case 1: V printf ("%s\n", buf); cp_items[i].value = strdup(buf); break; case 7: V printf ("%d\n", USHORT(buf,0)); break; case 8: V printf ("string ptr %d\n", USHORT(buf, 0)); break; case 9: case 11: case 10: // METHOD REF V printf("class = %d, ", USHORT(buf,0)); V printf("name_type = %d\n", USHORT(buf,2)); break; case 12: V printf("name = %d, ", USHORT(buf,0)); V printf("descriptor = %d\n", USHORT(buf,2)); break; default: V printf("%d\n", UINT(buf, 40)); } } fread (&cf2, sizeof (RBinJavaClass2), 1, fd); check_eof(fd); V printf("Access flags: 0x%04x\n", cf2.access_flags); this_class = r_num_ntohs (cf2.this_class); V printf ("This class: %d\n", this_class); check_eof (fd); //printf("This class: %d (%s)\n", ntohs(cf2.this_class), cp_items[ntohs(cf2.this_class)-1].value); // XXX this is a double pointer !!1 //printf("Super class: %d (%s)\n", ntohs(cf2.super_class), cp_items[ntohs(cf2.super_class)-1].value); sz = read_short (fd); V printf ("Interfaces count: %d\n", sz); if (sz>0) { fread (buf, sz*2, 1, fd); sz = read_short (fd); for (i=0; i<sz; i++) { eprintf ("interfaces: TODO\n"); } } sz = read_short(fd); V printf("Fields count: %d\n", sz); if (sz>0) { for (i=0;i<sz;i++) { fread(buf, 8, 1, fd); V printf("%2d: Access Flags: %d\n", i, USHORT(buf, 0)); V printf(" Name Index: %d (%s)\n", USHORT(buf, 2), get_cp(USHORT(buf,2)-1)->value); V printf(" Descriptor Index: %d\n", USHORT(buf, 4)); //, cp_items[USHORT(buf, 4)-1].value); sz2 = USHORT(buf, 6); V printf(" field Attributes Count: %d\n", sz2); attributes_walk(fd, sz2, 1, verbose); } } sz = read_short(fd); V printf("Methods count: %d\n", sz); if (sz>0) { for (i=0;i<sz;i++) { fread(buf, 8, 1, fd); check_eof(fd); V printf("%2d: Access Flags: %d\n", i, USHORT(buf, 0)); V printf(" Name Index: %d (%s)\n", USHORT(buf, 2), get_cp(USHORT(buf, 2)-1)->value); V printf(" Descriptor Index: %d (%s)\n", USHORT(buf, 4), get_cp(USHORT(buf, 4)-1)->value); sz2 = USHORT(buf, 6); V printf(" method Attributes Count: %d\n", sz2); attributes_walk(fd, sz2, 0, verbose); } } fclose(fd); return 0; }