go-hep.org/x/hep@v0.38.1/xrootd/xrdproto/verifyw/verifyw.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 verifyw contains the structures describing verifyw request.
     6  // See xrootd protocol specification (http://xrootd.org/doc/dev45/XRdv310.pdf, p. 124) for details.
     7  package verifyw // import "go-hep.org/x/hep/xrootd/xrdproto/verifyw"
     8  
     9  import (
    10  	"encoding/binary"
    11  	"hash/crc32"
    12  
    13  	"go-hep.org/x/hep/xrootd/internal/xrdenc"
    14  	"go-hep.org/x/hep/xrootd/xrdfs"
    15  )
    16  
    17  // RequestID is the id of the request, it is sent as part of message.
    18  // See xrootd protocol specification for details: http://xrootd.org/doc/dev45/XRdv310.pdf, 2.3 Client Request Format.
    19  const RequestID uint16 = 3026
    20  
    21  // Type identifies the checksum algorithm used.
    22  type Type uint8
    23  
    24  const (
    25  	NoCRC Type = iota // NoCRC identifies that no crc is used.
    26  	CRC32             // CRC#@ identifies that 32-bit crc is used.
    27  )
    28  
    29  // Request holds verifyw request parameters.
    30  type Request struct {
    31  	Handle       xrdfs.FileHandle
    32  	Offset       int64
    33  	PathID       uint8
    34  	Verification Type
    35  	_            [2]uint8
    36  	Data         []uint8
    37  }
    38  
    39  // MarshalXrd implements xrdproto.Marshaler.
    40  func (o Request) MarshalXrd(wBuffer *xrdenc.WBuffer) error {
    41  	wBuffer.WriteBytes(o.Handle[:])
    42  	wBuffer.WriteI64(o.Offset)
    43  	wBuffer.WriteU8(o.PathID)
    44  	wBuffer.WriteU8(uint8(o.Verification))
    45  	wBuffer.Next(2)
    46  	wBuffer.WriteLen(len(o.Data))
    47  	wBuffer.WriteBytes(o.Data)
    48  
    49  	return nil
    50  }
    51  
    52  // UnmarshalXrd implements xrdproto.Unmarshaler.
    53  func (o *Request) UnmarshalXrd(rBuffer *xrdenc.RBuffer) error {
    54  	rBuffer.ReadBytes(o.Handle[:])
    55  	o.Offset = rBuffer.ReadI64()
    56  	o.PathID = rBuffer.ReadU8()
    57  	o.Verification = Type(rBuffer.ReadU8())
    58  	rBuffer.Skip(2)
    59  	o.Data = make([]uint8, rBuffer.ReadLen())
    60  	rBuffer.ReadBytes(o.Data)
    61  	return nil
    62  }
    63  
    64  // NewRequestCRC32 forms a Request with crc32 verification according to provided parameters.
    65  func NewRequestCRC32(handle xrdfs.FileHandle, offset int64, data []uint8) *Request {
    66  	req := &Request{Handle: handle, Offset: offset, Verification: CRC32}
    67  	crc := crc32.ChecksumIEEE(data)
    68  	crcData := make([]uint8, 4, 4+len(data))
    69  	binary.BigEndian.PutUint32(crcData, crc)
    70  	req.Data = append(crcData, data...)
    71  	return req
    72  }
    73  
    74  // ReqID implements xrdproto.Request.ReqID.
    75  func (req *Request) ReqID() uint16 { return RequestID }
    76  
    77  // ShouldSign implements xrdproto.Request.ShouldSign.
    78  func (req *Request) ShouldSign() bool { return false }