Skip to content

Commit 60778a4

Browse files
committed
Avoid use of sqlite3HexToBlob.
1 parent e1d1994 commit 60778a4

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

cevfs_build/cevfs_mod.c

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
CEVFS - Compression & Encryption VFS
3-
cevfs_mod (shell module activation)
3+
cevfs_mod (module activation)
44
55
Copyright (c) 2016 Ryan Homer, Murage Inc.
66
@@ -29,8 +29,58 @@ SOFTWARE.
2929

3030
struct context ctx;
3131

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+
3282
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);
3484

3585
ctx.pKey = pKeyBytes; // 32-bit encryption hex key
3686
ctx.nKeySz = kCCKeySizeAES256; // key size in bytes

0 commit comments

Comments
 (0)