/ Check-in [94c4cdc5]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:When converting from a BLOB value in the tointeger() and toreal() SQL functions, make sure that endianness of the machine does not matter.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | toTypeFuncs
Files: files | file ages | folders
SHA1: 94c4cdc50d2753c859e494d53cebd09edd2e5663
User & Date: mistachkin 2013-09-06 20:30:53
Context
2013-09-06
21:41
Add the ability to generate assembly listing files using the MSVC makefile. check-in: 6caa2cd1 user: mistachkin tags: toTypeFuncs
20:30
When converting from a BLOB value in the tointeger() and toreal() SQL functions, make sure that endianness of the machine does not matter. check-in: 94c4cdc5 user: mistachkin tags: toTypeFuncs
2013-09-04
00:58
Additional test cases for the tointeger() and toreal() SQL functions when converting from a BLOB. check-in: e1814452 user: mistachkin tags: toTypeFuncs
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/func.c.

962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
...
992
993
994
995
996
997
998








999

1000
1001
1002
1003
1004
1005
1006
....
1019
1020
1021
1022
1023
1024
1025

1026
1027
1028
1029
1030
1031
1032
1033
1034
....
1051
1052
1053
1054
1055
1056
1057








1058

1059
1060
1061
1062
1063
1064
1065
      sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
      break;
    }
  }
}

/*
** tointeger(X):  If X is any value (integer, double, or string) that can
** be losslessly converted into an integer, then make the conversion and
** return the result.  Otherwise, return NULL.
*/
static void tointegerFunc(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
................................................................................
    }
    case SQLITE_BLOB: {
      const unsigned char *zBlob = sqlite3_value_blob(argv[0]);
      if( zBlob ){
        int nBlob = sqlite3_value_bytes(argv[0]);
        if( nBlob==sizeof(i64) ){
          i64 iVal;








          memcpy(&iVal, zBlob, sizeof(i64));

          sqlite3_result_int64(context, iVal);
        }
      }
      break;
    }
    case SQLITE_TEXT: {
      const unsigned char *zStr = sqlite3_value_text(argv[0]);
................................................................................
      assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
      break;
    }
  }
}

/*

** toreal(X):  If X can be losslessly converted into a real number, then
** do so and return that real number.  Otherwise return NULL.
*/
#if defined(_MSC_VER)
#pragma optimize("", off)
#endif
static void torealFunc(
  sqlite3_context *context,
  int argc,
................................................................................
    }
    case SQLITE_BLOB: {
      const unsigned char *zBlob = sqlite3_value_blob(argv[0]);
      if( zBlob ){
        int nBlob = sqlite3_value_bytes(argv[0]);
        if( nBlob==sizeof(double) ){
          double rVal;








          memcpy(&rVal, zBlob, sizeof(double));

          sqlite3_result_double(context, rVal);
        }
      }
      break;
    }
    case SQLITE_TEXT: {
      const unsigned char *zStr = sqlite3_value_text(argv[0]);







|
|







 







>
>
>
>
>
>
>
>
|
>







 







>
|
|







 







>
>
>
>
>
>
>
>
|
>







962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
...
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
....
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
....
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
      sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
      break;
    }
  }
}

/*
** tointeger(X):  If X is any value (integer, double, blob, or string) that
** can be losslessly converted into an integer, then make the conversion and
** return the result.  Otherwise, return NULL.
*/
static void tointegerFunc(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
................................................................................
    }
    case SQLITE_BLOB: {
      const unsigned char *zBlob = sqlite3_value_blob(argv[0]);
      if( zBlob ){
        int nBlob = sqlite3_value_bytes(argv[0]);
        if( nBlob==sizeof(i64) ){
          i64 iVal;
          if( SQLITE_BIGENDIAN ){
            int i;
            unsigned char *zBlobRev = contextMalloc(context, nBlob);
            if( !zBlobRev ) break;
            for(i=0; i<nBlob; i++) zBlobRev[i] = zBlob[nBlob-1-i];
            memcpy(&iVal, zBlobRev, sizeof(i64));
            sqlite3_free(zBlobRev);
          }else{
            memcpy(&iVal, zBlob, sizeof(i64));
          }
          sqlite3_result_int64(context, iVal);
        }
      }
      break;
    }
    case SQLITE_TEXT: {
      const unsigned char *zStr = sqlite3_value_text(argv[0]);
................................................................................
      assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
      break;
    }
  }
}

