/*
** 2026 October 13
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
******************************************************************************
**
** The "riscos" sqlite3_vfs: file access through FileSwitch SWIs
** (OS_Find, OS_GBPB, OS_Args, OS_File, OS_FSControl).
**
** RISC OS has no byte-range locks, but FileSwitch refuses a second open
** of a file that is already open for update, so a database opened for
** writing is exclusive to one handle.  Lock levels are tracked in memory.
** There is no shared memory, so WAL mode is not available.
*/
#include "os_riscos.h"
#include "sqlite3.h"

#if OS_RISCOS              /* This file is used on RISC OS only */

#include <stdlib.h>
#include <string.h>
#include "kernel.h"

#ifndef OS_Word
# define OS_Word               0x07
#endif
#ifndef OS_File
# define OS_File               0x08
#endif
#ifndef OS_Args
# define OS_Args               0x09
#endif
#ifndef OS_GBPB
# define OS_GBPB               0x0C
#endif
#ifndef OS_Find
# define OS_Find               0x0D
#endif
#ifndef OS_FSControl
# define OS_FSControl          0x29
#endif
#ifndef OS_ReadMonotonicTime
# define OS_ReadMonotonicTime  0x42
#endif
#ifndef OS_ReadSysInfo
# define OS_ReadSysInfo        0x58
#endif
#define RISCOS_CryptRandom_Byte 0x51980

#define OSFile_Delete              6
#define OSFile_ReadStampedNoPath  23
#define OSFile_NotFound            0
#define OSFile_IsFile              1
#define OSFile_IsImage             3
#define OSFile_AttrOwnerRead    0x01
#define OSFile_AttrOwnerWrite   0x02
#define OSFile_AttrLocked       0x08

#define OSArgs_ReadExt             2
#define OSArgs_SetExt              3

#define OSGBPB_WriteAt             1
#define OSGBPB_ReadAt              3

#define OSFind_Close            0x00
#define OSFind_Openin           0x40
#define OSFind_Openout          0x80
#define OSFind_Openup           0xC0
#define osfind_NO_PATH          0x03
#define osfind_ERROR_IF_DIR     0x08
#define osfind_ERROR_IF_ABSENT  0x04

#define OSFSControl_CanonicalisePath 37

#define RISCOS_MAX_PATHNAME 1024
#define RISCOS_MAX_FILE_OFFSET ((i64)0xFFFFFFFF)

typedef sqlite3_int64 i64;

/*
** The riscosFile structure is a subclass of sqlite3_file specific to the
** RISC OS portability layer.
*/
typedef struct riscosFile riscosFile;
struct riscosFile {
  sqlite3_file base;        /* Base class. Must be first. */
  int h;                    /* OS_Find file handle */
  int eLock;                /* SQLITE_LOCK_* level held */
  int openFlags;            /* SQLITE_OPEN_* flags used to open */
  char *zDelPath;           /* Delete this file on close, or NULL */
  i64 offset;               /* File pointer after the last read/write */
};

static unsigned int riscosTempCounter = 0;

/*
** Small SWI helpers.
*/
static _kernel_oserror *riscosFind(int reason, const char *zName, int *pH){
  _kernel_swi_regs r;
  _kernel_oserror *e;
  r.r[0] = reason;
  r.r[1] = (int)zName;
  r.r[2] = 0;
  e = _kernel_swi(OS_Find, &r, &r);
  *pH = e ? 0 : r.r[0];
  return e;
}

static void riscosCloseHandle(int h){
  _kernel_swi_regs r;
  if( h==0 ) return;
  r.r[0] = OSFind_Close;
  r.r[1] = h;
  _kernel_swi(OS_Find, &r, &r);
}

/* Object type (OSFile_*) of zName, with its attributes in *pAttr */
static int riscosObjectType(const char *zName, int *pAttr){
  _kernel_swi_regs r;
  r.r[0] = OSFile_ReadStampedNoPath;
  r.r[1] = (int)zName;
  if( _kernel_swi(OS_File, &r, &r)!=NULL ) return OSFile_NotFound;
  if( pAttr ) *pAttr = r.r[5];
  return r.r[0];
}

static int riscosFileExists(const char *zName){
  int t = riscosObjectType(zName, 0);
  return t==OSFile_IsFile || t==OSFile_IsImage;
}

