Message:
Hello golan...@googlegroups.com (cc: golan...@googlegroups.com),
I'd like you to review this change to
https://go.googlecode.com/hg/
Description:
encoding/binary: provide a NativeEndian ByteOrder.
This is very useful when serializing C structs to communicate
with the local kernel.
Please review this at http://codereview.appspot.com/4595047/
Affected files:
   M src/pkg/encoding/binary/binary.go
Index: src/pkg/encoding/binary/binary.go
===================================================================
--- a/src/pkg/encoding/binary/binary.go
+++ b/src/pkg/encoding/binary/binary.go
@@ -12,6 +12,7 @@ import (
  	"io"
  	"os"
  	"reflect"
+	"unsafe"
  )
  // A ByteOrder specifies how to convert byte sequences into
@@ -36,6 +37,21 @@ var LittleEndian littleEndian
  // BigEndian is the big-endian implementation of ByteOrder.
  var BigEndian bigEndian
+// NativeEndian is the ByteOrder of the current system.
+var NativeEndian ByteOrder
+
+func init() {
+	// Examine the memory layout of an int16 to determine system
+	// endianness.
+	var one int16 = 1
+	b := (*byte)(unsafe.Pointer(&one))
+	if *b == 0 {
+		NativeEndian = BigEndian
+	} else {
+		NativeEndian = LittleEndian
+	}
+}
+
  type littleEndian unused
  func (littleEndian) Uint16(b []byte) uint16 { return uint16(b[0]) |  
uint16(b[1])<<8 }
Also, it's a shame to put package unsafe in here.
This is in the wrong place. If the need is to serialize C structs, put
it in C code. Go shouldn't need to care about byte order.
Also, it's a shame to put package unsafe in here.
On Sat, Jun 11, 2011 at 14:47, <r...@golang.org> wrote:This is in the wrong place. If the need is to serialize C structs, put
it in C code. Go shouldn't need to care about byte order.It would be nice to be able to be byte-agnostic in all situations. My programs of choice however use strange linux kernel APIs (notably Netlink). These APIs look like network protocols, but mandate that the fields shall be encoded with the system's native endianness. In practice, on the kernel side a packed C struct is used to deserialize the packets, which is why I talked about serializing C structs. That was a misnomer, I want to serialize data to binary protocols that require native-endian encoding.Also, it's a shame to put package unsafe in here.I agree. The unsafe C-style cast is the only mechanism I know of to reliably determine endianness of the current machine. Any suggestions on how to avoid the use of unsafe?
-rob
That's not a serialization.  That's a pointer to a struct.
Look at how the netlink code in package syscall
(IfAddrmsg, for example) works.
Russ
RFC 3549 defines netlink as a packet protocol. As it happens, the initial C userspace implementation that led to that RFC used packed structs to construct those packets, because in C that's the most straightforward and kernelly way of doing it. But from other languages, netlink can (and should imho, but that's just personal religion) be considered purely as a network protocol, rather than as an arcane struct-based kernel API that happens to be bolted onto a socket.
-rob
Except for ARM processors, which are big-endian I believe, and PC
graphics, which is half big-endian, and ....
> Except for ARM processors, which are big-endian I believe, and PC
> graphics, which is half big-endian, and ....
ARM can be both, some implementations are even switchable (similar to
PowerPC).  Early ARM ABIs for Linux were big-endian, but current ones
are not.