Ejemplo n.º 1
0
/* Parse a dump header, including its tagged attributes, and call the
 * dump-header callback, if one is defined.
 */
static afs_uint32
parse_dumphdr(XFILE * X, unsigned char *tag, tagged_field * field,
	      afs_uint32 value, tag_parse_info * pi, void *g_refcon,
	      void *l_refcon)
{
    dump_parser *p = (dump_parser *) g_refcon;
    afs_dump_header hdr;
    dt_uint64 where;
    afs_uint32 r;

    memset(&hdr, 0, sizeof(hdr));
    if ((r = xftell(X, &where)))
	return r;
    sub64_32(hdr.offset, where, 1);

    if ((r = ReadInt32(X, &hdr.magic)))
	return r;
    if ((r = ReadInt32(X, &hdr.version)))
	return r;

    if (hdr.magic != DUMPBEGINMAGIC) {
	if (p->cb_error)
	    (p->cb_error) (DSERR_MAGIC, 1, p->err_refcon,
			   "Invalid magic number (0x%08x) in dump header",
			   hdr.magic);
	return DSERR_MAGIC;
    }
    if (hdr.version != DUMPVERSION) {
	if (p->cb_error)
	    (p->cb_error) (DSERR_MAGIC, 1, p->err_refcon,
			   "Unknown dump format version (%d) in dump header",
			   hdr.version);
	return DSERR_MAGIC;
    }

    if (p->print_flags & DSPRINT_DUMPHDR)
	printf("%s [%s = 0x%s]\n", field->label,
	       decimate_int64(&hdr.offset, 0), hexify_int64(&hdr.offset, 0));
    if (p->print_flags & DSPRINT_DUMPHDR) {
	printf(" Magic number: 0x%08x\n", hdr.magic);
	printf(" Version:      %d\n", hdr.version);
    }
    r = ParseTaggedData(X, dumphdr_fields, tag, pi, g_refcon, (void *)&hdr);

    if (!r && p->cb_dumphdr) {
	r = xftell(X, &where);
	if (!r)
	    r = (p->cb_dumphdr) (&hdr, X, p->refcon);
	if (p->flags & DSFLAG_SEEK) {
	    if (!r)
		r = xfseek(X, &where);
	    else
		xfseek(X, &where);
	}
    }
    if (hdr.field_mask & F_DUMPHDR_VOLNAME)
	free(hdr.volname);
    return r;
}
static unsigned long long lengthOfFile(FILE *file)
{
    long int currentPosition = xftell(file);
    xfseek(file , 0L , SEEK_END);
    long int length = xftell(file);
    xfseek(file, currentPosition, SEEK_SET);

    return length;
}
Ejemplo n.º 3
0
/*** THIS FUNCTION INTENDED FOR INTERNAL USE ONLY ***/
int
handle_return(int r, XFILE * X, unsigned char tag, dump_parser * p)
{
    u_int64 where, xwhere;

    switch (r) {
    case 0:
	if (p->cb_error) {
	    xftell(X, &where);
	    sub64_32(xwhere, where, 1);
	    (p->cb_error) (DSERR_TAG, 1, p->err_refcon,
			   (tag > 0x20 && tag < 0x7f)
			   ? "Unexpected tag '%c' at %s = 0x%s" :
			   "Unexpected tag 0x%02x at %s = 0x%s", tag,
			   decimate_int64(&xwhere, 0), hexify_int64(&xwhere,
								    0));
	}
	return DSERR_TAG;

    case ERROR_XFILE_EOF:
	if (p->cb_error) {
	    xftell(X, &where);
	    (p->cb_error) (ERROR_XFILE_EOF, 1, p->err_refcon,
			   "Unexpected EOF at %s = 0x%s",
			   decimate_int64(&where, 0), hexify_int64(&where,
								   0));
	}
	return ERROR_XFILE_EOF;

    case ENOMEM:
	if (p->cb_error) {
	    xftell(X, &where);
	    (p->cb_error) (ENOMEM, 1, p->err_refcon,
			   "Out of memory at %s = 0x%s",
			   decimate_int64(&where, 0), hexify_int64(&where,
								   0));
	}
	return ENOMEM;

    case DSERR_DONE:
	return 0;

    default:
	/* For other negative valuees, the callback was already done */
	if (r > 0 && p->cb_error)
	    (p->cb_error) (r, 1, p->err_refcon,
			   "System error %d reading dump file", r);
	return r;
    }
}
CSFAPI int CSFile_load(CSFile** outcsf, const char* filename, CSFileMemoryPTR mem)
{
  FILE* file;
  if (fopen_s(&file,filename,"rb")){
    *outcsf = 0;
    return CADSCENEFILE_ERROR_NOFILE;
  }

  CSFile header;
  size_t sizeshould = 0;
  if (!fread_s(&header,sizeof(header),sizeof(header),1,file) ||
      !(sizeshould=CSFile_getRawSize(&header)))
  {
    fclose(file);
    *outcsf = 0;
    return CADSCENEFILE_ERROR_VERSION;
  }
  
  // load the full file to memory
  xfseek(file, 0, SEEK_END);
  size_t size = (size_t)xftell(file);
  xfseek(file, 0, SEEK_SET);

  if (sizeshould != size){
    fclose(file);
    *outcsf = 0;
    return CADSCENEFILE_ERROR_VERSION;
  }
  
  char* data  = (char*)mem->alloc(size);
  fread_s(data,size,size,1,file);
  fclose(file);

  return CSFile_loadRaw(outcsf,size,data);
}
Ejemplo n.º 5
0
static afs_uint32
file_cb(afs_vnode * v, XFILE * X, void *refcon)
{
    char *vnodepath, vnpx[30];
    dt_uint64 where;
    XFILE OX;
    int r, use = 0;

    if (!dirs_done) {
	dirs_done = 1;
	if (verbose)
	    printf("* Extracting files...\n");
    }

    /* Should we even use this? */
    if (!use_vnum) {
	if ((r = Path_Build(X, &phi, v->vnode, &vnodepath, !use_realpath)))
	    return r;
	if (!(use = usevnode(X, v->vnode, vnodepath))) {
	    free(vnodepath);
	    return 0;
	}
	if (use == 2) {
	    free(vnodepath);
	    sprintf(vnpx, "#%d:%d", v->vnode, v->vuniq);
	    vnodepath = vnpx;
	}
    } else {
	sprintf(vnpx, "#%d:%d", v->vnode, v->vuniq);
	vnodepath = vnpx;
    }

    /* Print it out */
    if (verbose) {
	printf("-%s %3d %-11d %11d %s %s\n", modestr(v->mode), v->nlinks,
	       v->owner, v->size, datestr(v->server_date), vnodepath);
    } else if (!quiet) {
	printf("%s\n", vnodepath);
    }

    if (!nomode) {
	if ((r = xftell(X, &where))
	    || (r = xfseek(X, &v->d_offset))
	    || (r =
		xfopen_path(&OX, O_RDWR | O_CREAT | O_TRUNC, vnodepath + 1,
			    0644))) {
	    if (vnodepath != vnpx)
		free(vnodepath);
	    return r;
	}
	r = copyfile(X, &OX, v->size);
	xfclose(&OX);
	xfseek(X, &where);
    } else
	r = 0;

    if (vnodepath != vnpx)
	free(vnodepath);
    return r;
}
Ejemplo n.º 6
0
/* Parse a volume header, including any tagged attributes, and call the
 * volume-header callback, if one is defined.
 */
