MCPcopy Create free account
hub / github.com/GaijinEntertainment/daScript / readHexDb

Function readHexDb

modules/dasSQLITE/sqlite/shell.c:20161–20241  ·  view source on GitHub ↗

** Reconstruct an in-memory database using the output from the "dbtotxt" ** program. Read content from the file in p->aAuxDb[].zDbFilename. ** If p->aAuxDb[].zDbFilename is 0, then read from standard input. */

Source from the content-addressed store, hash-verified

20159** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
20160*/
20161static unsigned char *readHexDb(ShellState *p, int *pnData){
20162 unsigned char *a = 0;
20163 int nLine;
20164 int n = 0;
20165 int pgsz = 0;
20166 int iOffset = 0;
20167 int j, k;
20168 int rc;
20169 FILE *in;
20170 const char *zDbFilename = p->pAuxDb->zDbFilename;
20171 unsigned int x[16];
20172 char zLine[1000];
20173 if( zDbFilename ){
20174 in = fopen(zDbFilename, "r");
20175 if( in==0 ){
20176 utf8_printf(stderr, "cannot open \"%s\" for reading\n", zDbFilename);
20177 return 0;
20178 }
20179 nLine = 0;
20180 }else{
20181 in = p->in;
20182 nLine = p->lineno;
20183 if( in==0 ) in = stdin;
20184 }
20185 *pnData = 0;
20186 nLine++;
20187 if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
20188 rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
20189 if( rc!=2 ) goto readHexDb_error;
20190 if( n<0 ) goto readHexDb_error;
20191 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
20192 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
20193 a = sqlite3_malloc( n ? n : 1 );
20194 shell_check_oom(a);
20195 memset(a, 0, n);
20196 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
20197 utf8_printf(stderr, "invalid pagesize\n");
20198 goto readHexDb_error;
20199 }
20200 for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
20201 rc = sscanf(zLine, "| page %d offset %d", &j, &k);
20202 if( rc==2 ){
20203 iOffset = k;
20204 continue;
20205 }
20206 if( cli_strncmp(zLine, "| end ", 6)==0 ){
20207 break;
20208 }
20209 rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
20210 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
20211 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
20212 if( rc==17 ){
20213 k = iOffset+j;
20214 if( k+16<=n && k>=0 ){
20215 int ii;
20216 for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
20217 }
20218 }

Callers 1

ifFunction · 0.85

Calls 3

utf8_printfFunction · 0.85
shell_check_oomFunction · 0.85
cli_strncmpFunction · 0.85

Tested by

no test coverage detected