MCPcopy Create free account
hub / github.com/copernet/copernicus / Serialize

Method Serialize

model/script/scriptnum.go:153–190  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

151
152}
153func (n *ScriptNum) Serialize() (bytes []byte) {
154 if n.Value == 0 {
155 return nil
156 }
157
158 negative := n.Value < 0
159 absoluteValue := n.Value
160
161 if negative {
162 absoluteValue = -n.Value
163 }
164 bytes = make([]byte, 0, 9)
165 for absoluteValue > 0 {
166 bytes = append(bytes, byte(absoluteValue&0xff))
167 absoluteValue >>= 8
168 }
169 // - If the most significant byte is >= 0x80 and the value is positive, push a
170 // new zero-byte to make the significant byte < 0x80 again.
171
172 // - If the most significant byte is >= 0x80 and the value is negative, push a
173 // new 0x80 byte that will be popped off when converting to an integral.
174
175 // - If the most significant byte is < 0x80 and the value is negative, add
176 // 0x80 to it, since it will be subtracted and interpreted as a negative when
177 // converting to an integral.
178
179 if bytes[len(bytes)-1]&0x80 != 0 {
180 extraByte := byte(0x00)
181 if negative {
182 extraByte = 0x80
183 }
184 bytes = append(bytes, extraByte)
185 } else if negative {
186 bytes[len(bytes)-1] |= 0x80
187 }
188
189 return
190}
191
192func MinimallyEncode(data []byte) []byte {
193 dataLen := len(data)

Callers 2

doScriptJSONTestFunction · 0.95
EvalScriptFunction · 0.95

Calls

no outgoing calls

Tested by 1

doScriptJSONTestFunction · 0.76