github.com/RomiChan/protobuf@v0.1.1-0.20230204044148-2ed269a2e54d/proto/normal_codec.go (about)

     1  package proto
     2  
     3  import (
     4  	"io"
     5  	"math"
     6  	"unsafe"
     7  )
     8  
     9  var boolCodec = codec{
    10  	size:   sizeOfBool,
    11  	encode: encodeBool,
    12  	decode: decodeBool,
    13  }
    14  
    15  func sizeOfBool(p unsafe.Pointer, f *structField) int {
    16  	if *(*bool)(p) {
    17  		return 1 + int(f.tagsize)
    18  	}
    19  	return 0
    20  }
    21  
    22  func encodeBool(b []byte, p unsafe.Pointer, f *structField) []byte {
    23  	if *(*bool)(p) {
    24  		b = appendVarint(b, f.wiretag)
    25  		if *(*bool)(p) { // keep this for code generate
    26  			b = append(b, 1)
    27  		} else {
    28  			b = append(b, 0)
    29  		}
    30  	}
    31  	return b
    32  }
    33  
    34  func decodeBool(b []byte, p unsafe.Pointer) (int, error) {
    35  	if len(b) == 0 {
    36  		return 0, io.ErrUnexpectedEOF
    37  	}
    38  	*(*bool)(p) = b[0] != 0
    39  	return 1, nil
    40  }
    41  
    42  var bytesCodec = codec{
    43  	size:   sizeOfBytes,
    44  	encode: encodeBytes,
    45  	decode: decodeBytes,
    46  }
    47  
    48  func sizeOfBytes(p unsafe.Pointer, f *structField) int {
    49  	v := *(*[]byte)(p)
    50  	if v != nil {
    51  		return sizeOfVarlen(len(v)) + f.tagsize
    52  	}
    53  	return 0
    54  }
    55  
    56  func encodeBytes(b []byte, p unsafe.Pointer, f *structField) []byte {
    57  	v := *(*[]byte)(p)
    58  	if v != nil {
    59  		b = appendVarint(b, f.wiretag)
    60  		b = appendVarint(b, uint64(len(v)))
    61  		b = append(b, v...)
    62  	}
    63  	return b
    64  }
    65  
    66  func decodeBytes(b []byte, p unsafe.Pointer) (int, error) {
    67  	v, n, err := decodeVarlen(b)
    68  	pb := (*[]byte)(p)
    69  	if *pb == nil {
    70  		*pb = make([]byte, 0, len(v))
    71  	}
    72  	*pb = append((*pb)[:0], v...)
    73  	return n, err
    74  }
    75  
    76  var stringCodec = codec{
    77  	size:   sizeOfString,
    78  	encode: encodeString,
    79  	decode: decodeString,
    80  }
    81  
    82  func sizeOfString(p unsafe.Pointer, f *structField) int {
    83  	v := *(*string)(p)
    84  	if v != "" {
    85  		return sizeOfVarlen(len(v)) + f.tagsize
    86  	}
    87  	return 0
    88  }
    89  
    90  func encodeString(b []byte, p unsafe.Pointer, f *structField) []byte {
    91  	v := *(*string)(p)
    92  	if v != "" {
    93  		b = appendVarint(b, f.wiretag)
    94  		b = appendVarint(b, uint64(len(v)))
    95  		b = append(b, v...)
    96  	}
    97  	return b
    98  }
    99  
   100  func decodeString(b []byte, p unsafe.Pointer) (int, error) {
   101  	v, n, err := decodeVarlen(b)
   102  	if n == 0 {
   103  		*(*string)(p) = ""
   104  		return 0, err
   105  	}
   106  	*(*string)(p) = string(v)
   107  	return n, err
   108  }
   109  
   110  var float32Codec = codec{
   111  	size:   sizeOfFloat32,
   112  	encode: encodeFloat32,
   113  	decode: decodeFloat32,
   114  }
   115  
   116  func sizeOfFloat32(p unsafe.Pointer, f *structField) int {
   117  	if *(*float32)(p) != 0 || math.Signbit(float64(*(*float32)(p))) {
   118  		return 4 + int(f.tagsize)
   119  	}
   120  	return 0
   121  }
   122  
   123  func encodeFloat32(b []byte, p unsafe.Pointer, f *structField) []byte {
   124  	if v := *(*float32)(p); v != 0 || math.Signbit(float64(v)) {
   125  		b = appendVarint(b, f.wiretag)
   126  		b = encodeLE32(b, math.Float32bits(v))
   127  	}
   128  	return b
   129  }
   130  
   131  func decodeFloat32(b []byte, p unsafe.Pointer) (int, error) {
   132  	v, n, err := decodeLE32(b)
   133  	*(*float32)(p) = math.Float32frombits(v)
   134  	return n, err
   135  }
   136  
   137  var float64Codec = codec{
   138  	size:   sizeOfFloat64,
   139  	encode: encodeFloat64,
   140  	decode: decodeFloat64,
   141  }
   142  
   143  func sizeOfFloat64(p unsafe.Pointer, f *structField) int {
   144  	if *(*float64)(p) != 0 || math.Signbit(*(*float64)(p)) {
   145  		return 8 + int(f.tagsize)
   146  	}
   147  	return 0
   148  }
   149  
   150  func encodeFloat64(b []byte, p unsafe.Pointer, f *structField) []byte {
   151  	if v := *(*float64)(p); v != 0 || math.Signbit(v) {
   152  		b = appendVarint(b, f.wiretag)
   153  		b = encodeLE64(b, math.Float64bits(v))
   154  	}
   155  	return b
   156  }
   157  
   158  func decodeFloat64(b []byte, p unsafe.Pointer) (int, error) {
   159  	v, n, err := decodeLE64(b)
   160  	*(*float64)(p) = math.Float64frombits(v)
   161  	return n, err
   162  }
   163  
   164  var int32Codec = codec{
   165  	size:   sizeOfInt32,
   166  	encode: encodeInt32,
   167  	decode: decodeInt32,
   168  }
   169  
   170  func sizeOfInt32(p unsafe.Pointer, f *structField) int {
   171  	v := *(*int32)(p)
   172  	if v != 0 {
   173  		return sizeOfVarint(uint64(v)) + f.tagsize
   174  	}
   175  	return 0
   176  }
   177  
   178  func encodeInt32(b []byte, p unsafe.Pointer, f *structField) []byte {
   179  	v := *(*int32)(p)
   180  	if v != 0 {
   181  		b = appendVarint(b, f.wiretag)
   182  		b = appendVarint(b, uint64(v))
   183  	}
   184  	return b
   185  }
   186  
   187  func decodeInt32(b []byte, p unsafe.Pointer) (int, error) {
   188  	u, n, err := decodeVarint(b)
   189  	*(*int32)(p) = int32(int64(u))
   190  	return n, err
   191  }
   192  
   193  var int64Codec = codec{
   194  	size:   sizeOfInt64,
   195  	encode: encodeInt64,
   196  	decode: decodeInt64,
   197  }
   198  
   199  func sizeOfInt64(p unsafe.Pointer, f *structField) int {
   200  	v := *(*int64)(p)
   201  	if v != 0 {
   202  		return sizeOfVarint(uint64(v)) + f.tagsize
   203  	}
   204  	return 0
   205  }
   206  
   207  func encodeInt64(b []byte, p unsafe.Pointer, f *structField) []byte {
   208  	v := *(*int64)(p)
   209  	if v != 0 {
   210  		b = appendVarint(b, f.wiretag)
   211  		b = appendVarint(b, uint64(v))
   212  	}
   213  	return b
   214  }
   215  
   216  func decodeInt64(b []byte, p unsafe.Pointer) (int, error) {
   217  	v, n, err := decodeVarint(b)
   218  	*(*int64)(p) = int64(v)
   219  	return n, err
   220  }
   221  
   222  var uint32Codec = codec{
   223  	size:   sizeOfUint32,
   224  	encode: encodeUint32,
   225  	decode: decodeUint32,
   226  }
   227  
   228  func sizeOfUint32(p unsafe.Pointer, f *structField) int {
   229  	if v := *(*uint32)(p); v != 0 {
   230  		return sizeOfVarint(uint64(v)) + f.tagsize
   231  	}
   232  	return 0
   233  }
   234  
   235  func encodeUint32(b []byte, p unsafe.Pointer, f *structField) []byte {
   236  	if v := *(*uint32)(p); v != 0 {
   237  		b = appendVarint(b, f.wiretag)
   238  		b = appendVarint(b, uint64(v))
   239  	}
   240  	return b
   241  }
   242  
   243  func decodeUint32(b []byte, p unsafe.Pointer) (int, error) {
   244  	v, n, err := decodeVarint(b)
   245  	*(*uint32)(p) = uint32(v)
   246  	return n, err
   247  }
   248  
   249  var fixed32Codec = codec{
   250  	size:   sizeOfFixed32,
   251  	encode: encodeFixed32,
   252  	decode: decodeFixed32,
   253  }
   254  
   255  func sizeOfFixed32(p unsafe.Pointer, f *structField) int {
   256  	if *(*uint32)(p) != 0 {
   257  		return 4 + f.tagsize
   258  	}
   259  	return 0
   260  }
   261  
   262  func encodeFixed32(b []byte, p unsafe.Pointer, f *structField) []byte {
   263  	if v := *(*uint32)(p); v != 0 {
   264  		b = appendVarint(b, f.wiretag)
   265  		b = encodeLE32(b, v)
   266  	}
   267  	return b
   268  }
   269  
   270  func decodeFixed32(b []byte, p unsafe.Pointer) (int, error) {
   271  	v, n, err := decodeLE32(b)
   272  	*(*uint32)(p) = v
   273  	return n, err
   274  }
   275  
   276  var uint64Codec = codec{
   277  	size:   sizeOfUint64,
   278  	encode: encodeUint64,
   279  	decode: decodeUint64,
   280  }
   281  
   282  func sizeOfUint64(p unsafe.Pointer, f *structField) int {
   283  	if v := *(*uint64)(p); v != 0 {
   284  		return sizeOfVarint(v) + f.tagsize
   285  	}
   286  	return 0
   287  }
   288  
   289  func encodeUint64(b []byte, p unsafe.Pointer, f *structField) []byte {
   290  	if v := *(*uint64)(p); v != 0 {
   291  		b = appendVarint(b, f.wiretag)
   292  		b = appendVarint(b, v)
   293  	}
   294  	return b
   295  }
   296  
   297  func decodeUint64(b []byte, p unsafe.Pointer) (int, error) {
   298  	v, n, err := decodeVarint(b)
   299  	*(*uint64)(p) = v
   300  	return n, err
   301  }
   302  
   303  var fixed64Codec = codec{
   304  	size:   sizeOfFixed64,
   305  	encode: encodeFixed64,
   306  	decode: decodeFixed64,
   307  }
   308  
   309  func sizeOfFixed64(p unsafe.Pointer, f *structField) int {
   310  	if *(*uint64)(p) != 0 {
   311  		return 8 + f.tagsize
   312  	}
   313  	return 0
   314  }
   315  
   316  func encodeFixed64(b []byte, p unsafe.Pointer, f *structField) []byte {
   317  	if v := *(*uint64)(p); v != 0 {
   318  		b = appendVarint(b, f.wiretag)
   319  		b = encodeLE64(b, v)
   320  	}
   321  	return b
   322  }
   323  
   324  func decodeFixed64(b []byte, p unsafe.Pointer) (int, error) {
   325  	v, n, err := decodeLE64(b)
   326  	*(*uint64)(p) = v
   327  	return n, err
   328  }
   329  
   330  var zigzag32Codec = codec{
   331  	size:   sizeOfZigzag32,
   332  	encode: encodeZigzag32,
   333  	decode: decodeZigzag32,
   334  }
   335  
   336  func sizeOfZigzag32(p unsafe.Pointer, f *structField) int {
   337  	if v := *(*int32)(p); v != 0 {
   338  		return sizeOfVarint(encodeZigZag64(int64(v))) + f.tagsize
   339  	}
   340  	return 0
   341  }
   342  
   343  func encodeZigzag32(b []byte, p unsafe.Pointer, f *structField) []byte {
   344  	if v := *(*int32)(p); v != 0 {
   345  		b = appendVarint(b, f.wiretag)
   346  		b = appendVarint(b, encodeZigZag64(int64(v)))
   347  	}
   348  	return b
   349  }
   350  
   351  func decodeZigzag32(b []byte, p unsafe.Pointer) (int, error) {
   352  	u, n, err := decodeVarint(b)
   353  	*(*int32)(p) = int32(decodeZigZag64(u))
   354  	return n, err
   355  }
   356  
   357  var zigzag64Codec = codec{
   358  	size:   sizeOfZigzag64,
   359  	encode: encodeZigzag64,
   360  	decode: decodeZigzag64,
   361  }
   362  
   363  func sizeOfZigzag64(p unsafe.Pointer, f *structField) int {
   364  	if v := *(*int64)(p); v != 0 {
   365  		return sizeOfVarint(encodeZigZag64(v)) + f.tagsize
   366  	}
   367  	return 0
   368  }
   369  
   370  func encodeZigzag64(b []byte, p unsafe.Pointer, f *structField) []byte {
   371  	if v := *(*int64)(p); v != 0 {
   372  		b = appendVarint(b, f.wiretag)
   373  		b = appendVarint(b, encodeZigZag64(v))
   374  	}
   375  	return b
   376  }
   377  
   378  func decodeZigzag64(b []byte, p unsafe.Pointer) (int, error) {
   379  	v, n, err := decodeVarint(b)
   380  	*(*int64)(p) = decodeZigZag64(v)
   381  	return n, err
   382  }