func (be *BlockExtractor) SaveJSON(block interfaces.DatabaseBatchable) error { data, err := block.(interfaces.Printable).JSONByte() if err != nil { return err } var out bytes.Buffer json.Indent(&out, data, "", "\t") data = out.Next(out.Len()) strChainID := fmt.Sprintf("%x", block.GetChainID().Bytes()) dir := be.DataStorePath + strChainID if FileNotExists(dir) { err := os.MkdirAll(dir, 0777) if err == nil { fmt.Println("Created directory " + dir) } else { return err } } err = ioutil.WriteFile(fmt.Sprintf(dir+"/storeJSON.%09d.block", block.GetDatabaseHeight()), data, 0777) if err != nil { return err } return nil }
func Tokenize(buf *bytes.Buffer) []interface{} { if buf.Len() == 0 { return nil } r := []interface{}{} acc := "" for buf.Len() > 0 { c := rune(buf.Next(1)[0]) if unicode.IsLetter(c) || unicode.IsNumber(c) || c == '"' { acc += string(c) } else if c == '(' { var nested []interface{} = Tokenize(buf) r = append(r, nested) } else if c == ')' { break } else { if acc != "" { r = append(r, acc) } acc = "" } } if acc != "" { r = append(r, acc) } return r }
func (be *BlockExtractor) SaveEntryJSON(entry interfaces.DatabaseBatchable, blockHeight uint32) error { data, err := entry.(interfaces.Printable).JSONByte() if err != nil { return err } var out bytes.Buffer json.Indent(&out, data, "", "\t") data = out.Next(out.Len()) strChainID := fmt.Sprintf("%x", entry.GetChainID().Bytes()) dir := be.DataStorePath + strChainID + "/entries" if FileNotExists(dir) { err := os.MkdirAll(dir, 0777) if err == nil { fmt.Println("Created directory " + dir) } else { return err } } err = ioutil.WriteFile(fmt.Sprintf(dir+"/storeJSON.%09d.%v.entry", blockHeight, entry.DatabasePrimaryIndex().String()), data, 0777) if err != nil { return err } return nil }
func parseSegmentRunTable(r *bytes.Buffer) error { b, err := ReadBox(r) if err != nil { return err } if b.Type != "asrt" { return fmt.Errorf("%s: not a Segment Run Table box", b.Type) } r = bytes.NewBuffer(b.Data) r.Next(4) qualities, err := readStringList(r) _ = qualities if err != nil { return parseError{"quality entries", err} } var runCount uint32 err = binary.Read(r, binary.BigEndian, &runCount) if err != nil { return parseError{"run count", err} } run := make([][2]uint32, runCount) err = binary.Read(r, binary.BigEndian, &run) if err != nil { return parseError{"segment runs", err} } log.Println(run) return nil }
func Save(receipt *Receipt) error { data, err := receipt.JSONByte() if err != nil { return err } var out bytes.Buffer json.Indent(&out, data, "", "\t") data = out.Next(out.Len()) entryID := receipt.Entry.Key dir := DataStorePath // + entryID if FileNotExists(dir) { err := os.MkdirAll(dir, 0777) if err == nil { fmt.Println("Created directory " + dir) } else { return err } } fmt.Printf("Saving %v\n", fmt.Sprintf(dir+"/storeJSON.%v.block", entryID)) err = ioutil.WriteFile(fmt.Sprintf(dir+"/storeJSON.%v.block", entryID), data, 0777) if err != nil { return err } return nil }
// fills recv_buffer with a chunk of data starting with a 32-bit // integer representing the size of the remainder. Reads the size out // of recv_buffer before returning, so all the data you see in // recv_buffer is the payload, not including the size. func readSizePrefixedChunk(conn net.Conn, recv_buffer *bytes.Buffer) error { recv_slice := make([]byte, 500) n, err := conn.Read(recv_slice) if err != nil { return err } fmt.Printf("read %d bytes, err is %v\n", n, err) recv_buffer.Write(recv_slice[:n]) for recv_buffer.Len() < 4 { time.Sleep(time.Millisecond) n, err := conn.Read(recv_slice) if err != nil { return err } recv_buffer.Write(recv_slice[:n]) fmt.Printf("read2 %d bytes, err is %v\n", n, err) } size_slice := recv_buffer.Next(4) response_size := int(size_slice[0]) | int(size_slice[1])<<8 | int(size_slice[2])<<16 | int(size_slice[3])<<24 fmt.Printf("response size is %d\n", response_size) for recv_buffer.Len() < response_size { time.Sleep(time.Millisecond) n, err := conn.Read(recv_slice) if err != nil { return err } recv_buffer.Write(recv_slice[:n]) fmt.Printf("read3 %d bytes, err is %v\n", n, err) } return nil }
func decode_rikeying_chunk(srcAddr *string, r *bytes.Buffer, handler chunk_handler) { //fmt.Println("[RIKeying Chunk]") var responderSessionId uint32 binary.Read(r, binary.BigEndian, &responderSessionId) skrcLength := decode_vlu(r) sessionKeyResponderComponent := r.Bytes()[:skrcLength] r.Next(int(skrcLength)) //signature := r.Bytes() //fmt.Printf("responderSessionId:%d\nsignature:%v\n", // responderSessionId, signature) //fmt.Println("sessionKeyResponderComponent:") //fmt.Println(sessionKeyResponderComponent) //dump_options(sessionKeyResponderComponent) // //dh_pub_num := sessionKeyResponderComponent[len(sessionKeyResponderComponent)-128:] //fmt.Printf("resdoner-dh-number:%v\n", dh_pub_num) if handler != nil { handler.recv_rikeying(srcAddr, responderSessionId, sessionKeyResponderComponent) } }
// Parse metadata/markdown file func (j *JSONMetadataParser) Init(b *bytes.Buffer) bool { m := make(map[string]interface{}) err := json.Unmarshal(b.Bytes(), &m) if err != nil { var offset int if jerr, ok := err.(*json.SyntaxError); !ok { return false } else { offset = int(jerr.Offset) } m = make(map[string]interface{}) err = json.Unmarshal(b.Next(offset-1), &m) if err != nil { return false } } j.metadata = NewMetadata(m) j.markdown = bytes.NewBuffer(b.Bytes()) return true }
// Next returns the next n bytes from buf. func Next(buf *bytes.Buffer, n int) []byte { b := buf.Next(n) if len(b) != n { panic(NewBsonError("unexpected EOF")) } return b[:n:n] }
func readUint16(buf *bytes.Buffer) (uint16, error) { if buf.Len() < 2 { return 0, glog.NewError("Insufficient buffer size. Expecting %d, got %d.", 2, buf.Len()) } return binary.BigEndian.Uint16(buf.Next(2)), nil }
func ListenTo(url string) (net.Conn, chan []byte) { con, err := net.Dial("tcp", url) deny(err) ch := make(chan []byte) go func() { var replyBuffer bytes.Buffer readBuffer := make([]byte, 1024) for { con.SetDeadline(time.Now().Add(time.Minute)) bytesRead, err := con.Read(readBuffer) if err != nil { close(ch) log.Printf("ListenTo connection error: %s", err) return } replyBuffer.Write(readBuffer[:bytesRead]) lines := bytes.SplitAfter(replyBuffer.Bytes(), []byte("\n")) for _, line := range lines[:len(lines)-1] { n := len(line) if n > 1 { lineCopy := make([]byte, n) copy(lineCopy, line) ch <- lineCopy } replyBuffer.Next(n) } } }() return con, ch }
func (t *http2Server) writeHeaders(s *Stream, b *bytes.Buffer, endStream bool) error { first := true endHeaders := false var err error // Sends the headers in a single batch. for !endHeaders { size := t.hBuf.Len() if size > http2MaxFrameLen { size = http2MaxFrameLen } else { endHeaders = true } if first { p := http2.HeadersFrameParam{ StreamID: s.id, BlockFragment: b.Next(size), EndStream: endStream, EndHeaders: endHeaders, } err = t.framer.writeHeaders(endHeaders, p) first = false } else { err = t.framer.writeContinuation(endHeaders, s.id, endHeaders, b.Next(size)) } if err != nil { t.Close() return connectionErrorf(true, err, "transport: %v", err) } } return nil }
func (gaugeListener *GaugeListener) processMessages(buffer *bytes.Buffer) { for { messageLength, bytesRead := proto.DecodeVarint(buffer.Bytes()) if messageLength > 0 && messageLength < uint64(buffer.Len()) { message := &gauge_messages.Message{} messageBoundary := int(messageLength) + bytesRead err := proto.Unmarshal(buffer.Bytes()[bytesRead:messageBoundary], message) if err != nil { log.Printf("Failed to read proto message: %s\n", err.Error()) } else { if *message.MessageType == gauge_messages.Message_KillProcessRequest { gaugeListener.connection.Close() os.Exit(0) } if *message.MessageType == gauge_messages.Message_SuiteExecutionResult { result := message.GetSuiteExecutionResult() gaugeListener.onResultHandler(result) } buffer.Next(messageBoundary) if buffer.Len() == 0 { return } } } else { return } } }
// parsePacket publishes event according to data parsed func (n *Driver) parsePacket(buf *bytes.Buffer) { for buf.Len() > 0 { b, _ := buf.ReadByte() switch b { case CodeEx: n.Publish(n.Event("extended"), nil) case CodeSignalQuality: ret, _ := buf.ReadByte() n.Publish(n.Event("signal"), ret) case CodeAttention: ret, _ := buf.ReadByte() n.Publish(n.Event("attention"), ret) case CodeMeditation: ret, _ := buf.ReadByte() n.Publish(n.Event("meditation"), ret) case CodeBlink: ret, _ := buf.ReadByte() n.Publish(n.Event("blink"), ret) case CodeWave: buf.Next(1) var ret = make([]byte, 2) buf.Read(ret) n.Publish(n.Event("wave"), int16(ret[0])<<8|int16(ret[1])) case CodeAsicEEG: ret := make([]byte, 25) i, _ := buf.Read(ret) if i == 25 { n.Publish(n.Event("eeg"), n.parseEEG(ret)) } } } }
func EncodePayloadAsBinary(pkts []*Packet, callback EncodeCallback) { if len(pkts) == 0 { callback([]byte{}) } buf := new(bytes.Buffer) estLen := 0 for _, pkt := range pkts { // Estimated length of buffer // 1(binary indicator) + 4(length bytes) + 1(255) + len(pkt.Data) estLen += 6 + len(pkt.Data) } buf.Grow(estLen) for _, pkt := range pkts { EncodePacket(pkt, true, func(data []byte) { buf.WriteByte(1) length := len(data) bitsBuf := make([]byte, 10) bits := 0 for length > 0 { bitsBuf[bits] = byte(length % 10) bits++ length = length / 10 } for i := bits - 1; i >= 0; i-- { buf.WriteByte(bitsBuf[i]) } buf.WriteByte(255) buf.Write(data) }) } callback(buf.Next(buf.Len())) }
func nextBinaryString(buf *bytes.Buffer) (int, string) { intBytes := buf.Next(4) intVal := int(intBytes[3]) | int(intBytes[2])<<8 | int(intBytes[1])<<16 | int(intBytes[0])<<24 strBytes := buf.Next(intVal) return intVal, string(strBytes) }
func aboutCommonInterfaces() { { in := new(bytes.Buffer) in.WriteString("hello world") out := new(bytes.Buffer) out.Write(in.Bytes()) /* Your code goes here. Hint, use these resources: $ godoc -http=:8080 $ open http://localhost:8080/pkg/io/ $ open http://localhost:8080/pkg/bytes/ */ assert(out.String() == "hello world") // get data from the io.Reader to the io.Writer } { in := new(bytes.Buffer) in.WriteString("hello world") out := new(bytes.Buffer) out.Write(in.Next(5)) assert(out.String() == "hello") // duplicate only a portion of the io.Reader } }
func readUint16(buf *bytes.Buffer) (uint16, error) { read := buf.Next(2) if len(read) != 2 { return 0, ErrInvalid } return binary.BigEndian.Uint16(read), nil }
func EncodePayload(pkts []*Packet, supportsBinary bool, callback EncodeCallback) { if supportsBinary { EncodePayloadAsBinary(pkts, callback) return } if len(pkts) == 0 { callback([]byte("0:")) return } buf := new(bytes.Buffer) estLen := 0 for _, pkt := range pkts { // sample encoded: 102:bxmessage // message is in base 64 estLen += 6 + len(pkt.Data)*2 } buf.Grow(estLen) for _, pkt := range pkts { EncodePacket(pkt, supportsBinary, func(data []byte) { buf.Write([]byte(strconv.FormatInt(int64(len(data)), 10))) buf.WriteByte(':') buf.Write(data) }) } callback(buf.Next(buf.Len())) }
/* FUNCTION: sendEncryptedData(port uint16, data, ip string, mode int) RETURNS: Nothing ARGUMENTS: uint16 port - the port to send the data to string ip - the ip to send the data to string data - the data to send int mode - the mode of the send, either CMD or FTRANSFER (command/file transfer) ABOUT: Sends encrypted data over UDP covertly to the specified ip and port. Sends a completetion packet after the end of any transfer. */ func sendEncryptedData(port uint16, data, ip string, mode int) { var tmpBuffer bytes.Buffer var buffer []byte tmp := encrypt_data([]byte(data)) tmpBuffer.Write(tmp) if tmpBuffer.Len()%2 != 0 { tmpBuffer.WriteByte(0) } size := tmpBuffer.Len() for p := 0; p <= size; p = p + 2 { if p == size && mode == CMD { temp := []byte{0, 0} buffer = craftPacket(temp, ip, SND_CMPLETE, []byte{}) } else if p == size && mode == FTRANSFER { temp := []byte{0, 0} buffer = craftPacket(temp, ip, FSND_CMPLETE, []byte{}) } else { temp := tmpBuffer.Next(2) buffer = craftPacket(temp, ip, port, []byte{}) } if buffer == nil { // if original query was invalid fmt.Print("Buffer error, returned nil.\n") continue } err := handle.WritePacketData(buffer) checkError(err) } }
func (ev *EncryptEventV2) Decode(buffer *bytes.Buffer) (err error) { ev.EncryptType, err = DecodeUInt32Value(buffer) if err != nil { return err } length, err := DecodeUInt32Value(buffer) if err != nil { return } switch ev.EncryptType { case ENCRYPTER_NONE: err, ev.Ev = DecodeEvent(buffer) return err case ENCRYPTER_SE1: newbuf := util.SimpleDecrypt(bytes.NewBuffer(buffer.Next(int(length)))) err, ev.Ev = DecodeEvent(newbuf) newbuf.Reset() case ENCRYPTER_RC4: src := buffer.Next(int(length)) dst := make([]byte, int(length)) cipher, _ := rc4.NewCipher([]byte(rc4Key)) cipher.XORKeyStream(dst, src) err, ev.Ev = DecodeEvent(bytes.NewBuffer(dst)) default: return errors.New("Not supported encrypt type:" + strconv.Itoa(int(ev.EncryptType))) } return err }
func (header *idHeader) read(buffer *bytes.Buffer) { b, _ := buffer.ReadByte() if b != 1 { panic(fmt.Sprintf("Header type == %d, expected type == 1.", b)) } if string(buffer.Next(6)) != "vorbis" { panic("vorbis string not found in id header") } binary.Read(buffer, binary.LittleEndian, &header.idHeaderFixed) var block_sizes uint8 binary.Read(buffer, binary.LittleEndian, &block_sizes) header.Blocksize_0 = int(1 << (block_sizes & 0x0f)) header.Blocksize_1 = int(1 << ((block_sizes & 0xf0) >> 4)) var framing uint8 binary.Read(buffer, binary.LittleEndian, &framing) if framing != 1 { panic("Id header not properly framed") } if header.Version != 0 { panic(fmt.Sprintf("Unexpected version number in id header: %d", header.Version)) } if header.Channels == 0 { panic("Channels set to zero in id header") } if header.Sample_rate == 0 { panic("Sample rate set to zero in id header") } if header.Blocksize_0 != 64 && header.Blocksize_0 != 128 && header.Blocksize_0 != 256 && header.Blocksize_0 != 512 && header.Blocksize_0 != 1024 && header.Blocksize_0 != 2048 && header.Blocksize_0 != 4096 && header.Blocksize_0 != 8192 { panic(fmt.Sprintf("Invalid block 0 size: %d", header.Blocksize_0)) } if header.Blocksize_1 != 64 && header.Blocksize_1 != 128 && header.Blocksize_1 != 256 && header.Blocksize_1 != 512 && header.Blocksize_1 != 1024 && header.Blocksize_1 != 2048 && header.Blocksize_1 != 4096 && header.Blocksize_1 != 8192 { panic(fmt.Sprintf("Invalid block 1 size: %d", header.Blocksize_1)) } if header.Blocksize_0 > header.Blocksize_1 { panic(fmt.Sprintf("Block 0 size > block 1 size: %d > %d", header.Blocksize_0, header.Blocksize_1)) } if buffer.Len() > 0 { // TODO: Shouldn't be anything leftover, log a warning? } }
func decode_string(buff *bytes.Buffer) string { msb := getByte(buff) lsb := getByte(buff) pLen := (uint16(msb) << 8) | uint16(lsb) return string(buff.Next(int(pLen))) }
func EncodeBase64Packet(pkt *Packet, callback EncodeCallback) { buf := new(bytes.Buffer) buf.Grow(2 + len(pkt.Data)*2) buf.WriteByte('b') buf.Write([]byte(strconv.FormatInt(int64(Packets[pkt.Type]), 10))) buf.Write([]byte(base64.StdEncoding.EncodeToString(pkt.Data))) callback(buf.Next(buf.Len())) }
func GetRawCode(buffer *bytes.Buffer) (rawCode string, err error) { rawCode = string(buffer.Next(4)) if !temp.IsRawCode(rawCode) { return "", errorsutil.Errorf("Illegal raw code: [% X]", rawCode) } return rawCode, nil }
func ExportDatabaseJSON(db interfaces.IDatabase, convertNames bool) error { fmt.Printf("Exporting the database\n") if db == nil { return nil } buckets, err := db.ListAllBuckets() if err != nil { return err } answer := map[string]interface{}{} for _, bucket := range buckets { m := map[string]interface{}{} data, keys, err := db.GetAll(bucket, new(primitives.ByteSlice)) if err != nil { return err } for i, key := range keys { m[fmt.Sprintf("%x", key)] = data[i] } if convertNames == true { answer[KeyToName(bucket)] = m } else { answer[fmt.Sprintf("%x", bucket)] = m } } data, err := primitives.EncodeJSON(answer) if err != nil { return err } var out bytes.Buffer json.Indent(&out, data, "", "\t") data = out.Next(out.Len()) /* dir := be.DataStorePath if dir != "" { if FileNotExists(dir) { err := os.MkdirAll(dir, 0777) if err == nil { fmt.Println("Created directory " + dir) } else { return err } } } if dir != "" { dir = dir + "/db.txt" } else { dir = "db.txt" }*/ dir := "db.txt" err = ioutil.WriteFile(dir, data, 0777) if err != nil { return err } return nil }
// Reads one attribute into a structure. // dest must be a pointer to a struct. func readAttribute(r *bytes.Buffer, dest interface{}) (er error) { var attr syscall.RtAttr er = binary.Read(r, SystemEndianness, &attr) if er != nil { return er } dataLen := int(attr.Len) - syscall.SizeofRtAttr value, type_spec, er := getDestinationAndType(dest, attr.Type) switch true { case er != nil: return er case type_spec == "none": value.Set(reflect.ValueOf(true)) case type_spec == "fixed": // The payload is a binary struct if !value.CanAddr() { return fmt.Errorf("trying to read fixed-width data in a non addressable field!") } er = binary.Read(r, SystemEndianness, value.Addr().Interface()) case type_spec == "bytes": // The payload is a raw sequence of bytes buf := make([]byte, dataLen) _, er = r.Read(buf[:]) value.Set(reflect.ValueOf(buf)) case type_spec == "string": // The payload is a NUL-terminated byte array if value.Type().Kind() != reflect.String { return fmt.Errorf("unable to fill field of type %s with string!", value.Type()) } buf := make([]byte, dataLen) _, er = r.Read(buf[:]) s := string(buf[:len(buf)-1]) value.Set(reflect.ValueOf(s)) case type_spec == "nested": // The payload is a seralized sequence of attributes // <header> (<header1> <attribute1> ... <header n> <attribute n>) if !value.CanAddr() { return fmt.Errorf("trying to read nested attributes to a non addressable field!") } buf := make([]byte, dataLen) _, er = r.Read(buf[:]) er = ReadManyAttributes(bytes.NewBuffer(buf), value.Addr().Interface()) case type_spec == "nestedlist": // The payload is a sequence of nested attributes, each of them carrying // a payload describing a struct with attributes // <header (4 bytes)> <payload> // where payload is // <header1> <nested attributes 1> ... <headern> <nested attributes n> buf := make([]byte, dataLen) _, er = r.Read(buf[:]) er = readNestedAttributeList(bytes.NewBuffer(buf), value) default: return fmt.Errorf("Invalid format tag %s: expecting 'fixed', 'bytes', 'string', or 'nested'", type_spec) } r.Next(netlinkPadding(dataLen)) return er }
func ReadCString(buf *bytes.Buffer) string { index := bytes.IndexByte(buf.Bytes(), 0) if index < 0 { panic(NewBsonError("Unexpected EOF")) } // Read including null termination, but // return the string without the null. return hack.String(buf.Next(index + 1)[:index]) }
func DecodeFloat64(buf *bytes.Buffer, kind byte) float64 { switch kind { case Number: return float64(math.Float64frombits(Pack.Uint64(buf.Next(8)))) case Null: return 0 } panic(NewBsonError("Unexpected data type %v for int", kind)) }
func DecodeInt32(buf *bytes.Buffer, kind byte) int32 { switch kind { case Int: return int32(Pack.Uint32(buf.Next(4))) case Null: return 0 } panic(NewBsonError("Unexpected data type %v for int", kind)) }