Skip to content

Instantly share code, notes, and snippets.

@VitaSmith
Created October 29, 2017 00:19
Show Gist options
  • Save VitaSmith/b60071cf8b67b5695592001779507037 to your computer and use it in GitHub Desktop.
Save VitaSmith/b60071cf8b67b5695592001779507037 to your computer and use it in GitHub Desktop.
An implemention of a full SQLite VFS for Sony PS Vita, based on xyzz's original work
/*
Vita SQLite handling function for R/W
From https://github.com/henkaku/offline-installer/blob/master/src/vita_sqlite.c
Copyright (C) 2016 xyzz
Copyright (C) 2017 VitaSmith
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
Note: We implement our own VFS for full SQLite DB access because the
native Vita one, called "psp2", only allows read operations (which
Sony probably did to avoid exploit propagation from potential SQLite
vulnerabilities).
Short of using the VFS implementation below, any attempt to create
or write to an SQLite DB will fail with an SQLITE_CANTOPEN error...
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <psp2/io/fcntl.h>
#include <psp2/io/stat.h>
#include <psp2/kernel/threadmgr.h>
#include <psp2/rtc.h>
#include <psp2/sqlite.h>
#include "sqlite3.h"
//#define VERBOSE 1
#if VERBOSE
extern int psvDebugScreenPrintf(const char *format, ...);
#define LOG psvDebugScreenPrintf
#else
#define LOG(...)
#endif
#define IS_ERROR(x) ((unsigned)x & 0x80000000)
typedef struct VitaFile
{
sqlite3_file base;
unsigned fd;
} VitaFile;
// File ops
static int vita_xClose(sqlite3_file *pFile)
{
VitaFile *p = (VitaFile*)pFile;
sceIoClose(p->fd);
LOG("close %x\n", p->fd);
return SQLITE_OK;
}
static int vita_xRead(sqlite3_file *pFile, void *zBuf, int iAmt, sqlite_int64 iOfst)
{
VitaFile *p = (VitaFile*)pFile;
memset(zBuf, 0, iAmt);
sceIoLseek(p->fd, iOfst, SCE_SEEK_SET);
int read = sceIoRead(p->fd, zBuf, iAmt);
LOG("read %x %x %x => %x\n", p->fd, zBuf, iAmt, read);
if (read == iAmt)
return SQLITE_OK;
else if IS_ERROR(read)
return SQLITE_IOERR_READ;
return SQLITE_IOERR_SHORT_READ;
}
static int vita_xWrite(sqlite3_file *pFile, const void *zBuf, int iAmt, sqlite_int64 iOfst)
{
VitaFile *p = (VitaFile*)pFile;
int ofst = sceIoLseek(p->fd, iOfst, SCE_SEEK_SET);
LOG("seek %x %x => %x\n", p->fd, iOfst, ofst);
if (ofst != iOfst)
return SQLITE_IOERR_WRITE;
int write = sceIoWrite(p->fd, zBuf, iAmt);
LOG("write %x %x %x => %x\n", p->fd, zBuf, iAmt);
if (write != iAmt)
return SQLITE_IOERR_WRITE;
return SQLITE_OK;
}
static int vita_xTruncate(sqlite3_file *pFile, sqlite_int64 size)
{
LOG("truncate\n");
return SQLITE_OK;
}
static int vita_xSync(sqlite3_file *pFile, int flags)
{
return SQLITE_OK;
}
static int vita_xFileSize(sqlite3_file *pFile, sqlite_int64 *pSize)
{
VitaFile *p = (VitaFile*)pFile;
SceIoStat stat = { 0 };
sceIoGetstatByFd(p->fd, &stat);
LOG("filesize %x => %x\n", p->fd, stat.st_size);
*pSize = stat.st_size;
return SQLITE_OK;
}
static int vita_xLock(sqlite3_file *pFile, int eLock)
{
return SQLITE_OK;
}
static int vita_xUnlock(sqlite3_file *pFile, int eLock)
{
return SQLITE_OK;
}
static int vita_xCheckReservedLock(sqlite3_file *pFile, int *pResOut)
{
*pResOut = 0;
return SQLITE_OK;
}
static int vita_xFileControl(sqlite3_file *pFile, int op, void *pArg)
{
return SQLITE_OK;
}
static int vita_xSectorSize(sqlite3_file *pFile)
{
return 0;
}
static int vita_xDeviceCharacteristics(sqlite3_file *pFile)
{
return 0;
}
// VFS ops
static int vita_xOpen(sqlite3_vfs *vfs, const char *name, sqlite3_file *file, int flags, int *outFlags)
{
LOG("OPEN %s: %08x ", name, flags);
static const sqlite3_io_methods vitaio = {
1,
vita_xClose,
vita_xRead,
vita_xWrite,
vita_xTruncate,
vita_xSync,
vita_xFileSize,
vita_xLock,
vita_xUnlock,
vita_xCheckReservedLock,
vita_xFileControl,
vita_xSectorSize,
vita_xDeviceCharacteristics,
};
VitaFile *p = (VitaFile*)file;
unsigned oflags = 0;
if (flags & SQLITE_OPEN_EXCLUSIVE)
oflags |= SCE_O_EXCL;
if (flags & SQLITE_OPEN_CREATE)
oflags |= SCE_O_CREAT;
if (flags & SQLITE_OPEN_READONLY)
oflags |= SCE_O_RDONLY;
if (flags & SQLITE_OPEN_READWRITE)
oflags |= SCE_O_RDWR;
memset(p, 0, sizeof(*p));
// SQLite expects a journal file to be created if SQLITE_OPEN_READWRITE was
// specified, *EVEN* if SQLITE_OPEN_CREATE wasn't, which we address in this loop.
int i;
for (i = 0; i < 2; i++) {
p->fd = sceIoOpen(name, oflags, 0777);
if (!IS_ERROR(p->fd))
break;
if ((flags & SQLITE_OPEN_MAIN_JOURNAL) && (flags & SQLITE_OPEN_READWRITE))
oflags |= SCE_O_CREAT;
else
break;
}
LOG("(i = %d) FD=%x\n", i, p->fd);
if (IS_ERROR(p->fd))
return SQLITE_CANTOPEN;
if (outFlags)
*outFlags = flags;
p->base.pMethods = &vitaio;
return SQLITE_OK;
}
int vita_xDelete(sqlite3_vfs *vfs, const char *name, int syncDir)
{
int ret = sceIoRemove(name);
LOG("DELETE %s: 0x%08x\n", name, ret);
if (IS_ERROR(ret))
return SQLITE_IOERR_DELETE;
return SQLITE_OK;
}
int vita_xAccess(sqlite3_vfs *vfs, const char *name, int flags, int *pResOut)
{
*pResOut = 1;
return SQLITE_OK;
}
int vita_xFullPathname(sqlite3_vfs *vfs, const char *zName, int nOut, char *zOut)
{
snprintf(zOut, nOut, "%s", zName);
return 0;
}
void* vita_xDlOpen(sqlite3_vfs *vfs, const char *zFilename)
{
return NULL;
}
void vita_xDlError(sqlite3_vfs *vfs, int nByte, char *zErrMsg)
{
}
void(*vita_xDlSym(sqlite3_vfs *vfs, void*p, const char *zSymbol))(void)
{
return NULL;
}
void vita_xDlClose(sqlite3_vfs *vfs, void*p)
{
}
int vita_xRandomness(sqlite3_vfs *vfs, int nByte, char *zOut)
{
return SQLITE_OK;
}
int vita_xSleep(sqlite3_vfs *vfs, int microseconds)
{
sceKernelDelayThread(microseconds);
return SQLITE_OK;
}
int vita_xCurrentTime(sqlite3_vfs *vfs, double *pTime)
{
time_t t = 0;
SceDateTime time = { 0 };
sceRtcGetCurrentClock(&time, 0);
sceRtcGetTime_t(&time, &t);
*pTime = t / 86400.0 + 2440587.5;
return SQLITE_OK;
}
int vita_xGetLastError(sqlite3_vfs *vfs, int e, char *err)
{
return 0;
}
sqlite3_vfs vita_vfs = {
.iVersion = 1,
.szOsFile = sizeof(VitaFile),
.mxPathname = 0x100,
.pNext = NULL,
.zName = "psp2_rw",
.pAppData = NULL,
.xOpen = vita_xOpen,
.xDelete = vita_xDelete,
.xAccess = vita_xAccess,
.xFullPathname = vita_xFullPathname,
.xDlOpen = vita_xDlOpen,
.xDlError = vita_xDlError,
.xDlSym = vita_xDlSym,
.xDlClose = vita_xDlClose,
.xRandomness = vita_xRandomness,
.xSleep = vita_xSleep,
.xCurrentTime = vita_xCurrentTime,
.xGetLastError = vita_xGetLastError,
};
int sqlite_init()
{
SceSqliteMallocMethods mf = {
(void* (*) (int)) malloc,
(void* (*) (void*, int)) realloc,
free
};
sceSqliteConfigMallocMethods(&mf);
return sqlite3_vfs_register(&vita_vfs, 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment