go-hep.org/x/hep@v0.38.1/xrootd/internal/xrdenc/xrdenc.go (about)

     1  // Copyright ©2018 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package xrdenc // import "go-hep.org/x/hep/xrootd/internal/xrdenc"
     6  
     7  import "encoding/binary"
     8  
     9  // WBuffer encodes values to a buffer according to the XRootD protocol.
    10  type WBuffer struct {
    11  	buf []byte
    12  }
    13  
    14  func (w *WBuffer) Bytes() []byte { return w.buf }
    15  
    16  func (w *WBuffer) WriteU8(v uint8) {
    17  	w.buf = append(w.buf, v)
    18  }
    19  
    20  func (w *WBuffer) WriteU16(v uint16) {
    21  	var buf [2]byte
    22  	binary.BigEndian.PutUint16(buf[:], v)
    23  	w.buf = append(w.buf, buf[:]...)
    24  }
    25  
    26  func (w *WBuffer) WriteI32(v int32) {
    27  	var buf [4]byte
    28  	binary.BigEndian.PutUint32(buf[:], uint32(v))
    29  	w.buf = append(w.buf, buf[:]...)
    30  }
    31  
    32  func (w *WBuffer) WriteI64(v int64) {
    33  	var buf [8]byte
    34  	binary.BigEndian.PutUint64(buf[:], uint64(v))
    35  	w.buf = append(w.buf, buf[:]...)
    36  }
    37  
    38  func (w *WBuffer) WriteBool(v bool) {
    39  	if v {
    40  		w.buf = append(w.buf, 1)
    41  		return
    42  	}
    43  	w.buf = append(w.buf, 0)
    44  }
    45  
    46  func (w *WBuffer) WriteLen(n int) {
    47  	w.WriteI32(int32(n))
    48  }
    49  
    50  func (w *WBuffer) WriteBytes(vs []byte) {
    51  	w.buf = append(w.buf, vs...)
    52  }
    53  
    54  func (w *WBuffer) WriteStr(str string) {
    55  	w.WriteLen(len(str))
    56  	w.WriteBytes([]byte(str))
    57  }
    58  
    59  func (w *WBuffer) Next(n int) {
    60  	w.buf = append(w.buf, make([]byte, n)...)
    61  }
    62  
    63  // RBuffer decodes values from a buffer according to the XRootD protocol.
    64  type RBuffer struct {
    65  	buf []byte
    66  	pos int
    67  }
    68  
    69  func NewRBuffer(data []byte) *RBuffer {
    70  	return &RBuffer{buf: data}
    71  }
    72  
    73  func (r *RBuffer) ReadU8() uint8 {
    74  	o := r.buf[r.pos]
    75  	r.pos++
    76  	return o
    77  }
    78  
    79  func (r *RBuffer) Len() int {
    80  	return len(r.buf) - r.pos
    81  }
    82  
    83  func (r *RBuffer) ReadU16() uint16 {
    84  	beg := r.pos
    85  	end := r.pos + 2
    86  	r.pos += 2
    87  	o := binary.BigEndian.Uint16(r.buf[beg:end])
    88  	return o
    89  }
    90  
    91  func (r *RBuffer) ReadI32() int32 {
    92  	beg := r.pos
    93  	end := r.pos + 4
    94  	r.pos += 4
    95  	o := binary.BigEndian.Uint32(r.buf[beg:end])
    96  	return int32(o)
    97  }
    98  
    99  func (r *RBuffer) ReadI64() int64 {
   100  	beg := r.pos
   101  	end := r.pos + 8
   102  	r.pos += 8
   103  	o := binary.BigEndian.Uint64(r.buf[beg:end])
   104  	return int64(o)
   105  }
   106  
   107  func (r *RBuffer) ReadBool() bool {
   108  	r.pos += 1
   109  	return r.buf[r.pos] != 0
   110  }
   111  
   112  func (r *RBuffer) ReadLen() int {
   113  	return int(r.ReadI32())
   114  }
   115  
   116  func (r *RBuffer) ReadBytes(data []byte) {
   117  	n := len(data)
   118  	beg := r.pos
   119  	end := r.pos + n
   120  	copy(data, r.buf[beg:end])
   121  	r.pos += n
   122  }
   123  
   124  func (r *RBuffer) ReadStr() string {
   125  	n := r.ReadLen()
   126  	beg := r.pos
   127  	end := r.pos + n
   128  	r.pos += n
   129  	return string(r.buf[beg:end])
   130  }
   131  
   132  func (r *RBuffer) Skip(n int) {
   133  	r.pos += n
   134  }
   135  
   136  func (r *RBuffer) Bytes() []byte {
   137  	return r.buf[r.pos:]
   138  }
   139  
   140  func (r *RBuffer) Pos() int {
   141  	return r.pos
   142  }