static unsigned int riscosMonotonicTime(void){
  _kernel_swi_regs r;
  if( _kernel_swi(OS_ReadMonotonicTime, &r, &r)!=NULL ) return 0;
  return (unsigned int)r.r[0];
}

/* Centiseconds since 1900-01-01 00:00 UTC from the 5-byte clock */
static i64 riscosRealTime(void){
  unsigned int block[2];
  unsigned char *b = (unsigned char *)block;
  i64 t = 0;
  int i;
  block[0] = 0;
  block[1] = 0;
  b[0] = 3;                 /* OS_Word 14,3: read 5-byte UTC time */
  if( _kernel_osword(14, (int *)block)==_kernel_ERROR ) return 0;
  for(i=4; i>=0; i--) t = (t<<8) | b[i];
  return t;
}

/*
** Close a file.
*/
static int riscosClose(sqlite3_file *id){
  riscosFile *p = (riscosFile *)id;
  riscosCloseHandle(p->h);
  p->h = 0;
  if( p->zDelPath ){
    _kernel_swi_regs r;
    r.r[0] = OSFile_Delete;
    r.r[1] = (int)p->zDelPath;
    _kernel_swi(OS_File, &r, &r);
    sqlite3_free(p->zDelPath);
    p->zDelPath = 0;
  }
  p->eLock = SQLITE_LOCK_NONE;
  return SQLITE_OK;
}

/*
** Read data from a file into a buffer.  A short read fills the rest of
** the buffer with zeros and returns SQLITE_IOERR_SHORT_READ.
*/
static int riscosRead(sqlite3_file *id, void *pBuf, int amt, i64 iOfst){
  riscosFile *p = (riscosFile *)id;
  _kernel_swi_regs r;
  int got;
  if( iOfst<0 || iOfst+amt>RISCOS_MAX_FILE_OFFSET ) return SQLITE_IOERR_READ;
  r.r[0] = OSGBPB_ReadAt;
  r.r[1] = p->h;
  r.r[2] = (int)pBuf;
  r.r[3] = amt;
  r.r[4] = (int)(unsigned int)iOfst;
  if( _kernel_swi(OS_GBPB, &r, &r)!=NULL ) return SQLITE_IOERR_READ;
  got = amt - r.r[3];
  p->offset = iOfst + got;
  if( got==amt ) return SQLITE_OK;
  memset((char *)pBuf + got, 0, amt - got);
  return SQLITE_IOERR_SHORT_READ;
}

/*
** Write data from a buffer into a file.  Writing past the end extends
** the file (FileSwitch zero-fills any gap).
*/
static int riscosWrite(sqlite3_file *id, const void *pBuf, int amt, i64 iOfst){
  riscosFile *p = (riscosFile *)id;
  _kernel_swi_regs r;
  if( iOfst<0 || iOfst+amt>RISCOS_MAX_FILE_OFFSET ) return SQLITE_FULL;
  r.r[0] = OSGBPB_WriteAt;
  r.r[1] = p->h;
  r.r[2] = (int)pBuf;
  r.r[3] = amt;
  r.r[4] = (int)(unsigned int)iOfst;
  if( _kernel_swi(OS_GBPB, &r, &r)!=NULL ) return SQLITE_IOERR_WRITE;
  p->offset = iOfst + (amt - r.r[3]);
  return r.r[3]==0 ? SQLITE_OK : SQLITE_IOERR_WRITE;
}

/*
** Truncate an open file to a specified size.
*/
static int riscosTruncate(sqlite3_file *id, i64 nByte){
  riscosFile *p = (riscosFile *)id;
  _kernel_swi_regs r;
  if( nByte<0 || nByte>RISCOS_MAX_FILE_OFFSET ) return SQLITE_IOERR_TRUNCATE;
  r.r[0] = OSArgs_SetExt;
  r.r[1] = p->h;
  r.r[2] = (int)(unsigned int)nByte;
  if( _kernel_swi(OS_Args, &r, &r)!=NULL ) return SQLITE_IOERR_TRUNCATE;
  return SQLITE_OK;
}

/*
** FileSwitch writes buffered data out when the file is closed.
*/
static int riscosSync(sqlite3_file *id, int flags){
  (void)id;
  (void)flags;
  return SQLITE_OK;
}

