|
1 | 1 | /**
|
2 | 2 | CEVFS - Compression & Encryption VFS
|
3 |
| -cevfs_mod (shell module activation) |
| 3 | +cevfs_mod (module activation) |
4 | 4 |
|
5 | 5 | Copyright (c) 2016 Ryan Homer, Murage Inc.
|
6 | 6 |
|
@@ -29,8 +29,58 @@ SOFTWARE.
|
29 | 29 |
|
30 | 30 | struct context ctx;
|
31 | 31 |
|
| 32 | +/* |
| 33 | +** Taken from SQLite source code. |
| 34 | +** Check to see if this machine uses EBCDIC. (Yes, believe it or |
| 35 | +** not, there are still machines out there that use EBCDIC.) |
| 36 | +*/ |
| 37 | +#if 'A' == '\301' |
| 38 | +# define SQLITE_EBCDIC 1 |
| 39 | +#else |
| 40 | +# define SQLITE_ASCII 1 |
| 41 | +#endif |
| 42 | + |
| 43 | +/* |
| 44 | +** Taken from SQLite source code. |
| 45 | +** Translate a single byte of Hex into an integer. |
| 46 | +** This routine only works if h really is a valid hexadecimal |
| 47 | +** character: 0..9a..fA..F |
| 48 | +*/ |
| 49 | +static u8 hexToInt(int h){ |
| 50 | + assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') ); |
| 51 | +#ifdef SQLITE_ASCII |
| 52 | + h += 9*(1&(h>>6)); |
| 53 | +#endif |
| 54 | +#ifdef SQLITE_EBCDIC |
| 55 | + h += 9*(1&~(h>>4)); |
| 56 | +#endif |
| 57 | + return (u8)(h & 0xf); |
| 58 | +} |
| 59 | + |
| 60 | +/* |
| 61 | +** Taken from SQLite source code. |
| 62 | +** Convert a BLOB literal of the form "x'hhhhhh'" into its binary |
| 63 | +** value. Return a pointer to its binary value. Space to hold the |
| 64 | +** binary value has been obtained from malloc and must be freed by |
| 65 | +** the calling routine. |
| 66 | +*/ |
| 67 | +static void *hexToBlob(const char *z, int n){ |
| 68 | + char *zBlob; |
| 69 | + int i; |
| 70 | + |
| 71 | + zBlob = (char *)malloc(n/2 + 1); |
| 72 | + n--; |
| 73 | + if( zBlob ){ |
| 74 | + for(i=0; i<n; i+=2){ |
| 75 | + zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]); |
| 76 | + } |
| 77 | + zBlob[i/2] = 0; |
| 78 | + } |
| 79 | + return zBlob; |
| 80 | +} |
| 81 | + |
32 | 82 | void sqlite3_activate_cerod(const char *key) {
|
33 |
| - char *pKeyBytes = sqlite3HexToBlob(NULL, key+2, strlen(key+2)-1); |
| 83 | + char *pKeyBytes = hexToBlob(key+2, strlen(key+2)-1); |
34 | 84 |
|
35 | 85 | ctx.pKey = pKeyBytes; // 32-bit encryption hex key
|
36 | 86 | ctx.nKeySz = kCCKeySizeAES256; // key size in bytes
|
|
0 commit comments