/*
** toreal(X): If X is any value (integer, double, blob, or string) that can
** be losslessly converted into a real number, then do so and return that
** real number.  Otherwise return NULL.
*/
#if defined(_MSC_VER)
#pragma optimize("", off)
#endif
static void torealFunc(
  sqlite3_context *context,
  int argc,
................................................................................
    }
    case SQLITE_BLOB: {
      const unsigned char *zBlob = sqlite3_value_blob(argv[0]);
      if( zBlob ){
        int nBlob = sqlite3_value_bytes(argv[0]);
        if( nBlob==sizeof(double) ){
          double rVal;
          if( SQLITE_LITTLEENDIAN ){
            int i;
            unsigned char *zBlobRev = contextMalloc(context, nBlob);
            if( !zBlobRev ) break;
            for(i=0; i<nBlob; i++) zBlobRev[i] = zBlob[nBlob-1-i];
            memcpy(&rVal, zBlobRev, sizeof(double));
            sqlite3_free(zBlobRev);
          }else{
            memcpy(&rVal, zBlob, sizeof(double));
          }
          sqlite3_result_double(context, rVal);
        }
      }
      break;
    }
    case SQLITE_TEXT: {
      const unsigned char *zStr = sqlite3_value_text(argv[0]);

Changes to test/func4.test.

663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705

706
707
708

709
710
711

712
713
714

715
716
717

718
719
720

721
722
723

724
725
726

727
728
729

730
731
732

733
734
735

736
737
738

739
740
741

742
743
744

745
746
747

748
749
750

751
752
753

754
755
756

757
758
759

760
761
762

763
764
765

766
767
768

769
770
771

772
773
774

775
776
777

778
779
780
781
782
783
  ifcapable floatingpoint {
    do_execsql_test func4-6.1.$i.2 [subst {
      SELECT toreal(x'[string repeat 01 $i]');
    }] {{}}
  }
}

proc swapHexBytes { value } {
  if {[string length $value] % 2 != 0} {
    error "value \"$value\" must have an even number of characters"
  }
  if {![string is xdigit -strict $value]} then {
    error "value \"$value\" must contain only hexadecimal digits"
  }
  join [lreverse [regexp -all -inline {.{2}} $value]] ""
}

proc swapIntegerHexBytes { value } {
  if {![info exists ::tcl_platform(byteOrder)] || \
      $::tcl_platform(byteOrder) eq "littleEndian"} {
    return $value
  }
  return [swapHexBytes $value]
}

proc swapDoubleHexBytes { value } {
  if {![info exists ::tcl_platform(byteOrder)] || \
      $::tcl_platform(byteOrder) ne "littleEndian"} {
    return $value
  }
  return [swapHexBytes $value]
}

do_execsql_test func4-6.2.1 [subst {
  SELECT tointeger(x'[swapIntegerHexBytes 0102030405060708]');
}] {578437695752307201}
do_execsql_test func4-6.2.2 [subst {
  SELECT tointeger(x'[swapIntegerHexBytes 0807060504030201]');
}] {72623859790382856}

