github.com/nakagami/firebirdsql@v0.9.10/utils.go (about)

     1  /*******************************************************************************
     2  The MIT License (MIT)
     3  
     4  Copyright (c) 2013-2019 Hajime Nakagami
     5  
     6  Permission is hereby granted, free of charge, to any person obtaining a copy of
     7  this software and associated documentation files (the "Software"), to deal in
     8  the Software without restriction, including without limitation the rights to
     9  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
    10  the Software, and to permit persons to whom the Software is furnished to do so,
    11  subject to the following conditions:
    12  
    13  The above copyright notice and this permission notice shall be included in all
    14  copies or substantial portions of the Software.
    15  
    16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
    18  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
    19  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
    20  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    21  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    22  *******************************************************************************/
    23  
    24  package firebirdsql
    25  
    26  import (
    27  	"bytes"
    28  	"container/list"
    29  	"encoding/binary"
    30  	"math/big"
    31  	"strconv"
    32  	"time"
    33  )
    34  
    35  func str_to_bytes(s string) []byte {
    36  	return []byte(s)
    37  }
    38  
    39  func int32_to_bytes(i32 int32) []byte {
    40  	bs := []byte{
    41  		byte(i32 & 0xFF),
    42  		byte(i32 >> 8 & 0xFF),
    43  		byte(i32 >> 16 & 0xFF),
    44  		byte(i32 >> 24 & 0xFF),
    45  	}
    46  	return bs
    47  }
    48  
    49  func bint64_to_bytes(i64 int64) []byte {
    50  	bs := []byte{
    51  		byte(i64 >> 56 & 0xFF),
    52  		byte(i64 >> 48 & 0xFF),
    53  		byte(i64 >> 40 & 0xFF),
    54  		byte(i64 >> 32 & 0xFF),
    55  		byte(i64 >> 24 & 0xFF),
    56  		byte(i64 >> 16 & 0xFF),
    57  		byte(i64 >> 8 & 0xFF),
    58  		byte(i64 & 0xFF),
    59  	}
    60  	return bs
    61  }
    62  
    63  func bint32_to_bytes(i32 int32) []byte {
    64  	bs := []byte{
    65  		byte(i32 >> 24 & 0xFF),
    66  		byte(i32 >> 16 & 0xFF),
    67  		byte(i32 >> 8 & 0xFF),
    68  		byte(i32 & 0xFF),
    69  	}
    70  	return bs
    71  }
    72  
    73  func int16_to_bytes(i16 int16) []byte {
    74  	bs := []byte{
    75  		byte(i16 & 0xFF),
    76  		byte(i16 >> 8 & 0xFF),
    77  	}
    78  	return bs
    79  }
    80  func bytes_to_str(b []byte) string {
    81  	return string(b)
    82  }
    83  
    84  func bytes_to_bint32(b []byte) int32 {
    85  	return int32(binary.BigEndian.Uint32(b))
    86  }
    87  
    88  func bytes_to_int32(b []byte) int32 {
    89  	return int32(binary.LittleEndian.Uint32(b))
    90  }
    91  
    92  func bytes_to_bint16(b []byte) int16 {
    93  	return int16(binary.BigEndian.Uint16(b))
    94  }
    95  
    96  func bytes_to_buint16(b []byte) uint16 {
    97  	return uint16(binary.BigEndian.Uint16(b))
    98  }
    99  
   100  func bytes_to_int16(b []byte) int16 {
   101  	return int16(binary.LittleEndian.Uint16(b))
   102  }
   103  
   104  func bytes_to_bint64(b []byte) int64 {
   105  	return int64(binary.BigEndian.Uint64(b))
   106  }
   107  
   108  func bytes_to_int64(b []byte) int64 {
   109  	return int64(binary.LittleEndian.Uint64(b))
   110  }
   111  
   112  func bigIntFromHexString(s string) *big.Int {
   113  	ret := new(big.Int)
   114  	ret.SetString(s, 16)
   115  	return ret
   116  }
   117  
   118  func bigIntFromString(s string) *big.Int {
   119  	ret := new(big.Int)
   120  	ret.SetString(s, 10)
   121  	return ret
   122  }
   123  
   124  func flattenBytes(l *list.List) []byte {
   125  	n := 0
   126  	for e := l.Front(); e != nil; e = e.Next() {
   127  		n += len((e.Value).([]byte))
   128  	}
   129  
   130  	bs := make([]byte, n)
   131  
   132  	n = 0
   133  	for e := l.Front(); e != nil; e = e.Next() {
   134  		for i, b := range (e.Value).([]byte) {
   135  			bs[n+i] = b
   136  		}
   137  		n += len((e.Value).([]byte))
   138  	}
   139  
   140  	return bs
   141  }
   142  
   143  func xdrBytes(bs []byte) []byte {
   144  	// XDR encoding bytes
   145  	n := len(bs)
   146  	padding := 0
   147  	if n%4 != 0 {
   148  		padding = 4 - n%4
   149  	}
   150  	buf := make([]byte, 4+n+padding)
   151  	buf[0] = byte(n >> 24 & 0xFF)
   152  	buf[1] = byte(n >> 16 & 0xFF)
   153  	buf[2] = byte(n >> 8 & 0xFF)
   154  	buf[3] = byte(n & 0xFF)
   155  	for i, b := range bs {
   156  		buf[4+i] = b
   157  	}
   158  	return buf
   159  }
   160  
   161  func xdrString(s string) []byte {
   162  	// XDR encoding string
   163  	bs := bytes.NewBufferString(s).Bytes()
   164  	return xdrBytes(bs)
   165  }
   166  
   167  func _int64ToBlr(i64 int64) ([]byte, []byte) {
   168  	v := bint64_to_bytes(i64)
   169  	blr := []byte{16, 0}
   170  
   171  	return blr, v
   172  }
   173  
   174  func _int32ToBlr(i32 int32) ([]byte, []byte) {
   175  	v := bint32_to_bytes(i32)
   176  	blr := []byte{8, 0}
   177  
   178  	return blr, v
   179  }
   180  
   181  func _float64ToBlr(v float64) ([]byte, []byte) {
   182  	buf := new(bytes.Buffer)
   183  	binary.Write(buf, binary.BigEndian, v)
   184  	blr := []byte{27}
   185  	return blr, buf.Bytes()
   186  }
   187  
   188  func _bytesToBlr(v []byte) ([]byte, []byte) {
   189  	nbytes := len(v)
   190  	pad_length := ((4 - nbytes) & 3)
   191  	padding := make([]byte, pad_length)
   192  	v = bytes.Join([][]byte{
   193  		v,
   194  		padding,
   195  	}, nil)
   196  	blr := []byte{14, byte(nbytes & 255), byte(nbytes >> 8)}
   197  	return blr, v
   198  }
   199  
   200  func _convert_date(t time.Time) []byte {
   201  	i := int(t.Month()) + 9
   202  	jy := t.Year() + (i / 12) - 1
   203  	jm := i % 12
   204  	c := jy / 100
   205  	jy -= 100 * c
   206  	j := (146097*c)/4 + (1461*jy)/4 + (153*jm+2)/5 + t.Day() - 678882
   207  	return bint32_to_bytes(int32(j))
   208  }
   209  
   210  func _convert_time(t time.Time) []byte {
   211  	v := (t.Hour()*3600+t.Minute()*60+t.Second())*10000 + t.Nanosecond()/100000
   212  	return bint32_to_bytes(int32(v))
   213  }
   214  
   215  func _dateToBlr(t time.Time) ([]byte, []byte) {
   216  	v := _convert_date(t)
   217  	blr := []byte{12}
   218  	return blr, v
   219  }
   220  
   221  func _timeToBlr(t time.Time) ([]byte, []byte) {
   222  	v := _convert_time(t)
   223  	blr := []byte{13}
   224  	return blr, v
   225  }
   226  
   227  func _timestampToBlr(t time.Time) ([]byte, []byte) {
   228  	v := bytes.Join([][]byte{
   229  		_convert_date(t),
   230  		_convert_time(t),
   231  	}, nil)
   232  
   233  	blr := []byte{35}
   234  	return blr, v
   235  }
   236  
   237  func convertToBool(s string, defaultValue bool) bool {
   238  	v, err := strconv.ParseBool(s)
   239  	if err != nil {
   240  		v = defaultValue
   241  	}
   242  	return v
   243  }