Giter VIP home page Giter VIP logo

go-xdr's Introduction

go-xdr

[Build Status] (https://travis-ci.org/davecgh/go-xdr) [![Coverage Status] (https://coveralls.io/repos/davecgh/go-xdr/badge.png?branch=master)] (https://coveralls.io/r/davecgh/go-xdr?branch=master)

Go-xdr implements the data representation portion of the External Data Representation (XDR) standard protocol as specified in RFC 4506 (obsoletes RFC 1832 and RFC 1014) in Pure Go (Golang). A comprehensive suite of tests are provided to ensure proper functionality. It is licensed under the liberal ISC license, so it may be used in open source or commercial projects.

NOTE: Version 1 of this package is still available via the github.com/davecgh/go-xdr/xdr import path to avoid breaking existing clients. However, it is highly recommended that all old clients upgrade to version 2 and all new clients use version 2. In addition to some speed optimizations, version 2 has been been updated to work with standard the io.Reader and io.Writer interfaces instead of raw byte slices. This allows it to be much more flexible and work directly with files, network connections, etc.

Documentation

[GoDoc] (http://godoc.org/github.com/davecgh/go-xdr/xdr2)

Full go doc style documentation for the project can be viewed online without installing this package by using the excellent GoDoc site here: http://godoc.org/github.com/davecgh/go-xdr/xdr2

You can also view the documentation locally once the package is installed with the godoc tool by running godoc -http=":6060" and pointing your browser to http://localhost:6060/pkg/github.com/davecgh/go-xdr/xdr2/

Installation

$ go get github.com/davecgh/go-xdr/xdr2

Sample Decode Program

package main

import (
	"bytes"
    "fmt"

    "github.com/davecgh/go-xdr/xdr2"
)

func main() {
	// Hypothetical image header format.
	type ImageHeader struct {
		Signature   [3]byte
		Version     uint32
		IsGrayscale bool
		NumSections uint32
	}

	// XDR encoded data described by the above structure.  Typically this would
	// be read from a file or across the network, but use a manual byte array
	// here as an example.
	encodedData := []byte{
		0xAB, 0xCD, 0xEF, 0x00, // Signature
		0x00, 0x00, 0x00, 0x02, // Version
		0x00, 0x00, 0x00, 0x01, // IsGrayscale
		0x00, 0x00, 0x00, 0x0A, // NumSections
	}

	// Declare a variable to provide Unmarshal with a concrete type and instance
	// to decode into.
	var h ImageHeader
	bytesRead, err := xdr.Unmarshal(bytes.NewReader(encodedData), &h)
	if err != nil {
		fmt.Println(err)
		return
	}
  
	fmt.Println("bytes read:", bytesRead)
	fmt.Printf("h: %+v", h)
}

The struct instance, h, will then contain the following values:

h.Signature = [3]byte{0xAB, 0xCD, 0xEF}
h.Version = 2
h.IsGrayscale = true
h.NumSections = 10

Sample Encode Program

package main

import (
	"bytes"
    "fmt"

    "github.com/davecgh/go-xdr/xdr2"
)

func main() {
	// Hypothetical image header format.
	type ImageHeader struct {
		Signature   [3]byte
		Version     uint32
		IsGrayscale bool
		NumSections uint32
	}

	// Sample image header data.
	h := ImageHeader{[3]byte{0xAB, 0xCD, 0xEF}, 2, true, 10}

	// Use marshal to automatically determine the appropriate underlying XDR
	// types and encode.
	var w bytes.Buffer
	bytesWritten, err := xdr.Marshal(&w, &h)
	if err != nil {
		fmt.Println(err)
		return
	}

	encodedData := w.Bytes()
	fmt.Println("bytes written:", bytesWritten)
	fmt.Println("encoded data:", encodedData)
}

The result, encodedData, will then contain the following XDR encoded byte sequence:

0xAB, 0xCD, 0xEF, 0x00,
0x00, 0x00, 0x00, 0x02,
0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x0A,

License

Go-xdr is licensed under the liberal ISC License.

go-xdr's People

Contributors

dajohi avatar davecgh avatar marcopeereboom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

go-xdr's Issues

Accepting contributions?

Hi @davecgh,

Are you still maintaining this package and would you be open to accepting a contribution that adds an xdr3 package, that is a copy of xdr2 but adds optionals, unions, and change how pointers work to support using optionals?

We've been using a modified version of this package here: https://github.com/stellar/go-xdr and if you'd be up for us bringing our changes upstream we'd be up for preparing a PR.

This is approximately what the diff would look like, but we'd also need to merge downstream some of the changes in your master branch before we opened, so there would be some differences to keep xdr3 as a copy of xdr2 at the point in time the PR is opened:
master...stellar:master

No worries if you're no longer maintaining go-xdr, and if that is the case we'll move forward with developing our fork of go-xdr at https://github.com/stellar/go-xdr.

Cheers,
Leigh

cc @bartekn @ire-and-curses @marcopeereboom @rasky

Support for time.Time

I added XDR to my go_serialization_benchmarks and it is currently the fastest.

Unfortunately, it does not correctly serialize time.Time. This is not surprising as it isn't supported, but it would be great to support it if possible.

It could perhaps use a scheme similar to JSON, marshaling to RFC3339.

Please add max size to decoder.

When reading XDR directly of the wire a remote malicious person could craft packets that could use up all of machines memory because there are no limits. It would be helpful if we could set a limit on how large a read can be. For example:

dec := NewDecoderLimited(myReader, 1024*1024) // limit to 1MB
_, err := dec.Decode(&myXDR)
if err == xdr.ErrTooBig {
        myReader.Close()
        return err
}

unexpected EOF while decoding 260 bytes

Maybe you could help me solving my issue.

I've posted all necessary information in issue prashanthpai/sunrpc#3 because I thought it could be an issue with his library.
Now we figured out that it is more an issue with variable length opaque data on the decoding side ;)

If you want me to paste all information from the other issue in this issue, I'll do that.

Thanks :)

Support for optional data (4.19) of the XDR spec

I tried decoding optional-data 4.19 and came across this comment in the code

https://github.com/davecgh/go-xdr/blob/master/xdr2/decode.go#L572

// RFC Sections 4.15 though 4.19 only apply to the data specification language
// which is not implemented by this package

I don't understand the "only apply to the data specification language" bit. The API I'm using (NFSv3, to be specific) is encoding a linked-list with the above optional-data format and other than decoding each element and then checking for the "follows" 4B for zero before terminating (which is what I'm currently doing here), is there a way to decode this data?

If the loop-and-check method is the only way, are you averse to me pushing a PR to add support for this? I'm thinking it would require a StructTag to descend down the right path for a slice. Let me know and I'll be happy to get started on that.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.