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 a rot13() function and a rot13
14 ** collating sequence.
16 #include "sqlite3ext.h"
17 SQLITE_EXTENSION_INIT1
22 ** Perform rot13 encoding on a single ASCII character.
24 static unsigned char rot13(unsigned char c
){
25 if( c
>='a' && c
<='z' ){
28 }else if( c
>='A' && c
<='Z' ){
36 ** Implementation of the rot13() function.
38 ** Rotate ASCII alphabetic characters by 13 character positions.
39 ** Non-ASCII characters are unchanged. rot13(rot13(X)) should always
42 static void rot13func(
43 sqlite3_context
*context
,
47 const unsigned char *zIn
;
54 if( sqlite3_value_type(argv
[0])==SQLITE_NULL
) return;
55 zIn
= (const unsigned char*)sqlite3_value_text(argv
[0]);
56 nIn
= sqlite3_value_bytes(argv
[0]);
57 if( nIn
<sizeof(zTemp
)-1 ){
60 zOut
= zToFree
= sqlite3_malloc( nIn
+1 );
62 sqlite3_result_error_nomem(context
);
66 for(i
=0; i
<nIn
; i
++) zOut
[i
] = rot13(zIn
[i
]);
68 sqlite3_result_text(context
, (char*)zOut
, i
, SQLITE_TRANSIENT
);
69 sqlite3_free(zToFree
);
73 ** Implement the rot13 collating sequence so that if
79 ** rot13(x)=rot13(y) COLLATE binary
81 static int rot13CollFunc(
83 int nKey1
, const void *pKey1
,
84 int nKey2
, const void *pKey2
86 const char *zA
= (const char*)pKey1
;
87 const char *zB
= (const char*)pKey2
;
89 for(i
=0; i
<nKey1
&& i
<nKey2
; i
++){
90 x
= (int)rot13(zA
[i
]) - (int)rot13(zB
[i
]);
100 int sqlite3_rot_init(
103 const sqlite3_api_routines
*pApi
106 SQLITE_EXTENSION_INIT2(pApi
);
107 (void)pzErrMsg
; /* Unused parameter */
108 rc
= sqlite3_create_function(db
, "rot13", 1, SQLITE_UTF8
, 0,
111 rc
= sqlite3_create_collation(db
, "rot13", SQLITE_UTF8
, 0, rot13CollFunc
);