4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 ******************************************************************************
13 ** This SQLite extension implements SQL functions readfile() and
16 #include "sqlite3ext.h"
17 SQLITE_EXTENSION_INIT1
21 ** Implementation of the "readfile(X)" SQL function. The entire content
22 ** of the file named X is read and returned as a BLOB. NULL is returned
23 ** if the file does not exist or is unreadable.
25 static void readfileFunc(
26 sqlite3_context
*context
,
35 zName
= (const char*)sqlite3_value_text(argv
[0]);
36 if( zName
==0 ) return;
37 in
= fopen(zName
, "rb");
39 fseek(in
, 0, SEEK_END
);
42 pBuf
= sqlite3_malloc( nIn
);
43 if( pBuf
&& 1==fread(pBuf
, nIn
, 1, in
) ){
44 sqlite3_result_blob(context
, pBuf
, nIn
, sqlite3_free
);
52 ** Implementation of the "writefile(X,Y)" SQL function. The argument Y
53 ** is written into file X. The number of bytes written is returned. Or
54 ** NULL is returned if something goes wrong, such as being unable to open
55 ** file X for writing.
57 static void writefileFunc(
58 sqlite3_context
*context
,
67 zFile
= (const char*)sqlite3_value_text(argv
[0]);
68 if( zFile
==0 ) return;
69 out
= fopen(zFile
, "wb");
71 z
= (const char*)sqlite3_value_blob(argv
[1]);
75 rc
= fwrite(z
, 1, sqlite3_value_bytes(argv
[1]), out
);
78 sqlite3_result_int64(context
, rc
);
85 int sqlite3_fileio_init(
88 const sqlite3_api_routines
*pApi
91 SQLITE_EXTENSION_INIT2(pApi
);
92 (void)pzErrMsg
; /* Unused parameter */
93 rc
= sqlite3_create_function(db
, "readfile", 1, SQLITE_UTF8
, 0,
96 rc
= sqlite3_create_function(db
, "writefile", 2, SQLITE_UTF8
, 0,