예제 #1
0
파일: state.c 프로젝트: Aleyr/nesemu2
int state_load(memfile_t *file)
{
	stateheader_t header;
	block_t *block;
	u32 size = 0;
	int i;

	readvar(header.ident,4);
	readvar(header.version,2);
	readvar(header.flags,2);
	readvar(header.usize,4);
	readvar(header.csize,4);
	readvar(header.crc32,4);
	log_printf("state_load:  state header loaded.  version %04X\n",header.version);

	while(memfile_eof(file) == 0 && size < header.usize) {
		if((block = block_load(file)) == 0)
			break;
		size += 8 + block->size;
		log_printf("state_load:  loaded block '%4s' (%08X) (%d bytes)\n",&block->type,block->type,block->size);
		for(i=0;blockinfo[i].type;i++) {
			if(blockinfo[i].type == block->type) {
				blockinfo[i].func(STATE_LOAD,block->data);
				break;
			}
		}
		if(blockinfo[i].type == 0) {
			log_printf("state_load:  no handler for block type '%4s' (%d bytes)\n",&block->type,block->size);
		}
		block_destroy(block);
	}

	return(0);
}
예제 #2
0
파일: hfile.c 프로젝트: pipul/lab
entry_t *hfile_loup(hfile_t *fp, sds key)
{
	int32_t fd;
	meta_t *m;
	entry_t *t;
	sds firstkey;

	if (!fp || !key)
		return(NULL);
	if (-1 == bloom_check(fp->bloom,key))
		return(NULL);
	firstkey = (index_head(fp->metas))->key;
	if (sdscmp(key,firstkey) < 0)
		return(NULL);
	if (sdscmp(key,fp->fileinfo->lastkey) > 0)
		return(NULL);
	m = index_loup(fp->metas,key);
	if (m->state != IN_CACHE) {
		if ((fd = open(fp->name,O_RDONLY)) < 0)
			/* error handle */
			return(NULL);
		fp->blocks[m->id] = block_load(fd,m->offset,m->blocksize);
		m->state = IN_CACHE;
		close(fd);
	}
	t = block_loup(fp->blocks[m->id],key);
	return(t);
}
예제 #3
0
int block_load_article(wp_dump *d, char *name, int block, wp_article *a) {
  char *text = xalloc(BZ_MAX_BLOCK);
  int text_len = BZ_MAX_BLOCK, article_len;
  char *start;

  debug("opening %s from block %d (%llu)", name, block, (d->block_map)[block]);
  block_load(d, block, text, &text_len);

  if((article_len = article_bounds(name, text, &start, text_len)) < 0) {
    if(article_len == BOUNDS_HIT_END) {
      /* the start was in the block, but we hit the end of the block before finding
       * the end of the article text. Load the next block and re-enter...
       * We only consider the case of loading one extra block; no Wikipedia article will span
       * more than two (it'd have to be >900kb for it to be possible). 
       */

      if(!d->block_map[block + 1])
        fatal("trying to load another block, but don't have any blocks left. "
              "This probably indicates a malformed dump.");

      debug("Loading another block");
      if(!(text = realloc(text, BZ_MAX_BLOCK * 2))) fatal("realloc");
      int n_text_len = BZ_MAX_BLOCK;

      block_load(d, block + 1, text + text_len, &n_text_len);

      if((article_len = article_bounds(name, text, &start, n_text_len + text_len)) < 0)
        fatal("additional block loading failure: %d", article_len);
    } else
      fatal("couldn't find %s in block %d", name, block);
  }

  debug("start: 0x%x, len: %d", start, article_len);
  debug("a->text: 0x%x", a->text);

  *(start + article_len) = '\0';
  strncpy(a->text, start, article_len + 1);
  a->block = block;
  free(text);
  return article_len;
}