Пример #1
0
// PutUint16 serializes the provided uint16 using the given byte order into a
// buffer from the free list and writes the resulting two bytes to the given
// writer.
func (l binaryFreeList) PutUint16(w io.Writer, byteOrder binary.ByteOrder, val uint16) error {
	buf := l.Borrow()[:2]
	byteOrder.PutUint16(buf, val)
	_, err := w.Write(buf)
	l.Return(buf)
	return err
}
Пример #2
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
}
Пример #3
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
}
Пример #4
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
}
Пример #5
0
func (init *initConnT) send (out io.Writer) {
	var data [12]byte;
	data[0] = init.endianness;
	var end binary.ByteOrder;
	if data[0] == xLittleEndian {
		end = binary.LittleEndian;
	}
	else {
		end = binary.BigEndian;
	}
	end.PutUint16(data[2:4], init.protocolMajorVersion);
	end.PutUint16(data[4:6], init.protocolMinorVersion);
	end.PutUint16(data[6:8], init.authProtoNameSize);
	end.PutUint16(data[8:10], init.authProtoDataSize);
	out.Write(&data);
    out.Write(strings.Bytes(init.authProtoName));
    padding := 4 - (init.authProtoNameSize % 4);
    namePadding := make([]byte, padding);
    out.Write(namePadding);
    padding = 4 - (init.authProtoDataSize % 4);
    dataPadding := make([]byte, padding);
    out.Write(dataPadding);
}
Пример #6
0
func (b *Buffer) WriteUint16(u uint16, order binary.ByteOrder) error {
	i := b.Grows(2)
	order.PutUint16(b.Buf[i:], u)
	return nil
}