Commit b034ade0 authored by gwenn's avatar gwenn

Fix blob on 64-bits.

parent 28bfee41
...@@ -22,8 +22,8 @@ import ( ...@@ -22,8 +22,8 @@ import (
type BlobReader struct { type BlobReader struct {
c *Conn c *Conn
bl *C.sqlite3_blob bl *C.sqlite3_blob
size int size int32
offset int offset int32
} }
// BlobReadWriter is an io.ReadWriteCloser adapter for BLOB // BlobReadWriter is an io.ReadWriteCloser adapter for BLOB
...@@ -104,8 +104,8 @@ func (r *BlobReader) Read(v []byte) (int, error) { ...@@ -104,8 +104,8 @@ func (r *BlobReader) Read(v []byte) (int, error) {
return 0, io.EOF return 0, io.EOF
} }
n := size - r.offset n := size - r.offset
if len(v) < n { if len(v) < int(n) {
n = len(v) n = int32(len(v))
} }
p := &v[0] p := &v[0]
rv := C.sqlite3_blob_read(r.bl, unsafe.Pointer(p), C.int(n), C.int(r.offset)) rv := C.sqlite3_blob_read(r.bl, unsafe.Pointer(p), C.int(n), C.int(r.offset))
...@@ -113,23 +113,32 @@ func (r *BlobReader) Read(v []byte) (int, error) { ...@@ -113,23 +113,32 @@ func (r *BlobReader) Read(v []byte) (int, error) {
return 0, r.c.error(rv, "BlobReader.Read") return 0, r.c.error(rv, "BlobReader.Read")
} }
r.offset += n r.offset += n
return n, nil return int(n), nil
} }
// Seek sets the offset for the next Read or Write to offset. // Seek sets the offset for the next Read or Write to offset.
// SQLite is limited to 32-bits offset. // SQLite is limited to 32-bits offset.
func (r *BlobReader) Seek(offset int64, whence int) (int64, error) { func (r *BlobReader) Seek(offset int64, whence int) (int64, error) {
size, err := r.Size()
if err != nil {
return 0, err
}
switch whence { switch whence {
case 0: // SEEK_SET case 0: // SEEK_SET
r.offset = int(offset) if offset < 0 || offset > int64(size) {
return 0, r.c.specificError("Invalid offset: %d", offset)
}
r.offset = int32(offset)
case 1: // SEEK_CUR case 1: // SEEK_CUR
r.offset += int(offset) if (int64(r.offset)+offset) < 0 || (int64(r.offset)+offset) > int64(size) {
return 0, r.c.specificError("Invalid offset: %d", offset)
}
r.offset += int32(offset)
case 2: // SEEK_END case 2: // SEEK_END
size, err := r.Size() if (int64(size)+offset) < 0 || offset > 0 {
if err != nil { return 0, r.c.specificError("Invalid offset: %d", offset)
return 0, err
} }
r.offset = size + int(offset) r.offset = size + int32(offset)
default: default:
return 0, r.c.specificError("Bad seekMode: %d", whence) return 0, r.c.specificError("Bad seekMode: %d", whence)
} }
...@@ -138,12 +147,12 @@ func (r *BlobReader) Seek(offset int64, whence int) (int64, error) { ...@@ -138,12 +147,12 @@ func (r *BlobReader) Seek(offset int64, whence int) (int64, error) {
// Size returns the size of an opened BLOB. // Size returns the size of an opened BLOB.
// (See http://sqlite.org/c3ref/blob_bytes.html) // (See http://sqlite.org/c3ref/blob_bytes.html)
func (r *BlobReader) Size() (int, error) { func (r *BlobReader) Size() (int32, error) {
if r.bl == nil { if r.bl == nil {
return 0, errors.New("blob reader already closed") return 0, errors.New("blob reader already closed")
} }
if r.size < 0 { if r.size < 0 {
r.size = int(C.sqlite3_blob_bytes(r.bl)) r.size = int32(C.sqlite3_blob_bytes(r.bl))
} }
return r.size, nil return r.size, nil
} }
...@@ -163,8 +172,8 @@ func (w *BlobReadWriter) Write(v []byte) (int, error) { ...@@ -163,8 +172,8 @@ func (w *BlobReadWriter) Write(v []byte) (int, error) {
} }
/* Write must return a non-nil error if it returns n < len(v) */ /* Write must return a non-nil error if it returns n < len(v) */
n := size - w.offset n := size - w.offset
if len(v) <= n { if len(v) <= int(n) {
n = len(v) n = int32(len(v))
} else { } else {
err = io.EOF err = io.EOF
} }
...@@ -174,7 +183,7 @@ func (w *BlobReadWriter) Write(v []byte) (int, error) { ...@@ -174,7 +183,7 @@ func (w *BlobReadWriter) Write(v []byte) (int, error) {
return 0, w.c.error(rv, "BlobReadWiter.Write") return 0, w.c.error(rv, "BlobReadWiter.Write")
} }
w.offset += n w.offset += n
return n, err return int(n), err
} }
// Reopen moves a BLOB handle to a new row. // Reopen moves a BLOB handle to a new row.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment