Example #1
1
// PutUint32 serializes the provided uint32 using the given byte order into a
// buffer from the free list and writes the resulting four bytes to the given
// writer.
func (l binaryFreeList) PutUint32(w io.Writer, byteOrder binary.ByteOrder, val uint32) error {
	buf := l.Borrow()[:4]
	byteOrder.PutUint32(buf, val)
	_, err := w.Write(buf)
	l.Return(buf)
	return err
}
Example #2
0
func packParamString(bin binary.ByteOrder, s string) []byte {
	b := make([]byte, (4+len(s)+1+3) & ^0x3) // must be 32-bit aligned
	bin.PutUint32(b[0:], uint32(len(s)+1))
	copy(b[4:], []byte(s))
	b[4+len(s)] = 0
	return b
}
Example #3
0
func durationToBytes(d time.Duration, order binary.ByteOrder) []byte {
	buf := make([]byte, 8)
	nsec := d.Nanoseconds()
	order.PutUint32(buf, uint32(nsec/nanosPerSec))
	order.PutUint32(buf[4:], uint32(nsec%nanosPerSec))
	return buf
}
Example #4
0
func timeToBytes(t time.Time, order binary.ByteOrder) []byte {
	sec := uint32(t.Unix())
	frac := uint32((nanosPerSec - 1 + (int64(t.Nanosecond()) << 32)) / nanosPerSec)

	b := make([]byte, 8)
	order.PutUint32(b[0:], sec)
	order.PutUint32(b[4:], frac)
	return b
}
Example #5
0
func writeIFD(w io.Writer, ifdOffset int, d []IfdEntry, enc binary.ByteOrder) error {
	var buf [ifdLen]byte
	// Make space for "pointer area" containing IFD entry data
	// longer than 4 bytes.
	parea := make([]byte, 1024)
	pstart := ifdOffset + ifdLen*len(d) + 6
	var o int // Current offset in parea.

	// The IFD has to be written with the tags in ascending order.
	sort.Sort(ifdSortedByCode(d))

	// Write the number of entries in this IFD.
	if err := binary.Write(w, enc, uint16(len(d))); err != nil {
		return err
	}
	for _, ent := range d {
		enc.PutUint16(buf[0:2], uint16(ent.tag.Code))
		enc.PutUint16(buf[2:4], uint16(ent.dataType))
		count := uint32(ent.count)
		enc.PutUint32(buf[4:8], count)
		datalen := int(count * lengths[ent.dataType])
		if datalen <= 4 {
			for i, b := range ent.rawData {
				buf[8+i] = b
			}
		} else {
			if (o + datalen) > len(parea) {
				newlen := len(parea) + 1024
				for (o + datalen) > newlen {
					newlen += 1024
				}
				newarea := make([]byte, newlen)
				copy(newarea, parea)
				parea = newarea
			}
			for i, b := range ent.rawData {
				parea[o+i] = b
			}
			enc.PutUint32(buf[8:12], uint32(pstart+o))
			o += datalen
		}
		if _, err := w.Write(buf[:]); err != nil {
			return err
		}
	}
	// The IFD ends with the offset of the next IFD in the file,
	// or zero if it is the last one (page 14).
	if err := binary.Write(w, enc, uint32(0)); err != nil {
		return err
	}
	_, err := w.Write(parea[:o])
	return err
}
Example #6
0
func packUint(order binary.ByteOrder, v interface{}) (data []byte) {
	switch val := v.(type) {
	case uint64:
		data = make([]byte, 8)
		order.PutUint64(data, val)
	case uint32:
		data = make([]byte, 4)
		order.PutUint32(data, val)
	case uint16:
		data = make([]byte, 2)
		order.PutUint16(data, val)
	case uint8:
		data = []byte{byte(val)}
	default:
		panic("unsupported type")
	}
	return data
}
Example #7
0
func newSimpleProtocol(n int, byteOrder binary.ByteOrder) *simpleProtocol {
	protocol := &simpleProtocol{
		n:  n,
		bo: byteOrder,
	}

	switch n {
	case 1:
		protocol.encodeHead = func(buffer []byte) {
			buffer[0] = byte(len(buffer) - n)
		}
		protocol.decodeHead = func(buffer []byte) int {
			return int(buffer[0])
		}
	case 2:
		protocol.encodeHead = func(buffer []byte) {
			byteOrder.PutUint16(buffer, uint16(len(buffer)-n))
		}
		protocol.decodeHead = func(buffer []byte) int {
			return int(byteOrder.Uint16(buffer))
		}
	case 4:
		protocol.encodeHead = func(buffer []byte) {
			byteOrder.PutUint32(buffer, uint32(len(buffer)-n))
		}
		protocol.decodeHead = func(buffer []byte) int {
			return int(byteOrder.Uint32(buffer))
		}
	case 8:
		protocol.encodeHead = func(buffer []byte) {
			byteOrder.PutUint64(buffer, uint64(len(buffer)-n))
		}
		protocol.decodeHead = func(buffer []byte) int {
			return int(byteOrder.Uint64(buffer))
		}
	default:
		panic("unsupported packet head size")
	}

	return protocol
}
Example #8
0
func WriteFloat32(buf []byte, sample float32, format byte, endianness binary.ByteOrder) {
	encoding := format & EncodingMask

	if encoding == EncodingFloatingPoint {
		endianness.PutUint32(buf, math.Float32bits(sample))
	} else {
		mult := math.Pow(2, float64(len(buf)*8-1))
		var offset float64 = -0.5 // Round instead of floor
		if encoding == EncodingUnsignedInt {
			offset = mult - 1.5
		}

		bits := uint32(float64(sample)*mult + offset)

		tmp := make([]byte, 4)
		endianness.PutUint32(tmp, bits)

		if endianness == binary.BigEndian {
			copy(buf, tmp[4-len(buf):4])
		} else {
			copy(buf, tmp)
		}
	}
}
Example #9
0
func (s Custom) Pack(buf []byte, order binary.ByteOrder) ([]byte, error) {
	order.PutUint32(buf[0:4], uint32(*s.A))
	return buf[4:], nil
}
Example #10
0
func (b *Buffer) WriteUint32(u uint32, order binary.ByteOrder) error {
	i := b.Grows(4)
	order.PutUint32(b.Buf[i:], u)
	return nil
}
Example #11
0
// WriteUInt32 writes a uint32 to w.
func WriteUInt32(w io.Writer, byteOrder binary.ByteOrder, value uint32) error {
	var buf [4]byte
	byteOrder.PutUint32(buf[:], value)
	_, err := w.Write(buf[:])
	return err
}