** Try to deduce the type of file for zName based on its content. Return ** one of the SHELL_OPEN_* constants. ** ** If the file does not exist or is empty but its name looks like a ZIP ** archive and the dfltZip flag is true, then assume it is a ZIP archive. ** Otherwise, assume an ordinary database regardless of the filename if ** the type cannot be determined from content. */
| 25315 | ** the type cannot be determined from content. |
| 25316 | */ |
| 25317 | int deduceDatabaseType(const char *zName, int dfltZip){ |
| 25318 | FILE *f = sqlite3_fopen(zName, "rb"); |
| 25319 | size_t n; |
| 25320 | int rc = SHELL_OPEN_UNSPEC; |
| 25321 | char zBuf[100]; |
| 25322 | if( f==0 ){ |
| 25323 | if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){ |
| 25324 | return SHELL_OPEN_ZIPFILE; |
| 25325 | }else{ |
| 25326 | return SHELL_OPEN_NORMAL; |
| 25327 | } |
| 25328 | } |
| 25329 | n = fread(zBuf, 16, 1, f); |
| 25330 | if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){ |
| 25331 | fclose(f); |
| 25332 | return SHELL_OPEN_NORMAL; |
| 25333 | } |
| 25334 | fseek(f, -25, SEEK_END); |
| 25335 | n = fread(zBuf, 25, 1, f); |
| 25336 | if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){ |
| 25337 | rc = SHELL_OPEN_APPENDVFS; |
| 25338 | }else{ |
| 25339 | fseek(f, -22, SEEK_END); |
| 25340 | n = fread(zBuf, 22, 1, f); |
| 25341 | if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05 |
| 25342 | && zBuf[3]==0x06 ){ |
| 25343 | rc = SHELL_OPEN_ZIPFILE; |
| 25344 | }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){ |
| 25345 | rc = SHELL_OPEN_ZIPFILE; |
| 25346 | } |
| 25347 | } |
| 25348 | fclose(f); |
| 25349 | return rc; |
| 25350 | } |
| 25351 | |
| 25352 | #ifndef SQLITE_OMIT_DESERIALIZE |
| 25353 | /* |
no test coverage detected