Esempio n. 1
0
static int afm_close(AFFILE *af)
{
    struct afm_private *ap = AFM_PRIVATE(af);
    if(ap){
	if(ap->sr)  af_close(ap->sr);		// close the split files
	if(ap->aff) af_close(ap->aff);		// and close the AFF file
	memset(ap,0,sizeof(*ap));
	free(ap);
    }
    return 0;
}
Esempio n. 2
0
static void
aff_close(TSK_IMG_INFO * img_info)
{
    IMG_AFF_INFO *aff_info = (IMG_AFF_INFO *) img_info;
    af_close(aff_info->af_file);
    tsk_img_free(aff_info);
}
Esempio n. 3
0
void bugs_test()
{
    for(int i=0;fnames[i];i+=2){
	char buf[256];
	strcpy(buf,fnames[i]);
	if(split_raw_increment_fname(buf)){
	    err(1,"split_raw_increment_fname(%s) failed",fnames[i]);
	}
	printf("%s=>%s\n",fnames[i],buf);
	if(strcmp(buf,fnames[i+1])!=0){
	    err(1,"split_raw_increment_fname(%s) should have returned %s",
		fnames[i],fnames[i+1]);
	}
    }


    const char *buf = "This is a test\n";
    int len = strlen(buf);

    AFFILE *af = af_open("bugs.aff",O_RDWR|O_CREAT|O_TRUNC,0666);
    if(!af) err(1,"bugs.aff");
    int r = af_write(af,(unsigned char *)buf,strlen(buf));
    if(r!=len) err(1,"r=%d len=%d\n",r,len);
    af_close(af);
}
Esempio n. 4
0
static void
affuse_destroy(void* param)
{
    af_close(af_image);
    XFREE(raw_path);
    return;
}
Esempio n. 5
0
int reverse_test()
{
    char wbuf[1024];
    char rbuf[1024];

    printf("Reverse write test...\n");
    for(int pass=1;pass<=2;pass++){

	AFFILE *af = open_testfile("test_reverse",pass==1);
	for(int i=MAX_FMTS-1;i>=0;i--){
	    sprintf(wbuf,fmt,i);
	    af_seek(af,strlen(wbuf)*i,SEEK_SET);
	    if(pass==1){
		if(af_write(af,(unsigned char *)wbuf,strlen(wbuf))!=(int)strlen(wbuf)){
		    err(1,"Attempt to write buffer %d failed\n",i);
		}
	    }
	    if(pass==2){
		memset(rbuf,0,sizeof(rbuf));
		if(af_read(af,(unsigned char *)rbuf,strlen(wbuf))!=(int)strlen(wbuf)){
		    err(1,"Attempt to read buffer %d failed\n",i);
		}
		if(strcmp(rbuf,wbuf)!=0){
		    errx(1,"Data doesn't verify.\nWrote: '%s'\nRead: '%s'\n",wbuf,rbuf);
		}
	    }
	}
	af_close(af);
    }

    printf("\nReverse test passes.\n");
    printf("======================\n\n");
    return 0;
}
Esempio n. 6
0
int sequential_test()
{
    char buf[1024];
    const char *fmt = "this is line %d\n";

    printf("Sequential test...\n");
    
    AFFILE *af = open_testfile("test_sequential",1);
    for(int i=0;i<MAX_FMTS;i++){
	if(i%250==0) printf("\rwriting %d/%d...",i,MAX_FMTS);
	sprintf(buf,fmt,i);
	if(af_write(af,(unsigned char *)buf,strlen(buf))!=(int)strlen(buf)){
	    err(1,"Attempt to write buffer %d failed\n",i);
	}
    }
    /* Test for a random bug that was reported */
    af_update_seg(af,"test",0,(const u_char *)"foo",3);
    af_update_seg(af,"test",0,(const u_char *)"bar",3);
    af_del_seg(af,"test");
    af_del_seg(af,"test");
    af_close(af);

    printf("\nSequential file written.\n");
    printf("\n");
    printf("Now verifying the string...\n");
    af = open_testfile("test_sequential",0);
    if(!af) err(1,"af_open");
    for(int i=0;i<MAX_FMTS;i++){
	char rbuf[1024];
	sprintf(buf,fmt,i);
	int len = strlen(buf);
	if(af_read(af,(unsigned char *)rbuf,len)!=len){
	    err(1,"Attempt to read entry %d failed\n",i);
	}
	rbuf[len] = 0;			// terminate the string
	if(strcmp(buf,rbuf)!=0){
	    err(1,"Attempt to verify entry %d failed.\nExpected: (len=%zd) '%s'\nGot: (len=%zd) '%s'\n",
		i,strlen(buf),buf,strlen(rbuf),rbuf);
	}
    }
    af_close(af);

    printf("===========================\n\n");
    return 0;
}
Esempio n. 7
0
int split_raw_test(const char *fn)
{
    void srp_dump(AFFILE *af);
    AFFILE *af = af_open(fn,O_RDONLY,0666);
    printf("split_raw imagesize: %"PRId64"\n",af_get_imagesize(af));
    srp_dump(af);
    af_close(af);
    return 0;
}
Esempio n. 8
0
void zap(const char *fn)
{
    unsigned char buf[1024*1024];
    AFFILE *af = af_open(fn,O_RDWR,0666);
    if(!af) err(1,"af_open(%s)",fn);
    memset(buf,0,sizeof(buf));
    if(af_write(af,buf,sizeof(buf))!=sizeof(buf)){
	err(1,"af_write()");
    }
    af_close(af);
}
Esempio n. 9
0
static int evd_close(AFFILE *af)
{
    struct evd_private *ed = EVD_PRIVATE(af);

    /* Close all of the subfiles, then free the memory, then close this file */
    for(unsigned int i=0;i<ed->num_evfs;i++){
	af_close(ed->evfs[i]);
    }
    free(ed->evfs);
    memset(ed,0,sizeof(*ed));		// clean object reuse
    free(ed);				// won't need it again
    return 0;
}
Esempio n. 10
0
static int afd_close(AFFILE *af)
{
    struct afd_private *ap = AFD_PRIVATE(af);

    /* Close all of the subfiles, then free the memory, then close this file */
    for(int i=0;i<ap->num_afs;i++){
	ap->afs[i]->image_size = af->image_size; // set each to have correct imagesize
	af_close(ap->afs[i]);		// and close each file
    }
    free(ap->afs);
    memset(ap,0,sizeof(*ap));		// clean object reuse
    free(ap);				// won't need it again
    return 0;
}
Esempio n. 11
0
void large_file_test()
{
    int pagesize = 1024*1024;		// megabyte sized segments
    int64_t num_segments = 5000;
    int64_t i;
    char fn[1024];

    printf("Large file test... Creating a %"I64d"MB file...\n",pagesize*num_segments/(1024*1024));
    filename(fn,sizeof(fn),"large_file");
    AFFILE *af = af_open(fn,O_CREAT|O_RDWR|O_TRUNC,0666);

    unsigned char *buf = (unsigned char *)malloc(pagesize);

    memset(buf,'E', pagesize);
    af_enable_compression(af,opt_compression_type,opt_compression_level);
    af_set_pagesize(af,pagesize);
    af_set_maxsize(af,(int64_t)pagesize * 600);

    for(i=0;i<num_segments;i++){
	sprintf((char *)buf,"%"I64d" page is put here",i);
	if(i%25==0) printf("\rWriting page %"I64d"\r",i);
	if(af_write(af,buf,pagesize)!=pagesize){
	    err(1,"Can't write page %"I64d,i);
	}
    }
    printf("\n\n");
    /* Now let's just read some test locations */
    for(i=0;i<num_segments;i+=num_segments/25){	// check a few places
	int r;
	af_seek(af,pagesize*i,SEEK_SET);
	r = af_read(af,buf,1024);		// just read a bit
	if(r!=1024){
	    err(1,"Tried to read 1024 bytes; got %d\n",r);
	}
	if(atoi((char *)buf)!=i){
	    err(1,"at page %"I64d", expected %"I64d", got %s\n",i,i,buf);
	}
	printf("Page %"I64d" validates\n",i);
    }

    af_close(af);
    if(unlink("large_file.aff")){
	err(1,"Can't delete large_file.aff");
    }
    printf("Large file test passes\n");
}
Esempio n. 12
0
void readfile_test(const char *fname)
{
    unsigned char buf[1024];
    memset(buf,0,sizeof(buf));
    AFFILE *af = af_open(fname,O_RDONLY,0666);
    if(!af){
	af_perror(fname);
	err(1,"af_open(%s)",fname);
    }
    printf("using '%s'\n",af->v->name);
    printf("af_get_imagesize()=%"PRId64" errno=%d\n",af_get_imagesize(af),errno);

    int r = af_read(af,buf,sizeof(buf));
    printf("af_read(af,buf,1024)=%d  errno=%d\n",r,errno);
    r = fwrite(buf,1,512,stdout);
    assert(r==512);
    af_close(af);
    exit(0);
}
Esempio n. 13
0
void maxsize_test()
{
    printf("Maxsize test. This test is designed to test creation of files\n");
    printf("Larger than 4GB. Currently it's disabled, though.\n");
#if 0
    char segname[16];
    char buf[1024];
    char fn[1024];
    int numpages = 1000;

    AFFILE *af = af_open(filename(fn,sizeof(fn),"maxsize"),O_CREAT|O_RDWR|O_TRUNC,0666);
    memset(buf,0,sizeof(buf));
    for(int64_t i=0;i<numpages;i++){
	sprintf(buf,"This is page %"I64d". ****************************************************\n",i);
	sprintf(segname,AF_PAGE,i);
	af_update_seg(af,segname,0,buf,sizeof(buf));
    }
    af_close(af);
    printf("\nMaxsize test passes.\n");
#endif
    printf("\n====================\n");
}
Esempio n. 14
0
File: aff.c Progetto: anhkgg/temu
void
aff_close(IMG_INFO * img_info)
{
    IMG_AFF_INFO *aff_info = (IMG_AFF_INFO *) img_info;
    af_close(aff_info->af_file);
}
Esempio n. 15
0
int random_write_test()
{
    char buf[1024];
    char *tally = (char *)calloc(MAX_FMTS,1);
    int i;

    memset(tally,0,sizeof(tally));

    /* Create the AFF file */
    sprintf(buf,fmt,0);		// figure out how big fmt string is
    int fmt_size = strlen(buf);

    printf("Random write test...\n");
    printf("Creating test file with  %d byte records.\n", fmt_size);

    AFFILE *af = open_testfile("test_random",1);

    if(af_write(af,(unsigned char *)buf,fmt_size)!=fmt_size){
	err(1,"af_write");
    }
    for(i=0;i<MAX_FMTS;i++){
	/* Find a random spot that's available */
	int pos = rand() % MAX_FMTS;
	while(tally[pos]==1){		//  if this one is used, find next
	    pos = (pos + 1) % MAX_FMTS;
	}
	tally[pos] = 1;
	sprintf(buf,fmt,pos);
	assert((int)strlen(buf)==fmt_size);	// make sure
	af_seek(af,fmt_size*pos,SEEK_SET);
	int wrote = af_write(af,(unsigned char *)buf,fmt_size);
	if(wrote !=fmt_size){
	    fprintf(stderr,"Attempt to write buffer #%d \n",pos);
	    fprintf(stderr,"wrote %d bytes instead of %d bytes\n",wrote,fmt_size);
	    exit(1);
	}
	if(i%250==0) printf("\r%d ...",i);
	fflush(stdout);
    }
    af_close(af);
    
    /* Now verify what was written */
    printf("Verifying write test...\n");
    af = open_testfile("test_random",0);
    
    for(i=0;i<MAX_FMTS;i++){
	char should[256];		// what we should get
	sprintf(should,fmt,i);
	int got = af_read(af,(unsigned char *)buf,fmt_size);
	if(got != fmt_size){
	    fprintf(stderr,"Attempt to read %d bytes; got %d\n",fmt_size,got);
	    exit(1);
	}
	if(i%250==24) printf("\r%d .. %d okay",i-24,i);
    }
    af_close(af);
    printf("\n");
    printf("\nRandom write test passes.\n");
    printf("======================\n");
    return 0;
}
Esempio n. 16
0
static PyObject *
affile_close(affile *self) {
  af_close(self->af);
  Py_RETURN_NONE;
}
Esempio n. 17
0
Attributes	AffNode::_attributes()
{
    Attributes 	vmap;
    struct af_vnode_info vni;
    unsigned long total_segs = 0;
    unsigned long total_pages = 0;
    unsigned long total_hashes = 0;
    unsigned long total_signatures =0;
    unsigned long total_nulls = 0;

    vmap["orignal path"] =  new Variant(this->originalPath);
    AFFILE*  affile = af_open(this->originalPath.c_str(), O_RDONLY, 0);
    if (affile)
    {
        vmap["dump type"] = new Variant(std::string(af_identify_file_name(this->originalPath.c_str(), 1)));
        if (af_vstat(affile, &vni) == 0)
        {
            if (vni.segment_count_encrypted > 0 || vni.segment_count_signed > 0)
            {
                vmap["encrypted segments"] = new Variant(vni.segment_count_encrypted);
                vmap["signed segments"] = new Variant(vni.segment_count_signed);
            }
            vector <string> segments;
            char segname[AF_MAX_NAME_LEN];
            af_rewind_seg(affile);
            int64_t total_datalen = 0;
            size_t total_segname_len = 0;
            size_t datalen = 0;
            int aes_segs=0;
            while(af_get_next_seg(affile, segname, sizeof(segname), 0, 0, &datalen)==0)
            {
                total_segs++;
                total_datalen += datalen;
                total_segname_len += strlen(segname);
                if(segname[0]==0)
                    total_nulls++;

                char hash[64];
                int64_t page_num = af_segname_page_number(segname);
                int64_t hash_num = af_segname_hash_page_number(segname,hash,sizeof(hash));
                if(page_num>=0)
                    total_pages++;
                if(hash_num>=0)
                    total_hashes++;
                if(strstr(segname,AF_SIG256_SUFFIX))
                    total_signatures++;
                if(strstr(segname,AF_AES256_SUFFIX))
                    aes_segs++;
                if((page_num>=0||hash_num>=0))
                    continue;
                if(af_is_encrypted_segment(segname))
                    continue;
                this->addSegmentAttribute(&vmap, affile, segname);
            }
            vmap["Total segments"] = new Variant((uint64_t)total_segs);
            vmap["Total segments real"] = new Variant((uint64_t)(total_segs - total_nulls));
            if (aes_segs)
                vmap["Encrypted segments"] = new Variant(aes_segs);
            vmap["Page segments"] = new Variant((uint64_t)total_pages);
            vmap["Hash segments"] = new Variant((uint64_t)total_hashes);
            vmap["Signature segments"] = new Variant((uint64_t)total_signatures);
            vmap["Null segments"] = new Variant((uint64_t)total_nulls);
            vmap["Total data bytes"] = new Variant((uint64_t)total_datalen);
        }

    }

    af_close(affile);

    return vmap;
}
Esempio n. 18
0
int fix(const char *infile)
{
    char buf[1024];
    int flags = (opt_fix ? O_RDWR : O_RDONLY) | O_BINARY;
    switch(af_identify_file_type(infile,1)){
    case AF_IDENTIFY_ERR:
	perror(infile);
	return 0;
    default:
	fprintf(stderr,"%s is not an AFF file\n",infile);
	return 0;
    case AF_IDENTIFY_AFF:
	break;
    }

    printf("%s  ",infile);
    int r=0;

    /* First see if the if the file begins with an AFF flag */
    int fd = open(infile,flags,0666);
    if(fd<0) err(1,"fopen(%s)",infile);
    if(read(fd,buf,strlen(AF_HEADER)+1)!=strlen(AF_HEADER)+1)
	err(1,"can't read AFF file header. Stop.");
    if(strcmp(buf,AF_HEADER)!=0)
	err(1,"%s does not begin with an AF_HEADER. Stop.",infile);
    if(read(fd,buf,strlen(AF_SEGHEAD)+1)!=strlen(AF_SEGHEAD)+1)
	err(1,"Can't read AF_SEGHEAD after AF_HEADER. Stop.");
    if(strcmp(buf,AF_SEGHEAD)!=0)
	err(1,"%s does not have an AF_SEGHEAD after AF_SEGEADER. Stop.",infile);

    /* Figure out length */
    off_t len = lseek(fd,0,SEEK_END);
    if(len<0) err(1,"Can't seek to end of %s. Stop.",infile);
    close(fd);
    
    AFFILE *af = af_open_with(infile,AF_HALF_OPEN|flags,0,&vnode_aff);
    printf("Scanning AFF file...\n");
    r = (*af->v->open)(af);
    /* See if we can build a TOC */
    if(r<0){
	printf("AFF file corrupt at %"I64d" out of %"I64d" (%"I64d" bytes from end)\n",
	       ftello(af->aseg),(int64_t)len,len-ftello(af->aseg));
	if(opt_fix){
	    printf("Truncating... %d \n",fileno(af->aseg));
	    if(ftruncate(fileno(af->aseg),ftello(af->aseg))){
		err(1,"ftruncate");
	    }
	}
    }

    /* See if it has a GID or an encrypted GID */
    if(af_get_seg(af,AF_IMAGE_GID,0,0,0)!=0 &&
       af_get_seg(af,AF_IMAGE_GID AF_AES256_SUFFIX,0,0,0)!=0){
	printf("AFF file is missing a GID. ");
	if(opt_fix){
	    printf("Making one...");
	    if(af_make_gid(af)<0) af_err(1,"af_make_gid");
	}
	putchar('\n');
    }

    af_close(af);

    return 0;
}
Esempio n. 19
0
int random_read_test(int total_bytes,int data_page_size)
{
    printf("\n\n\nrandom read test. filesize=%d, page_size=%d\n",
	   total_bytes,data_page_size);

    /* Create a regular file and an AFF file */

    printf("Creating random_contents.img and random_contents.%s, "
	   "both with %d bytes of user data...\n",
	   opt_ext,total_bytes);

    int    fd = open("test_random_contents.img",
		     O_CREAT|O_RDWR|O_TRUNC|O_BINARY,0666);
    if(fd<0) err(1,"fopen");

    AFFILE *af = open_testfile("test_random_contents",1);

    /* Just write it out as one big write */

    unsigned char *buf = (unsigned char *)malloc(total_bytes);
    unsigned char *buf2 = (unsigned char *)malloc(total_bytes);

    /* First half is random */
#ifdef HAVE_RAND_PSEUDO_BYTES
    RAND_pseudo_bytes(buf,total_bytes/2);
#else
    for(int i=0;i<total_bytes/2;i++){
	buf[i] = random();
    }
#endif

    /* Second half is a bit more predictable */
    for(int i=total_bytes/2;i<total_bytes;i++){
	buf[i] = ((i % 256) + (i / 256)) % 256;
    }

    if(write(fd,buf,total_bytes)!=total_bytes) err(1,"fwrite");
    if(af_write(af,buf,total_bytes)!=(int)total_bytes) err(1,"af_write");

    /* Now try lots of seeks and reads */
    for(int i=0;i<MAX_FMTS;i++){
	uint32_t loc = rand() % total_bytes;
	uint32_t len = rand() % total_bytes;
	memset(buf,0,total_bytes);
	memset(buf2,0,total_bytes);

	if(i%250==0) printf("\r#%d  reading %"PRIu32" bytes at %"PRIu32"    ...",i,loc,len);
	fflush(stdout);

	uint32_t l1 = (uint32_t)lseek(fd,loc,SEEK_SET);
	uint32_t l2 = (uint32_t)af_seek(af,loc,SEEK_SET);


	if(l1!=l2){
	    err(1,"l1 (%"PRIu32") != l2 (%"PRIu32")",l1,l2);
	}

	int r1 = read(fd,buf,len);
	int r2 = af_read(af,buf2,len);

	if(r1!=r2){
	    err(1,"r1 (%d) != r2 (%d)",r1,r2);
	}
    }
    af_close(af);
    close(fd);
    printf("\nRandom read test passes\n");
    return 0;
}
Esempio n. 20
0
void sparse_test()
{
    printf("Sparse test...\n");

    char buf[1024];
    char fn[1024];

    uint64_t mult = (uint64_t)3 * (uint64_t)1000000000;		// 3GB in

    AFFILE *af = af_open(filename(fn,sizeof(fn),"sparse"),O_CREAT|O_RDWR|O_TRUNC,0666);
    af_enable_compression(af,opt_compression_type,opt_compression_level);
    af_set_maxsize(af,(int64_t)1024*1024*256);
    af_set_pagesize(af,1024*1024*16);

    for(u_int i=0;i<10;i++){
	uint64_t pos = mult*i;
	memset(buf,0,sizeof(buf));
	snprintf(buf,sizeof(buf),"This is at location=%"I64u"\n",pos);
	af_seek(af,pos,SEEK_SET);
	af_write(af,(unsigned char *)buf,sizeof(buf));
    }

    /* Now verify */
    for(u_int i=0;i<10;i++){
	uint64_t pos = mult*i;
	uint64_t q;
	af_seek(af,pos,SEEK_SET);
	af_read(af,(unsigned char *)buf,sizeof(buf));
	char *cc = strchr(buf,'=');
	if(!cc){
	    printf("Garbage read at location %"I64u"\n.",pos);
	    exit(1);
	}
	if(sscanf(cc+1,"%"I64u,&q)!=1){
	    printf("Could not decode value at location %"I64u"(%s)\n",pos,cc+1);
	    exit(1);
	}
	if(pos!=q){
	    printf("Wrong value at location %"I64u"; read %"I64u" in error.\n", pos,q);
	    exit(1);
	}
    }

    /* Now seek to somewhere that no data has been written and see if we get 0s. */
    memset(buf,'g',sizeof(buf));
    af_seek(af,mult/2,SEEK_SET);
    ssize_t r = af_read(af,(unsigned char *)buf,sizeof(buf));
    if(r!=sizeof(buf)){
	err(1,"Tried to read %zd bytes at mult/2; got %zd bytes\n",sizeof(buf),r);
    }
    for(u_int i=0;i<sizeof(buf);i++){
	if(buf[i]!=0) err(1,"data error; buf[%d]=%d\n",i,buf[i]);
    }

    /* Now try to read the last page in the file */
    unsigned char big_buf[65536];
    af_seek(af,9*mult,SEEK_SET);
    r = af_read(af,big_buf,sizeof(big_buf));
    if(r!=sizeof(buf)){
	errx(1,"Tried to read %zd bytes at the end of the file; got %zd bytes (should get %zd)",
	    sizeof(big_buf),r,sizeof(buf));
    }
			  

    /* Now see if we can read past the end of the file */
    af_seek(af,11*mult,SEEK_SET);
    r = af_read(af,(unsigned char *)buf,sizeof(buf));
    if(r!=0) errx(1,"Tried to read past end of file; got %zd bytes (should get 0)",r);

    af_close(af);
    printf("\nSprase test passes.\n");
    printf("=====================\n\n");
}
Esempio n. 21
0
int aestest()
{
    unsigned char keyblock[32];
     
    /* Make a key; doesn't need to be a good key; make it 256 bits */
    for(int i=0;i<32;i++){
	keyblock[i] = i;
    }

    AFFILE *af = af_open("crypto.aff",O_CREAT|O_RDWR|O_TRUNC,0666);
    if(!af) err(1,"af_open");
    if(af_set_aes_key(af,keyblock,256)) err(1,"af_set_aes_key");
    af_set_pagesize(af,65536);

    /* Now, let's write some data of various sizes */

    u_char test[1024],buf[1024],rbuf[1024];
    size_t  buflen = sizeof(buf);
    make_test_seg(test,0);
    for(u_int len=0;len<=strlen((const char *)test);len++){
	if(af_update_seg(af,"page0",0,test,len)) err(1,"af_update_seg len=%d",len);
	
	/* Now try to read the segment */
	memset(buf,0,sizeof(buf));
	buflen = sizeof(buf);
	if(af_get_seg(af,"page0",0,(unsigned char *)buf,&buflen)){
	    err(1,"Could not read encrypted segment with length %d.\n",len);
	}
	if(buflen!=len){
	    printf("size of returned segment = %zd ",buflen);
	    printf("(should be %d) \n",len);
	    exit(0);
	}
	if(memcmp(buf,test,len)!=0){
	    printf("does not match\n");
	    printf("  wanted: %s\n",test);
	    printf("  got: %s\n",buf);
	    exit(0);
	}
    }
    if(af_close(af)) err(1,"af_close");

    /* Now re-open the file, do not set the encryption key, and see if we can read it */
    int r;
    memset(buf,0,sizeof(buf));
    af = af_open("crypto.aff",O_RDONLY,0666);
    buflen = sizeof(buf);
    r = af_get_seg(af,"page0",0,(unsigned char *)buf,&buflen);
    if(r!=-1) {
	errx(1,"Error; attempt to read segment 'encrypted' succeded. It should have failed.");
    }

    /* Try to read 'encrypted/aes' */
    r = af_get_seg(af,"encrypted/aes",0,(unsigned char *)buf,&buflen);
    if(memcmp(buf,test,buflen)==0){
	errx(1,"Error: segment encrypted/aes wasn't actually encrypted.");
    }
    af_close(af);
    
    /* Now set the correct encryption key and see if we can read it */
    af = af_open("crypto.aff",O_RDONLY,0666);
    if(af_set_aes_key(af,keyblock,256)) err(1,"af_set_aes_key");
    buflen = sizeof(buf);
    memset(buf,0,sizeof(buf));
    r = af_get_seg(af,"page0",0,(unsigned char *)buf,&buflen);
    if(buflen != strlen((const char *)test)){
	errx(1,"Error: Could not read encrypted segment after re-opening file");
    }
    if(memcmp(buf,test,buflen)!=0) errx(1,"Error: Re-read of file produces wrong data.");
    printf("encrypted data read and decrypted: '%s'\n",buf);
    /* Try to read a segment that doesn't eixst */
    buflen = 0;
    if(af_get_seg(af,"encrypted2",0,0,&buflen)==0){
	errx(1,"Error: Attempt to get size of non-existant segment 'encrypted2' got %zd\n",buflen);
    }
    af_close(af);


    /* Now set the wrong encryption key and see if we can read it */
    memset(buf,0,sizeof(buf));
    af = af_open("crypto.aff",O_RDONLY,0666);
    keyblock[3] = 42;
    if(af_set_aes_key(af,keyblock,256)) err(1,"af_set_aes_key");
    buflen = sizeof(buf);
    r = af_get_seg(af,"page0",0,(unsigned char *)buf,&buflen);
    if(memcmp(buf,test,buflen)==0) errx(1,"Error: Setting wrong key still produces correct data.");
    af_close(af);

    printf("Basic crypto checks. Now check passphrase....\n");

    /* Write the data with a passphrase and try to read it back */ 
    af = af_open("crypto_pass.aff",O_CREAT|O_RDWR|O_TRUNC,0666);
    if(!af) err(1,"af_open 3");
    af_set_pagesize(af,65536);
    if(af_establish_aes_passphrase(af,"yummy")) err(1,"af_establish_aes_passphrase");
    if(af_use_aes_passphrase(af,"yummy")) err(1,"af_use_aes_passphrase");
    if(af_update_seg(af,"page0",0,(const u_char *)test,strlen((const char *)test))) err(1,"af_update_seg failed at 3");
    if(af_close(af)) err(1,"af_close at 3");


    /* Now try to read it back */
    memset(rbuf,0,sizeof(rbuf));
    size_t rbuflen = sizeof(rbuf);
    af = af_open("crypto_pass.aff",O_RDONLY,0666);
    if(!af) err(1,"af_open 4");
    if(af_get_seg(af,"page0",0,(unsigned char *)buf,&buflen)==0){
	errx(1,"af_get_seg should have failed and didn't");
    }
    if(af_use_aes_passphrase(af,"yummy")) err(1,"af_set_passphrase 2");
    rbuflen=sizeof(rbuf);
    if(af_get_seg(af,"page0",0,(unsigned char *)rbuf,&rbuflen)){
	errx(1,"af_get_seg failed");
    }
    if(rbuflen!=strlen((const char *)test)) errx(1,"Reading encrypted data returned wrong size");
    if(memcmp(rbuf,test,rbuflen)!=0) errx(1,"Error: wrong data");
    printf("encrypted data read with passphrase 'yummy': %s\n",rbuf);
    af_close(af);

    /* Try to change the passphrase */
    af = af_open("crypto_pass.aff",O_RDWR,0666);
    if(!af) err(1,"af_open 5");
    if(af_change_aes_passphrase(af,"yummy","dummy")) err(1,"could not change passphrase");
    af_close(af);

    /* Try to read with new passphrase */
    af = af_open("crypto_pass.aff",O_RDONLY,0666);
    if(!af) err(1,"af_open 5");
    memset(rbuf,0,sizeof(rbuf));
    rbuflen = sizeof(rbuf);
    if(af_use_aes_passphrase(af,"dummy")) err(1,"af_set_passphrase 2");
    rbuflen=sizeof(rbuf);
    if(af_get_seg(af,"page0",0,(unsigned char *)rbuf,&rbuflen)){
	errx(1,"af_get_seg failed");
    }
    if(rbuflen!=strlen((const char *)test)) errx(1,"Reading encrypted with new passphrase data returned wrong size");
    if(memcmp(rbuf,test,rbuflen)!=0) errx(1,"Error: wrong data");
    printf("encrypted data read with new passphrase 'dummy': %s\n",rbuf);
    af_close(af);
    exit(0);
    
    
    /* Now try to read with the wrong passphrase */
    af = af_open("crypto.aff",O_RDONLY,0666);
    if(af_use_aes_passphrase(af,"yummy2")) err(1,"af_set_passphrase 3");
    buflen=sizeof(buf);
    memset(buf,0,sizeof(buf));
    if(af_get_seg(af,"page0",0,(unsigned char *)buf,&buflen)){
	printf("Couldn't get data with wrong passphrase (that's good)\n");
    }
    printf("data read with wrong passphrase: %s\n",buf);
    if(buflen>0 && memcmp(buf,test,buflen)==0){
	errx(1,"Error: data fetched with wrong passphrase was not scrambled.");
    }
    af_close(af);
    exit(0);
}