Codebase list afflib / c65b2e3
Imported Upstream version 3.7.7+git20160511.4fd23a Joao Eriberto Mota Filho 8 years ago
29 changed file(s) with 149 addition(s) and 212 deletion(s). Raw diff Collapse all Expand all
2929
3030 - raw or "dd"
3131 - splitraw (in which a single image is split between mulitple files)
32 - EnCase or "E01" format
3332 - AFF format (in which the entire disk image is stored in a single file.)
3433 - AFD format (in which a disk image is stored in mulitple AFF files
3534 stored in a single directory.)
8080
8181
8282 # Specific headers that I plan to use
83 AC_CHECK_HEADERS([arpa/inet.h assert.h ctype.h dmalloc.h err.h errno.h fcntl.h getopt.h inttypes.h linux/fs.h malloc.h netinet/in.h regex.h signal.h stdint.h stdio.h stdlib.h string.h sys/cdefs.h sys/disk.h sys/file.h sys/ioctl.h sys/ioctl.h sys/param.h sys/param.h sys/socket.h sys/signal.h sys/stat.h sys/time.h sys/types.h sys/vfs.h sysexits.h term.h time.h unistd.h zlib.h _mingw.h])
83 AC_CHECK_HEADERS([arpa/inet.h assert.h ctype.h dmalloc.h err.h errno.h fcntl.h getopt.h inttypes.h linux/fs.h malloc.h netinet/in.h regex.h signal.h stdint.h stdio.h stdlib.h string.h sys/cdefs.h sys/disk.h sys/file.h sys/ioctl.h sys/ioctl.h sys/param.h sys/param.h sys/socket.h sys/signal.h sys/stat.h sys/time.h sys/types.h sys/vfs.h sysexits.h time.h unistd.h zlib.h _mingw.h])
8484
8585 AC_CHECK_LIB([regex],[regcomp]) # see if we need -lregex
8686
112112 ################################################################
113113 # For AFF tools
114114 AC_CHECK_HEADERS([readline/readline.h])
115 AC_CHECK_HEADERS([curses.h termcap.h])
116115 AC_CHECK_LIB([readline],[readline],, AC_MSG_RESULT([readline not installed]))
117116 AC_CHECK_LIB([z],[uncompress],, AC_MSG_ERROR([zlib not installed; cannot continue. Try adding zlib-dev or zlib1g-dev.]))
118117 AC_CHECK_LIB([rt],[aio_error64])
119 AC_SEARCH_LIBS(tgetent, termlib termcap curses)
120 AC_CHECK_FUNCS(putp tputs tgoto tgetstr tgetnum gotorc beep endwin setupterm printw)
121118
122119 ################################################################
123120 ## Expat
4949 uint32_t name_len = ntohl(segh.name_len);
5050 uint32_t datasize = ntohl(segh.data_len);
5151 if(name_len>AF_MAX_NAME_LEN){
52 snprintf(af->error_str,sizeof(af->error_str),"afflib: name_len=%"PRIu32" (an outrageous value)",name_len);
52 snprintf(af->error_str,sizeof(af->error_str),"afflib: name_len=%" PRIu32 " (an outrageous value)",name_len);
5353 return AF_ERROR_NAME;
5454 }
5555
2828 printf("AF DIRECTORY:\n");
2929 for(int i=0;i<af->toc_count;i++){
3030 if(af->toc[i].name){
31 printf("%-32s @%"I64u" len: %"I64u" \n",af->toc[i].name, af->toc[i].offset,af->toc[i].segment_len);
31 printf("%-32s @%" I64u " len: %" I64u " \n",af->toc[i].name, af->toc[i].offset,af->toc[i].segment_len);
3232 }
3333 }
3434 }
454454 uint64_t af_seek(AFFILE *af,int64_t pos,int whence)
455455 {
456456 AF_WRLOCK(af);
457 if(af_trace) fprintf(af_trace,"af_seek(%p,%"I64d",%d)\n",af,pos,whence);
457 if(af_trace) fprintf(af_trace,"af_seek(%p,%" I64d ",%d)\n",af,pos,whence);
458458 uint64_t new_pos=0;
459459 switch(whence){
460460 case SEEK_SET:
787787 int af_update_segf(AFFILE *af, const char *segname,
788788 uint32_t arg,const u_char *data,uint32_t datalen,uint32_t flag)
789789 {
790 if(af_trace) fprintf(af_trace,"af_update_segf(%p,segname=%s,arg=%"PRIu32",datalen=%d)\n",
790 if(af_trace) fprintf(af_trace,"af_update_segf(%p,segname=%s,arg=%" PRIu32 ",datalen=%d)\n",
791791 af,segname,arg,datalen);
792792 AF_WRLOCK(af);
793793 if(af->v->update_seg==0){
953953 {
954954 AF_READLOCK(af);
955955 fprintf(f,"AFSTATS for %s\n",af_filename(af));
956 fprintf(f,"Pages read: %"I64u"\n",af->pages_read);
957 fprintf(f,"Pages written: %"I64u"\n",af->pages_written);
958 fprintf(f,"Pages compressed: %"I64u"\n",af->pages_compressed);
959 fprintf(f,"Pages decompressed: %"I64u"\n",af->pages_decompressed);
960 fprintf(f,"Cache hits: %"I64u"\n",af->cache_hits);
961 fprintf(f,"Cache misses: %"I64u"\n",af->cache_misses);
962 fprintf(f,"Bytes copied: %"I64u"\n",af->bytes_memcpy);
956 fprintf(f,"Pages read: %" I64u "\n",af->pages_read);
957 fprintf(f,"Pages written: %" I64u "\n",af->pages_written);
958 fprintf(f,"Pages compressed: %" I64u "\n",af->pages_compressed);
959 fprintf(f,"Pages decompressed: %" I64u "\n",af->pages_decompressed);
960 fprintf(f,"Cache hits: %" I64u "\n",af->cache_hits);
961 fprintf(f,"Cache misses: %" I64u "\n",af->cache_misses);
962 fprintf(f,"Bytes copied: %" I64u "\n",af->bytes_memcpy);
963963 AF_UNLOCK(af);
964964 }
965965
405405 #define AF_SECTORSIZE "sectorsize" // in bytes, stored in arg
406406 #define AF_DEVICE_SECTORS "devicesectors"// stored as a 64-bit number
407407 #define AF_BADFLAG "badflag" // data used to mark a bad sector
408 #define AF_PAGE "page%"I64d // segment flag indicates compression (replaces seg%d)
408 #define AF_PAGE "page%" I64d // segment flag indicates compression (replaces seg%d)
409409 #define AF_PAGE_MD5 AF_PAGE"_md5" // md5 hash of page
410410 #define AF_PAGE_SHA1 AF_PAGE"_sha1" // sha1 hash of page
411411 #define AF_PAGE_SHA256 AF_PAGE"_sha256"// sha256 hash of page
427427 #define AF_BOM_SEG "affbom%d"
428428
429429 /* Deprecated terminology; pages were originally called data segments */
430 #define AF_SEG_D "seg%"I64d // segment flag indicates compression (deprecated)
430 #define AF_SEG_D "seg%" I64d // segment flag indicates compression (deprecated)
431431 #define AF_SEGSIZE_D "segsize" // segment data size (deprecated)
432432
433433 /* Bill of Materials */
167167 size_t page_len=0;
168168
169169 if (af_trace){
170 fprintf(af_trace,"af_get_page(%p,pagenum=%"I64d",buf=%p,bytes=%u)\n",af,pagenum,data,(int)*bytes);
170 fprintf(af_trace,"af_get_page(%p,pagenum=%" I64d ",buf=%p,bytes=%u)\n",af,pagenum,data,(int)*bytes);
171171 }
172172
173173 /* Find out the size of the segment and if it is compressed or not.
246246 case Z_OK:
247247 break;
248248 case Z_ERRNO:
249 (*af->error_reporter)("Z_ERRNOR decompressing segment %"I64d,pagenum);
249 (*af->error_reporter)("Z_ERRNOR decompressing segment %" I64d,pagenum);
250250 case Z_STREAM_ERROR:
251 (*af->error_reporter)("Z_STREAM_ERROR decompressing segment %"I64d,pagenum);
251 (*af->error_reporter)("Z_STREAM_ERROR decompressing segment %" I64d,pagenum);
252252 case Z_DATA_ERROR:
253 (*af->error_reporter)("Z_DATA_ERROR decompressing segment %"I64d,pagenum);
253 (*af->error_reporter)("Z_DATA_ERROR decompressing segment %" I64d,pagenum);
254254 case Z_MEM_ERROR:
255 (*af->error_reporter)("Z_MEM_ERROR decompressing segment %"I64d,pagenum);
255 (*af->error_reporter)("Z_MEM_ERROR decompressing segment %" I64d,pagenum);
256256 case Z_BUF_ERROR:
257 (*af->error_reporter)("Z_BUF_ERROR decompressing segment %"I64d,pagenum);
257 (*af->error_reporter)("Z_BUF_ERROR decompressing segment %" I64d,pagenum);
258258 case Z_VERSION_ERROR:
259 (*af->error_reporter)("Z_VERSION_ERROR decompressing segment %"I64d,pagenum);
259 (*af->error_reporter)("Z_VERSION_ERROR decompressing segment %" I64d,pagenum);
260260 default:
261261 (*af->error_reporter)("uncompress returned an invalid value in get_segment");
262262 }
265265 #ifdef USE_LZMA
266266 case AF_PAGE_COMP_ALG_LZMA:
267267 res = lzma_uncompress(data,bytes,compressed_data,compressed_data_len);
268 if (af_trace) fprintf(af_trace," LZMA decompressed page %"I64d". %d bytes => %u bytes\n",
268 if (af_trace) fprintf(af_trace," LZMA decompressed page %" I64d ". %d bytes => %u bytes\n",
269269 pagenum,(int)compressed_data_len,(int)*bytes);
270270 switch(res){
271271 case 0:break; // OK
272 case 1:(*af->error_reporter)("LZMA header error decompressing segment %"I64d"\n",pagenum);
272 case 1:(*af->error_reporter)("LZMA header error decompressing segment %" I64d "\n",pagenum);
273273 break;
274 case 2:(*af->error_reporter)("LZMA memory error decompressing segment %"I64d"\n",pagenum);
274 case 2:(*af->error_reporter)("LZMA memory error decompressing segment %" I64d "\n",pagenum);
275275 break;
276276 }
277277 break;
552552 ret = -1; // got an error; keep going, though
553553 }
554554 p->pagebuf_dirty = 0;
555 if(af_trace) fprintf(af_trace,"af_cache_flush: slot %d page %"PRIu64" flushed.\n",i,p->pagenum);
555 if(af_trace) fprintf(af_trace,"af_cache_flush: slot %d page %" PRIu64 " flushed.\n",i,p->pagenum);
556556 }
557557 }
558558 return ret; // now return the error that I might have gotten
569569 struct aff_pagebuf *p = &af->pbcache[i];
570570 if(p->pagenum_valid && p->pagenum == pagenum){
571571 if(p->pagebuf_dirty){
572 (*af->error_reporter)("af_cache_writethrough: overwriting page %"I64u".\n",pagenum);
572 (*af->error_reporter)("af_cache_writethrough: overwriting page %" I64u ".\n",pagenum);
573573 exit(-1); // this shouldn't happen
574574 }
575575 memcpy(p->pagebuf,buf,bufflen);
592592
593593 struct aff_pagebuf *af_cache_alloc(AFFILE *af,int64_t pagenum)
594594 {
595 if(af_trace) fprintf(af_trace,"af_cache_alloc(%p,%"I64d")\n",af,pagenum);
595 if(af_trace) fprintf(af_trace,"af_cache_alloc(%p,%" I64d ")\n",af,pagenum);
596596
597597 /* Make sure nothing in the cache is dirty */
598598 if(af_cache_flush(af) < 0)
603603 struct aff_pagebuf *p = &af->pbcache[i];
604604 if(p->pagenum_valid && p->pagenum==pagenum){
605605 af->cache_hits++;
606 if(af_trace) fprintf(af_trace," page %"I64d" satisfied fromcache\n",pagenum);
606 if(af_trace) fprintf(af_trace," page %" I64d " satisfied fromcache\n",pagenum);
607607 p->last = cachetime++;
608608 return p;
609609 }
616616 struct aff_pagebuf *p = &af->pbcache[i];
617617 if(p->pagenum_valid==0){
618618 slot = i;
619 if(af_trace) fprintf(af_trace," slot %d given to page %"I64d"\n",slot,pagenum);
619 if(af_trace) fprintf(af_trace," slot %d given to page %" I64d "\n",slot,pagenum);
620620 break;
621621 }
622622 }
631631 }
632632 }
633633 slot = oldest_i;
634 if(af_trace) fprintf(af_trace," slot %d assigned to page %"I64d"\n",slot,pagenum);
634 if(af_trace) fprintf(af_trace," slot %d assigned to page %" I64d "\n",slot,pagenum);
635635 }
636636 /* take over this slot */
637637 struct aff_pagebuf *p = &af->pbcache[slot];
655655 if(af_trace){
656656 fprintf(af_trace," current pages in cache: ");
657657 for(int i=0;i<af->num_pbufs;i++){
658 fprintf(af_trace," %"I64d,af->pbcache[i].pagenum);
658 fprintf(af_trace," %" I64d,af->pbcache[i].pagenum);
659659 }
660660 fprintf(af_trace,"\n");
661661 }
2828 {
2929 AF_WRLOCK(af);
3030 if(af->image_size>0){
31 (*af->error_reporter)("Cannot set maxsize as imagesize is already set (%"I64d")",af->image_size);
31 (*af->error_reporter)("Cannot set maxsize as imagesize is already set (%" I64d ")",af->image_size);
3232 AF_UNLOCK(af);
3333 return -1; // now allowed to set if imagesize is bigger than 0
3434 }
3535 if((af->image_pagesize!=0)
3636 && (af->v->type & AF_VNODE_MAXSIZE_MULTIPLE)
3737 && (maxsize % af->image_pagesize != 0)){
38 (*af->error_reporter)("Cannot set maxsize to %"I64d" --- not multiple of pagesize=%d\n",
38 (*af->error_reporter)("Cannot set maxsize to %" I64d " --- not multiple of pagesize=%d\n",
3939 maxsize,af->image_pagesize);
4040 AF_UNLOCK(af);
4141 return -1;
8282 int total = 0;
8383
8484 AF_WRLOCK(af); // wrlock because cache may change
85 if (af_trace) fprintf(af_trace,"af_read(%p,%p,%d) (pos=%"I64d")\n",af,buf,(int)count,af->pos);
85 if (af_trace) fprintf(af_trace,"af_read(%p,%p,%d) (pos=%" I64d ")\n",af,buf,(int)count,af->pos);
8686 if (af->v->read){ // check for bypass
8787 int r = (af->v->read)(af, buf, af->pos, count);
8888 if(r>0) af->pos += r;
161161 {
162162 AF_WRLOCK(af);
163163 if (af_trace){
164 fprintf(af_trace,"af_write(af=%p,buf=%p,count=%d) pos=%"I64d"\n", af,buf,(int)count,af->pos);
164 fprintf(af_trace,"af_write(af=%p,buf=%p,count=%d) pos=%" I64d "\n", af,buf,(int)count,af->pos);
165165 }
166166 /* Invalidate caches */
167167 af_invalidate_vni_cache(af);
763763 uint32_t siglen = sizeof(sig);
764764
765765 BIO *bp = BIO_new_file("signing_key.pem","r");
766 if (!bp) {
767 perror("BIO_new_file");
768 return;
769 }
766770
767771 EVP_MD_CTX md;
768772 EVP_PKEY *pkey = PEM_read_bio_PrivateKey(bp,0,0,0);
773777
774778 /* let's try to verify it */
775779 bp = BIO_new_file("signing_cert.pem","r");
780 if (!bp) {
781 perror("BIO_new_file");
782 return;
783 }
784
776785 X509 *x = 0;
777786 PEM_read_bio_X509(bp,&x,0,0);
778787 EVP_PKEY *pubkey = X509_get_pubkey(x);
110110
111111 int aff_write_seg(AFFILE *af, const char *segname,uint32_t arg,const u_char *data,size_t datalen)
112112 {
113 if(af_trace) fprintf(af_trace,"aff_write_seg(%p,%s,%"PRIu32",%p,len=%u)\n",
113 if(af_trace) fprintf(af_trace,"aff_write_seg(%p,%s,%" PRIu32 ",%p,len=%u)\n",
114114 af,segname,arg,data,(int)datalen);
115115
116116 struct af_segment_head segh;
140140 aff_toc_update(af,segname,ftello(af->aseg),datalen);
141141
142142
143 if(af_trace) fprintf(af_trace,"aff_write_seg: putting segment %s (datalen=%d) offset=%"PRId64"\n",
143 if(af_trace) fprintf(af_trace,"aff_write_seg: putting segment %s (datalen=%d) offset=%" PRId64 "\n",
144144 segname,(int)datalen,ftello(af->aseg));
145145
146146 if(fwrite(&segh,sizeof(segh),1,af->aseg)!=1) return -10;
306306 return AF_ERROR_TAIL;
307307 }
308308 if(stl != calculated_segment_len){
309 snprintf(af->error_str,sizeof(af->error_str),"af_get_next_segv: AF file corrupt (%"PRIu32"!=%"PRIu32")/!",
309 snprintf(af->error_str,sizeof(af->error_str),"af_get_next_segv: AF file corrupt (%" PRIu32 "!=%" PRIu32 ")/!",
310310 stl,calculated_segment_len);
311311 fseeko(af->aseg,start,SEEK_SET); // go back to last good position
312312 return AF_ERROR_TAIL;
362362 size_t size_needed = vallen+aff_segment_overhead(name);
363363 struct aff_toc_mem *adm = aff_toc(af,name);
364364
365 if(af_trace) fprintf(af_trace,"aff_update_seg(name=%s,arg=%"PRIu32",vallen=%u)\n",name,arg,vallen);
365 if(af_trace) fprintf(af_trace,"aff_update_seg(name=%s,arg=%" PRIu32 ",vallen=%u)\n",name,arg,vallen);
366366
367367 if(adm)
368368 {
186186 return -1;
187187 }
188188 if (af->maxsize % af->image_pagesize) {
189 (*af->error_reporter)("afm_split_raw_setup: maxsize (%"I64d") "
189 (*af->error_reporter)("afm_split_raw_setup: maxsize (%" I64d ") "
190190 "not a multiple of image_pagesize (%d)\n",
191191 af->maxsize,af->image_pagesize);
192192 return -1;
211211 */
212212 if (ap->aff->image_size && ap->aff->image_size != ap->sr->image_size) {
213213 (*af->error_reporter)("afm_split_raw_setup: internal error. "
214 "AFF image_size %"I64d" != SR image_size %"I64d"\n",
214 "AFF image_size %" I64d " != SR image_size %" I64d "\n",
215215 ap->aff->image_size,ap->sr->image_size);
216216 return -1;
217217 }
224224 /* Verify that the parameters make sense */
225225 if (ap->sr->maxsize != (pages_per_file * af->image_pagesize) && pages_per_file>0) {
226226 (*af->error_reporter)("afm_split_raw_setup: %s: per size indicated by metadata (%d * %d) "
227 "doesn't match maxsize (%"I64d")\n",
227 "doesn't match maxsize (%" I64d ")\n",
228228 af_filename(af),pages_per_file,af->image_pagesize,ap->sr->maxsize);
229229 return -1;
230230 }
168168 {
169169 struct split_raw_private *srp = SPLIT_RAW_PRIVATE(af);
170170 for(uint32_t i=0;i<srp->num_raw_files;i++){
171 fprintf(stderr," fds[%d]=%d pos[%d]=%"I64d"\n",i,srp->fds[i],i,srp->pos[i]);
171 fprintf(stderr," fds[%d]=%d pos[%d]=%" I64d "\n",i,srp->fds[i],i,srp->pos[i]);
172172 }
173173 srp_validate(af);
174174 fprintf(stderr,"===================\n");
286286 af->image_pagesize *= 2;
287287
288288 if ((ret == 0) && (af->maxsize % af->image_pagesize!=0)) {
289 (*af->error_reporter)("split_raw_open: %s: raw_file_size (%"I64d" not a multiple of pagesize %lu\n",
289 (*af->error_reporter)("split_raw_open: %s: raw_file_size (%" I64d " not a multiple of pagesize %lu\n",
290290 af->fname, af->maxsize,af->image_pagesize);
291291 split_raw_close (af);
292292 return -1;
416416 for(int i = 0; i < _length; i++)
417417 if (s.Find(_chars[i]) >= 0)
418418 return i;
419 return -1;
419 return -1;
420420 }
421421
422422 void TrimLeft(T c)
177177 for(int i = 0; i < Size(); i++)
178178 if (item == (*this)[i])
179179 return i;
180 return -1;
180 return -1;
181181 }
182182 int FindInSorted(const T& item) const
183183 {
2727 .IP "-n"
2828 Noisy mode. Tell when pages are skipped.
2929 .IP "-l"
30 List all the segment names, rather than outputing data.
30 List all the segment names, rather than outputting data.
3131 .IP "-L"
3232 List Long. Prints segment names, lengths, and args.
3333 .IP "-d"
7777 .TP
7878 .B
7979 \fB-r\fP
80 Force raw output.
80 Generate raw output.
8181 .RE
8282 .PP
8383 Dangerous input \fIoptions\fP:
103103
104104 .fam T
105105 .fi
106 Convert file1.aff to file1.raw:
107 .PP
108 .nf
109 .fam C
110 $ affconvert -r file1.aff
111
112 .fam T
113 .fi
106114 Batch convert \fIfiles\fP:
107115 .PP
108116 .nf
109117 .fam C
110118 $ affconvert file1.raw file2.raw file3.raw
119
120 .fam T
121 .fi
122 .PP
123 .nf
124 .fam C
125 $ affconvert -r file1.aff file2.aff file3.aff
111126
112127 .fam T
113128 .fi
1212 * United States Government and/or for any works created by United
1313 * States Government employees. User acknowledges that this software
1414 * contains work which was created by NPS employee(s) and is therefore
15 * in the public domain and not subject to copyright.
15 * in the public domain and not subject to copyright.
1616 * --------------------------------------------------------------------
1717 *
1818 * Change History:
9696 while(notes){
9797 char buf2[1024];
9898 char *val=0;
99
99
100100 #ifdef HAVE_LIBREADLINE
101101 if(isatty(fileno(stdin))){
102102 val = readline("");
119119 int aff_bom::read_files(const char *cert_file,const char *key_file)
120120 {
121121 BIO *bp_cert = BIO_new_file(cert_file,"r"); // read the certfile
122 if (!bp_cert)
123 return -1;
122124 PEM_read_bio_X509(bp_cert,&cert,0,0); // get an x509 cert
123125 BIO_free(bp_cert);
124126 if(!cert) return -1; // can't read certificate file
125
127
126128 /* Now read the private key */
127129 BIO *bp_privkey = BIO_new_file(key_file,"r");
128130 privkey = PEM_read_bio_PrivateKey(bp_privkey,0,0,0);
132134 cert = 0;
133135 return -1;
134136 }
135
137
136138 bom_open = true;
137139 xml = BIO_new(BIO_s_mem()); // where we are writing
138140 time_t clock = time(0);
139141 struct tm *tm = localtime(&clock);
140142 char timebuf[1024];
141143 strftime(timebuf,sizeof(timebuf),"<date type='ISO 8601'>%FT%T</date>",tm);
142
144
143145 BIO_printf(xml,"<%s version=\"1\">\n",AF_XML_AFFBOM);
144146 BIO_printf(xml," %s\n",timebuf);
145147 BIO_printf(xml," <program>afcopy</program>\n");
184186 size_t xlen = BIO_get_mem_data(xml,&xbuf);
185187 unsigned char sig[1024];
186188 u_int siglen = sizeof(sig);
187
189
188190 EVP_MD_CTX md;
189191 EVP_SignInit(&md,sha256);
190192 EVP_SignUpdate(&md,xbuf,xlen);
191193 EVP_SignFinal(&md,sig,&siglen,privkey);
192
194
193195 /* Write the signature in base64 encoding... */
194196 BIO *b64 = BIO_new(BIO_f_base64());
195197 xml = BIO_push(b64,xml);
196198 BIO_write(xml,sig,siglen);
197199 if(BIO_flush(xml)!=1) return; // something wrong
198
200
199201 /* Remove the base64 bio */
200202 xml = BIO_pop(b64);
201203 }
236238 size_t datalen = 0;
237239 if(af_get_seg(af,segname,0,0,&datalen)<0) return -1;
238240 uint32_t arg;
239 u_char *segdata = (u_char *)malloc(datalen);/* Allocate memory */
240 if(segdata<0) return -1;
241 u_char *segdata = (u_char *)malloc(datalen);
242 if(!segdata) return -1;
241243 if(af_get_seg(af,segname,&arg,segdata,&datalen)<0){
242244 free(segdata);
243245 return -1;
247249 add(segname,AF_SIGNATURE_MODE0,seghash,sizeof(seghash));
248250 free(segdata);
249251 return(0);
250
252
251253 }
252254
253255
6060 {
6161 fprintf(stderr,"affcat ");
6262 if(current_fname) fprintf(stderr,"%s: ",current_fname);
63 if(current_page>=0) fprintf(stderr,"[%"PRId64"] ",current_page);
63 if(current_page>=0) fprintf(stderr,"[%" PRId64 "] ",current_page);
6464 fflush(stderr);
6565 }
6666
9898 int bytes = af_read(af,buf,af->image_pagesize); // read what we can
9999
100100 if(bytes<0){
101 if(opt_debug) fprintf(stderr,"affcat: cannot read page %"I64d"\n",pagenum);
101 if(opt_debug) fprintf(stderr,"affcat: cannot read page %" I64d "\n",pagenum);
102102 return -1;
103103 }
104104
105105 if(opt_debug){
106 fprintf(stderr,"affcat: page:%"I64d" bytes: %d offset:%"I64d"\n",
106 fprintf(stderr,"affcat: page:%" I64d" bytes: %d offset:%" I64d "\n",
107107 pagenum, bytes,offset);
108108 }
109109
116116 }
117117 }
118118
119 if(opt_debug) fprintf(stderr," outputing %d bytes\n",bytes);
119 if(opt_debug) fprintf(stderr," outputting %d bytes\n",bytes);
120120 int count = fwrite(buf,1,bytes,outfile); // send to the output
121121 if(count!=bytes) fprintf(stderr,"fwrite(buf,1,%d,outfile) only wrote %d bytes\n",bytes,count);
122122 free(buf);
195195 const char *opt = opts.c_str();
196196 uint64_t offset=0;
197197 int count=0;
198 if(sscanf(opt,"%"I64u":%d",&offset,&count)!=2){
198 if(sscanf(opt,"%" I64u ":%d",&offset,&count)!=2){
199199 err(1,"Cannot decode '%s'\n",opt);
200200 }
201201 af_seek(af,offset,SEEK_SET);
226226 if(opt_list){
227227 printf("%s",segname);
228228 if(opt_list_long){
229 printf("\targ:%"PRIu32"\tlen:%d",arg,(int)datalen);
229 printf("\targ:%" PRIu32 "\tlen:%d",arg,(int)datalen);
230230 }
231231 putchar('\n');
232232 }
269269
270270 //fprintf(stderr,"bytes written=%qd imagesize=%qd\n",total_bytes_written,imagesize);
271271 if((total_bytes_written > imagesize) && (imagesize>0)){
272 err(1,"affcat internal error. bytes written=%"I64d" imagesize=%" I64d,
272 err(1,"affcat internal error. bytes written=%" I64d " imagesize=%" I64d,
273273 (int64_t)total_bytes_written,
274274 (int64_t)imagesize);
275275 return -1;
283283 {
284284 int64_t r=0;
285285 char ch;
286 if(sscanf(buf,"%"I64d"%c",&r,&ch)==1) return r;
286 if(sscanf(buf,"%" I64d "%c",&r,&ch)==1) return r;
287287 fprintf(stderr,"Cannot parse '%s'\n",buf);
288288 exit(0);
289289 }
152152 void print_info(char dir,const char *segname,uint32_t arg,size_t len,
153153 unsigned char *data,int mcr)
154154 {
155 printf(" %c %s arg=%"PRIu32" len=%d\n",dir,segname,arg,(int)len);
155 printf(" %c %s arg=%" PRIu32 " len=%d\n",dir,segname,arg,(int)len);
156156 printf(" ");
157157 if((arg == AF_SEG_QUADWORD) && (len==8)){
158 printf("data=%"I64d" as a 64-bit value\n",af_decode_q(data));
158 printf("data=%" I64d " as a 64-bit value\n",af_decode_q(data));
159159 return;
160160 }
161161 /* Otherwise, just print some stuff... */
289289
290290 /* Find the size of each page, then get the page */
291291 if(af_get_page(af1,pagenum,0,&data1_len)<0)
292 err(1,"Cannot read page %"I64d" size from %s\n",pagenum,af_filename(af1));
292 err(1,"Cannot read page %" I64d " size from %s\n",pagenum,af_filename(af1));
293293 if(af_get_page(af1,pagenum,data1,&data1_len)<0)
294 err(1,"Cannot read page %"I64d" from %s",pagenum,af_filename(af1));
294 err(1,"Cannot read page %" I64d " from %s",pagenum,af_filename(af1));
295295
296296 if(af_get_page(af2,pagenum,0,&data2_len)<0)
297 err(1,"Cannot read page %"I64d" size from %s\n",pagenum,af_filename(af2));
297 err(1,"Cannot read page %" I64d " size from %s\n",pagenum,af_filename(af2));
298298 if(af_get_page(af2,pagenum,data2,&data2_len)<0)
299 err(1,"Cannot read page %"I64d" from %s",pagenum,af_filename(af2));
299 err(1,"Cannot read page %" I64d " from %s",pagenum,af_filename(af2));
300300
301301 if(data1_len != data2_len){
302 printf("page %"I64d" size %d != size %d\n",pagenum,(int)data1_len,(int)data2_len);
302 printf("page %" I64d " size %d != size %d\n",pagenum,(int)data1_len,(int)data2_len);
303303 return 1;
304304 }
305305
340340 outline[0] = 0;
341341 if(opt_all || (no_match>0) || af1_bad || af2_bad){
342342 snprintf(outline,sizeof(outline),
343 " page%"I64d" sectors:%4d matching: %3d different:%3d",
343 " page%" I64d " sectors:%4d matching: %3d different:%3d",
344344 pagenum,total_sectors,matching_sectors,no_match);
345345 }
346346 if(af1_bad){
372372 if(i==0){
373373 printf("\n ");
374374 }
375 printf(" %"I64d,*j);
375 printf(" %" I64d,*j);
376376 i = (i+1) % 10;
377377 }
378378 putchar('\n');
390390 memcpy(b2,data1+offset,16);
391391 b2[15]=0;
392392
393 printf("=== sector %"I64d" (offset=%d) ===\n",*j,offset);
393 printf("=== sector %" I64d " (offset=%d) ===\n",*j,offset);
394394 printf(" %s:\n",af_filename(af1));
395395 print_sector(af1,data1+offset);
396396 printf("-------------------------------------\n");
428428 size_t len1,len2;
429429
430430 if(af_get_page_raw(af1,*i,&arg1,0,&len1)){
431 err(1,"Could not read page %"I64d" in file %s\n",*i,af_filename(af1));
431 err(1,"Could not read page %" I64d " in file %s\n",*i,af_filename(af1));
432432 }
433433 if(af_get_page_raw(af2,*i,&arg2,0,&len2)){
434 err(1,"Page %"I64d" is in file %s but not in %s\n",*i,af_filename(af1),
434 err(1,"Page %" I64d " is in file %s but not in %s\n",*i,af_filename(af1),
435435 af_filename(af2));
436436 }
437437 if(arg1==arg2 && len1==len2){
449449 continue;
450450 }
451451 }
452 printf("%s -> %s Nochg: %d NUL: %d LZMA: %d old: %"I64d" new: %"I64d" LZred: %6.2f%%\n",
452 printf("%s -> %s Nochg: %d NUL: %d LZMA: %d old: %" I64d " new: %" I64d " LZred: %6.2f%%\n",
453453 af_filename(af1),
454454 af_filename(af2),
455455 comp_unchanged,comp_zero,comp_lzma,bytes_old,bytes_new,(bytes_old-bytes_new)*100.0/bytes_old);
491491 if(af1->image_pagesize != af2->image_pagesize){
492492 fprintf(stderr,"Currently, %s requires that both images have the "
493493 "same image datsegsize.\n"
494 " pagesize(%s)=%"PRIu32"\n"
495 " pagesize(%s)=%"PRIu32"\n",
494 " pagesize(%s)=%" PRIu32 "\n"
495 " pagesize(%s)=%" PRIu32 "\n",
496496 progname,file1,af1->image_pagesize, file2,af2->image_pagesize);
497497 fprintf(stderr,"Data segments will be ignored.\n");
498498 no_data_segments = true;
501501 if(af1->image_sectorsize != af2->image_sectorsize){
502502 fprintf(stderr,"Currently, %s requires that both images have the "
503503 "same image sectorsize.\n"
504 " sectorsize(%s)=%"PRIu32"\n"
505 " sectorsize(%s)=%"PRIu32"\n",
504 " sectorsize(%s)=%" PRIu32 "\n"
505 " sectorsize(%s)=%" PRIu32 "\n",
506506 progname,file1,af1->image_sectorsize, file2,af2->image_sectorsize);
507507 fprintf(stderr,"Data segments will be ignored.\n");
508508 no_data_segments = true;
2020 #ifdef WIN32
2121 #include "unix4win32.h"
2222 #endif
23
24 #ifdef HAVE_CURSES_H
25 #include <curses.h>
26 #endif
27
28 #ifdef HAVE_TERM_H
29 #include <term.h>
30 #endif
31
3223
3324 #ifdef HAVE_SYS_TIME_H
3425 #include <sys/time.h>
422413
423414 int64_t pagenum = *i;
424415
425 if(!opt_quiet) printf("Converting page %"I64d" of %"I64d"\r",pagenum,highest_pagenum);fflush(stdout);
416 if(!opt_quiet) { printf("Converting page %" I64d " of %" I64d "\r",pagenum,highest_pagenum); fflush(stdout); }
426417
427418 size_t data_len = image_pagesize;
428419 if(af_get_page(a_in,pagenum,data,&data_len)){
429 err(1,"af_get_page(file=%s,page=%"I64d")",
420 err(1,"af_get_page(file=%s,page=%" I64d ")",
430421 af_filename(a_in),pagenum);
431422 }
432423 if(af_update_page(a_out,pagenum,data,data_len)){
433 err(1,"af_update_page(file=%s,page=%"I64d")",
424 err(1,"af_update_page(file=%s,page=%" I64d ")",
434425 af_filename(a_out),pagenum);
435426 }
436427
447438 /* Go back and update the image size (necessary since I have been writing page-by-page) */
448439 if(af_update_segq(a_out,AF_IMAGESIZE,last_byte_in_image)
449440 && errno!=ENOTSUP){
450 err(1,"Could not upate AF_IMAGESIZE");
441 err(1,"Could not update AF_IMAGESIZE");
451442 }
452443 } else {
453444 /* No page support; Copy from beginning to end */
498489
499490 /* Finish the hash calculations and write to the db */
500491 if(!opt_quiet){
501 printf("bytes converted: %"I64d" \n",total_bytes_converted);
492 printf("bytes converted: %" I64d " \n",total_bytes_converted);
502493 /* If the vnode implementation tracked segments written, report it. */
503494 if(a_out->pages_written || a_out->pages_compressed){
504 printf("Total pages: %"I64u" (%"I64u" compressed)\n",
495 printf("Total pages: %" I64u " (%" I64u " compressed)\n",
505496 a_out->pages_written,a_out->pages_compressed);
506497 }
507498 }
551542 int64_t atoi64(const char *buf)
552543 {
553544 int64_t r=0;
554 sscanf(buf,"%"I64d,&r);
545 sscanf(buf,"%" I64d,&r);
555546 return r;
556547 }
557548
3636
3737 #ifdef HAVE_UNISTD_H
3838 #include <unistd.h>
39 #endif
40
41 #ifdef HAVE_TERM_H
42 #include <term.h>
4339 #endif
4440
4541 #ifdef WIN32
468464 AFFILE *af = afouts.begin()->af;
469465 uint64_t w = af->bytes_written;
470466 double sec = ((t1.tv_sec-t0.tv_sec)+(t1.tv_usec-t0.tv_usec)/1000000.0);
471 printf("%s: %"I64d" bytes transferred in %.2f seconds. xfer rate: %.2f MBytes/sec\n",
467 printf("%s: %" I64d " bytes transferred in %.2f seconds. xfer rate: %.2f MBytes/sec\n",
472468 af_filename(af),w,sec,(w/1000000.0) / sec);
473469 }
474470
9696 uint64_t atoi64(const char *buf)
9797 {
9898 uint64_t ret=0;
99 sscanf(buf,"%"PRIu64,&ret);
99 sscanf(buf,"%" PRIu64,&ret);
100100 return ret;
101101 }
102102
200200 int64_t imagesize = af_get_imagesize(af);
201201 if(imagesize>0){
202202 char buf[32];
203 snprintf(buf,sizeof(buf),"%"PRIu64,imagesize);
203 snprintf(buf,sizeof(buf),"%" PRIu64,imagesize);
204204 out_xmlstr(cout,2,AF_IMAGESIZE,buf);
205205 cout << "\n";
206206 }
211211 int64_t sectors = imagesize/sectorsize;
212212 if(sectors>0){
213213 char buf[32];
214 snprintf(buf,sizeof(buf),"%"PRIu32"",sectorsize);
214 snprintf(buf,sizeof(buf),"%" PRIu32,sectorsize);
215215 out_xmlstr(cout,2,AF_SECTORSIZE,buf);
216216 cout << "\n";
217217 }
3131 #endif
3232
3333 using namespace std;
34
35 #ifdef HAVE_CURSES_H
36 #include <curses.h>
37 #endif
38
39 #ifdef HAVE_TERM_H
40 #include <term.h>
41 #endif
4234
4335 #ifdef WIN32
4436 #include "unix4win32.h"
7870
7971 vector<string> opt_seglist; // just info these segments
8072 bool something_was_decrypted = false;
81 const char *term = 0;
82
83
84 /**
85 * select bold on or off
86 */
87 void bold(int on)
88 {
89 if(!term) return;
90 #ifdef HAVE_ISATTY
91 if(!isatty(fileno(stdout))) return;
92 #endif
93 #if defined(HAVE_TPUTS) && defined(HAVE_CURSES_H) && defined(HAVE_TERM_H)
94 if(on) tputs(enter_bold_mode,1,putchar);
95 else tputs(exit_attribute_mode,0,putchar);
96 #endif
97 }
98
99 /**
100 * select a color.
101 * @param num - 0 is black; 1 red; 2 green; 3 yellow; 4 blue; 5 magenta; 6 cyan; 7 white;
102 */
103
104 #define RED 1
105 #define WHITE 7
106
107 void color(int num)
108 {
109 #ifdef HAVE_ISATTY
110 if(!isatty(fileno(stdout))) return;
111 #endif
112 #if defined(HAVE_TIGETSTR) && defined(HAVE_PUTP) && defined(HAVE_TPARM)
113 char *setf = tigetstr((char *)"setf");
114 if(!setf) setf = tigetstr((char *)"setaf");
115 if(setf){
116 putp(tparm(setf,num));
117 }
118 #endif
119 }
120
12173
12274 void usage()
12375 {
163115 #endif
164116 printf("\n");
165117
166 if(opt_debug){
167 for(int i=0;i<9;i++){
168 color(i);printf("Color %d\n",i);color(7);
169 }
170 }
171
172118 exit(0);
173119 }
174120
178124 void sig_info(int arg)
179125 {
180126 if(af==0) return;
181 printf("Validating %"I64d" of %"I64d"\n", af->pos,af->image_size);
127 printf("Validating %" I64d" of %" I64d"\n", af->pos,af->image_size);
182128 }
183129
184130
407353 af_set_option(af,AF_OPTION_AUTO_DECRYPT,prev);
408354
409355 if(was_decrypted){
410 bold(1);
411356 something_was_decrypted = true; // print key at bottom
412357 }
413358
416361 memset(output_line,0,sizeof(output_line));
417362
418363 /* Now append the arg and the data len */
419 sprintf(output_line,"%-24s %8"PRIu32" %8d ",segname,arg,(int)data_len);
364 sprintf(output_line,"%-24s %8" PRIu32 " %8d ",segname,arg,(int)data_len);
420365
421366 if(opt_no_preview){
422367 printf("%s\n",output_line);
446391 */
447392 switch(data_len){
448393 case 8:
449 printf("%s= %"I64d" (64-bit value)\n",
394 printf("%s= %" I64d" (64-bit value)\n",
450395 output_line,af_decode_q(data));
451396 break;
452397 case 0:
551496 putchar('\n');
552497 done:
553498 if(data) free(data);
554 bold(0); // make sure bold is off
555
556 //color(WHITE); // make sure we are back to normal color
557499 }
558500
559501
665607
666608 /* Print the key */
667609 if(something_was_decrypted){
668 bold(1);
669 printf("Bold indicates segments that were decrypted.\n");
670 bold(0);
610 printf("Some segments were decrypted.\n");
671611 }
672612
673613
674614 printf("\n");
675 printf("Total segments: %8"PRIu32" (%"PRIu32" real)\n", total_segs,(total_segs-total_nulls));
615 printf("Total segments: %8" PRIu32 " (%" PRIu32" real)\n", total_segs,(total_segs-total_nulls));
676616 if(aes_segs){
677617 printf(" Encrypted segments: %8u\n",aes_segs);
678618 }
679 printf(" Page segments: %8"PRIu32"\n",total_pages);
680 printf(" Hash segments: %8"PRIu32"\n",total_hashes);
681 printf(" Signature segments: %8"PRIu32"\n",total_signatures);
682 printf(" Null segments: %8"PRIu32"\n",total_nulls);
619 printf(" Page segments: %8" PRIu32 "\n",total_pages);
620 printf(" Hash segments: %8" PRIu32 "\n",total_hashes);
621 printf(" Signature segments: %8" PRIu32 "\n",total_signatures);
622 printf(" Null segments: %8" PRIu32 "\n",total_nulls);
683623 if(opt_all){
684 printf(" Empty segments: %8"PRIu32"\n",total_nulls);
624 printf(" Empty segments: %8" PRIu32 "\n",total_nulls);
685625 printf("\n");
686 printf("Total data bytes in segments: %"I64d"\n",total_datalen);
626 printf("Total data bytes in segments: %" I64d"\n",total_datalen);
687627
688628 printf("Total space in file dedicated to segment names: %zd\n",
689629 total_segname_len);
690 printf("Total overhead for %"PRIu32" segments: %zd bytes (%"PRIu32"*(%zd+%zd))\n",
630 printf("Total overhead for %" PRIu32 " segments: %zd bytes (%" PRIu32 "*(%zd+%zd))\n",
691631 total_segs,
692632 (size_t) total_segs*(sizeof(struct af_segment_head) +sizeof(struct af_segment_tail)),
693633 total_segs,
719659 //printf("device_bytes=%"I64d"\n",device_bytes);
720660 //printf("device_pages=%"I64d"\n",device_pages);
721661 if(missing_pages!=0){
722 printf("Missing page segments: %8"I64u"\n",missing_pages);
662 printf("Missing page segments: %8" I64u"\n",missing_pages);
723663 }
724664 else {
725665 some_missing_pages=0;
734674
735675 if(some_missing_pages && opt_debug){
736676 printf("Cannot calculate missing pages\n");
737 printf(" device_sectors=%"I64d" image_pagesize=%"PRIu32" sectorsize=%"PRIu32"\n",
677 printf(" device_sectors=%" I64d" image_pagesize=%" PRIu32 " sectorsize=%" PRIu32 "\n",
738678 device_sectors,af->image_pagesize,af->image_sectorsize);
739679 }
740680 af_close(af);
755695 printf("<!DOCTYPE Server >\n");
756696 printf("<device name='%s'>\n",fn);
757697 printf(" <sector_size>%d</sector_size>\n",afb.sector_size);
758 printf(" <total_sectors>%"PRId64"</total_sectors>\n",afb.total_sectors);
759 printf(" <max_read_blocks>%"PRIu64"</max_read_blocks>\n",afb.max_read_blocks);
698 printf(" <total_sectors>%" PRId64 "</total_sectors>\n",afb.total_sectors);
699 printf(" <max_read_blocks>%" PRIu64 "</max_read_blocks>\n",afb.max_read_blocks);
760700 printf("</device>\n");
761701 close(fd);
762702 }
2121
2222 #ifdef HAVE_UNISTD_H
2323 #include <unistd.h>
24 #endif
25
26 #ifdef HAVE_TERM_H
27 #include <term.h>
2824 #endif
2925
3026 #ifdef WIN32
9288 r = (*af->v->open)(af);
9389 /* See if we can build a TOC */
9490 if(r<0){
95 printf("AFF file corrupt at %"I64d" out of %"I64d" (%"I64d" bytes from end)\n",
91 printf("AFF file corrupt at %" I64d " out of %" I64d " (%" I64d " bytes from end)\n",
9692 ftello(af->aseg),(int64_t)len,len-ftello(af->aseg));
9793 if(opt_fix){
9894 printf("Truncating... %d \n",fileno(af->aseg));
2020
2121 #ifdef HAVE_UNISTD_H
2222 #include <unistd.h>
23 #endif
24
25 #ifdef HAVE_TERM_H
26 #include <term.h>
2723 #endif
2824
2925 #ifdef WIN32
229229 free(buf);
230230 continue;
231231 }
232 if(opt_debug) fprintf(stderr," arg=%"PRIu32" len=%zd\n",arg,len);
232 if(opt_debug) fprintf(stderr," arg=%" PRIu32 " len=%zd\n",arg,len);
233233 int p = 1;
234234
235235 if(filecount>1) printf("%s:",fn);
236236 if(print_segs.size()>1) printf("%s=",segname);
237237 if(opt_quad && len==8){
238238 uint64_t quad = af_decode_q(buf);
239 printf("%"I64u"\n",quad);
239 printf("%" I64u "\n",quad);
240240 p = 0;
241241 }
242242
243243 if(opt_arg){
244 printf("%"PRIu32"\n",arg);
244 printf("%" PRIu32 "\n",arg);
245245 p = 0;
246246 }
247247
6464 #include <unistd.h>
6565 #endif
6666
67 #ifdef HAVE_TERM_H
68 #include <term.h>
69 #endif
70
7167 #ifdef WIN32
7268 #include "unix4win32.h"
7369 #include <malloc.h>
9793 printf("%u",(unsigned int)(s/(1024*1024)));
9894 return;
9995 }
100 printf("%"I64u,s);
96 printf("%" I64u,s);
10197 }
10298
10399 void affstats_title()
149145 print_size(compressed_bytes);
150146 printf("\t");
151147 print_size(uncompressed_bytes);
152 printf(" %"I64d" %"I64d,blanksectors,badsectors);
148 printf(" %" I64d " %" I64d,blanksectors,badsectors);
153149 putchar('\n');
154150
155151
450450 printf("\n");
451451 do {
452452 double frac = (double)total_read / af_get_imagesize(af);
453 printf(" Read %14zd/%14"PRId64" bytes; done in %s\n",
453 printf(" Read %14zd/%14" PRId64" bytes; done in %s\n",
454454 total_read,
455455 af_get_imagesize(af),
456456 t.eta_text(frac).c_str());
134134
135135 void print_xml64(const char *name,int64_t val)
136136 {
137 printf(" <%s coding='base10'>%"I64d"</%s>\n\n",name,val,name);
137 printf(" <%s coding='base10'>%" I64d"</%s>\n\n",name,val,name);
138138 }
139139
140140 int xml_info(const char *infile)
190190 size_t pagesize = af_page_size(af);
191191 size_t sectorsize = af_get_sectorsize(af);
192192 if(af_get_page(af,*it,data,&pagesize)){
193 err(1,"Can't read page %"PRId64,*it);
193 err(1,"Can't read page %" PRId64,*it);
194194 }
195195 psb.pages++;
196196 bool allblank = true;
249249 * The strcmp is there because early AF_IMAGESIZE segs didn't set
250250 * AF_SEG_QUADWORD...
251251 */
252 printf(" <%s coding='base10'>%"I64d"</%s>\n",segname,af_decode_q(data),segname);
252 printf(" <%s coding='base10'>%" I64d"</%s>\n",segname,af_decode_q(data),segname);
253253 free(data);
254254 continue;
255255 }
256256
257257 /* If datalen==0, just print the arg as an unsigned number */
258258 if(datalen==0){
259 printf(" <%s coding='base10'>%"PRIu32"</%s>\n",segname,arg,segname);
259 printf(" <%s coding='base10'>%" PRIu32 "</%s>\n",segname,arg,segname);
260260 free(data);
261261 continue;
262262 }
264264 /* Just handle it as binhex ... */
265265 printf(" <%s",segname);
266266 if(datalen==0){
267 printf(" arg='%"PRIu32"' />\n",arg);
267 printf(" arg='%" PRIu32 "' />\n",arg);
268268 free(data);
269269 continue;
270270 }