afs_uint32
parse_volhdr(XFILE * X, unsigned char *tag, tagged_field * field,
	     afs_uint32 value, tag_parse_info * pi, void *g_refcon,
	     void *l_refcon)
{
    dump_parser *p = (dump_parser *) g_refcon;
    afs_vol_header hdr;
    u_int64 where;
    afs_uint32 r;

    memset(&hdr, 0, sizeof(hdr));
    if (r = xftell(X, &where))
	return r;
    sub64_32(hdr.offset, where, 1);
    if (p->print_flags & DSPRINT_VOLHDR)
	printf("%s [%s = 0x%s]\n", field->label,
	       decimate_int64(&hdr.offset, 0), hexify_int64(&hdr.offset, 0));

    r = ParseTaggedData(X, volhdr_fields, tag, pi, g_refcon, (void *)&hdr);

    if (!r && p->cb_volhdr) {
	if (r = xftell(X, &where))
	    return r;
	r = (p->cb_volhdr) (&hdr, X, p->refcon);
	if (p->flags & DSFLAG_SEEK) {
	    if (!r)
		r = xfseek(X, &where);
	    else
		xfseek(X, &where);
	}
    }
    if (hdr.field_mask & F_VOLHDR_VOLUNIQ)
	p->vol_uniquifier = hdr.voluniq;
    if (hdr.field_mask & F_VOLHDR_VOLNAME)
	free(hdr.volname);
    if (hdr.field_mask & F_VOLHDR_OFFLINE_MSG)
	free(hdr.offline_msg);
    if (hdr.field_mask & F_VOLHDR_MOTD)
	free(hdr.motd_msg);
    return r;
}
Ejemplo n.º 7
0
/* do_tell for profiled xfiles */
static afs_uint32
xf_PROFILE_do_tell(XFILE * X, u_int64 * offset)
{
    PFILE *PF = X->refcon;
    afs_uint32 err;

    err = xftell(&PF->content, offset);
    if (err)
	xfprintf(&PF->profile, "TELL ERR =%ld\n", (long)err);
    else
	xfprintf(&PF->profile, "TELL %s =0\n", hexify_int64(offset, 0));
    return err;
}
Ejemplo n.º 8
0
static afs_uint32
resync_vnode(XFILE * X, dump_parser * p, afs_vnode * v, int start, int limit)
{
    dt_uint64 where, expected_where;
    afs_uint32 r;
    int i;

    if ((r = xftell(X, &expected_where)))
	return r;
    cp64(where, expected_where);

    r = match_next_vnode(X, p, &where, v->vnode);
    if (r && r != DSERR_FMT)
	return r;
    if (r)
	for (i = -start; i < limit; i++) {
	    add64_32(where, expected_where, i);
	    r = match_next_vnode(X, p, &where, v->vnode);
	    if (!r)
		break;
	    if (r != DSERR_FMT)
		return r;
	}
    if (r) {
	if (p->cb_error)
	    (p->cb_error) (r, 1, p->err_refcon,
			   "Unable to resync after vnode %d [%s = 0x%s]",
			   v->vnode, decimate_int64(&expected_where, 0),
			   hexify_int64(&expected_where, 0));
	return r;
    }
    if (ne64(where, expected_where) && p->cb_error) {
	(p->cb_error) (DSERR_FMT, 0, p->err_refcon,
		       "Vnode after %d not in expected location", v->vnode);
	(p->cb_error) (DSERR_FMT, 0, p->err_refcon,
		       "Expected location: %s = 0x%s",
		       decimate_int64(&expected_where, 0),
		       hexify_int64(&expected_where, 0));
	(p->cb_error) (DSERR_FMT, 0, p->err_refcon,
		       "Actual location: %s = 0x%s", decimate_int64(&where,
								    0),
		       hexify_int64(&where, 0));
    }
    return xfseek(X, &where);
}
Ejemplo n.º 9
0
Archivo: port.c Proyecto: KeenS/benz
struct pic_string *
pic_get_output_string(pic_state *pic, struct pic_port *port)
{
  size_t size;
  char *buf;

  /* get endpos */
  xfflush(port->file);
  size = (size_t)xftell(port->file);
  xrewind(port->file);

  /* copy to buf */
  buf = (char *)pic_alloc(pic, size + 1);
  buf[size] = 0;
  xfread(buf, size, 1, port->file);

  return pic_make_str(pic, buf, size);
}
Ejemplo n.º 10
0
afs_uint32
xfskip(XFILE * X, afs_uint32 count)
{
    afs_uint32 code;
    u_int64 tmp64;

    /* Use the skip method, if there is one */
    if (X->do_skip && !X->passthru) {
	code = (X->do_skip) (X, count);
	if (code)
	    return code;
	add64_32(tmp64, X->filepos, count);
	cp64(X->filepos, tmp64);
	return 0;
    }

    /* Simulate using absolute seek, if available */
    if (X->do_seek && !X->passthru) {
	if ((code = xftell(X, &tmp64)))
	    return code;
	add64_32(X->filepos, tmp64, count);
	cp64(tmp64, X->filepos);
	return xfseek(X, &tmp64);
    }

    /* Do it the hard/slow way - read all the data to be skipped.
     * This is done if no other method is available, or if we are
     * supposed to be copying all the data to another XFILE
     */
    {
	char buf[SKIP_SIZE];
	afs_uint32 n;

	while (count) {
	    n = (count > SKIP_SIZE) ? SKIP_SIZE : count;
	    if ((code = xfread(X, buf, n)))
		return code;
	    count -= n;
	}
	return 0;
    }
}
Ejemplo n.º 11
0
afs_uint32
try_backuphdr(XFILE * X, char *tag, tagged_field * field, afs_uint32 value,
	      tag_parse_info * pi, void *g_refcon, void *l_refcon)
{
    dump_parser *p = (dump_parser *) g_refcon;
    backup_system_header bh;
    u_int64 where;
    afs_uint32 r;

    /* Which header should we try (if any)? */
    switch (*tag) {
    case STAGE_VERSMIN:
      r = ParseStageHdr(X, (unsigned char *)tag, &bh);
	break;
    default:
	return DSERR_MAGIC;
    }
    if (r)
	return r;

    /* Do something with it... */
    if (p->print_flags & DSPRINT_BCKHDR)
	PrintBackupHdr(&bh);
    if (p->cb_bckhdr) {
	r = xftell(X, &where);
	if (!r && p->cb_bckhdr)
	    r = (p->cb_bckhdr) (&bh, X, p->refcon);
	if (p->flags & DSFLAG_SEEK) {
	    if (!r)
		r = xfseek(X, &where);
	    else
		xfseek(X, &where);
	}
    }
    if (bh.server)
	free(bh.server);
    if (bh.part)
	free(bh.part);
    if (bh.volname)
	free(bh.volname);
    return r;
}
Ejemplo n.º 12
0
Archivo: port.c Proyecto: KeenS/benz
static pic_value
pic_port_get_output_bytevector(pic_state *pic)
{
  struct pic_port *port = pic_stdout(pic);
  pic_blob *blob;
  size_t size;

  pic_get_args(pic, "|p", &port);

  assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_BINARY, PIC_PORT_OPEN, "get-output-bytevector");

  /* get endpos */
  xfflush(port->file);
  size = (size_t)xftell(port->file);
  xrewind(port->file);

  /* copy to buf */
  blob = pic_make_blob(pic, size);
  xfread(blob->data, 1, size, port->file);

  return pic_obj_value(blob);
}
Ejemplo n.º 13
0
/* Parse a file containing tagged data and attributes **/
afs_uint32
ParseTaggedData(XFILE * X, tagged_field * fields, unsigned char *tag,
		tag_parse_info * pi, void *g_refcon, void *l_refcon)
{
    int i = -1;
    afs_uint32 r, val;
    afs_uint16 val16;
    unsigned char val8;
    unsigned char *strval;

    for (;;) {
	if (i < 0 || (fields[i].kind & DKIND_MASK) != DKIND_SPECIAL) {
	    /* Need to read in a tag */
	    if ((r = ReadByte(X, tag)))
		return r;
	}

	/* Simple error recovery - if we encounter a 0, it can never be
	 * a valid tag.  If TPFLAG_SKIP is set, we can skip over any
	 * such null bytes, and process whatever tag we find beyond.
	 * In addition, if TPFLAG_RSKIP is set, then the next time
	 * we encounter a 0, try skipping backwards.  That seems to
	 * work much of the time.
	 */
	if (!*tag && pi->shift_offset && (pi->flags & TPFLAG_RSKIP)) {
	    u_int64 where, tmp64a, tmp64b;
	    char buf1[21], buf2[21], buf3[21];
	    char *p1, *p2, *p3;

	    if ((r = xftell(X, &tmp64a)))
		return r;
	    sub64_32(where, tmp64a, pi->shift_offset + 1);
	    if ((r = xfseek(X, &where)))
		return r;
	    if (pi->cb_error) {
		(pi->cb_error) (DSERR_FMT, 0, pi->err_refcon,
				"Inserted %d bytes before offset %d",
				pi->shift_offset, decimate_int64(&where, 0));
		add64_32(tmp64a, pi->shift_start, pi->shift_offset);
		p1 = decimate_int64(&tmp64a, buf1);
		sub64_64(tmp64b, where, tmp64a);
		p2 = decimate_int64(&tmp64b, buf2);
		p3 = decimate_int64(&pi->shift_start, buf3);
		(pi->cb_error) (DSERR_FMT, 0, pi->err_refcon,
				">>> SHIFT start=%s length=%s target=%s", p1,
				p2, p3);
	    }
	    pi->shift_offset = 0;
	    if ((r = ReadByte(X, tag)))
		return r;
	}
	if (!*tag && (pi->flags & TPFLAG_SKIP)) {
	    int count = 0;
	    u_int64 where, tmp64a;

	    if ((r = xftell(X, &where)))
		return r;

	    while (!*tag) {
		if ((r = ReadByte(X, tag)))
		    return r;
		count++;
	    }
	    pi->shift_offset += count;
	    cp64(pi->shift_start, where);
	    if (pi->cb_error) {
		sub64_32(tmp64a, where, 1);
		(pi->cb_error) (DSERR_FMT, 0, pi->err_refcon,
				"Skipped %d bytes at offset %s", count,
				decimate_int64(&tmp64a, 0));
	    }
	}

	for (i = 0; fields[i].tag && fields[i].tag != *tag; i++);
	if (!fields[i].tag)
	    return 0;

	switch (fields[i].kind & DKIND_MASK) {
	case DKIND_NOOP:
	    if (fields[i].func) {
		r = (fields[i].func) (X, 0, fields + i, 0, pi, g_refcon,
				      l_refcon);
		if (r)
		    return r;
	    }
	    break;

	case DKIND_BYTE:
	    if ((r = ReadByte(X, &val8)))
		return r;
	    if (fields[i].func) {
		r = (fields[i].func) (X, 0, fields + i, val8, pi, g_refcon,
				      l_refcon);
		if (r)
		    return r;
	    }
	    break;

	case DKIND_INT16:
	    if ((r = ReadInt16(X, &val16)))
		return r;
	    if (fields[i].func) {
		r = (fields[i].func) (X, 0, fields + i, val16, pi, g_refcon,
				      l_refcon);
		if (r)
		    return r;
	    }
	    break;

	case DKIND_INT32:
	    if ((r = ReadInt32(X, &val)))
		return r;
	    if (fields[i].func) {
		r = (fields[i].func) (X, 0, fields + i, val, pi, g_refcon,
				      l_refcon);
		if (r)
		    return r;
	    }
	    break;

	case DKIND_STRING:
	    if ((r = ReadString(X, &strval)))
		return r;
	    if (fields[i].func) {
		r = (fields[i].func) (X, strval, fields + i, 0, pi, g_refcon,
				      l_refcon);
		if (r != DSERR_KEEP)
		    free(strval);
		if (r && r != DSERR_KEEP)
		    return r;
	    } else
		free(strval);
	    break;

	case DKIND_SPECIAL:
	    if (fields[i].func) {
		r = (fields[i].func) (X, tag, fields + i, 0, pi, g_refcon,
				      l_refcon);
		if (r)
		    return r;
	    } else
		i = -1;
	}
    }
}
Ejemplo n.º 14
0
/* Parse or skip over the vnode data */
static afs_uint32
parse_vdata(XFILE * X, unsigned char *tag, tagged_field * field,
	    afs_uint32 value, tag_parse_info * pi, void *g_refcon,
	    void *l_refcon)
{
    dump_parser *p = (dump_parser *) g_refcon;
    afs_vnode *v = (afs_vnode *) l_refcon;
    static char *symlink_buf = 0;
    static int symlink_size = 0;
    afs_uint32 r;

    if ((r = ReadInt32(X, &v->size)))
	return r;
    v->field_mask |= F_VNODE_SIZE;

    if (v->size) {
	v->field_mask |= F_VNODE_DATA;
	if ((r = xftell(X, &v->d_offset)))
	    return r;
	if (p->print_flags & DSPRINT_VNODE)
	    printf("%s%d (0x%08x) bytes at %s (0x%s)\n", field->label,
		   v->size, v->size, decimate_int64(&v->d_offset, 0),
		   hexify_int64(&v->d_offset, 0));

	switch (v->type) {
	case vSymlink:
	    if (v->size > symlink_size) {
		if (symlink_buf)
		    symlink_buf = realloc(symlink_buf, v->size + 1);
		else
		    symlink_buf = (char *)malloc(v->size + 1);
		symlink_size = symlink_buf ? v->size : 0;
	    }
	    if (symlink_buf) {
		if ((r = xfread(X, symlink_buf, v->size)))
		    return r;
		symlink_buf[v->size] = 0;
		if (p->print_flags & DSPRINT_VNODE)
		    printf(" Target:       %s\n", symlink_buf);
	    } else {
		/* Call the callback here, because it's non-fatal */
		if (p->cb_error)
		    (p->cb_error) (ENOMEM, 0, p->err_refcon,
				   "Out of memory reading symlink");
		if ((r = xfskip(X, v->size)))
		    return r;
	    }
	    break;

	case vDirectory:
	    if (p->cb_dirent || (p->print_flags & DSPRINT_DIR)) {
		if ((r = parse_directory(X, p, v, v->size, 0)))
		    return r;
		break;
	    }

	default:
	    if ((r = xfskip(X, v->size)))
		return r;
	}
    } else if (p->print_flags & DSPRINT_VNODE) {
	printf("%sEmpty\n", field->label);
    }
    if (p->repair_flags & DSFIX_VDSYNC) {
	r = resync_vnode(X, p, v, 10, 15);
	if (r)
	    return r;
    }
    return ReadByte(X, tag);
}
Ejemplo n.º 15
0
/* Parse a VNode, including any tagged attributes and data, and call the
 * appropriate callback, if one is defined.
 */