/*
** Determine the current size of a file in bytes.
*/
static int riscosFileSize(sqlite3_file *id, i64 *pSize){
  riscosFile *p = (riscosFile *)id;
  _kernel_swi_regs r;
  r.r[0] = OSArgs_ReadExt;
  r.r[1] = p->h;
  if( _kernel_swi(OS_Args, &r, &r)!=NULL ){
    *pSize = 0;
    return SQLITE_IOERR_FSTAT;
  }
  *pSize = (i64)(unsigned int)r.r[2];
  return SQLITE_OK;
}

/*
** Locking.  The open handle already excludes other writers, so only the
** lock level is recorded.
*/
static int riscosLock(sqlite3_file *id, int eLock){
  riscosFile *p = (riscosFile *)id;
  if( p->eLock<eLock ) p->eLock = eLock;
  return SQLITE_OK;
}

static int riscosUnlock(sqlite3_file *id, int eLock){
  riscosFile *p = (riscosFile *)id;
  if( p->eLock>eLock ) p->eLock = eLock;
  return SQLITE_OK;
}

static int riscosCheckReservedLock(sqlite3_file *id, int *pResOut){
  riscosFile *p = (riscosFile *)id;
  *pResOut = p->eLock>=SQLITE_LOCK_RESERVED;
  return SQLITE_OK;
}

static int riscosFileControl(sqlite3_file *id, int op, void *pArg){
  riscosFile *p = (riscosFile *)id;
  if( op==SQLITE_FCNTL_LOCKSTATE ){
    *(int *)pArg = p->eLock;
    return SQLITE_OK;
  }
  return SQLITE_NOTFOUND;
}

static int riscosSectorSize(sqlite3_file *id){
  (void)id;
  return 512;
}

static int riscosDeviceCharacteristics(sqlite3_file *id){
  (void)id;
  return 0;
}

/*
** Shared memory is not available (no mmap), so WAL cannot be used.
*/
static int riscosShmMap(sqlite3_file *id, int iPg, int pgsz, int bExtend,
                        void volatile **pp){
  (void)id; (void)iPg; (void)pgsz; (void)bExtend;
  *pp = 0;
  return SQLITE_CANTOPEN;
}

static int riscosShmLock(sqlite3_file *id, int ofst, int n, int flags){
  (void)id; (void)ofst; (void)n; (void)flags;
  return SQLITE_CANTOPEN;
}

static void riscosShmBarrier(sqlite3_file *id){
  (void)id;
}

static int riscosShmUnmap(sqlite3_file *id, int deleteFlag){
  (void)id; (void)deleteFlag;
  return SQLITE_OK;
}

static const sqlite3_io_methods riscosIoMethods = {
  2,                              /* iVersion */
  riscosClose,
  riscosRead,
  riscosWrite,
  riscosTruncate,
  riscosSync,
  riscosFileSize,
  riscosLock,
  riscosUnlock,
  riscosCheckReservedLock,
  riscosFileControl,
  riscosSectorSize,
  riscosDeviceCharacteristics,
  riscosShmMap,
  riscosShmLock,
  riscosShmBarrier,
  riscosShmUnmap,
  0,                              /* xFetch */
  0                               /* xUnfetch */
};

/*
** Fill zBuf (nBuf bytes) with a new temporary file name in
** sqlite3_temp_directory, <Wimp$ScrapDir> or <Temp$Dir>, with leafname
** SQLxxxxxxx (ten characters, safe on short-name filing systems).
*/
static int riscosTempName(int nBuf, char *zBuf){
  static const char zHex[] = "0123456789ABCDEF";
  const char *zDir = sqlite3_temp_directory;
  int tries;
  if( zDir==0 || zDir[0]==0 ) zDir = getenv("Wimp$ScrapDir");
  if( zDir==0 || zDir[0]==0 ) zDir = getenv("Temp$Dir");
  if( zDir==0 || zDir[0]==0 ) zDir = "<Wimp$ScrapDir>";
  for(tries=0; tries<100; tries++){
    unsigned int u;
    char zLeaf[16];
    int i, n;
    riscosTempCounter++;
    u = (riscosTempCounter<<20) ^ (riscosMonotonicTime()*2654435761u)
        ^ (unsigned int)tries;
    strcpy(zLeaf, SQLITE_RISCOS_TEMP_PREFIX);
    n = (int)strlen(zLeaf);
    for(i=0; i<7; i++){
      zLeaf[n++] = zHex[u & 15];
      u >>= 4;
    }
    zLeaf[n] = 0;
    if( (int)(strlen(zDir) + 1 + n + 1) > nBuf ) return SQLITE_CANTOPEN;
    strcpy(zBuf, zDir);
    strcat(zBuf, ".");
    strcat(zBuf, zLeaf);
    if( riscosObjectType(zBuf, 0)==OSFile_NotFound ) return SQLITE_OK;
  }
  return SQLITE_CANTOPEN;
}

