Beispiel #1
0
func collectInt(r Reader, delim byte) (buf []byte, err os.Error) {
	for {
		var c byte
		c, err = r.ReadByte()
		if err != nil {
			return
		}
		if c == delim {
			return
		}
		if !(c == '-' || (c >= '0' && c <= '9')) {
			err = os.NewError("expected digit")
			return
		}
		buf = bytes.AddByte(buf, c)
	}
	return
}
Beispiel #2
0
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()
}
Beispiel #3
0
func writeBigInt(value *big.Int, buffer *bytes.Buffer, cutoff *big.Int) (positive bool) {
	if value.IsZero() {
		buffer.Write(zeroBase)
		return !value.RawNeg()
	}
	if !value.RawNeg() {
		positive = true
		if value.Cmp(cutoff) == -1 {
			encoding := []byte{'\x80', '\x01', '\x01'}
			mod := big.NewInt(0)
			div, mod := value.DivMod(value, bigint255, mod)
			encoding[2] = byte(mod.Int64()) + 1
			if div.Sign() == 1 {
				div, mod = div.DivMod(div, bigint255, mod)
				encoding[1] = byte(mod.Int64()) + 1
				if div.Sign() == 1 {
					encoding[0] = byte(div.Int64()) + 128
				}
			}
			buffer.Write(encoding)
		} else {
			value = value.Sub(value, cutoff)
			buffer.WriteByte('\xff')
			left := big.NewInt(0)
			lead, left := value.DivMod(value, bigint255, left)
			var n int64 = 1
			exp := big.NewInt(0)
			div := big.NewInt(0)
			for (div.Div(lead, exp.Exp(big.NewInt(253), big.NewInt(n), nil))).Sign() == 1 {
				n += 1
			}
			buffer.WriteByte(byte(n) + 1)
			buffer.WriteByte('\xff')
			leadChars := make([]byte, 0)
			mod := big.NewInt(0)
			for {
				if lead.IsZero() {
					break
				}
				lead, mod = lead.DivMod(lead, bigint253, mod)
				leadChars = bytes.AddByte(leadChars, byte(mod.Int64())+2)
			}
			lenLead := len(leadChars)
			if lenLead > 0 {
				for i := lenLead - 1; i >= 0; i-- {
					buffer.WriteByte(leadChars[i])
				}
			}
			if left.Sign() == 1 {
				buffer.WriteByte('\x01')
				buffer.WriteByte(byte(left.Int64()))
			}
		}
	} else {
		value = value.Neg(value)
		if value.Cmp(cutoff) == -1 {
			encoding := []byte{'\x7f', '\xfe', '\xfe'}
			mod := big.NewInt(0)
			div, mod := value.DivMod(value, bigint255, mod)
			encoding[2] = 254 - byte(mod.Int64())
			if div.Sign() == 1 {
				div, mod = div.DivMod(div, bigint255, mod)
				encoding[1] = 254 - byte(mod.Int64())
				if div.Sign() == 1 {
					encoding[0] = 127 - byte(div.Int64())
				}
			}
			buffer.Write(encoding)
		} else {
			value = value.Sub(value, cutoff)
			buffer.WriteByte('\x00')
			left := big.NewInt(0)
			lead, left := value.DivMod(value, bigint254, left)
			var n int64 = 1
			exp := big.NewInt(0)
			div := big.NewInt(0)
			for (div.Div(lead, exp.Exp(big.NewInt(253), big.NewInt(n), nil))).Sign() == 1 {
				n += 1
			}
			buffer.WriteByte(254 - byte(n))
			buffer.WriteByte('\x00')
			leadChars := make([]byte, 0)
			mod := big.NewInt(0)
			for {
				if lead.IsZero() {
					break
				}
				lead, mod = lead.DivMod(lead, bigint253, mod)
				leadChars = bytes.AddByte(leadChars, byte(253-mod.Int64()))
			}
			lenLead := len(leadChars)
			if lenLead > 0 {
				for i := lenLead - 1; i >= 0; i-- {
					buffer.WriteByte(leadChars[i])
				}
			}
			if lenLead > 1 {
				buffer.WriteByte('\x00')
			}
			buffer.WriteByte('\xfe')
			if left.Sign() == 1 {
				buffer.WriteByte('\x01')
				buffer.WriteByte(254 - byte(left.Int64()))
			} else {
				buffer.WriteByte('\xfe')
			}
		}
	}
	return
}
Beispiel #4
0
func WriteInt(value int64, buffer *bytes.Buffer) {
	if value == 0 {
		buffer.Write(zero)
		return
	}
	if value > 0 {
		if value < magicNumber {
			encoding := []byte{'\x01', '\x80', '\x01', '\x01'}
			div, mod := value/255, value%255
			encoding[3] = byte(mod) + 1
			if div > 0 {
				div, mod = div/255, div%255
				encoding[2] = byte(mod) + 1
				if div > 0 {
					encoding[1] = byte(div) + 128
				}
			}
			buffer.Write(encoding)
		} else {
			value -= magicNumber
			buffer.WriteByte('\x01')
			buffer.WriteByte('\xff')
			lead, left := value/255, value%255
			var n int64 = 1
			for (lead / pow(253, n)) > 0 {
				n += 1
			}
			buffer.WriteByte(byte(n) + 1)
			buffer.WriteByte('\xff')
			leadChars := make([]byte, 0)
			for {
				var mod int64
				if lead == 0 {
					break
				}
				lead, mod = lead/253, lead%253
				leadChars = bytes.AddByte(leadChars, byte(mod)+2)
			}
			lenLead := len(leadChars)
			if lenLead > 0 {
				for i := lenLead - 1; i >= 0; i-- {
					buffer.WriteByte(leadChars[i])
				}
			}
			if left > 0 {
				buffer.WriteByte('\x01')
				buffer.WriteByte(byte(left))
			}
		}
	} else {
		value = -value
		if value < magicNumber {
			encoding := []byte{'\x01', '\x7f', '\xfe', '\xfe'}
			div, mod := value/255, value%255
			encoding[3] = 254 - byte(mod)
			if div > 0 {
				div, mod = div/255, div%255
				encoding[2] = 254 - byte(mod)
				if div > 0 {
					encoding[1] = 127 - byte(div)
				}
			}
			buffer.Write(encoding)
		} else {
			value -= magicNumber
			buffer.WriteByte('\x01')
			buffer.WriteByte('\x00')
			lead, left := value/254, value%254
			var n int64 = 1
			for (lead / pow(253, n)) > 0 {
				n += 1
			}
			buffer.WriteByte(254 - byte(n))
			buffer.WriteByte('\x00')
			leadChars := make([]byte, 0)
			for {
				var mod int64
				if lead == 0 {
					break
				}
				lead, mod = lead/253, lead%253
				leadChars = bytes.AddByte(leadChars, 253-byte(mod))
			}
			lenLead := len(leadChars)
			if lenLead > 0 {
				for i := lenLead - 1; i >= 0; i-- {
					buffer.WriteByte(leadChars[i])
				}
			}
			if lenLead > 1 {
				buffer.WriteByte('\x00')
			}
			buffer.WriteByte('\xfe')
			if left > 0 {
				buffer.WriteByte(254 - byte(left))
			} else {
				buffer.WriteByte('\xfe')
			}
		}
	}
}
Beispiel #5
0
func NextToken(input *bufio.Reader) (encoded []byte, betype int, err os.Error) {
	/*
		dict returns just d
		list returns just l
		string returns encoded string
		int returns encoded int

	*/

	c, err := input.ReadByte()
	if err == os.EOF {
		return nil, 0, err
	}
	if err != nil {
		//print(err.String());
		return nil, 0, err
	}

	switch {
	case c == 'i': // it's an integer i..e
		//i:= 0;
		number := make([]byte, 1, 10)
		c, err = input.ReadByte()
		if err != nil {
			return nil, 0, err
		}
		for c != 'e' {
			number = bytes.AddByte(number, c)
			if c, err = input.ReadByte(); err != nil {
				return
			}
		}
		return number, Beint, nil

	case c == 'l': //it's a list l...e
		alist := make([]byte, 1, 1)
		alist = bytes.AddByte(alist, c)
		return alist, Belist, nil

	case c == 'e': // the end of a dict or list
		end := make([]byte, 1, 1)
		end = bytes.AddByte(end, c)
		return end, Beend, nil

	case c >= '0' && c <= '9': // it's a string and c is the size
		str := make([]byte, 1, 10)
		strSize := make([]byte, 1, 10)
		strSize = bytes.AddByte(strSize, c)
		str = bytes.AddByte(str, c)
		var c1 uint8
		c1, err = input.ReadByte()
		if err != nil {
			return nil, 0, err
		}
		for c1 >= '0' && c1 <= '9' {
			strSize = bytes.AddByte(strSize, c1)
			str = bytes.AddByte(str, c1)

			c1, err = input.ReadByte()
			if err != nil {
				return nil, 0, err
			}
		}
		strLen, err := strconv.Atoi(string(strSize))
		if err != nil {
			return nil, 0, os.NewError("strlength doesn't convert to int")
		}
		for i := 0; i < strLen; i++ {
			c2, err := input.ReadByte()
			if err != nil {
				return nil, 0, err
			}
			//fmt.Printf("the c is: %s\n",string(c1));
			str = bytes.AddByte(str, c2)
		}
		return str, Bestr, nil

	case c == 'd': //it's a dict
		dict := make([]byte, 1, 1)
		dict = bytes.AddByte(dict, c)
		return dict, Bedict, nil
	}
	//shouldn't get here
	return nil, 0, os.NewError("error, shouldn't be here")
}