Created
December 5, 2013 01:04
-
-
Save olliebun/7798493 to your computer and use it in GitHub Desktop.
C OID handling in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type OID []uint32 | |
// Create a new OID | |
func NewOID(num ...uint32) OID { | |
return num | |
} | |
// Create an OID from the C representation | |
func NewOIDFromCArray(coid *C.oid, oid_length C.int) (OID, error) { | |
// See http://stackoverflow.com/questions/14826319/go-cgo-how-do-you-use-a-c-array-passed-as-a-pointer | |
var ( | |
o OID | |
err error | |
buf *bytes.Reader | |
result = make([]uint32, int(oid_length)) | |
size = C.int(unsafe.Sizeof(*coid)) | |
b = C.GoBytes(unsafe.Pointer(coid), size*oid_length) | |
) | |
// Read a single uint32 from the buffer | |
// Each OID is 4 little-endian, with 4 bytes of padding between them | |
for i := 0; i < int(size*oid_length); i += 8 { | |
var out uint32 | |
buf = bytes.NewReader(b[i : i+8]) | |
if err = binary.Read(buf, binary.LittleEndian, &out); err != nil { | |
return o, BadOID | |
} | |
// Append the number to the result | |
result[i/8] = out | |
} | |
return NewOID(result...), nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment