System.Data.SQLite
Check-in [a90b51ae3c]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:NTFS EFS (Encrypted file system) and compression support
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sourceforge
Files: files | file ages | folders
SHA1: a90b51ae3c19192e956f9ed134e27fbc18b96ccd
User & Date: rmsimpson 2005-08-25 18:30:08
Context
2005-08-25
18:37
pre 3.2.5 update check-in: f4aa990e73 user: rmsimpson tags: sourceforge
18:30
NTFS EFS (Encrypted file system) and compression support check-in: a90b51ae3c user: rmsimpson tags: sourceforge
05:42
Enhanced schema support and bugfixes check-in: bc941432db user: rmsimpson tags: sourceforge
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to SQLite.Interop/interop.h.

108
109
110
111
112
113
114


115
116
117













118
119
120
121
122
123
124
...
136
137
138
139
140
141
142





















































































































143
144
145
146
147
148
149
      sqlite3VdbeSetColName(v, i, zName, 0);
    }
  }
  generateColumnTypes(pParse, pTabList, pEList);
}

#ifdef OS_WIN



typedef void (__stdcall *SQLITEUSERFUNC)(void *, int, void **);
typedef int  (__stdcall *SQLITECOLLATION)(int, const void *, int, const void*);














// Callback wrappers
int sqlite3_interop_collationfunc(void *pv, int len1, const void *pv1, int len2, const void *pv2)
{
  SQLITECOLLATION *p = (SQLITECOLLATION *)pv;
  return p[0](len1, pv1, len2, pv2);
}
................................................................................
}

void sqlite3_interop_final(sqlite3_context *pctx)
{
  SQLITEUSERFUNC *pf = (SQLITEUSERFUNC *)sqlite3_user_data(pctx);
  pf[2](pctx, 0, 0);
}






















































































































__declspec(dllexport) void __stdcall sqlite3_function_free_callbackcookie(void *pCookie)
{
  if (pCookie)
    free(pCookie);
}








>
>



>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
...
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
      sqlite3VdbeSetColName(v, i, zName, 0);
    }
  }
  generateColumnTypes(pParse, pTabList, pEList);
}

#ifdef OS_WIN

#include <tchar.h>

typedef void (__stdcall *SQLITEUSERFUNC)(void *, int, void **);
typedef int  (__stdcall *SQLITECOLLATION)(int, const void *, int, const void*);

typedef int (__stdcall *ENCRYPTFILEW)(const wchar_t *);
typedef int (__stdcall *ENCRYPTEDSTATUSW)(const wchar_t *, unsigned long *);
typedef int (__stdcall *DECRYPTFILEW)(const wchar_t *, unsigned long);

typedef HANDLE (__stdcall *CREATEFILEW)(
    LPCWSTR,
    DWORD,
    DWORD,
    LPSECURITY_ATTRIBUTES,
    DWORD,
    DWORD,
    HANDLE);

// Callback wrappers
int sqlite3_interop_collationfunc(void *pv, int len1, const void *pv1, int len2, const void *pv2)
{
  SQLITECOLLATION *p = (SQLITECOLLATION *)pv;
  return p[0](len1, pv1, len2, pv2);
}
................................................................................
}

void sqlite3_interop_final(sqlite3_context *pctx)
{
  SQLITEUSERFUNC *pf = (SQLITEUSERFUNC *)sqlite3_user_data(pctx);
  pf[2](pctx, 0, 0);
}

__declspec(dllexport) void __stdcall sqlite3_sleep_interop(int milliseconds)
{
  Sleep(milliseconds);
}

__declspec(dllexport) int sqlite3_encryptfile(const wchar_t *pwszFilename)
{
  HMODULE hMod = LoadLibrary(_T("ADVAPI32"));
  if (hMod == NULL)
  {
    SetLastError(ERROR_NOT_SUPPORTED);
    return 0;
  }

  ENCRYPTFILEW pfunc = (ENCRYPTFILEW)GetProcAddress(hMod, _T("EncryptFileW"));
  if (pfunc == NULL)
  {
    SetLastError(ERROR_NOT_SUPPORTED);
    return 0;
  }

  int n = pfunc(pwszFilename);

  FreeLibrary(hMod);

  return n;
}