/*
** Open a file.  A NULL zName (temporary database, temporary file,
** sub-journal or master journal without a name) gets a scrap file name.
*/
static int riscosOpen(sqlite3_vfs *pVfs, sqlite3_filename zName,
                      sqlite3_file *id, int flags, int *pOutFlags){
  riscosFile *p = (riscosFile *)id;
  char zTemp[RISCOS_MAX_PATHNAME+1];
  const char *zPath = zName;
  int isReadWrite = (flags & SQLITE_OPEN_READWRITE)!=0;
  int isCreate = (flags & SQLITE_OPEN_CREATE)!=0;
  int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE)!=0;
  int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE)!=0;
  int h = 0;
  (void)pVfs;

  memset(p, 0, sizeof(*p));
  if( zPath==0 ){
    int rc = riscosTempName(sizeof(zTemp), zTemp);
    if( rc!=SQLITE_OK ) return rc;
    zPath = zTemp;
    isCreate = 1;
    isExclusive = 1;
    isReadWrite = 1;
    flags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
    flags &= ~SQLITE_OPEN_READONLY;
  }

  if( riscosFileExists(zPath) ){
    if( isExclusive && isCreate ) return SQLITE_CANTOPEN;
  }else{
    if( !isCreate || !isReadWrite ) return SQLITE_CANTOPEN;
    if( riscosFind(OSFind_Openout | osfind_NO_PATH | osfind_ERROR_IF_DIR,
                   zPath, &h)!=NULL || h==0 ){
      return SQLITE_CANTOPEN;
    }
    riscosCloseHandle(h);
    h = 0;
  }

  if( isReadWrite ){
    riscosFind(OSFind_Openup | osfind_NO_PATH | osfind_ERROR_IF_DIR
               | osfind_ERROR_IF_ABSENT, zPath, &h);
    if( h==0 && !isDelete ){
      /* Locked file or read-only media: fall back to read-only */
      riscosFind(OSFind_Openin | osfind_NO_PATH | osfind_ERROR_IF_DIR
                 | osfind_ERROR_IF_ABSENT, zPath, &h);
      if( h!=0 ){
        flags &= ~(SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
        flags |= SQLITE_OPEN_READONLY;
      }
    }
  }else{
    riscosFind(OSFind_Openin | osfind_NO_PATH | osfind_ERROR_IF_DIR
               | osfind_ERROR_IF_ABSENT, zPath, &h);
  }
  if( h==0 ) return SQLITE_CANTOPEN;

  if( isDelete ){
    p->zDelPath = sqlite3_mprintf("%s", zPath);
    if( p->zDelPath==0 ){
      riscosCloseHandle(h);
      return SQLITE_NOMEM;
    }
  }
  p->h = h;
  p->eLock = SQLITE_LOCK_NONE;
  p->openFlags = flags;
  p->offset = 0;
  p->base.pMethods = &riscosIoMethods;
  if( pOutFlags ) *pOutFlags = flags;
  return SQLITE_OK;
}

/*
** Delete the named file.
*/
static int riscosDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
  _kernel_swi_regs r;
  (void)pVfs;
  (void)dirSync;
  if( !riscosFileExists(zPath) ) return SQLITE_IOERR_DELETE_NOENT;
  r.r[0] = OSFile_Delete;
  r.r[1] = (int)zPath;
  if( _kernel_swi(OS_File, &r, &r)!=NULL ) return SQLITE_IOERR_DELETE;
  return SQLITE_OK;
}

