]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Add experimental user function unhex().
authordan <Dan Kennedy>
Mon, 23 Jan 2023 14:11:34 +0000 (14:11 +0000)
committerdan <Dan Kennedy>
Mon, 23 Jan 2023 14:11:34 +0000 (14:11 +0000)
FossilOrigin-Name: dbe424b5db33ce2c7562dfb44daf2969cf3074234cc891eb9b8d0d907faf6a78

manifest
manifest.uuid
src/func.c
test/exprfault.test
test/unhex.test [new file with mode: 0644]

index f58d749141644005b2ac6b5f363868592112ff44..e67118aea2fd483bc1979863be87942d53a20c64 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Give\sCLI\s.version\sa\splace\sin\s.help\soutput.
-D 2023-01-22T21:54:17.682
+C Add\sexperimental\suser\sfunction\sunhex().
+D 2023-01-23T14:11:34.494
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -573,7 +573,7 @@ F src/delete.c 86573edae75e3d3e9a8b590d87db8e47222103029df4f3e11fa56044459b514e
 F src/expr.c 204af6a83c191f5ac19ec4af6ecc546f188cc2dd1c76fc5280982f710ec4b9c4
 F src/fault.c 460f3e55994363812d9d60844b2a6de88826e007
 F src/fkey.c 722f20779f5342a787922deded3628d8c74b5249cab04098cf17ee2f2aaff002
-F src/func.c 68f610a44962814a4ba593fc95137a6682cb7e65086f22167e167b75ee3432e7
+F src/func.c b17ff98c7665bba857608b0c837103a81a5370362ebc0d69ac6a53123ec7be9b
 F src/global.c e06ff8e0acd85aec13563c9ecb44fbbf38232ccf73594998fd880b92d619594b
 F src/hash.c c6af5f96a7a76d000f07c5402c48c318c2566beecdee9e78b9d9f60ce7119565
 F src/hash.h 3340ab6e1d13e725571d7cee6d3e3135f0779a7d8e76a9ce0a85971fa3953c51
@@ -976,7 +976,7 @@ F test/exec.test e949714dc127eaa5ecc7d723efec1ec27118fdd7
 F test/exists.test 79a75323c78f02bbe9c251ea502a092f9ef63dac
 F test/expr.test 5c06696478212e5a04e04b043f993373f6f8e5ce5a80f5548a84703b123b6caa
 F test/expr2.test c27327ae9c017a7ff6280123f67aff496f912da74d78c888926d68b46ec75fd8
-F test/exprfault.test 497cc0b8fe6a677f49b55cb485e040f709ec2834b84f25912fe9c2dfeeda33db
+F test/exprfault.test da33606d799718e2f8e34efd0e5858884a1ad87f608774c552a7f5517cc27181
 F test/extension01.test 00d13cec817f331a687a243e0e5a2d87b0e358c9
 F test/external_reader.test c7d34694f1b25c32d866f56ac80c1e29edddc42b4ef90cad589263ffac2cde0c
 F test/extraquick.test cb254400bd42bfb777ff675356aabf3287978f79
@@ -1758,6 +1758,7 @@ F test/tt3_vacuum.c 71b254cde1fc49d6c8c44efd54f4668f3e57d7b3a8f4601ade069f75a999
 F test/types.test bf816ce73c7dfcfe26b700c19f97ef4050d194ff
 F test/types2.test 1aeb81976841a91eef292723649b5c4fe3bc3cac
 F test/types3.test 99e009491a54f4dc02c06bdbc0c5eea56ae3e25a
+F test/unhex.test abf3d0adbf40357bdeeabfb8a0073d7f3a704af3529c63645fcc8bb398c37d4d
 F test/unionall.test eb9afa030897af75fd2f0dd28354ef63c8a5897b6c76aa1f15acae61a12eabcf
 F test/unionall2.test 71e8fa08d5699d50dc9f9dc0c9799c2e7a6bb7931a330d369307a4df7f157fa1
 F test/unionallfault.test 652bfbb630e6c43135965dc1e8f0a9a791da83aec885d626a632fe1909c56f73
@@ -2043,8 +2044,11 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P f608a3a45609693c1c0237f12c394275ec9a6225fa986e62345f21af763293a0
-R 9efeb3e5ed84cfc70072df0c9463cd19
-U larrybr
-Z f6cf53c748aca5dc650c6d451ffc417b
+P 5f2dfdcc345453ee0a05311f6826d90b7c1d7b95fdaf77a0a8383923a8fc7213
+R 828038e1afa4103669415a1c409f187d
+T *branch * unhex-function
+T *sym-unhex-function *
+T -sym-trunk *
+U dan
+Z efd8b9c01e0025c3579168914aaf4748
 # Remove this line to create a well-formed Fossil manifest.