ifcapable floatingpoint {
  do_execsql_test func4-6.3.1 [subst {
    SELECT toreal(x'[swapDoubleHexBytes ffefffffffffffff]');

  }] {-1.7976931348623157e+308}
  do_execsql_test func4-6.3.2 [subst {
    SELECT toreal(x'[swapDoubleHexBytes 8010000000000000]');

  }] {-2.2250738585072014e-308}
  do_execsql_test func4-6.3.3 [subst {
    SELECT toreal(x'[swapDoubleHexBytes c000000000000000]');

  }] {-2.0}
  do_execsql_test func4-6.3.4 [subst {
    SELECT toreal(x'[swapDoubleHexBytes bff0000000000000]');

  }] {-1.0}
  do_execsql_test func4-6.3.5 [subst {
    SELECT toreal(x'[swapDoubleHexBytes 8000000000000000]');

  }] {-0.0}
  do_execsql_test func4-6.3.6 [subst {
    SELECT toreal(x'[swapDoubleHexBytes 0000000000000000]');

  }] {0.0}
  do_execsql_test func4-6.3.7 [subst {
    SELECT toreal(x'[swapDoubleHexBytes 3ff0000000000000]');

  }] {1.0}
  do_execsql_test func4-6.3.8 [subst {
    SELECT toreal(x'[swapDoubleHexBytes 4000000000000000]');

  }] {2.0}
  do_execsql_test func4-6.3.9 [subst {
    SELECT toreal(x'[swapDoubleHexBytes 0010000000000000]');

  }] {2.2250738585072014e-308}
  do_execsql_test func4-6.3.10 [subst {
    SELECT toreal(x'[swapDoubleHexBytes 7fefffffffffffff]');

  }] {1.7976931348623157e+308}
  do_execsql_test func4-6.3.11 [subst {
    SELECT toreal(x'[swapDoubleHexBytes 8000000000000001]');

  }] {-5e-324}
  do_execsql_test func4-6.3.12 [subst {
    SELECT toreal(x'[swapDoubleHexBytes 800fffffffffffff]');

  }] {-2.225073858507201e-308}
  do_execsql_test func4-6.3.13 [subst {
    SELECT toreal(x'[swapDoubleHexBytes 0000000000000001]');

  }] {5e-324}
  do_execsql_test func4-6.3.14 [subst {
    SELECT toreal(x'[swapDoubleHexBytes 000fffffffffffff]');

  }] {2.225073858507201e-308}
  do_execsql_test func4-6.3.15 [subst {
    SELECT toreal(x'[swapDoubleHexBytes fff0000000000000]');

  }] {-Inf}
  do_execsql_test func4-6.3.16 [subst {
    SELECT toreal(x'[swapDoubleHexBytes 7ff0000000000000]');

  }] {Inf}
  do_execsql_test func4-6.3.17 [subst {
    SELECT toreal(x'[swapDoubleHexBytes fff8000000000000]');

  }] {{}}
  do_execsql_test func4-6.3.18 [subst {
    SELECT toreal(x'[swapDoubleHexBytes fff0000000000001]');

  }] {{}}
  do_execsql_test func4-6.3.19 [subst {
    SELECT toreal(x'[swapDoubleHexBytes fff7ffffffffffff]');

  }] {{}}
  do_execsql_test func4-6.3.20 [subst {
    SELECT toreal(x'[swapDoubleHexBytes 7ff0000000000001]');

  }] {{}}
  do_execsql_test func4-6.3.21 [subst {
    SELECT toreal(x'[swapDoubleHexBytes 7ff7ffffffffffff]');

  }] {{}}
  do_execsql_test func4-6.3.22 [subst {
    SELECT toreal(x'[swapDoubleHexBytes fff8000000000001]');

  }] {{}}
  do_execsql_test func4-6.3.23 [subst {
    SELECT toreal(x'[swapDoubleHexBytes ffffffffffffffff]');

  }] {{}}
  do_execsql_test func4-6.3.24 [subst {
    SELECT toreal(x'[swapDoubleHexBytes 7ff8000000000000]');

  }] {{}}
  do_execsql_test func4-6.3.25 [subst {
    SELECT toreal(x'[swapDoubleHexBytes 7fffffffffffffff]');

  }] {{}}
}

set tcl_precision $saved_tcl_precision
unset saved_tcl_precision
finish_test







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
|
|
|
|


|
<
>
|
|
<
>
|
|
<
>
|
|
<
>
|
|
<
>
|
|
<
>
|
|
<
>
|
|
<
>
|
|
<
>
|
|
<
>
|
|
<
>
|
|
<
>
|
|
<
>
|
|
<
>
|
|
<
>
|
|
<
>
|
|
<
>
|
|
<
>
|
|
<
>
|
|
<
>
|
|
<
>
|
|
<
>
|
|
<
>
|
|
<
>
|
|
<
>
|





663
664
665
666
667
668
669


























