** Reconstruct an in-memory database using the output from the "dbtotxt" ** program. Read content from the file in p->zDbFilename. If p->zDbFilename ** is 0, then read from standard input. */
| 14463 | ** is 0, then read from standard input. |
| 14464 | */ |
| 14465 | static unsigned char *readHexDb(ShellState *p, int *pnData){ |
| 14466 | unsigned char *a = 0; |
| 14467 | int nLine; |
| 14468 | int n = 0; |
| 14469 | int pgsz = 0; |
| 14470 | int iOffset = 0; |
| 14471 | int j, k; |
| 14472 | int rc; |
| 14473 | FILE *in; |
| 14474 | unsigned int x[16]; |
| 14475 | char zLine[1000]; |
| 14476 | if( p->zDbFilename ){ |
| 14477 | in = fopen(p->zDbFilename, "r"); |
| 14478 | if( in==0 ){ |
| 14479 | utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename); |
| 14480 | return 0; |
| 14481 | } |
| 14482 | nLine = 0; |
| 14483 | }else{ |
| 14484 | in = p->in; |
| 14485 | nLine = p->lineno; |
| 14486 | if( in==0 ) in = stdin; |
| 14487 | } |
| 14488 | *pnData = 0; |
| 14489 | nLine++; |
| 14490 | if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error; |
| 14491 | rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz); |
| 14492 | if( rc!=2 ) goto readHexDb_error; |
| 14493 | if( n<0 ) goto readHexDb_error; |
| 14494 | if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error; |
| 14495 | n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */ |
| 14496 | a = sqlite3_malloc( n ? n : 1 ); |
| 14497 | if( a==0 ){ |
| 14498 | utf8_printf(stderr, "Out of memory!\n"); |
| 14499 | goto readHexDb_error; |
| 14500 | } |
| 14501 | memset(a, 0, n); |
| 14502 | if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){ |
| 14503 | utf8_printf(stderr, "invalid pagesize\n"); |
| 14504 | goto readHexDb_error; |
| 14505 | } |
| 14506 | for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){ |
| 14507 | rc = sscanf(zLine, "| page %d offset %d", &j, &k); |
| 14508 | if( rc==2 ){ |
| 14509 | iOffset = k; |
| 14510 | continue; |
| 14511 | } |
| 14512 | if( strncmp(zLine, "| end ", 6)==0 ){ |
| 14513 | break; |
| 14514 | } |
| 14515 | rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x", |
| 14516 | &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], |
| 14517 | &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]); |
| 14518 | if( rc==17 ){ |
| 14519 | k = iOffset+j; |
| 14520 | if( k+16<=n ){ |
| 14521 | int ii; |
| 14522 | for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff; |