func readUntilCrLf(con *net.TCPConn) (line []byte, err os.Error) { buf := make([]byte, 1) var data []byte crSeen := false for { _, err := con.Read(buf) if err != nil { if err == os.EOF { break } else { return nil, err } } if crSeen { if buf[0] == 10 { break } else { crSeen = false data = bytes.Add(data, buf) } } else { if buf[0] == 13 { crSeen = true } else { data = bytes.Add(data, buf) } } } return data, nil }
// Extract regular text from the beginning of the pattern. // That text can be used by doExecute to speed up matching. func (re *Regexp) setPrefix() { var b []byte var utf = make([]byte, utf8.UTFMax) // First instruction is start; skip that. i := re.inst.At(0).(instr).next().index() Loop: for i < re.inst.Len() { inst := re.inst.At(i).(instr) // stop if this is not a char if inst.kind() != _CHAR { break } // stop if this char can be followed by a match for an empty string, // which includes closures, ^, and $. switch re.inst.At(inst.next().index()).(instr).kind() { case _BOT, _EOT, _ALT: break Loop } n := utf8.EncodeRune(inst.(*_Char).char, utf) b = bytes.Add(b, utf[0:n]) i = inst.next().index() } // point prefixStart instruction to first non-CHAR after prefix re.prefixStart = re.inst.At(i).(instr) re.prefixBytes = b re.prefix = string(b) }
// UnmarshalJSON sets *m to a copy of data. func (m *RawMessage) UnmarshalJSON(data []byte) os.Error { if m == nil { return os.NewError("json.RawMessage: UnmarshalJSON on nil pointer") } *m = bytes.Add((*m)[0:0], data) return nil }
// readHandshake reads the next handshake message from // the record layer. // c.in.Mutex < L; c.out.Mutex < L. func (c *Conn) readHandshake() (interface{}, os.Error) { for c.hand.Len() < 4 { if c.err != nil { return nil, c.err } c.readRecord(recordTypeHandshake) } data := c.hand.Bytes() n := int(data[1])<<16 | int(data[2])<<8 | int(data[3]) if n > maxHandshake { c.sendAlert(alertInternalError) return nil, c.err } for c.hand.Len() < 4+n { if c.err != nil { return nil, c.err } c.readRecord(recordTypeHandshake) } data = c.hand.Next(4 + n) var m handshakeMessage switch data[0] { case typeClientHello: m = new(clientHelloMsg) case typeServerHello: m = new(serverHelloMsg) case typeCertificate: m = new(certificateMsg) case typeCertificateRequest: m = new(certificateRequestMsg) case typeCertificateStatus: m = new(certificateStatusMsg) case typeServerHelloDone: m = new(serverHelloDoneMsg) case typeClientKeyExchange: m = new(clientKeyExchangeMsg) case typeCertificateVerify: m = new(certificateVerifyMsg) case typeNextProtocol: m = new(nextProtoMsg) case typeFinished: m = new(finishedMsg) default: c.sendAlert(alertUnexpectedMessage) return nil, alertUnexpectedMessage } // The handshake message unmarshallers // expect to be able to keep references to data, // so pass in a fresh copy that won't be overwritten. data = bytes.Add(nil, data) if !m.unmarshal(data) { c.sendAlert(alertUnexpectedMessage) return nil, alertUnexpectedMessage } return m, nil }
func EncodeSOA(soa SOArecord) []byte { var ( result []byte temp32 []byte ) mname := Encode(soa.Mname) length := len(mname) rname := Encode(soa.Rname) length = length + len(rname) length = length + (5 * 4) // Five 32-bits counter at the end /* "It's probably cleaner to write to a bytes.Buffer than to repeatedly call bytes.Add." Russ Cox, go-nuts ML */ result = bytes.Add(result, mname) result = bytes.Add(result, rname) temp32 = make([]byte, 4) binary.BigEndian.PutUint32(temp32, soa.Serial) result = bytes.Add(result, temp32) binary.BigEndian.PutUint32(temp32, soa.Refresh) result = bytes.Add(result, temp32) binary.BigEndian.PutUint32(temp32, soa.Retry) result = bytes.Add(result, temp32) binary.BigEndian.PutUint32(temp32, soa.Expire) result = bytes.Add(result, temp32) binary.BigEndian.PutUint32(temp32, soa.Minimum) result = bytes.Add(result, temp32) return result[0:length] }
// To use with messages that receive a response from database // 'opQuery', 'opGetMore'. func (self *Connection) sendMessageToReply(m message, reqID int32) os.Error { body := m.Bytes() h := header(msgHeader{int32(len(body) + _HEADER_SIZE), reqID, 0, m.OpCode()}) msg := bytes.Add(h, body) _, err := self.conn.Write(msg) return err }
func Decode(b []byte) []byte { r := make([]byte, len(b)*2)[0:0] for { if b[0] != 128 { break } if b[1] == 3 { break } l := int(b[2]) + (int(b[3]) << 8) + (int(b[4]) << 16) + 6 if b[1] == 1 { r = bytes.Add(r, b[6:l]) } else { r = bytes.Add(r, hex.Encode(b[6:l])) } b = b[l:] } return r }
func (c *Connection) writeMessage(m message) os.Error { body := m.Bytes() hb := header(int32(len(body)+16), m.RequestID(), 0, m.OpCode()) msg := bytes.Add(hb, body) _, err := c.conn.Write(msg) last_req = m.RequestID() return err }
func (self *_Object) Bytes() []byte { buf := bytes.NewBuffer([]byte{}) for k, v := range self.value { buf.WriteByte(byte(v.Kind())) buf.WriteString(k) buf.WriteByte(0) buf.Write(v.Bytes()) } buf.WriteByte(0) l := buf.Len() + 4 w32 := make([]byte, _WORD32) pack.PutUint32(w32, uint32(l)) return bytes.Add(w32, buf.Bytes()) }
func (self *_Array) Bytes() []byte { buf := bytes.NewBuffer([]byte{}) for i := 0; i < self.value.Len(); i++ { v := self.value.At(i).(BSON) buf.WriteByte(byte(v.Kind())) buf.WriteString(strconv.Itoa(i)) buf.WriteByte(0) buf.Write(v.Bytes()) } buf.WriteByte(0) l := buf.Len() + 4 w32 := make([]byte, _WORD32) pack.PutUint32(w32, uint32(l)) return bytes.Add(w32, buf.Bytes()) }
func Repeat(alu []byte, n int) { buf := bytes.Add(alu, alu) off := 0 for n > 0 { m := n if m > Line { m = Line } buf1 := out.NextWrite(m + 1) copy(buf1, buf[off:]) buf1[m] = '\n' if off += m; off >= len(alu) { off -= len(alu) } n -= m } }
func (self *Client) simpleQuery(command int, version int, size int, body []byte) ([]byte, os.Error) { conn, err := self.connect() if err != nil { return nil, err } header := make([]byte, 8+len(body)) binary.BigEndian.PutUint16(header[0:2], uint16(command)) binary.BigEndian.PutUint16(header[2:4], uint16(version)) binary.BigEndian.PutUint32(header[4:8], uint32(size)) request := bytes.Add(header, body) _, err = conn.Write(request) if err != nil { return nil, err } return self.getResponse(conn) }
// ReadAll takes an os.File as it's argument and reads in all of it until // it signals to stop with an EOF in func ReadAll(file * os.File) ([]byte, os.Error) { const BUFSIZE = 1024 result := make([]byte, 0) buf := make([]byte, BUFSIZE) var sub []byte for { // ever count, err2 := file.Read(buf) sub = buf[0:count] // get right slice to append to result // append bytes to result if count > 0 { result = bytes.Add(result, sub) } // if EOF, return the result. if err2 == os.EOF { return result, nil } // on any other error return nil if err2 != nil { return nil , err2 } } // should not get here return nil, os.NewError("Can't happen?!"); }
func TestRecords(t *testing.T) { for i, test := range recordsTests { buf := bytes.NewBuffer(test.raw) content, reqId, kind, err := readRecord(buf) if err != nil { t.Errorf("%d error reading record: %s", i, err.String()) goto write } for buf.Len() > 0 { moreContent, _, _, err := readRecord(buf) if err != nil { t.Errorf("%d error reading additional record: %s", i, err.String()) goto write } content = bytes.Add(content, moreContent) } if kind != test.kind { t.Errorf("%d got kind %d expected %d", i, kind, test.kind) } if reqId != test.reqId { t.Errorf("%d got request ID %d expected %d", i, kind, test.kind) } if !bytes.Equal(content, test.content) { t.Errorf("%d read wrong content", i) } write: buf.Reset() c := &lockReadWriteCloser{ReadWriteCloser: &nilCloser{buf}} err = writeRecord(c, test.kind, test.reqId, test.content) if err != nil { t.Errorf("%d error writing record: %s", i, err.String()) continue } if !bytes.Equal(buf.Bytes(), test.raw) { t.Errorf("%d wrote wrong content", i) } } }
func TestNextValueBig(t *testing.T) { var scan scanner item, rest, err := nextValue(jsonBig, &scan) if err != nil { t.Fatalf("nextValue: ", err) } if len(item) != len(jsonBig) || &item[0] != &jsonBig[0] { t.Errorf("invalid item: %d %d", len(item), len(jsonBig)) } if len(rest) != 0 { t.Errorf("invalid rest: %d", len(rest)) } item, rest, err = nextValue(bytes.Add(jsonBig, []byte("HELLO WORLD")), &scan) if err != nil { t.Fatalf("nextValue extra: ", err) } if len(item) != len(jsonBig) { t.Errorf("invalid item: %d %d", len(item), len(jsonBig)) } if string(rest) != "HELLO WORLD" { t.Errorf("invalid rest: %d", len(rest)) } }
// ReadContinuedLineBytes is like ReadContinuedLine but // returns a []byte instead of a string. func (r *Reader) ReadContinuedLineBytes() ([]byte, os.Error) { // Read the first line. line, err := r.ReadLineBytes() if err != nil { return line, err } if len(line) == 0 { // blank line - no continuation return line, nil } line = trim(line) // Look for a continuation line. c, err := r.R.ReadByte() if err != nil { // Delay err until we read the byte next time. return line, nil } if c != ' ' && c != '\t' { // Not a continuation. r.R.UnreadByte() return line, nil } // Read continuation lines. for { // Consume leading spaces; one already gone. for { c, err = r.R.ReadByte() if err != nil { break } if c != ' ' && c != '\t' { r.R.UnreadByte() break } } var cont []byte cont, err = r.ReadLineBytes() cont = trim(cont) line = bytes.Add(line, space) line = bytes.Add(line, cont) if err != nil { break } // Check for leading space on next line. if c, err = r.R.ReadByte(); err != nil { break } if c != ' ' && c != '\t' { r.R.UnreadByte() break } } // Delay error until next call. if len(line) > 0 { err = nil } return line, err }
func TestEncodeFloat64(t *testing.T) { expected := bytes.Add([]byte{MAGIC, FLOAT}, strings.Bytes(fmt.Sprintf("%.20e", float64(1.2e3)))) testEncode(t, "Encoding float64 failed", 1.2e3, expected) }
func TestEncodeString(t *testing.T) { expected := bytes.Add([]byte{MAGIC, BIN, 0, 0, 0, 3}, strings.Bytes("foo")) testEncode(t, "Encoding string failed", "foo", expected) }
// Unmarshal a single XML element into val. func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error { // Find start element if we need it. if start == nil { for { tok, err := p.Token() if err != nil { return err } if t, ok := tok.(StartElement); ok { start = &t break } } } if pv, ok := val.(*reflect.PtrValue); ok { if pv.Get() == 0 { zv := reflect.MakeZero(pv.Type().(*reflect.PtrType).Elem()) pv.PointTo(zv) val = zv } else { val = pv.Elem() } } var ( data []byte saveData reflect.Value comment []byte saveComment reflect.Value sv *reflect.StructValue styp *reflect.StructType ) switch v := val.(type) { default: return os.ErrorString("unknown type " + v.Type().String()) case *reflect.BoolValue: v.Set(true) case *reflect.SliceValue: typ := v.Type().(*reflect.SliceType) if _, ok := typ.Elem().(*reflect.Uint8Type); ok { // []byte saveData = v break } // Slice of element values. // Grow slice. n := v.Len() if n >= v.Cap() { ncap := 2 * n if ncap < 4 { ncap = 4 } new := reflect.MakeSlice(typ, n, ncap) reflect.ArrayCopy(new, v) v.Set(new) } v.SetLen(n + 1) // Recur to read element into slice. if err := p.unmarshal(v.Elem(n), start); err != nil { v.SetLen(n) return err } return nil case *reflect.StringValue, *reflect.IntValue, *reflect.UintValue, *reflect.UintptrValue, *reflect.Int8Value, *reflect.Int16Value, *reflect.Int32Value, *reflect.Int64Value, *reflect.Uint8Value, *reflect.Uint16Value, *reflect.Uint32Value, *reflect.Uint64Value, *reflect.FloatValue, *reflect.Float32Value, *reflect.Float64Value: saveData = v case *reflect.StructValue: if _, ok := v.Interface().(Name); ok { v.Set(reflect.NewValue(start.Name).(*reflect.StructValue)) break } sv = v typ := sv.Type().(*reflect.StructType) styp = typ // Assign name. if f, ok := typ.FieldByName("XMLName"); ok { // Validate element name. if f.Tag != "" { tag := f.Tag ns := "" i := strings.LastIndex(tag, " ") if i >= 0 { ns, tag = tag[0:i], tag[i+1:] } if tag != start.Name.Local { return UnmarshalError("expected element type <" + tag + "> but have <" + start.Name.Local + ">") } if ns != "" && ns != start.Name.Space { e := "expected element <" + tag + "> in name space " + ns + " but have " if start.Name.Space == "" { e += "no name space" } else { e += start.Name.Space } return UnmarshalError(e) } } // Save v := sv.FieldByIndex(f.Index) if _, ok := v.Interface().(Name); !ok { return UnmarshalError(sv.Type().String() + " field XMLName does not have type xml.Name") } v.(*reflect.StructValue).Set(reflect.NewValue(start.Name).(*reflect.StructValue)) } // Assign attributes. // Also, determine whether we need to save character data or comments. for i, n := 0, typ.NumField(); i < n; i++ { f := typ.Field(i) switch f.Tag { case "attr": strv, ok := sv.FieldByIndex(f.Index).(*reflect.StringValue) if !ok { return UnmarshalError(sv.Type().String() + " field " + f.Name + " has attr tag but is not type string") } // Look for attribute. val := "" k := strings.ToLower(f.Name) for _, a := range start.Attr { if fieldName(a.Name.Local) == k { val = a.Value break } } strv.Set(val) case "comment": if saveComment == nil { saveComment = sv.FieldByIndex(f.Index) } case "chardata": if saveData == nil { saveData = sv.FieldByIndex(f.Index) } } } } // Find end element. // Process sub-elements along the way. Loop: for { tok, err := p.Token() if err != nil { return err } switch t := tok.(type) { case StartElement: // Sub-element. // Look up by tag name. // If that fails, fall back to mop-up field named "Any". if sv != nil { k := fieldName(t.Name.Local) any := -1 for i, n := 0, styp.NumField(); i < n; i++ { f := styp.Field(i) if strings.ToLower(f.Name) == k { if err := p.unmarshal(sv.FieldByIndex(f.Index), &t); err != nil { return err } continue Loop } if any < 0 && f.Name == "Any" { any = i } } if any >= 0 { if err := p.unmarshal(sv.FieldByIndex(styp.Field(any).Index), &t); err != nil { return err } continue Loop } } // Not saving sub-element but still have to skip over it. if err := p.Skip(); err != nil { return err } case EndElement: break Loop case CharData: if saveData != nil { data = bytes.Add(data, t) } case Comment: if saveComment != nil { comment = bytes.Add(comment, t) } } } var err os.Error // Helper functions for integer and unsigned integer conversions var itmp int64 getInt64 := func() bool { itmp, err = strconv.Atoi64(string(data)) // TODO: should check sizes return err == nil } var utmp uint64 getUint64 := func() bool { utmp, err = strconv.Atoui64(string(data)) // TODO: check for overflow? return err == nil } var ftmp float64 getFloat64 := func() bool { ftmp, err = strconv.Atof64(string(data)) // TODO: check for overflow? return err == nil } // Save accumulated data and comments switch t := saveData.(type) { case nil: // Probably a comment, handled below default: return os.ErrorString("cannot happen: unknown type " + t.Type().String()) case *reflect.IntValue: if !getInt64() { return err } t.Set(int(itmp)) case *reflect.Int8Value: if !getInt64() { return err } t.Set(int8(itmp)) case *reflect.Int16Value: if !getInt64() { return err } t.Set(int16(itmp)) case *reflect.Int32Value: if !getInt64() { return err } t.Set(int32(itmp)) case *reflect.Int64Value: if !getInt64() { return err } t.Set(itmp) case *reflect.UintValue: if !getUint64() { return err } t.Set(uint(utmp)) case *reflect.Uint8Value: if !getUint64() { return err } t.Set(uint8(utmp)) case *reflect.Uint16Value: if !getUint64() { return err } t.Set(uint16(utmp)) case *reflect.Uint32Value: if !getUint64() { return err } t.Set(uint32(utmp)) case *reflect.Uint64Value: if !getUint64() { return err } t.Set(utmp) case *reflect.UintptrValue: if !getUint64() { return err } t.Set(uintptr(utmp)) case *reflect.FloatValue: if !getFloat64() { return err } t.Set(float(ftmp)) case *reflect.Float32Value: if !getFloat64() { return err } t.Set(float32(ftmp)) case *reflect.Float64Value: if !getFloat64() { return err } t.Set(ftmp) case *reflect.StringValue: t.Set(string(data)) case *reflect.SliceValue: t.Set(reflect.NewValue(data).(*reflect.SliceValue)) } switch t := saveComment.(type) { case *reflect.StringValue: t.Set(string(comment)) case *reflect.SliceValue: t.Set(reflect.NewValue(comment).(*reflect.SliceValue)) } return nil }
func main() { pg_adr, err := net.ResolveTCPAddr("152.7.16.113:5432") if err != nil { log.Exitf("Error while resolving address: %s", err) } conn, err := net.DialTCP("tcp", nil, pg_adr) if err != nil { log.Exitf("Error while connecting: %s", err) } log.Stdout("I think we connected") strs := make([]string, 4) strs[0] = "user" strs[1] = "alex" strs[2] = "database" strs[3] = "ar_test" str2 := strings.Join(strs, "\x00") + "\x00\x00" mesg2 := strings.Bytes(str2) hmesg := make([]byte, 4+4) binary.BigEndian.PutUint32(hmesg[4:8], uint32(3<<16)) hmesg = bytes.Add(hmesg, mesg2) binary.BigEndian.PutUint32(hmesg[0:4], uint32(len(hmesg))) n, err := conn.Write(hmesg) if err != nil { log.Exitf("Error writing TCP: %s", err) } log.Stdoutf("wrote %d", n) result := make([]byte, 12) // the largest response we can get is 12 bytes if n, err = conn.Read(result); err != nil { log.Exitf("Error reading TCP (Read %d bytes): %s", n, err) } log.Stdoutf("%c", result[0]) switch string(result[0]) { case psql_constants.Authentication: log.Stdout("Got authentication message") default: log.Exit("Did not get authentication message") } mesglen := binary.BigEndian.Uint32(result[1:5]) mesgtype := binary.BigEndian.Uint32(result[5:9]) fmt.Println(mesglen, mesgtype) passhash := md5.New() passhash.Write(strings.Bytes("jar1!" + "alex")) salthash := md5.New() salthash.Write(passhash.Sum()) salthash.Write(result[9:12]) passresponse := make([]byte, 5) passresponse[0] = 'p' binary.BigEndian.PutUint32(passresponse[1:5], uint32(4+salthash.Size()+1)) passresponse = bytes.Add(passresponse, strings.Bytes("md5")) passresponse = bytes.Add(passresponse, salthash.Sum()) passresponse = bytes.AddByte(passresponse, byte(0)) fmt.Println(passresponse) n, err = conn.Write(passresponse) if err != nil { log.Exitf("Error writing TCP: %s", err) } log.Stdoutf("wrote %d", n) result = make([]byte, 18) // the largest response we can get is 12 bytes if n, err = conn.Read(result); err != nil { log.Exitf("Error reading TCP (Read %d bytes): %s", n, err) } fmt.Println(result) conn.Close() }
func (c *Client) run(complete chan bool) { print("client running\n") print("sending handshake\n") c.HandShake() print("waiting for handshake\n") if !c.WaitHandShake() { complete <- false return } c.processHandShake(msg) blank := make([]byte, 9) msg := make([]byte, 49+pstrlen) buffer := bytes.NewBuffer(msg) buffer.WriteByte(pstrlen) buffer.WriteString(c.torrent.Pstr) buffer.Write(blank) buffer.Write(&(c.torrent.info_hash)) buffer.Write(&(c.torrent.peer_id)) _, err = c.conn.Write(buffer.Bytes()) return for { print("waiting for msg\n") _, err := c.conn.Read(c.buffer[0:4]) //read msg length if err != nil { complete <- false return } length := binary.BigEndian.Uint32(c.buffer[0:4]) print("recieved msg length" + strconv.Itoa(int(length)) + "\n") switch { case length == 0: print("keepalive msg") continue case length >= MaxInMsgSize: complete <- true return } _, err = c.conn.Read(c.buffer[4 : 4+length]) if err != nil { complete <- true return } print("msg recieved payload\n") msg := new(message) msg.length = length msg.msgId = c.buffer[4] bytes.Add(msg.payLoad, c.buffer[5:len(c.buffer)]) err = c.processMsg(msg) if err != nil { print(err.String()) complete <- true return } runtime.Gosched() } }
func main() { flag.Parse() // parsovani parametru prikazove radky rand.Seed(time.Nanoseconds()) // inicializace generatoru nahodnych cisel if *generateKeys { SaveKeys(GenerateKeys(*keyLength)) } // rozdeleni souboru na vice mensich podle delky klice if *splitFile != "" { rights := 0600 n, _ := ReadPublicKey() bytes := readFile(*splitFile) nl := len(n.Bytes()) - 1 i := 0 for ; i < len(bytes)/nl; i++ { writeToFile(partfile(i), bytes[i*nl:(i+1)*nl], rights) } if len(bytes[i*nl:]) > 0 { // pokud deleni nevychazi presne writeToFile(partfile(i), bytes[i*nl:], rights) i++ } // informacni soubor writeToFile(*prefix+".info", strings.Bytes(fmt.Sprintf("%d", i)), rights) } // zasifruje/rozsifruje vsechny soubory verejnym klicem if *encrypFiles { n, e := ReadPublicKey() max := msgscount() for i := 0; i < max; i++ { file := readFile(partfile(i)) bs := crypt(e, n, file) writeToFile(partfile(i), bs, 0600) } } // rozsifruje/zasifruje vsechny soubory soukromym klicem if *decrypFiles { p, q, d := ReadPrivateKey() pinv := new(big.Int).Mul(p, invMod(p, q)) // = p*(p^-1 mod q) qinv := new(big.Int).Mul(q, invMod(q, p)) // = q*(q^-1 mod p) n := new(big.Int).Mul(p, q) max := msgscount() for i := 0; i < max; i++ { file := readFile(partfile(i)) // CINSKA VETA O ZBYTCICH bp := new(big.Int).SetBytes(crypt(d, p, file)) // decrypt mod p bq := new(big.Int).SetBytes(crypt(d, q, file)) // decrypt mod q // bs = bp * qinv + bq * binv (mod n) bs := new(big.Int).Mul(bp, qinv) bs.Add(bs, new(big.Int).Mul(bq, pinv)) bs.Mod(bs, n) writeToFile(partfile(i), bs.Bytes(), 0600) } } // spojeni souboru do jednoho if *joinFile != "" { max := msgscount() var bs []byte for i := 0; i < max; i++ { file := readFile(partfile(i)) bs = bytes.Add(bs, file) } writeToFile(*joinFile, bs, 0600) } }