/*
** Test for the existence or accessibility of a file.
*/
static int riscosAccess(sqlite3_vfs *pVfs, const char *zPath, int flags,
                        int *pResOut){
  int attr = 0;
  int type = riscosObjectType(zPath, &attr);
  (void)pVfs;
  if( type!=OSFile_IsFile && type!=OSFile_IsImage ){
    *pResOut = 0;
  }else if( flags==SQLITE_ACCESS_READWRITE ){
    *pResOut = (attr & OSFile_AttrOwnerWrite)!=0
            && (attr & OSFile_AttrLocked)==0;
  }else if( flags==SQLITE_ACCESS_READ ){
    *pResOut = (attr & OSFile_AttrOwnerRead)!=0;
  }else{
    *pResOut = 1;
  }
  return SQLITE_OK;
}

/*
** Turn a relative pathname into a full canonical pathname.
*/
static int riscosFullPathname(sqlite3_vfs *pVfs, const char *zPath,
                              int nOut, char *zOut){
  _kernel_swi_regs r;
  (void)pVfs;
  if( zPath[0]!=0 ){
    r.r[0] = OSFSControl_CanonicalisePath;
    r.r[1] = (int)zPath;
    r.r[2] = (int)zOut;
    r.r[3] = 0;
    r.r[4] = 0;
    r.r[5] = nOut;
    if( _kernel_swi(OS_FSControl, &r, &r)==NULL && r.r[5]>=0 ){
      return SQLITE_OK;
    }
  }
  if( (int)strlen(zPath)>=nOut ) return SQLITE_CANTOPEN;
  strcpy(zOut, zPath);
  return SQLITE_OK;
}

/*
** No dynamic loading on RISC OS.
*/
static void *riscosDlOpen(sqlite3_vfs *pVfs, const char *zPath){
  (void)pVfs;
  (void)zPath;
  return 0;
}

static void riscosDlError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
  (void)pVfs;
  if( nBuf>0 ){
    strncpy(zBuf, "Loadable extensions are not supported on RISC OS", nBuf-1);
    zBuf[nBuf-1] = 0;
  }
}

static void (*riscosDlSym(sqlite3_vfs *pVfs, void *pH, const char *z))(void){
  (void)pVfs;
  (void)pH;
  (void)z;
  return 0;
}

static void riscosDlClose(sqlite3_vfs *pVfs, void *pH){
  (void)pVfs;
  (void)pH;
}

/*
** Seed data for SQLite's PRNG: the real-time and monotonic clocks,
** OS_ReadSysInfo machine IDs, stack/heap addresses and, when the
** CryptRandom module is loaded, its output.
*/
static int riscosRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
  unsigned int seed[12];
  unsigned int x;
  _kernel_swi_regs r;
  i64 now;
  int i;
  (void)pVfs;

  memset(seed, 0, sizeof(seed));
  now = riscosRealTime();
  seed[0] = (unsigned int)now;
  seed[1] = (unsigned int)(now>>32);
  seed[2] = riscosMonotonicTime();
  r.r[0] = 2;               /* OS_ReadSysInfo 2: chip presence and IDs */
  if( _kernel_swi(OS_ReadSysInfo, &r, &r)==NULL ){
    seed[3] = r.r[0]; seed[4] = r.r[1]; seed[5] = r.r[2];
    seed[6] = r.r[3]; seed[7] = r.r[4];
  }
  seed[8] = (unsigned int)&r;
  seed[9] = (unsigned int)zBuf;
  seed[10] = ++riscosTempCounter;

  x = 2166136261u;
  for(i=0; i<11; i++){
    x = (x ^ seed[i]) * 16777619u;
  }
  for(i=0; i<nBuf; i++){
    unsigned int c = 0;
    if( _kernel_swi(RISCOS_CryptRandom_Byte, &r, &r)==NULL ){
      c = (unsigned int)r.r[0];
    }
    x ^= x << 13;
    x ^= x >> 17;
    x ^= x << 5;
    x += riscosMonotonicTime() + (unsigned int)i;
    zBuf[i] = (char)((x >> 11) ^ c);
  }
  return nBuf;
}

/*
** Sleep for at least the requested time (busy wait on the monotonic
** centisecond clock).  Returns the time slept in microseconds.
*/
static int riscosSleep(sqlite3_vfs *pVfs, int microseconds){
  unsigned int cs = (unsigned int)(microseconds + 9999) / 10000;
  unsigned int start = riscosMonotonicTime();
  (void)pVfs;
  while( riscosMonotonicTime() - start < cs ){
    /* wait */
  }
  return (int)(cs * 10000);
}

