github.com/insolar/vanilla@v0.0.0-20201023172447-248fdf805322/protokit/encode.go (about)

     1  // Copyright 2020 Insolar Network Ltd.
     2  // All rights reserved.
     3  // This material is licensed under the Insolar License version 1.0,
     4  // available at https://github.com/insolar/assured-ledger/blob/master/LICENSE.md.
     5  
     6  package protokit
     7  
     8  import (
     9  	"io"
    10  
    11  	"github.com/insolar/vanilla/throw"
    12  )
    13  
    14  func SizeTag(fieldID int) int {
    15  	switch {
    16  	case fieldID <= 0:
    17  	case fieldID > MaxFieldID:
    18  	default:
    19  		return SizeVarint32(uint32(fieldID))
    20  	}
    21  	panic(throw.IllegalValue())
    22  }
    23  
    24  func SizeVarint32(x uint32) int {
    25  	switch {
    26  	case x < 1<<7:
    27  		return 1
    28  	case x < 1<<14:
    29  		return 2
    30  	case x < 1<<21:
    31  		return 3
    32  	case x < 1<<28:
    33  		return 4
    34  	}
    35  	return 5
    36  }
    37  
    38  // SizeVarint returns the varint encoding size of an integer.
    39  func SizeVarint64(x uint64) int {
    40  	switch {
    41  	case x < 1<<7:
    42  		return 1
    43  	case x < 1<<14:
    44  		return 2
    45  	case x < 1<<21:
    46  		return 3
    47  	case x < 1<<28:
    48  		return 4
    49  	case x < 1<<35:
    50  		return 5
    51  	case x < 1<<42:
    52  		return 6
    53  	case x < 1<<49:
    54  		return 7
    55  	case x < 1<<56:
    56  		return 8
    57  	case x < 1<<63:
    58  		return 9
    59  	}
    60  	return 10
    61  }
    62  
    63  func EncodeVarint(w io.ByteWriter, u uint64) error {
    64  	for u > 0x7F {
    65  		if err := w.WriteByte(byte(u | 0x80)); err != nil {
    66  			return err
    67  		}
    68  		u >>= 7
    69  	}
    70  	return w.WriteByte(byte(u))
    71  }
    72  
    73  func EncodeVarintToBytes(b []byte, u uint64) (n int) {
    74  	for u > 0x7F {
    75  		b[n] = byte(u | 0x80)
    76  		n++
    77  		u >>= 7
    78  	}
    79  	b[n] = byte(u)
    80  	n++
    81  	return
    82  }
    83  
    84  func EncodeVarintToBytesWithError(b []byte, u uint64) (n int, err error) {
    85  	for u > 0x7F {
    86  		if n >= len(b) {
    87  			return 0, io.ErrShortBuffer
    88  		}
    89  		b[n] = byte(u | 0x80)
    90  		n++
    91  		u >>= 7
    92  	}
    93  	if n >= len(b) {
    94  		return 0, io.ErrShortBuffer
    95  	}
    96  	b[n] = byte(u)
    97  	n++
    98  	return
    99  }
   100  
   101  func EncodeFixed64(w io.ByteWriter, u uint64) error {
   102  	if err := EncodeFixed32(w, uint32(u)); err != nil {
   103  		return err
   104  	}
   105  	return EncodeFixed32(w, uint32(u>>32))
   106  }
   107  
   108  func EncodeFixed32(w io.ByteWriter, u uint32) error {
   109  	if err := w.WriteByte(byte(u)); err != nil {
   110  		return err
   111  	}
   112  	u >>= 8
   113  	if err := w.WriteByte(byte(u)); err != nil {
   114  		return err
   115  	}
   116  	u >>= 8
   117  	if err := w.WriteByte(byte(u)); err != nil {
   118  		return err
   119  	}
   120  	u >>= 8
   121  	if err := w.WriteByte(byte(u)); err != nil {
   122  		return err
   123  	}
   124  	return nil
   125  }
   126  
   127  func EncodeFixed64ToBytes(b []byte, u uint64) (int, error) {
   128  	if len(b) < 8 {
   129  		return 0, io.ErrShortBuffer
   130  	}
   131  
   132  	b[0] = byte(u)
   133  	b[1] = byte(u >> 8)
   134  	b[2] = byte(u >> 16)
   135  	b[3] = byte(u >> 24)
   136  	b[4] = byte(u >> 32)
   137  	b[5] = byte(u >> 40)
   138  	b[6] = byte(u >> 48)
   139  	b[7] = byte(u >> 56)
   140  	return 8, nil
   141  }
   142  
   143  func EncodeFixed32ToBytes(b []byte, u uint32) (int, error) {
   144  	if len(b) < 4 {
   145  		return 0, io.ErrShortBuffer
   146  	}
   147  
   148  	b[0] = byte(u)
   149  	b[1] = byte(u >> 8)
   150  	b[2] = byte(u >> 16)
   151  	b[3] = byte(u >> 24)
   152  	return 4, nil
   153  }