__declspec(dllexport) int sqlite3_decryptfile(const wchar_t *pwszFilename)
{
  HMODULE hMod = LoadLibrary(_T("ADVAPI32"));
  if (hMod == NULL)
  {
    SetLastError(ERROR_NOT_SUPPORTED);
    return 0;
  }

  DECRYPTFILEW pfunc = (DECRYPTFILEW)GetProcAddress(hMod, _T("DecryptFileW"));
  if (pfunc == NULL)
  {
    SetLastError(ERROR_NOT_SUPPORTED);
    return 0;
  }

  int n = pfunc(pwszFilename, 0);

  FreeLibrary(hMod);

  return n;
}

__declspec(dllexport) unsigned long sqlite3_encryptedstatus(const wchar_t *pwszFilename, unsigned long *pdwStatus)
{
  HMODULE hMod = LoadLibrary(_T("ADVAPI32"));
  if (hMod == NULL)
  {
    SetLastError(ERROR_NOT_SUPPORTED);
    return 0;
  }

  ENCRYPTEDSTATUSW pfunc = (ENCRYPTEDSTATUSW)GetProcAddress(hMod, _T("FileEncryptionStatusW"));
  if (pfunc == NULL)
  {
    SetLastError(ERROR_NOT_SUPPORTED);
    return 0;
  }

  int n = pfunc(pwszFilename, pdwStatus);

  FreeLibrary(hMod);

  return n;
}

