1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
/*
* affcat.cpp:
*
* cat the contents of an AFF file...
* Distributed under the Berkeley 4-part license
*/
#include "affconfig.h"
#include "afflib.h"
#include "afflib_i.h"
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string>
#ifdef HAVE_CSTRING
#include <cstring>
#endif
using namespace std;
vector <int64_t> pages;
const char *progname = "affcat";
int opt_info = 0;
char *opt_segname=0;
int64_t opt_pagenum = -1;
int opt_quiet = 1;
int opt_list= 0 ;
int opt_list_long = 0;
int opt_debug = 0;
int64_t opt_sector = -1;
int opt_badflag = 0;
vector<string> opt_r;
void usage()
{
printf("affcat version %s\n",PACKAGE_VERSION);
printf("usage: affcat [options] infile [... more infiles]\n");
printf("options:\n");
printf(" -s name --- Just output segment name\n");
printf(" -p ### --- just output data page number ###\n");
printf(" -S ### --- Just output data sector ### (assumes 512-byte sectors). Sector #0 is first\n");
printf(" -q --- quiet; don't print to STDERR if a page is skipped\n");
printf(" -n --- noisy; tell when pages are skipped.\n");
printf(" -l --- List all of the segment names\n");
printf(" -L --- List segment names, lengths, and args\n");
printf(" -d --- debug. Print the page numbers to stderr as data goes to stdout\n");
printf(" -b --- Output BADFALG for bad blocks (default is NULLs)\n");
printf(" -v --- Just print the version number and exit.\n");
printf(" -r offset:count --- seek to offset and output count characters in each file; may be repeated\n");
exit(0);
}
const char *current_fname = 0;
int64_t current_page = -1;
void sig_info(int arg)
{
fprintf(stderr,"affcat ");
if(current_fname) fprintf(stderr,"%s: ",current_fname);
if(current_page>=0) fprintf(stderr,"[%" PRId64 "] ",current_page);
fflush(stderr);
}
int compar(const void *a_,const void *b_)
{
int64_t a = *(int *)a_;
int64_t b = *(int *)b_;
if(a<b) return -1;
if(a>b) return 1;
return 0;
}
struct afm_private {
AFFILE *aff; // the AFFILE we use for the actual metadata
AFFILE *sr; // the AFFILE we use for the splitraw
int sr_initialized; // has the split-raw been setup from AFM?
};
int output_page(AFFILE *af,FILE *outfile,int64_t pagenum)
{
current_fname = af_filename(af);
current_page = pagenum;
unsigned char *buf = (unsigned char *)malloc(af->image_pagesize);
if(buf==0){
err(1,"malloc(%d) failed",(int)af->image_pagesize);
}
uint64_t offset = (uint64_t)pagenum * af->image_pagesize; // go to that location
af_seek(af,offset,SEEK_SET);
int bytes = af_read(af,buf,af->image_pagesize); // read what we can
if(bytes<0){
if(opt_debug) fprintf(stderr,"affcat: cannot read page %" I64d "\n",pagenum);
return -1;
}
if(opt_debug){
fprintf(stderr,"affcat: page:%" I64d" bytes: %d offset:%" I64d "\n",
pagenum, bytes,offset);
}
/* Check each sector to see if it is badflag or not.
* If it is and if opt_badflag is not set, make it all NULs.
*/
for(unsigned char *cc=buf;cc<buf+bytes;cc+=af->image_sectorsize){
if(af_is_badsector(af,cc) && opt_badflag==0){
memset(cc,0,af->image_sectorsize);
}
}
if(opt_debug) fprintf(stderr," outputting %d bytes\n",bytes);
int count = fwrite(buf,1,bytes,outfile); // send to the output
if(count!=bytes) fprintf(stderr,"fwrite(buf,1,%d,outfile) only wrote %d bytes\n",bytes,count);
free(buf);
return bytes;
}
int affcat(AFFILE *af)
{
int64_t total_bytes_written = 0;
/* Read all of the pages from beginning to end and capture
* all the segment numbers...
*/
#ifdef WIN32
_setmode(fileno(stdout),_O_BINARY);
#endif
if(opt_debug) fprintf(stderr,"affcat(%s)\n",af_filename(af));
if(opt_segname){
/* First figure out how big the segment is */
size_t datalen = 0;
if(af_get_seg(af,opt_segname,0,0,&datalen)){
fprintf(stderr,"%s: segment '%s' does not exist\n",
af_filename(af),opt_segname);
return -1;
}
unsigned char *data = (unsigned char *)malloc(datalen);
if(data==0) err(1,"malloc");
if(af_get_seg(af,opt_segname,0,data,&datalen)){
free(data);
fprintf(stderr,"%s: could not read segment '%s'\n",
af_filename(af),opt_segname);
return -1;
}
int count = fwrite(data,1,datalen,stdout);
if(count!=(ssize_t)datalen){
fprintf(stderr,"fwrite(buf,1,%d,outfile) only wrote %d bytes\n",(int)datalen,count);
}
free(data);
return 0;
}
if(opt_pagenum != -1){ // just write a particular page?
int r = output_page(af,stdout,opt_pagenum);
return r>=0 ? 0 : -1;
}
if(opt_sector>=0){
unsigned char *buf = (unsigned char *)malloc(af->image_sectorsize);
af_seek(af,(uint64_t)opt_sector*af->image_sectorsize,SEEK_SET);
int bytes_read = af_read(af,buf,af->image_sectorsize);
if(bytes_read>0){
int bytes_written = fwrite(buf,1,bytes_read,stdout);
if(bytes_read!=bytes_written){
fprintf(stderr,"fwrite(buf,1,%d,outfile) only wrote %d bytes\n",
bytes_read,bytes_written);
}
}
free(buf);
return 0;
}
/* Get a list of all the segments. If we are doing a list, just print them.
* If we are not doing a list, capture the data pages and put their numbers
* into an array.
*/
if(opt_debug) fprintf(stderr,"af_rewind_seg()\n");
if(opt_r.size()>0){
unsigned char *buf = (unsigned char *)malloc(af->image_pagesize);
for(vector<string>::const_iterator offset_count=opt_r.begin(); offset_count != opt_r.end(); offset_count++){
string opts = *offset_count;
const char *opt = opts.c_str();
uint64_t offset=0;
int count=0;
if(sscanf(opt,"%" I64u ":%d",&offset,&count)!=2){
err(1,"Cannot decode '%s'\n",opt);
}
af_seek(af,offset,SEEK_SET);
int r= af_read(af,buf,count);
if(r>0){
int bytes_written = fwrite(buf,1,r,stdout);
if(bytes_written!=r) {
fprintf(stderr,"fwrite(buf,1,%d,outfile) only wrote %d bytes\n",r,bytes_written);
}
}
}
free(buf);
return 0;
}
af_rewind_seg(af); // start at the beginning
char segname[AF_MAX_NAME_LEN];
uint32_t arg;
size_t datalen = 0;
memset(segname,0,sizeof(segname));
int encrypted_segments = 0;
while(af_get_next_seg(af,segname,sizeof(segname),&arg,0,&datalen)==0){
if(opt_debug) fprintf(stderr,"af_get_next_seg found segment %s\n",segname);
if(segname[0]==0) continue; // ignore sector
if(opt_list){
printf("%s",segname);
if(opt_list_long){
printf("\targ:%" PRIu32 "\tlen:%d",arg,(int)datalen);
}
putchar('\n');
}
else {
int64_t pagenum = af_segname_page_number(segname);
if(pagenum>=0) pages.push_back(pagenum);
if(af_is_encrypted_segment(segname)) encrypted_segments++;
}
datalen = 0; // allow to get the next one
}
if(opt_list) return 0; // that's all that was wanted.
sort(pages.begin(),pages.end());
if(pages.size()==0 && encrypted_segments){
fprintf(stderr,"affcat: This file has %d encrypted segments.\n",encrypted_segments);
fprintf(stderr,"affcat: No unencrypted pages could be found.\n");
}
/* Now I have a list of pages; cat each one */
int next_page = 0; // starting page number
int64_t imagesize = af_get_imagesize(af);
for(vector<int64_t>::iterator i = pages.begin(); i != pages.end(); i++){
int page = *i;
if(page != next_page && opt_quiet==0){
if(page == next_page+1 ){
fprintf(stderr,"affcat: page %d not in file\n",next_page);
}
else{
fprintf(stderr,"affcat: pages %d through %d not in file\n",
next_page,page-1);
}
}
int r = output_page(af,stdout,page);
if(r<0) return -1;
total_bytes_written += r;
next_page = page + 1; // note what should be next
//fprintf(stderr,"bytes written=%qd imagesize=%qd\n",total_bytes_written,imagesize);
if((total_bytes_written > imagesize) && (imagesize>0)){
err(1,"affcat internal error. bytes written=%" I64d " imagesize=%" I64d,
(int64_t)total_bytes_written,
(int64_t)imagesize);
return -1;
}
}
return 0;
}
int64_t atoi64(const char *buf)
{
int64_t r=0;
char ch;
if(sscanf(buf,"%" I64d "%c",&r,&ch)==1) return r;
fprintf(stderr,"Cannot parse '%s'\n",buf);
exit(0);
}
int main(int argc,char **argv)
{
int ch;
#ifdef SIGINFO
signal(SIGINFO,sig_info);
#endif
while ((ch = getopt(argc, argv, "s:S:p:lLh?dqnbvr:")) != -1) {
switch (ch) {
case 's':
opt_segname = optarg;
break;
case 'S':
opt_sector = atoi64(optarg);
break;
case 'p':
opt_pagenum = atoi64(optarg);
break;
case 'q':
opt_quiet = 1;
break;
case 'n':
opt_quiet = 0;
break;
case 'l':
opt_list = 1;
break;
case 'r':
opt_r.push_back(optarg);
break;
case 'L':
opt_list = 1;
opt_list_long = 1;
break;
case 'b':
opt_badflag = 1;
break;
case 'd':
opt_debug++;
break;
case 'h':
case '?':
default:
usage();
break;
case 'v':
printf("%s version %s\n",progname,PACKAGE_VERSION);
exit(0);
}
}
argc -= optind;
argv += optind;
if(argc<1){
usage();
}
while(*argv){
AFFILE *af = af_open(*argv,O_RDONLY,0);
if(!af) af_err(1,"affcat(%s)",*argv);
if(affcat(af)) err(1,"affcat");
af_close(af);
argv++;
argc--;
}
}
|