1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-01 06:27:03 +03:00

Add the rot13.c loadable extension.

FossilOrigin-Name: 8f9bd8e7a88eb11fb17d29954fa4b8c2840a5019
This commit is contained in:
drh
2013-05-15 13:05:03 +00:00
parent 6cf9d8d63a
commit 15912e70b7
3 changed files with 121 additions and 6 deletions

114
ext/misc/rot13.c Normal file
View File

@ -0,0 +1,114 @@
/*
** 2013-05-15
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
******************************************************************************
**
** This SQLite extension implements a rot13() function and a rot13
** collating sequence.
*/
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
#include <assert.h>
#include <string.h>
/*
** Perform rot13 encoding on a single ASCII character.
*/
static unsigned char rot13(unsigned char c){
if( c>='a' && c<='z' ){
c += 13;
if( c>'z' ) c -= 26;
}else if( c>='A' && c<='Z' ){
c += 13;
if( c>'Z' ) c -= 26;
}
return c;
}
/*
** Implementation of the rot13() function.
**
** Rotate ASCII alphabetic characters by 13 character positions.
** Non-ASCII characters are unchanged. rot13(rot13(X)) should always
** equal X.
*/
static void rot13func(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
const unsigned char *zIn;
int nIn;
unsigned char *zOut;
char *zToFree = 0;
int i;
char zTemp[100];
assert( argc==1 );
if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
zIn = (const unsigned char*)sqlite3_value_text(argv[0]);
nIn = sqlite3_value_bytes(argv[0]);
if( nIn<sizeof(zTemp)-1 ){
zOut = zTemp;
}else{
zOut = zToFree = sqlite3_malloc( nIn+1 );
if( zOut==0 ){
sqlite3_result_error_nomem(context);
return;
}
}
for(i=0; i<nIn; i++) zOut[i] = rot13(zIn[i]);
zOut[i] = 0;
sqlite3_result_text(context, (char*)zOut, i, SQLITE_TRANSIENT);
sqlite3_free(zToFree);
}
/*
** Implement the rot13 collating sequence so that if
**
** x=y COLLATE rot13
**
** Then
**
** rot13(x)=rot13(y) COLLATE binary
*/
static int rot13CollFunc(
void *notUsed,
int nKey1, const void *pKey1,
int nKey2, const void *pKey2
){
const char *zA = (const char*)pKey1;
const char *zB = (const char*)pKey2;
int i, x;
for(i=0; i<nKey1 && i<nKey2; i++){
x = (int)rot13(zA[i]) - (int)rot13(zB[i]);
if( x!=0 ) return x;
}
return nKey1 - nKey2;
}
#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_rot_init(
sqlite3 *db,
char **pzErrMsg,
const sqlite3_api_routines *pApi
){
int rc = SQLITE_OK;
SQLITE_EXTENSION_INIT2(pApi);
(void)pzErrMsg; /* Unused parameter */
rc = sqlite3_create_function(db, "rot13", 1, SQLITE_UTF8, 0,
rot13func, 0, 0);
if( rc==SQLITE_OK ){
rc = sqlite3_create_collation(db, "rot13", SQLITE_UTF8, 0, rot13CollFunc);
}
return rc;
}

View File

@ -1,5 +1,5 @@
C Add\sassert()s\sto\sthe\simplementation\sof\sxRead()\sin\sthe\sbuilt-in\sVFSes\sto\s\nverify\sthat\sthe\soffset\sparameter\sis\salways\snon-negative.
D 2013-05-09T18:12:40.898
C Add\sthe\srot13.c\sloadable\sextension.
D 2013-05-15T13:05:03.677
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in ce81671efd6223d19d4c8c6b88ac2c4134427111
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@ -89,6 +89,7 @@ F ext/misc/fuzzer.c fb64a15af978ae73fa9075b9b1dfbe82b8defc6f
F ext/misc/ieee754.c 2565ce373d842977efe0922dc50b8a41b3289556
F ext/misc/nextchar.c 1131e2b36116ffc6fe6b2e3464bfdace27978b1e
F ext/misc/regexp.c c25c65fe775f5d9801fb8573e36ebe73f2c0c2e0
F ext/misc/rot13.c 1ac6f95f99b575907b9b09c81a349114cf9be45a
F ext/misc/spellfix.c f9d24a2b2617cee143b7841b453e4e1fd8f189cc
F ext/misc/wholenumber.c 784b12543d60702ebdd47da936e278aa03076212
F ext/rtree/README 6315c0d73ebf0ec40dedb5aa0e942bc8b54e3761
@ -1062,7 +1063,7 @@ F tool/vdbe-compress.tcl f12c884766bd14277f4fcedcae07078011717381
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
F tool/win/sqlite.vsix 97894c2790eda7b5bce3cc79cb2a8ec2fde9b3ac
P 1128575d0ab24f7023a0f6e6ce4828b9a09a7c6c
R 1cad29e868ca8cf8d5b7d354acd90934
P cf5c3642247fdd34d87f0368594cd7b8f081636a
R 44b4748622864d8325bfa1188d6d5de6
U drh
Z 3b3f34da85296e9879a826be138cb510
Z 1bcbc481bbe6dde5c937a1d1c9d5e280

View File

@ -1 +1 @@
cf5c3642247fdd34d87f0368594cd7b8f081636a
8f9bd8e7a88eb11fb17d29954fa4b8c2840a5019