dan | 961ff45 | 2010-06-03 18:20:19 +0000 | [diff] [blame] | 1 | # 2010 June 03 |
| 2 | # |
| 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
| 5 | # |
| 6 | # May you do good and not evil. |
| 7 | # May you find forgiveness for yourself and forgive others. |
| 8 | # May you share freely, never taking more than you give. |
| 9 | # |
| 10 | #*********************************************************************** |
| 11 | # |
| 12 | # This file contains common code used by many different malloc tests |
| 13 | # within the test suite. |
| 14 | # |
| 15 | |
| 16 | proc wal_file_size {nFrame pgsz} { |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 17 | expr {32 + ($pgsz+24)*$nFrame} |
dan | 961ff45 | 2010-06-03 18:20:19 +0000 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | proc wal_frame_count {zFile pgsz} { |
| 21 | set f [file size $zFile] |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 22 | expr {($f - 32) / ($pgsz+24)} |
dan | 961ff45 | 2010-06-03 18:20:19 +0000 | [diff] [blame] | 23 | } |
| 24 | |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 25 | proc wal_cksum_intlist {ckv1 ckv2 intlist} { |
| 26 | upvar $ckv1 c1 |
| 27 | upvar $ckv2 c2 |
| 28 | foreach {v1 v2} $intlist { |
| 29 | set c1 [expr {($c1 + $v1 + $c2)&0xFFFFFFFF}] |
| 30 | set c2 [expr {($c2 + $v2 + $c1)&0xFFFFFFFF}] |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | |
| 35 | # This proc calculates checksums in the same way as those used by SQLite |
| 36 | # in WAL files. If the $endian argument is "big", then checksums are |
| 37 | # calculated by interpreting data as an array of big-endian integers. If |
| 38 | # it is "little", data is interpreted as an array of little-endian integers. |
| 39 | # |
| 40 | proc wal_cksum {endian ckv1 ckv2 blob} { |
| 41 | upvar $ckv1 c1 |
| 42 | upvar $ckv2 c2 |
| 43 | |
| 44 | if {$endian!="big" && $endian!="little"} { |
| 45 | return -error "Bad value \"$endian\" - must be \"big\" or \"little\"" |
| 46 | } |
| 47 | set scanpattern I* |
| 48 | if {$endian == "little"} { set scanpattern i* } |
| 49 | |
| 50 | binary scan $blob $scanpattern values |
| 51 | wal_cksum_intlist c1 c2 $values |
| 52 | } |
| 53 | |
| 54 | proc wal_set_walhdr {filename {intlist {}}} { |
| 55 | if {[llength $intlist]==6} { |
| 56 | set blob [binary format I6 $intlist] |
| 57 | set endian little |
| 58 | if {[lindex $intlist 0] & 0x00000001} { set endian big } |
| 59 | set c1 0 |
| 60 | set c2 0 |
| 61 | wal_cksum $endian c1 c2 $blob |
| 62 | append blob [binary format II $c1 $c2] |
| 63 | |
| 64 | set fd [open $filename r+] |
| 65 | fconfigure $fd -translation binary |
| 66 | fconfigure $fd -encoding binary |
| 67 | seek $fd 0 |
| 68 | puts -nonewline $fd $blob |
| 69 | close $fd |
| 70 | } |
| 71 | |
| 72 | set fd [open $filename] |
| 73 | fconfigure $fd -translation binary |
| 74 | fconfigure $fd -encoding binary |
| 75 | set blob [read $fd 24] |
shaneh | 538fa0b | 2010-07-07 15:41:55 +0000 | [diff] [blame^] | 76 | close $fd |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 77 | |
| 78 | binary scan $blob I6 ints |
| 79 | set ints |
| 80 | } |
| 81 | |
| 82 | proc wal_fix_walindex_cksum {hdrvar} { |
| 83 | upvar $hdrvar hdr |
| 84 | set c1 0 |
| 85 | set c2 0 |
| 86 | wal_cksum_intlist c1 c2 [lrange $hdr 0 9] |
| 87 | lset hdr 10 $c1 |
| 88 | lset hdr 11 $c2 |
| 89 | } |
dan | 961ff45 | 2010-06-03 18:20:19 +0000 | [diff] [blame] | 90 | |
| 91 | |