1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
/*
Copyright 2020 SAP SE
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package encoding
import (
"encoding/binary"
"io"
"math"
"github.com/SAP/go-hdb/internal/unicode"
"golang.org/x/text/transform"
)
const readScratchSize = 512 // used for skip as well - size not too small!
// Decoder decodes hdb protocol datatypes an basis of an io.Reader.
type Decoder struct {
rd io.Reader
err error
b [readScratchSize]byte // scratch buffer
tr transform.Transformer
cnt int
dfv int
}
// NewDecoder creates a new Decoder instance based on an io.Reader.
func NewDecoder(rd io.Reader) *Decoder {
return &Decoder{
rd: rd,
tr: unicode.Cesu8ToUtf8Transformer,
}
}
// Dfv returns the data format version.
func (d *Decoder) Dfv() int {
return d.dfv
}
// SetDfv sets the data format version.
func (d *Decoder) SetDfv(dfv int) {
d.dfv = dfv
}
// ResetCnt resets the byte read counter.
func (d *Decoder) ResetCnt() {
d.cnt = 0
}
// Cnt returns the value of the byte read counter.
func (d *Decoder) Cnt() int {
return d.cnt
}
// Error returns the reader error.
func (d *Decoder) Error() error {
return d.err
}
// ResetError return and resets reader error.
func (d *Decoder) ResetError() error {
err := d.err
d.err = nil
return err
}
// Skip skips cnt bytes from reading.
func (d *Decoder) Skip(cnt int) {
var n int
for n < cnt {
if d.err != nil {
return
}
to := cnt - n
if to > readScratchSize {
to = readScratchSize
}
var m int
m, d.err = io.ReadFull(d.rd, d.b[:to])
n += m
d.cnt += m
}
}
// Byte reads and returns a byte.
func (d *Decoder) Byte() byte { // ReadB as sig differs from ReadByte (vet issues)
if d.err != nil {
return 0
}
var n int
n, d.err = io.ReadFull(d.rd, d.b[:1])
d.cnt += n
if d.err != nil {
return 0
}
return byte(d.b[0])
}
// Bytes reads and returns a byte slice.
func (d *Decoder) Bytes(p []byte) {
if d.err != nil {
return
}
var n int
n, d.err = io.ReadFull(d.rd, p)
d.cnt += n
}
// Bool reads and returns a boolean.
func (d *Decoder) Bool() bool {
if d.err != nil {
return false
}
return d.Byte() != 0
}
// Int8 reads and returns an int8.
func (d *Decoder) Int8() int8 {
return int8(d.Byte())
}
// Int16 reads and returns an int16.
func (d *Decoder) Int16() int16 {
if d.err != nil {
return 0
}
var n int
n, d.err = io.ReadFull(d.rd, d.b[:2])
d.cnt += n
if d.err != nil {
return 0
}
return int16(binary.LittleEndian.Uint16(d.b[:2]))
}
// Uint16 reads and returns an uint16.
func (d *Decoder) Uint16() uint16 {
if d.err != nil {
return 0
}
var n int
n, d.err = io.ReadFull(d.rd, d.b[:2])
d.cnt += n
if d.err != nil {
return 0
}
return binary.LittleEndian.Uint16(d.b[:2])
}
// Int32 reads and returns an int32.
func (d *Decoder) Int32() int32 {
if d.err != nil {
return 0
}
var n int
n, d.err = io.ReadFull(d.rd, d.b[:4])
d.cnt += n
if d.err != nil {
return 0
}
return int32(binary.LittleEndian.Uint32(d.b[:4]))
}
// Uint32 reads and returns an uint32.
func (d *Decoder) Uint32() uint32 {
if d.err != nil {
return 0
}
var n int
n, d.err = io.ReadFull(d.rd, d.b[:4])
d.cnt += n
if d.err != nil {
return 0
}
return binary.LittleEndian.Uint32(d.b[:4])
}
// Uint32ByteOrder reads and returns an uint32 in given byte order.
func (d *Decoder) Uint32ByteOrder(byteOrder binary.ByteOrder) uint32 {
if d.err != nil {
return 0
}
var n int
n, d.err = io.ReadFull(d.rd, d.b[:4])
d.cnt += n
if d.err != nil {
return 0
}
return byteOrder.Uint32(d.b[:4])
}
// Int64 reads and returns an int64.
func (d *Decoder) Int64() int64 {
if d.err != nil {
return 0
}
var n int
n, d.err = io.ReadFull(d.rd, d.b[:8])
d.cnt += n
if d.err != nil {
return 0
}
return int64(binary.LittleEndian.Uint64(d.b[:8]))
}
// Uint64 reads and returns an uint64.
func (d *Decoder) Uint64() uint64 {
if d.err != nil {
return 0
}
var n int
n, d.err = io.ReadFull(d.rd, d.b[:8])
d.cnt += n
if d.err != nil {
return 0
}
return binary.LittleEndian.Uint64(d.b[:8])
}
// Float32 reads and returns a float32.
func (d *Decoder) Float32() float32 {
if d.err != nil {
return 0
}
var n int
n, d.err = io.ReadFull(d.rd, d.b[:4])
d.cnt += n
if d.err != nil {
return 0
}
bits := binary.LittleEndian.Uint32(d.b[:4])
return math.Float32frombits(bits)
}
// Float64 reads and returns a float64.
func (d *Decoder) Float64() float64 {
if d.err != nil {
return 0
}
var n int
n, d.err = io.ReadFull(d.rd, d.b[:8])
d.cnt += n
if d.err != nil {
return 0
}
bits := binary.LittleEndian.Uint64(d.b[:8])
return math.Float64frombits(bits)
}
// CESU8Bytes reads a size CESU-8 encoded byte sequence and returns an UTF-8 byte slice.
func (d *Decoder) CESU8Bytes(size int) []byte {
if d.err != nil {
return nil
}
p := make([]byte, size)
var n int
n, d.err = io.ReadFull(d.rd, p)
d.cnt += n
if d.err != nil {
return nil
}
d.tr.Reset()
if n, _, d.err = d.tr.Transform(p, p, true); d.err != nil { // inplace transformation
return nil
}
return p[:n]
}
// // ShortCESU8Bytes reads a CESU-8 encoded byte sequence and returns an UTF-8 byte slice.
// // Size is encoded in one byte.
// func (d *Decoder) ShortCESU8Bytes() ([]byte, int) {
// size := d.Byte()
// return d.CESU8Bytes(int(size)), int(size)
// }
// // ShortCESU8String reads a CESU-8 encoded byte sequence and returns an UTF-8 string.
// // Size is encoded in one byte.
// func (d *Decoder) ShortCESU8Bytes() (string, int) {
// b, n := d.ShortCESU8Bytes()
// return b, n
// }
// // ShortBytes reads a byte sequence and returns a byte slice.
// // Size is encoded in one byte.
// func (d *Decoder) ShortBytes() ([]byte, int) {
// size := d.Byte()
// b := make([]byte, size)
// d.Bytes(b)
// return b
// }
|