Browse code

network byte order to bitseq serializer

Signed-off-by: Alessandro Boch <aboch@docker.com>

Alessandro Boch authored on 2015/06/19 11:14:03
Showing 5 changed files
... ...
@@ -4,11 +4,11 @@
4 4
 package bitseq
5 5
 
6 6
 import (
7
+	"encoding/binary"
7 8
 	"fmt"
8 9
 	"sync"
9 10
 
10 11
 	"github.com/docker/libnetwork/datastore"
11
-	"github.com/docker/libnetwork/netutils"
12 12
 )
13 13
 
14 14
 // Block Sequence constants
... ...
@@ -134,14 +134,15 @@ func (s *Sequence) Equal(o *Sequence) bool {
134 134
 }
135 135
 
136 136
 // ToByteArray converts the sequence into a byte array
137
-// TODO (aboch): manage network/host order stuff
138 137
 func (s *Sequence) ToByteArray() ([]byte, error) {
139 138
 	var bb []byte
140 139
 
141 140
 	p := s
142 141
 	for p != nil {
143
-		bb = append(bb, netutils.U32ToA(p.Block)...)
144
-		bb = append(bb, netutils.U32ToA(p.Count)...)
142
+		b := make([]byte, 8)
143
+		binary.BigEndian.PutUint32(b[0:], p.Block)
144
+		binary.BigEndian.PutUint32(b[4:], p.Count)
145
+		bb = append(bb, b...)
145 146
 		p = p.Next
146 147
 	}
147 148
 
... ...
@@ -149,7 +150,6 @@ func (s *Sequence) ToByteArray() ([]byte, error) {
149 149
 }
150 150
 
151 151
 // FromByteArray construct the sequence from the byte array
152
-// TODO (aboch): manage network/host order stuff
153 152
 func (s *Sequence) FromByteArray(data []byte) error {
154 153
 	l := len(data)
155 154
 	if l%8 != 0 {
... ...
@@ -159,8 +159,8 @@ func (s *Sequence) FromByteArray(data []byte) error {
159 159
 	p := s
160 160
 	i := 0
161 161
 	for {
162
-		p.Block = netutils.ATo32(data[i : i+4])
163
-		p.Count = netutils.ATo32(data[i+4 : i+8])
162
+		p.Block = binary.BigEndian.Uint32(data[i : i+4])
163
+		p.Count = binary.BigEndian.Uint32(data[i+4 : i+8])
164 164
 		i += 8
165 165
 		if i == l {
166 166
 			break
... ...
@@ -229,12 +229,12 @@ func (h *Handle) Destroy() {
229 229
 
230 230
 // ToByteArray converts this handle's data into a byte array
231 231
 func (h *Handle) ToByteArray() ([]byte, error) {
232
-	ba := make([]byte, 8)
233 232
 
234 233
 	h.Lock()
235 234
 	defer h.Unlock()
236
-	copy(ba[0:4], netutils.U32ToA(h.bits))
237
-	copy(ba[4:8], netutils.U32ToA(h.unselected))
235
+	ba := make([]byte, 8)
236
+	binary.BigEndian.PutUint32(ba[0:], h.bits)
237
+	binary.BigEndian.PutUint32(ba[4:], h.unselected)
238 238
 	bm, err := h.head.ToByteArray()
239 239
 	if err != nil {
240 240
 		return nil, fmt.Errorf("failed to serialize head: %s", err.Error())
... ...
@@ -258,8 +258,8 @@ func (h *Handle) FromByteArray(ba []byte) error {
258 258
 
259 259
 	h.Lock()
260 260
 	h.head = nh
261
-	h.bits = netutils.ATo32(ba[0:4])
262
-	h.unselected = netutils.ATo32(ba[4:8])
261
+	h.bits = binary.BigEndian.Uint32(ba[0:4])
262
+	h.unselected = binary.BigEndian.Uint32(ba[4:8])
263 263
 	h.Unlock()
264 264
 
265 265
 	return nil
... ...
@@ -2,6 +2,8 @@ package bitseq
2 2
 
3 3
 import (
4 4
 	"testing"
5
+
6
+	_ "github.com/docker/libnetwork/netutils"
5 7
 )
6 8
 
7 9
 func TestSequenceGetAvailableBit(t *testing.T) {
... ...
@@ -2,6 +2,8 @@ package idm
2 2
 
3 3
 import (
4 4
 	"testing"
5
+
6
+	_ "github.com/docker/libnetwork/netutils"
5 7
 )
6 8
 
7 9
 func TestNew(t *testing.T) {
... ...
@@ -7,6 +7,7 @@ import (
7 7
 	"time"
8 8
 
9 9
 	"github.com/docker/libnetwork/bitseq"
10
+	_ "github.com/docker/libnetwork/netutils"
10 11
 )
11 12
 
12 13
 func getAllocator(t *testing.T, subnet *net.IPNet) *Allocator {
... ...
@@ -180,53 +180,3 @@ func GenerateIfaceName(prefix string, len int) (string, error) {
180 180
 	}
181 181
 	return "", types.InternalErrorf("could not generate interface name")
182 182
 }
183
-
184
-func byteArrayToInt(array []byte, numBytes int) uint64 {
185
-	if numBytes <= 0 || numBytes > 8 {
186
-		panic("Invalid argument")
187
-	}
188
-	num := 0
189
-	for i := 0; i <= len(array)-1; i++ {
190
-		num += int(array[len(array)-1-i]) << uint(i*8)
191
-	}
192
-	return uint64(num)
193
-}
194
-
195
-// ATo64 converts a byte array into a uint32
196
-func ATo64(array []byte) uint64 {
197
-	return byteArrayToInt(array, 8)
198
-}
199
-
200
-// ATo32 converts a byte array into a uint32
201
-func ATo32(array []byte) uint32 {
202
-	return uint32(byteArrayToInt(array, 4))
203
-}
204
-
205
-// ATo16 converts a byte array into a uint16
206
-func ATo16(array []byte) uint16 {
207
-	return uint16(byteArrayToInt(array, 2))
208
-}
209
-
210
-func intToByteArray(val uint64, numBytes int) []byte {
211
-	array := make([]byte, numBytes)
212
-	for i := numBytes - 1; i >= 0; i-- {
213
-		array[i] = byte(val & 0xff)
214
-		val = val >> 8
215
-	}
216
-	return array
217
-}
218
-
219
-// U64ToA converts a uint64 to a byte array
220
-func U64ToA(val uint64) []byte {
221
-	return intToByteArray(uint64(val), 8)
222
-}
223
-
224
-// U32ToA converts a uint64 to a byte array
225
-func U32ToA(val uint32) []byte {
226
-	return intToByteArray(uint64(val), 4)
227
-}
228
-
229
-// U16ToA converts a uint64 to a byte array
230
-func U16ToA(val uint16) []byte {
231
-	return intToByteArray(uint64(val), 2)
232
-}