github.com/dubbogo/gost@v1.14.0/math/big/integer.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package gxbig
    19  
    20  import (
    21  	"encoding/binary"
    22  	"fmt"
    23  	"math/big"
    24  )
    25  
    26  // Integer represents a integer value.
    27  type Integer struct {
    28  	value big.Int
    29  
    30  	// used in hessian encoding/decoding
    31  	// You Should not use it in go
    32  	Signum int32
    33  	Mag    []int
    34  
    35  	// Deprecated: compatible with java8 serialize
    36  	FirstNonzeroIntNum int
    37  	LowestSetBit       int
    38  	BitLength          int
    39  	BitCount           int
    40  }
    41  
    42  func (Integer) JavaClassName() string {
    43  	return "java.math.BigInteger"
    44  }
    45  
    46  // FromString set data from a 10-bases number
    47  func (i *Integer) FromString(s string) (err error) {
    48  	_, ok := i.value.SetString(s, 10)
    49  	if !ok {
    50  		err = fmt.Errorf("'%s' is not a 10-based number", s)
    51  	}
    52  	return
    53  }
    54  
    55  // FromSignAndMag set data from a array of big-endian unsigned uint32, it's used in hessian decoding
    56  // @see https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#BigInteger-int-byte:A-
    57  func (i *Integer) FromSignAndMag(signum int32, mag []int) {
    58  	if signum == 0 && len(mag) == 0 {
    59  		return
    60  	}
    61  
    62  	bytes := make([]byte, 4*len(mag))
    63  	for j := 0; j < len(mag); j++ {
    64  		binary.BigEndian.PutUint32(bytes[j*4:(j+1)*4], uint32(mag[j]))
    65  	}
    66  	i.value.SetBytes(bytes)
    67  
    68  	if signum == -1 {
    69  		i.value.Neg(&i.value)
    70  	}
    71  }
    72  
    73  // GetSignAndMag is used in hessian encoding
    74  func (i *Integer) GetSignAndMag() (signum int32, mag []int) {
    75  	signum = int32(i.value.Sign())
    76  
    77  	bytes := i.value.Bytes()
    78  	outOf4 := len(bytes) % 4
    79  	if outOf4 > 0 {
    80  		bytes = append(make([]byte, 4-outOf4), bytes...)
    81  	}
    82  
    83  	size := len(bytes) / 4
    84  
    85  	mag = make([]int, size)
    86  
    87  	for i := 0; i < size; i++ {
    88  		mag[i] = int(binary.BigEndian.Uint32(bytes[i*4 : (i+1)*4]))
    89  	}
    90  
    91  	return
    92  }
    93  
    94  func (i *Integer) Value() *big.Int {
    95  	return &i.value
    96  }
    97  
    98  func (i *Integer) SetValue(value *big.Int) {
    99  	i.value = *value
   100  }
   101  
   102  func (i *Integer) String() string { return i.value.String() }
   103  
   104  func (i *Integer) Format(s fmt.State, ch rune) { i.value.Format(s, ch) }
   105  
   106  func (i *Integer) GobEncode() ([]byte, error) { return i.value.GobEncode() }
   107  func (i *Integer) GobDecode(buf []byte) error { return i.value.GobDecode(buf) }
   108  
   109  func (i *Integer) MarshalText() (text []byte, err error) { return i.value.MarshalText() }
   110  func (i *Integer) UnmarshalText(text []byte) error       { return i.value.UnmarshalText(text) }
   111  
   112  func (i *Integer) MarshalJSON() ([]byte, error)    { return i.value.MarshalJSON() }
   113  func (i *Integer) UnmarshalJSON(text []byte) error { return i.value.UnmarshalJSON(text) }