index 63a339c900ddf100ddfc0ebca2e8972845fcf711..491c8fc32f3f83df612ed502e89b761c04da8237 100644 (file)
@@ -1 +1 @@
-5f2dfdcc345453ee0a05311f6826d90b7c1d7b95fdaf77a0a8383923a8fc7213
\ No newline at end of file
+dbe424b5db33ce2c7562dfb44daf2969cf3074234cc891eb9b8d0d907faf6a78
\ No newline at end of file
index 9a62470a0c6c9629e09757d58e8967aa9b56f090..619c8143025ca81e5af5b8e9824abaeeb686fc96 100644 (file)
@@ -1223,6 +1223,39 @@ static void hexFunc(
   }
 }
 
+/*
+** The unhex() function. Interpret the argument as text. If it consists
+** of an even number of hexadecimal digits, return the equivalent blob.
+** Otherwise, return NULL.
+*/
+static void unhexFunc(
+  sqlite3_context *pCtx,
+  int argc,
+  sqlite3_value **argv
+){
+  const u8 *zHex = sqlite3_value_text(argv[0]);
+  int nHex = sqlite3_value_bytes(argv[0]);
+  if( zHex && (nHex%2)==0 ){
+    u8 *pBlob = contextMalloc(pCtx, (nHex/2)+1);
+    if( pBlob ){
+      const u8 *zEnd = &zHex[nHex];
+      u8 *p = pBlob;
+      while( zHex<zEnd ){
+        unsigned char c = *(zHex++);
+        unsigned char d = *(zHex++);
+        if( !sqlite3Isxdigit(c) || !sqlite3Isxdigit(d) ){
+          sqlite3_free(pBlob);
+          return;
+        }else{
+          *(p++) = (sqlite3HexToInt(c)<<4) | sqlite3HexToInt(d);
+        }
+      }
+      sqlite3_result_blob(pCtx, pBlob, (p - pBlob), sqlite3_free);
+    }
+  }
+}
+
+
 /*
 ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
 */
@@ -2287,6 +2320,7 @@ void sqlite3RegisterBuiltinFunctions(void){
     FUNCTION(upper,              1, 0, 0, upperFunc        ),
     FUNCTION(lower,              1, 0, 0, lowerFunc        ),
     FUNCTION(hex,                1, 0, 0, hexFunc          ),
+    FUNCTION(unhex,              1, 0, 0, unhexFunc        ),
     INLINE_FUNC(ifnull,          2, INLINEFUNC_coalesce, 0 ),
     VFUNCTION(random,            0, 0, 0, randomFunc       ),
     VFUNCTION(randomblob,        1, 0, 0, randomBlob       ),
index 9e5977e1d254bc80e201727e200c3a37b116c479..f0abe9ac39938793aafa0fa04729378ba0a75f51 100644 (file)
@@ -31,5 +31,15 @@ do_faultsim_test 1.1 -faults oom* -prep {
   faultsim_test_result {0 {}}
 }
 
+do_faultsim_test 2 -faults oom* -prep {
+  faultsim_restore_and_reopen
+} -body {
+  execsql {
+    SELECT hex ( unhex('ABCDEF') );
+  }
+} -test {
+  faultsim_test_result {0 ABCDEF}
+}
+
 
 finish_test
diff --git a/test/unhex.test b/test/unhex.test
new file mode 100644 (file)
index 0000000..0bd2dfd
--- /dev/null
@@ -0,0 +1,57 @@
+# 2023 January 23
+#
+# 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.
+#
+#***********************************************************************
+#
+
+set testdir [file dirname $argv0]
+source [file join $testdir tester.tcl]
+
+set testprefix unhex
+
+
+foreach {tn hex} {
+  1  0000
+  2  FFFF
+  3  0123456789ABCDEF
+} {
+  do_execsql_test 1.$tn.1 {
+    SELECT hex( unhex( $hex ) );
+  } $hex
+
+  do_execsql_test 1.$tn.2 {
+    SELECT hex( unhex( lower( $hex ) ) );
+  } $hex
+}
+
+do_execsql_test 2.0 {
+  SELECT typeof( unhex('') ), length( unhex('') );
+} {blob 0}
+
+foreach {tn hex} {
+  1  ABC
+  2  hello
+  3  123456x7
+  4  0xff
+} {
+  do_execsql_test 2.$tn {
+    SELECT unhex( $hex ) IS NULL;
+  } 1
+}
+
+do_catchsql_test 3.0 {
+  SELECT unhex();
+} {1 {wrong number of arguments to function unhex()}}
+do_catchsql_test 3.1 {
+  SELECT unhex('ABCD', '1234');
+} {1 {wrong number of arguments to function unhex()}}
+
+finish_test
+
+