int SetCompression(const wchar_t *pwszFilename, unsigned short ufLevel)
{
#ifdef FSCTL_SET_COMPRESSION
  HMODULE hMod = GetModuleHandle(_T("KERNEL32"));
  if (hMod == NULL)
  {
    SetLastError(ERROR_NOT_SUPPORTED);
    return 0;
  }

  CREATEFILEW pfunc = (CREATEFILEW)GetProcAddress(hMod, _T("CreateFileW"));
  if (pfunc == NULL)
  {
    SetLastError(ERROR_NOT_SUPPORTED);
    return 0;
  }

  HANDLE hFile = pfunc(pwszFilename, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  if (hFile == NULL)
    return 0;

  unsigned long dw = 0;
  int n = DeviceIoControl(hFile, FSCTL_SET_COMPRESSION, &ufLevel, sizeof(ufLevel), NULL, 0, &dw, NULL);

  CloseHandle(hFile);

  return n;
#else
  SetLastError(ERROR_NOT_SUPPORTED);
  return 0;
#endif
}

__declspec(dllexport) int __stdcall sqlite3_compressfile(const wchar_t *pwszFilename)
{
  return SetCompression(pwszFilename, COMPRESSION_FORMAT_DEFAULT);
}

__declspec(dllexport) int __stdcall sqlite3_decompressfile(const wchar_t *pwszFilename)
{
  return SetCompression(pwszFilename, COMPRESSION_FORMAT_NONE);
}

__declspec(dllexport) void __stdcall sqlite3_function_free_callbackcookie(void *pCookie)
{
  if (pCookie)
    free(pCookie);
}

Changes to System.Data.SQLite/SQLiteConnection.cs.

213
214
215
216
217
218
219

















































































220
221
222
223
224
225
226
    /// </summary>
    /// <param name="disposing">True if the connection is being explicitly closed.</param>
    protected override void Dispose(bool disposing)
    {
      base.Dispose(disposing);
      Close();
    }


















































































    /// <summary>
    /// Raises the state change event when the state of the connection changes
    /// </summary>
    /// <param name="newState">The new state.  If it is different from the previous state, an event is raised.</param>
    internal void OnStateChange(ConnectionState newState)
    {







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
    /// </summary>
    /// <param name="disposing">True if the connection is being explicitly closed.</param>
    protected override void Dispose(bool disposing)
    {
      base.Dispose(disposing);
      Close();
    }

    /// <summary>
    /// Creates a database file.  This just creates a zero-byte file which SQLite
    /// will turn into a database when the file is opened properly.
    /// </summary>
    /// <param name="databaseFilename">The file to create</param>
    static public void CreateFile(string databaseFilename)
    {
      IO.FileStream fs = IO.File.Create(databaseFilename);
      fs.Close();
    }

    /// <summary>
    /// On NTFS volumes, this function turns on the compression attribute for the given file.
    /// It must not be open or referenced at the time of the function call.
    /// </summary>
    /// <param name="databaseFilename">The file to compress</param>
    static public void CompressFile(string databaseFilename)
    {
      UnsafeNativeMethods.sqlite3_compressfile(databaseFilename);
    }

    /// <summary>
    /// On NTFS volumes, this function removes the compression attribute for the given file.
    /// It must not be open or referenced at the time of the function call.
    /// </summary>
    /// <param name="databaseFilename">The file to decompress</param>
    static public void DecompressFile(string databaseFilename)
    {
      UnsafeNativeMethods.sqlite3_decompressfile(databaseFilename);
    }

    /// <summary>
    /// On NTFS volumes, this function turns on the EFS (Encrypted File System) attribute
    /// for the given file, which causes the file to be encrypted.
    /// For a full description of EFS, see the MSDN documentation.
    /// </summary>
    /// <remarks>
    /// Requires Win2K and above, plus a valid EFS certificate (which is beyond the scope
    /// of this function description).
    /// </remarks>
    /// <param name="databaseFilename">The file to encrypt</param>
    static public void EncryptFile(string databaseFilename)
    {
      int n = UnsafeNativeMethods.sqlite3_encryptfile(databaseFilename);
      if (n == 0) throw new System.ComponentModel.Win32Exception();
    }

    /// <summary>
    /// On NTFS volumes, this function removes the encryption attribute from the file,
    /// causing the file to be decrypted.  See the MSDN documentation for full details on
    /// EFS (Encrypted File System).
    /// </summary>
    /// <remarks>
    /// Requires Win2K and above, plus a valid EFS certificate (which is beyond the scope
    /// of this function description).
    /// </remarks>
    /// <param name="databaseFilename">The file to decrypt</param>
    static public void DecryptFile(string databaseFilename)
    {
      int n = UnsafeNativeMethods.sqlite3_decryptfile(databaseFilename);
      if (n == 0) throw new System.ComponentModel.Win32Exception();
    }

    /// <summary>
    /// Returns true if the file is encrypted, or false otherwise.
    /// </summary>
    /// <remarks>
    /// Requires Win2K and above, plus a valid EFS certificate (which is beyond the scope
    /// of this function description).
    /// </remarks>
    /// <param name="databaseFilename">The file to check</param>
    /// <returns>true if the file is encrypted</returns>
    static public bool IsEncrypted(string databaseFilename)
    {
      long status;
      int n = UnsafeNativeMethods.sqlite3_encryptedstatus(databaseFilename, out status);
      if (n == 0) throw new System.ComponentModel.Win32Exception();

      return (status == 1);
    }

    /// <summary>
    /// Raises the state change event when the state of the connection changes
    /// </summary>
    /// <param name="newState">The new state.  If it is different from the previous state, an event is raised.</param>
    internal void OnStateChange(ConnectionState newState)
    {

Changes to System.Data.SQLite/UnsafeNativeMethods.cs.

216
217
218
219
220
221
222
223


224














    internal static extern IntPtr sqlite3_value_text16_interop(int p, out int len);

    [DllImport(SQLITE_DLL, CharSet = CharSet.Unicode)]
    internal static extern void sqlite3_result_error16_interop(int context, string strName, int nLen);

    [DllImport(SQLITE_DLL, CharSet = CharSet.Unicode)]
    internal static extern void sqlite3_result_text16_interop(int context, string strName, int nLen, int pvReserved);
  }


}





















|
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
    internal static extern IntPtr sqlite3_value_text16_interop(int p, out int len);

    [DllImport(SQLITE_DLL, CharSet = CharSet.Unicode)]
    internal static extern void sqlite3_result_error16_interop(int context, string strName, int nLen);

    [DllImport(SQLITE_DLL, CharSet = CharSet.Unicode)]
    internal static extern void sqlite3_result_text16_interop(int context, string strName, int nLen, int pvReserved);

    [DllImport(SQLITE_DLL, CharSet = CharSet.Unicode, SetLastError = true)]
    internal static extern int sqlite3_encryptfile(string fileName);

    [DllImport(SQLITE_DLL, CharSet = CharSet.Unicode, SetLastError = true)]
    internal static extern int sqlite3_decryptfile(string fileName);

    [DllImport(SQLITE_DLL, CharSet = CharSet.Unicode, SetLastError = true)]
    internal static extern int sqlite3_encryptedstatus(string fileName, out long fileStatus);

    [DllImport(SQLITE_DLL, CharSet = CharSet.Unicode, SetLastError = true)]
    internal static extern int sqlite3_compressfile(string fileName);

    [DllImport(SQLITE_DLL, CharSet = CharSet.Unicode, SetLastError = true)]
    internal static extern int sqlite3_decompressfile(string fileName);

  }
}