github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/syllab/decode-safe.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package syllab
     4  
     5  import "../convert"
     6  
     7  /*
     8  **************************************************************************************************
     9  *****************************************Fixed Size Data******************************************
    10  **************************************************************************************************
    11   */
    12  
    13  // GetFixedByteArray decodes fixed sized byte array from the payload buffer.
    14  // If you want array instead of slice from function below, You can copy function and edit it for your usage! e.g.
    15  // for get a [2]byte  ```var array [2]byte = copy(array[:], p[stackIndex:])```
    16  // for get a [32]byte  ```var array [32]byte = copy(array[:], p[stackIndex:])```
    17  func GetFixedByteArray(p []byte, stackIndex uint32, n uint32) (array []byte) {
    18  	copy(array[:], p[stackIndex:stackIndex+n])
    19  	return
    20  }
    21  
    22  // GetByte decodes BYTE from the payload buffer.
    23  func GetByte(p []byte, stackIndex uint32) byte {
    24  	return p[stackIndex]
    25  }
    26  
    27  // GetInt8 decodes INT8 from the payload buffer.
    28  func GetInt8(p []byte, stackIndex uint32) int8 {
    29  	return int8(p[stackIndex])
    30  }
    31  
    32  // GetUInt8 decodes UINT8 from the payload buffer.
    33  func GetUInt8(p []byte, stackIndex uint32) uint8 {
    34  	return uint8(p[stackIndex])
    35  }
    36  
    37  // GetBool decodes BOOL from the payload buffer.
    38  func GetBool(p []byte, stackIndex uint32) bool {
    39  	return p[stackIndex] == 1
    40  }
    41  
    42  // GetInt16 decodes INT16 from the payload buffer.
    43  func GetInt16(p []byte, stackIndex uint32) int16 {
    44  	return int16(p[stackIndex]) | int16(p[stackIndex+1])<<8
    45  }
    46  
    47  // GetUInt16 decodes UINT16 from the payload buffer.
    48  func GetUInt16(p []byte, stackIndex uint32) uint16 {
    49  	return uint16(p[stackIndex]) | uint16(p[stackIndex+1])<<8
    50  }
    51  
    52  // GetInt32 decodes INT32 from the payload buffer.
    53  func GetInt32(p []byte, stackIndex uint32) int32 {
    54  	return int32(p[stackIndex]) | int32(p[stackIndex+1])<<8 | int32(p[stackIndex+2])<<16 | int32(p[stackIndex+3])<<24
    55  }
    56  
    57  // GetUInt32 decodes UINT32 from the payload buffer.
    58  func GetUInt32(p []byte, stackIndex uint32) uint32 {
    59  	return uint32(p[stackIndex]) | uint32(p[stackIndex+1])<<8 | uint32(p[stackIndex+2])<<16 | uint32(p[stackIndex+3])<<24
    60  }
    61  
    62  // GetFloat32 decodes FLOAT32 from the payload buffer.
    63  func GetFloat32(p []byte, stackIndex uint32) float32 {
    64  	return float32(GetUInt32(p, stackIndex))
    65  }
    66  
    67  // GetInt64 decodes INT64 from the payload buffer.
    68  func GetInt64(p []byte, stackIndex uint32) int64 {
    69  	return int64(p[stackIndex]) | int64(p[stackIndex+1])<<8 | int64(p[stackIndex+2])<<16 | int64(p[stackIndex+3])<<24 |
    70  		int64(p[stackIndex+4])<<32 | int64(p[stackIndex+5])<<40 | int64(p[stackIndex+6])<<48 | int64(p[stackIndex+7])<<56
    71  }
    72  
    73  // GetUInt64 decodes UINT64 from the payload buffer.
    74  func GetUInt64(p []byte, stackIndex uint32) uint64 {
    75  	return uint64(p[stackIndex]) | uint64(p[stackIndex+1])<<8 | uint64(p[stackIndex+2])<<16 | uint64(p[stackIndex+3])<<24 |
    76  		uint64(p[stackIndex+4])<<32 | uint64(p[stackIndex+5])<<40 | uint64(p[stackIndex+6])<<48 | uint64(p[stackIndex+7])<<56
    77  }
    78  
    79  // GetFloat64 decodes FLOAT64 from the payload buffer.
    80  func GetFloat64(p []byte, stackIndex uint32) float64 {
    81  	return float64(GetUInt64(p, stackIndex))
    82  }
    83  
    84  // GetComplex64 decodes COMPLEX64 from the payload buffer.
    85  func GetComplex64(p []byte, stackIndex uint32) complex64 {
    86  	return complex(GetFloat32(p, stackIndex), GetFloat32(p, stackIndex+4))
    87  }
    88  
    89  // GetComplex128 decodes COMPLEX128 from the payload buffer.
    90  func GetComplex128(p []byte, stackIndex uint32) complex128 {
    91  	return complex(GetFloat64(p, stackIndex), GetFloat64(p, stackIndex+8))
    92  }
    93  
    94  /*
    95  **************************************************************************************************
    96  **************************************Dynamically size Data**************************************
    97  **************************************************************************************************
    98   */
    99  
   100  // GetString decodes string from the payload buffer!
   101  func GetString(p []byte, stackIndex uint32) string {
   102  	return string(GetByteArray(p, stackIndex))
   103  }
   104  
   105  // GetByteArray decodes byte||uint8 array from the payload buffer!
   106  func GetByteArray(p []byte, stackIndex uint32) (slice []byte) {
   107  	var add uint32 = GetUInt32(p, stackIndex)
   108  	var ln uint32 = GetUInt32(p, stackIndex+4)
   109  	slice = make([]byte, ln)
   110  	copy(slice, p[add:])
   111  	return
   112  }
   113  
   114  // GetInt8Array decodes int8 array from the payload buffer!
   115  func GetInt8Array(p []byte, stackIndex uint32) (slice []int8) {
   116  	var add uint32 = GetUInt32(p, stackIndex)
   117  	var ln uint32 = GetUInt32(p, stackIndex+4)
   118  	slice = make([]int8, ln)
   119  	copy(slice, convert.UnsafeByteSliceToInt8Slice(p[add:]))
   120  	return
   121  }
   122  
   123  // GetBoolArray decodes bool array from the payload buffer!
   124  func GetBoolArray(p []byte, stackIndex uint32) (slice []bool) {
   125  	var add uint32 = GetUInt32(p, stackIndex)
   126  	var ln uint32 = GetUInt32(p, stackIndex+4)
   127  	slice = make([]bool, ln)
   128  	copy(slice, convert.UnsafeByteSliceToBoolSlice(p[add:]))
   129  	return
   130  }
   131  
   132  // GetInt16Array decode Int16 array from the payload buffer
   133  func GetInt16Array(p []byte, stackIndex uint32) (slice []int16) {
   134  	var add uint32 = GetUInt32(p, stackIndex)
   135  	var ln uint32 = GetUInt32(p, stackIndex+4)
   136  	slice = make([]int16, ln)
   137  	copy(slice, convert.UnsafeByteSliceToInt16Slice(p[add:]))
   138  	return
   139  }
   140  
   141  // GetUInt16Array decode UInt16 array from the payload buffer
   142  func GetUInt16Array(p []byte, stackIndex uint32) (slice []uint16) {
   143  	var add uint32 = GetUInt32(p, stackIndex)
   144  	var ln uint32 = GetUInt32(p, stackIndex+4)
   145  	slice = make([]uint16, ln)
   146  	copy(slice, convert.UnsafeByteSliceToUInt16Slice(p[add:]))
   147  	return
   148  }
   149  
   150  // GetInt32Array decode fixed size Int32 array from the payload buffer
   151  func GetInt32Array(p []byte, stackIndex uint32) (slice []int32) {
   152  	var add uint32 = GetUInt32(p, stackIndex)
   153  	var ln uint32 = GetUInt32(p, stackIndex+4)
   154  	slice = make([]int32, ln)
   155  	copy(slice, convert.UnsafeByteSliceToInt32Slice(p[add:]))
   156  	return
   157  }
   158  
   159  // GetUInt32Array decode fixed size UInt32 array from the payload buffer
   160  func GetUInt32Array(p []byte, stackIndex uint32) (slice []uint32) {
   161  	var add uint32 = GetUInt32(p, stackIndex)
   162  	var ln uint32 = GetUInt32(p, stackIndex+4)
   163  	slice = make([]uint32, ln)
   164  	copy(slice, convert.UnsafeByteSliceToUInt32Slice(p[add:]))
   165  	return
   166  }
   167  
   168  // GetInt64Array decode fixed size Int64 array from the payload buffer
   169  func GetInt64Array(p []byte, stackIndex uint32) (slice []int64) {
   170  	var add uint32 = GetUInt32(p, stackIndex)
   171  	var ln uint32 = GetUInt32(p, stackIndex+4)
   172  	slice = make([]int64, ln)
   173  	copy(slice, convert.UnsafeByteSliceToInt64Slice(p[add:]))
   174  	return
   175  }
   176  
   177  // GetUInt64Array decode fixed size UInt64 array from the payload buffer
   178  func GetUInt64Array(p []byte, stackIndex uint32) (slice []uint64) {
   179  	var add uint32 = GetUInt32(p, stackIndex)
   180  	var ln uint32 = GetUInt32(p, stackIndex+4)
   181  	slice = make([]uint64, ln)
   182  	copy(slice, convert.UnsafeByteSliceToUInt64Slice(p[add:]))
   183  	return
   184  }
   185  
   186  // GetFloat32Array decode fixed size Float32 array from the payload buffer
   187  func GetFloat32Array(p []byte, stackIndex uint32) (slice []float32) {
   188  	var add uint32 = GetUInt32(p, stackIndex)
   189  	var ln uint32 = GetUInt32(p, stackIndex+4)
   190  	slice = make([]float32, ln)
   191  	copy(slice, convert.UnsafeByteSliceToFloat32Slice(p[add:]))
   192  	return
   193  }
   194  
   195  // GetFloat64Array decode fixed size Float64 array from the payload buffer
   196  func GetFloat64Array(p []byte, stackIndex uint32) (slice []float64) {
   197  	var add uint32 = GetUInt32(p, stackIndex)
   198  	var ln uint32 = GetUInt32(p, stackIndex+4)
   199  	slice = make([]float64, ln)
   200  	copy(slice, convert.UnsafeByteSliceToFloat64Slice(p[add:]))
   201  	return
   202  }
   203  
   204  // GetComplex64Array decode fixed size Complex64 array from the payload buffer
   205  func GetComplex64Array(p []byte, stackIndex uint32) (slice []complex64) {
   206  	var add uint32 = GetUInt32(p, stackIndex)
   207  	var ln uint32 = GetUInt32(p, stackIndex+4)
   208  	slice = make([]complex64, ln)
   209  	copy(slice, convert.UnsafeByteSliceToComplex64Slice(p[add:]))
   210  	return
   211  }
   212  
   213  // GetComplex128Array decode fixed size Complex128 array from the payload buffer
   214  func GetComplex128Array(p []byte, stackIndex uint32) (slice []complex128) {
   215  	var add uint32 = GetUInt32(p, stackIndex)
   216  	var ln uint32 = GetUInt32(p, stackIndex+4)
   217  	slice = make([]complex128, ln)
   218  	copy(slice, convert.UnsafeByteSliceToComplex128Slice(p[add:]))
   219  	return
   220  }
   221  
   222  /*
   223  **************************************************************************************************
   224  *******************Dynamically size ARRAY inside other Dynamically size Array*******************
   225  **************************************************************************************************
   226   */
   227  
   228  // GetStringArray encode string array to the payload buffer!
   229  func GetStringArray(p []byte, stackIndex uint32) (slice []string) {
   230  	var add uint32 = GetUInt32(p, stackIndex)
   231  	var ln uint32 = GetUInt32(p, stackIndex+4)
   232  	slice = make([]string, ln)
   233  
   234  	for i := 0; i < int(ln); i++ {
   235  		var eachAdd uint32 = GetUInt32(p, add)
   236  		var eachLn uint32 = GetUInt32(p, add+4)
   237  		slice[i] = string(p[eachAdd : eachAdd+eachLn])
   238  		add += 8
   239  	}
   240  	return
   241  }