1//
2//  sqlutils.h
3//  Security
4//
5//  Created by Fabrice Gautier on 8/26/11.
6//  Copyright (c) 2011 Apple, Inc. All rights reserved.
7//
8
9/*
10 * sqlutils.h - some wrapper for sql3lite
11 */
12#ifndef _SECURITY_UTILITIES_SQLUTILS_H_
13#define _SECURITY_UTILITIES_SQLUTILS_H_
14
15#include <sqlite3.h>
16
17/* Those are just wrapper around the sqlite3 functions, but they have size_t for some len parameters,
18   and checks for overflow before casting to int */
19static inline int sqlite3_bind_blob_wrapper(sqlite3_stmt* pStmt, int i, const void* zData, size_t n, void(*xDel)(void*))
20{
21    if(n>INT_MAX) return SQLITE_TOOBIG;
22    return sqlite3_bind_blob(pStmt, i, zData, (int)n, xDel);
23}
24
25static inline int sqlite3_bind_text_wrapper(sqlite3_stmt* pStmt, int i, const void* zData, size_t n, void(*xDel)(void*))
26{
27    if(n>INT_MAX) return SQLITE_TOOBIG;
28    return sqlite3_bind_text(pStmt, i, zData, (int)n, xDel);
29}
30
31static inline int sqlite3_prepare_wrapper(sqlite3 *db, const char *zSql, size_t nByte, sqlite3_stmt **ppStmt, const char **pzTail)
32{
33    if(nByte>INT_MAX) return SQLITE_TOOBIG;
34    return sqlite3_prepare(db, zSql, (int)nByte, ppStmt, pzTail);
35}
36
37#endif
38