/*
** Current time as milliseconds since the Julian epoch.  The RISC OS clock
** counts centiseconds from 1900-01-01 00:00 UTC, Julian day 2415020.5.
*/
static int riscosCurrentTimeInt64(sqlite3_vfs *pVfs, i64 *piNow){
  static const i64 epoch1900 = ((i64)24150205) * 8640000;
  i64 cs = riscosRealTime();
  (void)pVfs;
  if( cs==0 ) return SQLITE_ERROR;
  *piNow = epoch1900 + cs * 10;
  return SQLITE_OK;
}

static int riscosCurrentTime(sqlite3_vfs *pVfs, double *prNow){
  i64 i = 0;
  int rc = riscosCurrentTimeInt64(pVfs, &i);
  *prNow = i / 86400000.0;
  return rc;
}

static int riscosGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
  (void)pVfs;
  (void)nBuf;
  (void)zBuf;
  return 0;
}

static int riscosSetSystemCall(sqlite3_vfs *pVfs, const char *zName,
                               sqlite3_syscall_ptr pNew){
  (void)pVfs;
  (void)zName;
  (void)pNew;
  return SQLITE_NOTFOUND;
}

static sqlite3_syscall_ptr riscosGetSystemCall(sqlite3_vfs *pVfs,
                                               const char *zName){
  (void)pVfs;
  (void)zName;
  return 0;
}

static const char *riscosNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
  (void)pVfs;
  (void)zName;
  return 0;
}

/*
** FPA builds: SharedCLibrary enables the overflow/divide/invalid traps,
** but SQLite expects IEEE results (Inf/NaN).  s.riscosfp clears the trap
** enables while SQLite is initialised.  Define SQLITE_RISCOS_KEEP_FP_TRAPS
** to leave the application's FP status alone.
*/
#if defined(SQLITE_MIXED_ENDIAN_64BIT_FLOAT) \
    && !defined(SQLITE_RISCOS_KEEP_FP_TRAPS)
# define RISCOS_FPA_TRAPS_OFF 1
extern unsigned int riscos_fp_traps_off(void);
extern void riscos_fp_restore(unsigned int fpsr);
static unsigned int riscosSavedFpsr;
static int riscosFpsrSaved = 0;
#endif

static sqlite3_vfs riscosVfs = {
  3,                        /* iVersion */
  sizeof(riscosFile),       /* szOsFile */
  RISCOS_MAX_PATHNAME,      /* mxPathname */
  0,                        /* pNext */
  "riscos",                 /* zName */
  0,                        /* pAppData */
  riscosOpen,
  riscosDelete,
  riscosAccess,
  riscosFullPathname,
  riscosDlOpen,
  riscosDlError,
  riscosDlSym,
  riscosDlClose,
  riscosRandomness,
  riscosSleep,
  riscosCurrentTime,
  riscosGetLastError,
  riscosCurrentTimeInt64,
  riscosSetSystemCall,
  riscosGetSystemCall,
  riscosNextSystemCall
};

/*
** Register the "riscos" VFS as the default.  Refuses to start if the
** double word order doesn't match SQLITE_MIXED_ENDIAN_64BIT_FLOAT, as
** REAL values would otherwise be stored in a non-portable format.
*/
int sqlite3_os_init(void){
  union { double d; unsigned int w[2]; } u;
  u.d = 1.0;
#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
  if( u.w[0]!=0x3FF00000 ) return SQLITE_ERROR;
#else
  if( u.w[1]!=0x3FF00000 ) return SQLITE_ERROR;
#endif
#ifdef RISCOS_FPA_TRAPS_OFF
  if( !riscosFpsrSaved ){
    riscosSavedFpsr = riscos_fp_traps_off();
    riscosFpsrSaved = 1;
  }
#endif
  return sqlite3_vfs_register(&riscosVfs, 1);
}

int sqlite3_os_end(void){
#ifdef RISCOS_FPA_TRAPS_OFF
  if( riscosFpsrSaved ){
    riscos_fp_restore(riscosSavedFpsr);
    riscosFpsrSaved = 0;
  }
#endif
  return SQLITE_OK;
}

#endif /* OS_RISCOS */