afs_uint32
parse_vnode(XFILE * X, unsigned char *tag, tagged_field * field,
	    afs_uint32 value, tag_parse_info * pi, void *g_refcon,
	    void *l_refcon)
{
    dump_parser *p = (dump_parser *) g_refcon;
    afs_uint32(*cb) (afs_vnode *, XFILE *, void *);
    dt_uint64 where, offset2k;
    afs_vnode v;
    afs_uint32 r;


    if ((r = xftell(X, &where)))
	return r;
    memset(&v, 0, sizeof(v));
    sub64_32(v.offset, where, 1);
    if ((r = ReadInt32(X, &v.vnode)))
	return r;
    if ((r = ReadInt32(X, &v.vuniq)))
	return r;

    mk64(offset2k, 0, 2048);
    if (!LastGoodVNode
	|| ((p->flags & DSFLAG_SEEK) && v.vnode == 1
	    && lt64(v.offset, offset2k)))
	LastGoodVNode = -1;

    if (p->print_flags & DSPRINT_ITEM) {
	printf("%s %d/%d [%s = 0x%s]\n", field->label, v.vnode, v.vuniq,
	       decimate_int64(&where, 0), hexify_int64(&where, 0));
    }

    r = ParseTaggedData(X, vnode_fields, tag, pi, g_refcon, (void *)&v);

    /* Try to resync, if requested */
    if (!r && (p->repair_flags & DSFIX_VFSYNC)) {
	afs_uint32 drop;
	dt_uint64 xwhere;

	if ((r = xftell(X, &where)))
	    return r;
	sub64_32(xwhere, where, 1);

	/* Are we at the start of a valid vnode (or dump end)? */
	r = match_next_vnode(X, p, &xwhere, v.vnode);
	if (r && r != DSERR_FMT)
	    return r;
	if (r) {		/* Nope. */
	    /* Was _this_ a valid vnode?  If so, we can keep it and search for
	     * the next one.  Otherwise, we throw it out, and start the search
	     * at the starting point of this vnode.
	     */
	    drop = r = match_next_vnode(X, p, &v.offset, LastGoodVNode);
	    if (r && r != DSERR_FMT)
		return r;
	    if (!r) {
		add64_32(where, v.offset, 1);
		if ((r = xfseek(X, &v.offset)))
		    return r;
	    } else {
		if ((r = xfseek(X, &xwhere)))
		    return r;
	    }
	    if ((r = resync_vnode(X, p, &v, 0, 1024)))
		return r;
	    if ((r = ReadByte(X, tag)))
		return r;
	    if (drop) {
		if (p->cb_error)
		    (p->cb_error) (DSERR_FMT, 0, p->err_refcon,
				   "Dropping vnode %d", v.vnode);
		return 0;
	    }
	} else {
	    if ((r = xfseek(X, &where)))
		return r;
	}
    }
    LastGoodVNode = v.vnode;

    if (!r) {
	if (v.field_mask & F_VNODE_TYPE)
	    switch (v.type) {
	    case vFile:
		cb = p->cb_vnode_file;
		break;
	    case vDirectory:
		cb = p->cb_vnode_dir;
		break;
	    case vSymlink:
		cb = p->cb_vnode_link;
		break;
	    default:
		cb = p->cb_vnode_wierd;
		break;
	} else
	    cb = p->cb_vnode_empty;
	if (cb) {
	    dt_uint64 where;

	    if ((r = xftell(X, &where)))
		return r;
	    r = (cb) (&v, X, p->refcon);
	    if (p->flags & DSFLAG_SEEK) {
		if (!r)
		    r = xfseek(X, &where);
		else
		    xfseek(X, &where);
	    }
	}
    }
    return r;
}
Ejemplo n.º 16
0
void read_jpg_info(integer img)
{
    int i, units = 0;
    unsigned char jpg_id[] = "JFIF";
    img_xres(img) = img_yres(img) = 0;
    jpg_ptr(img)->file = xfopen(img_name(img), FOPEN_RBIN_MODE);
    /* no LFS needed, as JPEG is limited to <4GiB */
    xfseek(jpg_ptr(img)->file, 0, SEEK_END, cur_file_name);
    jpg_ptr(img)->length = xftell(jpg_ptr(img)->file, cur_file_name);
    xfseek(jpg_ptr(img)->file, 0, SEEK_SET, cur_file_name);
    if (read2bytes(jpg_ptr(img)->file) != 0xFFD8)
        pdftex_fail("reading JPEG image failed (no JPEG header found)");
    /* currently only true JFIF files allow extracting img_xres and img_yres */
    if (read2bytes(jpg_ptr(img)->file) == 0xFFE0) {     /* check for JFIF */
        (void) read2bytes(jpg_ptr(img)->file);
        for (i = 0; i < 5; i++) {
            if (xgetc(jpg_ptr(img)->file) != jpg_id[i])
                break;
        }
        if (i == 5) {           /* it's JFIF */
            read2bytes(jpg_ptr(img)->file);
            units = xgetc(jpg_ptr(img)->file);
            img_xres(img) = read2bytes(jpg_ptr(img)->file);
            img_yres(img) = read2bytes(jpg_ptr(img)->file);
            switch (units) {
            case 1:
                break;          /* pixels per inch */
            case 2:
                img_xres(img) *= 2.54;
                img_yres(img) *= 2.54;
                break;          /* pixels per cm */
            default:
                img_xres(img) = img_yres(img) = 0;
                break;
            }
        }
    }
    xfseek(jpg_ptr(img)->file, 0, SEEK_SET, cur_file_name);
    while (1) {
        if (feof(jpg_ptr(img)->file))
            pdftex_fail("reading JPEG image failed (premature file end)");
        if (fgetc(jpg_ptr(img)->file) != 0xFF)
            pdftex_fail("reading JPEG image failed (no marker found)");
        switch (xgetc(jpg_ptr(img)->file)) {
        case M_SOF5:
        case M_SOF6:
        case M_SOF7:
        case M_SOF9:
        case M_SOF10:
        case M_SOF11:
        case M_SOF13:
        case M_SOF14:
        case M_SOF15:
            pdftex_fail("unsupported type of compression");
        case M_SOF2:
            if (fixedpdfminorversion <= 2)
                pdftex_fail("cannot use progressive DCT with PDF-1.2");
        case M_SOF0:
        case M_SOF1:
        case M_SOF3:
            (void) read2bytes(jpg_ptr(img)->file);      /* read segment length  */
            jpg_ptr(img)->bits_per_component = xgetc(jpg_ptr(img)->file);
            img_height(img) = read2bytes(jpg_ptr(img)->file);
            img_width(img) = read2bytes(jpg_ptr(img)->file);
            jpg_ptr(img)->color_space = xgetc(jpg_ptr(img)->file);
            xfseek(jpg_ptr(img)->file, 0, SEEK_SET, cur_file_name);
            switch (jpg_ptr(img)->color_space) {
            case JPG_GRAY:
                img_color(img) = IMAGE_COLOR_B;
                break;
            case JPG_RGB:
                img_color(img) = IMAGE_COLOR_C;
                break;
            case JPG_CMYK:
                img_color(img) = IMAGE_COLOR_C;
                break;
            default:
                pdftex_fail("Unsupported color space %i",
                            (int) jpg_ptr(img)->color_space);
            }
            return;
        case M_SOI:            /* ignore markers without parameters */
        case M_EOI:
        case M_TEM:
        case M_RST0:
        case M_RST1:
        case M_RST2:
        case M_RST3:
        case M_RST4:
        case M_RST5:
        case M_RST6:
        case M_RST7:
            break;
        default:               /* skip variable length markers */
            xfseek(jpg_ptr(img)->file, read2bytes(jpg_ptr(img)->file) - 2,
                   SEEK_CUR, cur_file_name);
            break;
        }
    }
}
Ejemplo n.º 17
0
/* Main program */
int
main(int argc, char **argv)
{
    XFILE input_file;
    afs_uint32 r;

    parse_options(argc, argv);
    initialize_acfg_error_table();
    initialize_AVds_error_table();
    initialize_rxk_error_table();
    initialize_u_error_table();
    initialize_vl_error_table();
    initialize_vols_error_table();
    initialize_xFil_error_table();
    r = xfopen(&input_file, O_RDONLY, input_path);
    if (r) {
	afs_com_err(argv0, r, "opening %s", input_path);
	exit(2);
    }

    memset(&dp, 0, sizeof(dp));
    dp.cb_error = my_error_cb;
    if (input_file.is_seekable)
	dp.flags |= DSFLAG_SEEK;
    dirs_done = 0;

    if (!use_vnum) {
	dt_uint64 where;

	memset(&phi, 0, sizeof(phi));
	phi.p = &dp;

	if (verbose)
	    printf("* Building pathname info...\n");
	if ((r = xftell(&input_file, &where))
	    || (r = Path_PreScan(&input_file, &phi, 1))
	    || (r = xfseek(&input_file, &where))) {
	    afs_com_err(argv0, r, "- path initialization failed");
	    xfclose(&input_file);
	    exit(1);
	}
    }

    dp.cb_vnode_dir = directory_cb;
    dp.cb_vnode_file = file_cb;
    dp.cb_vnode_link = symlink_cb;
    dp.cb_vnode_empty = lose_cb;
    dp.cb_vnode_wierd = lose_cb;
    if (do_headers) {
	dp.cb_dumphdr = dumphdr_cb;
	dp.cb_volhdr = volhdr_cb;
    }

    if (!nomode) {
	mkdir(target, 0755);
	if (chdir(target)) {
	    fprintf(stderr, "chdir %s failed: %s\n", target, strerror(errno));
	    exit(1);
	}
    }
    r = ParseDumpFile(&input_file, &dp);

    if (verbose && error_count)
	fprintf(stderr, "*** %d errors\n", error_count);
    if (r && !quiet)
	fprintf(stderr, "*** FAILED: %s\n", afs_error_message(r));
    exit(0);
}
Ejemplo n.º 18
0
static afs_uint32
symlink_cb(afs_vnode * v, XFILE * X, void *refcon)
{
    char *vnodepath, *linktarget, vnpx[30];
    dt_uint64 where;
    int r, use = 0;

    if (!dirs_done) {
	dirs_done = 1;
	if (verbose)
	    printf("* Extracting files...\n");
    }

    /* Should we even use this? */
    if (!use_vnum) {
	if ((r = Path_Build(X, &phi, v->vnode, &vnodepath, !use_realpath)))
	    return r;
	if (!(use = usevnode(X, v->vnode, vnodepath))) {
	    free(vnodepath);
	    return 0;
	}
	if (use == 2) {
	    free(vnodepath);
	    sprintf(vnpx, "#%d:%d", v->vnode, v->vuniq);
	    vnodepath = vnpx;
	}
    } else {
	sprintf(vnpx, "#%d:%d", v->vnode, v->vuniq);
	vnodepath = vnpx;
    }

    if (!(linktarget = malloc(v->size + 1))) {
	if (vnodepath != vnpx)
	    free(vnodepath);
	return DSERR_MEM;
    }
    if ((r = xftell(X, &where))
	|| (r = xfseek(X, &v->d_offset))
	|| (r = xfread(X, linktarget, v->size))) {
	if (vnodepath != vnpx)
	    free(vnodepath);
	free(linktarget);
	return r;
    }
    xfseek(X, &where);
    linktarget[v->size] = 0;

    /* Print it out */
    if (verbose)
	printf("l%s %3d %-11d %11d %s %s -> %s\n", modestr(v->mode),
	       v->nlinks, v->owner, v->size, datestr(v->server_date),
	       vnodepath, linktarget);
    else if (!quiet)
	printf("%s\n", vnodepath);

    r = 0;
    if (!nomode) {
	if (symlink(linktarget, vnodepath + 1))
	    r = errno;
    }

    free(linktarget);
    if (vnodepath != vnpx)
	free(vnodepath);
    return r;
}
Ejemplo n.º 19
0
/* Main program */
void
main(int argc, char **argv)
{
    XFILE *X;
    afs_uint32 r;

    parse_options(argc, argv);
    initialize_UB_error_table();
    initialize_UBsp_error_table();
    initialize_AVds_error_table();
    r = xfopen(&X, input_path, O_RDONLY, 0);
    if (r) {
	afs_com_err(argv0, r, "opening %s", input_path);
	exit(2);
    }

    bzero(&dp, sizeof(dp));
    dp.cb_error = my_error_cb;
    dp.repair_flags = repairflags;
    if (X->is_seekable)
	dp.flags |= DSFLAG_SEEK;
    else {
	if (repairflags)
	    fprintf(stderr,
		    "Repair modes available only for seekable dumps\n");
	if (printflags & DSPRINT_PATH)
	    fprintf(stderr,
		    "Path-printing available only for seekable dumps\n");
	if (repairflags || (printflags & DSPRINT_PATH))
	    exit(1);
    }

    if (gendump_path && (r = setup_repair())) {
	afs_com_err(argv0, r, "setting up repair output");
	xfclose(X);
	exit(2);
    }

    if (printflags & DSPRINT_PATH) {
	u_int64 where;

	dp.print_flags = printflags & DSPRINT_DEBUG;
	bzero(&phi, sizeof(phi));
	phi.p = &dp;

	if ((r = xftell(X, &where))
	    || (r = Path_PreScan(X, &phi, 0))
	    || (r = xfseek(X, &where))) {
	    afs_com_err(argv0, r, "- path initialization failed");
	    xfclose(X);
	    exit(2);
	}

	dp.cb_vnode_dir = print_vnode_path;
	dp.cb_vnode_file = print_vnode_path;
	dp.cb_vnode_link = print_vnode_path;
	dp.cb_vnode_empty = print_vnode_path;
	dp.cb_vnode_wierd = print_vnode_path;
    }

    if (add_admin) {
	dp.cb_vnode_dir = munge_admin_acl;
    }

    dp.print_flags = printflags;
    r = ParseDumpFile(X, &dp);
    if (gendump_path) {
	if (!r)
	    r = DumpDumpEnd(&repair_output);
	if (!r)
	    r = xfclose(&repair_output);
	else
	    xfclose(&repair_output);
    }

    if (verbose && error_count)
	fprintf(stderr, "*** %d errors\n", error_count);
    if (r && !quiet)
	fprintf(stderr, "*** FAILED: %s\n", afs_error_message(r));
    exit(0);
}
Ejemplo n.º 20
0
/* Parse a stage backup header.
 * If tag is non-NULL, *tag should contain the first byte (already read),
 * and will be filled in with the first byte after the header, if one exists.
 * On success, returns 0 and leaves us positioned after the header
 * On failure, returns an error and position is undefined
 * Iff there is no header, returns DSERR_MAGIC and leaves us
 * positioned where we started.
 */