670
671
672
673
674
675
676
677
678

679
680
681

682
683
684

685
686
687

688
689
690

691
692
693

694
695
696

697
698
699

700
701
702

703
704
705

706
707
708

709
710
711

712
713
714

715
716
717

718
719
720

721
722
723

724
725
726

727
728
729

730
731
732

733
734
735

736
737
738

739
740
741

742
743
744

745
746
747

748
749
750

751
752
753
754
755
756
757
  ifcapable floatingpoint {
    do_execsql_test func4-6.1.$i.2 [subst {
      SELECT toreal(x'[string repeat 01 $i]');
    }] {{}}
  }
}



























do_execsql_test func4-6.2.1 {
  SELECT tointeger(x'0102030405060708');
} {578437695752307201}
do_execsql_test func4-6.2.2 {
  SELECT tointeger(x'0807060504030201');
} {72623859790382856}

ifcapable floatingpoint {
  do_execsql_test func4-6.3.1 {

    SELECT toreal(x'ffefffffffffffff');
  } {-1.7976931348623157e+308}
  do_execsql_test func4-6.3.2 {

    SELECT toreal(x'8010000000000000');
  } {-2.2250738585072014e-308}
  do_execsql_test func4-6.3.3 {

    SELECT toreal(x'c000000000000000');
  } {-2.0}
  do_execsql_test func4-6.3.4 {

    SELECT toreal(x'bff0000000000000');
  } {-1.0}
  do_execsql_test func4-6.3.5 {

    SELECT toreal(x'8000000000000000');
  } {-0.0}
  do_execsql_test func4-6.3.6 {

    SELECT toreal(x'0000000000000000');
  } {0.0}
  do_execsql_test func4-6.3.7 {

    SELECT toreal(x'3ff0000000000000');
  } {1.0}
  do_execsql_test func4-6.3.8 {

    SELECT toreal(x'4000000000000000');
  } {2.0}
  do_execsql_test func4-6.3.9 {

    SELECT toreal(x'0010000000000000');
  } {2.2250738585072014e-308}
  do_execsql_test func4-6.3.10 {

    SELECT toreal(x'7fefffffffffffff');
  } {1.7976931348623157e+308}
  do_execsql_test func4-6.3.11 {

    SELECT toreal(x'8000000000000001');
  } {-5e-324}
  do_execsql_test func4-6.3.12 {

    SELECT toreal(x'800fffffffffffff');
  } {-2.225073858507201e-308}
  do_execsql_test func4-6.3.13 {

    SELECT toreal(x'0000000000000001');
  } {5e-324}
  do_execsql_test func4-6.3.14 {

    SELECT toreal(x'000fffffffffffff');
  } {2.225073858507201e-308}
  do_execsql_test func4-6.3.15 {

    SELECT toreal(x'fff0000000000000');
  } {-Inf}
  do_execsql_test func4-6.3.16 {

    SELECT toreal(x'7ff0000000000000');
  } {Inf}
  do_execsql_test func4-6.3.17 {

    SELECT toreal(x'fff8000000000000');
  } {{}}
  do_execsql_test func4-6.3.18 {

    SELECT toreal(x'fff0000000000001');
  } {{}}
  do_execsql_test func4-6.3.19 {

    SELECT toreal(x'fff7ffffffffffff');
  } {{}}
  do_execsql_test func4-6.3.20 {

    SELECT toreal(x'7ff0000000000001');
  } {{}}
  do_execsql_test func4-6.3.21 {

    SELECT toreal(x'7ff7ffffffffffff');
  } {{}}
  do_execsql_test func4-6.3.22 {

    SELECT toreal(x'fff8000000000001');
  } {{}}
  do_execsql_test func4-6.3.23 {

    SELECT toreal(x'ffffffffffffffff');
  } {{}}
  do_execsql_test func4-6.3.24 {

    SELECT toreal(x'7ff8000000000000');
  } {{}}
  do_execsql_test func4-6.3.25 {

    SELECT toreal(x'7fffffffffffffff');
  } {{}}
}

set tcl_precision $saved_tcl_precision
unset saved_tcl_precision
finish_test