afs_uint32
ParseStageHdr(XFILE * X, unsigned char *tag, backup_system_header * hdr)
{
    char buf[STAGE_HDRLEN];
    struct stage_header *bckhdr = (struct stage_header *)buf;
    u_int64 where;
    afs_int32 r;

    if ((r = xftell(X, &where)))
	return r;
    if (hdr)
	memset(hdr, 0, sizeof(*hdr));
    if (tag) {
	if (*tag != STAGE_VERSMIN)
	    return DSERR_MAGIC;
	buf[0] = *tag;
	r = xfread(X, buf + 1, STAGE_HDRLEN - 1);
    } else {
	r = xfread(X, buf, STAGE_HDRLEN);
    }

    if (r == ERROR_XFILE_EOF) {
	r = xfseek(X, &where);
	return r ? r : DSERR_MAGIC;
    } else if (r)
	return r;

    if (bckhdr->c_vers < STAGE_VERSMIN
	|| ntohl(bckhdr->c_magic) != STAGE_MAGIC
	|| hdr_checksum(buf, STAGE_HDRLEN) != STAGE_CHECKSUM) {
	r = xfseek(X, &where);
	return r ? r : DSERR_MAGIC;
    }

    if (hdr) {
	hdr->version = bckhdr->c_vers;
	hdr->from_date = ntohl(bckhdr->c_fdate);
	hdr->to_date = ntohl(bckhdr->c_tdate);
	hdr->dump_date = ntohl(bckhdr->c_time);
	hdr->filenum = ntohl(bckhdr->c_filenum);
	hdr->volid = ntohl(bckhdr->c_id);
#ifdef NATIVE_INT64
	hdr->dumplen = ntohl(bckhdr->c_length);
#else
        hdr->dumplen.hi = 0;
        hdr->dumplen.lo = ntohl(bckhdr->c_length);
#endif
	hdr->level = ntohl(bckhdr->c_level);
	hdr->magic = ntohl(bckhdr->c_magic);
	hdr->cksum = ntohl(bckhdr->c_checksum);
	hdr->flags = ntohl(bckhdr->c_flags);
	hdr->server = malloc(strlen(bckhdr->c_host) + 1);
	hdr->part = malloc(strlen(bckhdr->c_disk) + 1);
	hdr->volname = malloc(strlen(bckhdr->c_name) + 1);

	if (!hdr->server || !hdr->part || !hdr->volname) {
	    if (hdr->server)
		free(hdr->server);
	    if (hdr->part)
		free(hdr->part);
	    if (hdr->volname)
		free(hdr->volname);
	    return ENOMEM;
	}
	strcpy((char *)hdr->server, bckhdr->c_host);
	strcpy((char *)hdr->part, bckhdr->c_disk);
	strcpy((char *)hdr->volname, bckhdr->c_name);
    }

    if (tag)
	return ReadByte(X, tag);
    else
	return 0;
}