Пример #1
0
func (v *visitor) VisitFile(file string, f *os.FileInfo) {
	var match, err = filepath.Match("c.*.*.dat", filepath.Base(file))
	if match && err == nil {
		var (
			s       = strings.SplitN(filepath.Base(file), ".", 4)
			x, xErr = strconv.Btoi64(s[1], 36)
			z, zErr = strconv.Btoi64(s[2], 36)
		)
		if xErr == nil && zErr == nil && !v.mask.IsMasked(int(x), int(z)) {
			v.chunks[file] = true
		}
	}
}
Пример #2
0
func (qd qDecoder) Read(p []byte) (n int, err error) {
	// This method writes at most one byte into p.
	if len(p) == 0 {
		return 0, nil
	}
	if _, err := qd.r.Read(qd.scratch[:1]); err != nil {
		return 0, err
	}
	switch c := qd.scratch[0]; {
	case c == '=':
		if _, err := io.ReadFull(qd.r, qd.scratch[:2]); err != nil {
			return 0, err
		}
		x, err := strconv.Btoi64(string(qd.scratch[:2]), 16)
		if err != nil {
			return 0, fmt.Errorf("mail: invalid RFC 2047 encoding: %q", qd.scratch[:2])
		}
		p[0] = byte(x)
	case c == '_':
		p[0] = ' '
	default:
		p[0] = c
	}
	return 1, nil
}
Пример #3
0
// articlesIndex is the handler for the site's index page.
// It shows up to the last 10 article summaries (all public info except
// for the Body) on a single page.
func articlesIndex(w http.ResponseWriter, r *http.Request) {
	// Get the user-entered offset, or default to a zero offset.
	offsetS := r.FormValue("Page")
	var offset int
	if len(offsetS) > 0 {
		offset64, err := strconv.Btoi64(offsetS, 10)
		check(err)
		offset = int(offset64)
	} else {
		offset = 0
	}

	// Fetch public data from datastore, up to 10, with offset `offset * 10`
	c := appengine.NewContext(r)
	q := datastore.NewQuery("Article").Order("-Date").Filter("Public =", true).Limit(10).Offset(10 * offset)
	a := make([]Article, 0, 10)
	_, err := q.GetAll(c, &a)
	check(err)

	// Prepares the `indexTemplate.html` template
	t := template.New("index")
	t, err = t.ParseFile("templates/indexTemplate.html")
	check(err)

	// Executes the template, providing the data gathered from the datastore
	err = t.Execute(w, a)
	check(err)
}
Пример #4
0
func (self *String) Status() (i int64) {
	var err os.Error
	if i, err = strconv.Btoi64(string(*self), 0); err != nil {
		panic(err)
	}
	return i
}
Пример #5
0
// scanInt returns the value of the integer represented by the next
// token, checking for overflow.  Any error is stored in s.err.
func (s *ss) scanInt(verb int, bitSize int) int64 {
	if verb == 'c' {
		return s.scanRune(bitSize)
	}
	s.skipSpace(false)
	base, digits := s.getBase(verb)
	haveDigits := false
	if verb == 'U' {
		if !s.consume("U", false) || !s.consume("+", false) {
			s.errorString("bad unicode format ")
		}
	} else {
		s.accept(sign) // If there's a sign, it will be left in the token buffer.
		if verb == 'v' {
			base, digits, haveDigits = s.scanBasePrefix()
		}
	}
	tok := s.scanNumber(digits, haveDigits)
	i, err := strconv.Btoi64(tok, base)
	if err != nil {
		s.error(err)
	}
	n := uint(bitSize)
	x := (i << (64 - n)) >> (64 - n)
	if x != i {
		s.errorString("integer overflow on token " + tok)
	}
	return i
}
Пример #6
0
// Allocate a new variable-evaluation element.
func (t *Template) newVariable(words []string) *variableElement {
	// After the final space-separated argument, formatters may be specified separated
	// by pipe symbols, for example: {a b c|d|e}

	// Until we learn otherwise, formatters contains a single name: "", the default formatter.
	formatters := []string{""}
	lastWord := words[len(words)-1]
	bar := strings.IndexRune(lastWord, '|')
	if bar >= 0 {
		words[len(words)-1] = lastWord[0:bar]
		formatters = strings.Split(lastWord[bar+1:], "|", -1)
	}

	args := make([]interface{}, len(words))

	// Build argument list, processing any literals
	for i, word := range words {
		var lerr os.Error
		switch word[0] {
		case '"', '`', '\'':
			v, err := strconv.Unquote(word)
			if err == nil && word[0] == '\'' {
				args[i] = []int(v)[0]
			} else {
				args[i], lerr = v, err
			}

		case '.', '+', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
			v, err := strconv.Btoi64(word, 0)
			if err == nil {
				args[i] = v
			} else {
				v, err := strconv.Atof64(word)
				args[i], lerr = v, err
			}

		default:
			args[i] = fieldName(word)
		}
		if lerr != nil {
			t.parseError("invalid literal: %q: %s", word, lerr)
		}
	}

	// We could remember the function address here and avoid the lookup later,
	// but it's more dynamic to let the user change the map contents underfoot.
	// We do require the name to be present, though.

	// Is it in user-supplied map?
	for _, f := range formatters {
		if t.formatter(f) == nil {
			t.parseError("unknown formatter: %q", f)
		}
	}

	return &variableElement{t.linenum, args, formatters}
}
Пример #7
0
func parseModeUidGid(s_mode, s_uid, s_gid string) (mode int64, uid, gid int, err error) {
	mode, err = strconv.Btoi64(s_mode, 0)
	if err != nil {
		return
	}
	uid, err = strconv.Atoi(s_uid)
	if err != nil {
		return
	}
	gid, err = strconv.Atoi(s_gid)
	return mode, uid, gid, nil
}
Пример #8
0
// newVariable allocates a new variable-evaluation element.
func (t *Template) newVariable(words []string) *variableElement {
	formatters := extractFormatters(words)
	args := make([]interface{}, len(words))

	// Build argument list, processing any literals
	for i, word := range words {
		var lerr error
		switch word[0] {
		case '"', '`', '\'':
			v, err := strconv.Unquote(word)
			if err == nil && word[0] == '\'' {
				args[i], _ = utf8.DecodeRuneInString(v)
			} else {
				args[i], lerr = v, err
			}

		case '.', '+', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
			v, err := strconv.Btoi64(word, 0)
			if err == nil {
				args[i] = v
			} else {
				v, err := strconv.Atof64(word)
				args[i], lerr = v, err
			}

		default:
			args[i] = fieldName(word)
		}
		if lerr != nil {
			t.parseError("invalid literal: %q: %s", word, lerr)
		}
	}

	// We could remember the function address here and avoid the lookup later,
	// but it's more dynamic to let the user change the map contents underfoot.
	// We do require the name to be present, though.

	// Is it in user-supplied map?
	for _, f := range formatters {
		if t.formatter(f) == nil {
			t.parseError("unknown formatter: %q", f)
		}
	}

	return &variableElement{t.linenum, args, formatters}
}
Пример #9
0
// scanInt returns the value of the integer represented by the next
// token, checking for overflow.  Any error is stored in s.err.
func (s *ss) scanInt(verb int, bitSize int) int64 {
	if verb == 'c' {
		return s.scanRune(bitSize)
	}
	base, digits := s.getBase(verb)
	s.skipSpace(false)
	s.accept(sign) // If there's a sign, it will be left in the token buffer.
	tok := s.scanNumber(digits)
	i, err := strconv.Btoi64(tok, base)
	if err != nil {
		s.error(err)
	}
	n := uint(bitSize)
	x := (i << (64 - n)) >> (64 - n)
	if x != i {
		s.errorString("integer overflow on token " + tok)
	}
	return i
}
Пример #10
0
func decodeRFC2047Word(s string) (string, os.Error) {
	fields := strings.Split(s, "?", -1)
	if len(fields) != 5 || fields[0] != "=" || fields[4] != "=" {
		return "", os.ErrorString("mail: address not RFC 2047 encoded")
	}
	charset, enc := strings.ToLower(fields[1]), strings.ToLower(fields[2])
	// TODO(dsymonds): Support "b" encoding too.
	if enc != "q" {
		return "", fmt.Errorf("mail: RFC 2047 encoding not supported: %q", enc)
	}
	if charset != "iso-8859-1" && charset != "utf-8" {
		return "", fmt.Errorf("mail: charset not supported: %q", charset)
	}

	in := fields[3]
	b := new(bytes.Buffer)
	for i := 0; i < len(in); i++ {
		switch c := in[i]; {
		case c == '=' && i+2 < len(in):
			x, err := strconv.Btoi64(in[i+1:i+3], 16)
			if err != nil {
				return "", fmt.Errorf("mail: invalid RFC 2047 encoding: %q", in[i:i+3])
			}
			i += 2
			switch charset {
			case "iso-8859-1":
				b.WriteRune(int(x))
			case "utf-8":
				b.WriteByte(byte(x))
			}
		case c == '_':
			b.WriteByte(' ')
		default:
			b.WriteByte(c)
		}
	}
	return b.String(), nil
}
Пример #11
0
func readInt(lx *Lexer) int64 {
	base := 10
	strVal := ""

	if lx.curChar == '0' {
		lx.nextChar()
		base = readBase(lx)
	}
	for ; isNumChar(lx.curChar, base); lx.nextChar() {
		if lx.curChar != '_' {
			strVal += string(lx.curChar)
		}
	}
	var ret int64 = 0
	if len(strVal) > 0 {
		val, err := strconv.Btoi64(strVal, base)
		if err != nil {
			lx.Error(err.String())
		}
		ret = val
	}
	return ret
}
Пример #12
0
// VerifyValue extracts a value from a string created by SignValue. An error is
// returned if the expiration time has elapsed or the signature is not correct.
func VerifyValue(secret, context string, signedValue string) (string, os.Error) {
	a := strings.Split(signedValue, "~", 3)
	if len(a) != 3 {
		return "", errVerificationFailure
	}
	expiration, err := strconv.Btoi64(a[1], 16)
	if err != nil || expiration < time.Seconds() {
		return "", errVerificationFailure
	}
	expectedSig := signature(secret, context, a[1], a[2])
	actualSig := a[0]
	if len(actualSig) != len(expectedSig) {
		return "", errVerificationFailure
	}
	// Time independent compare
	eq := 0
	for i := 0; i < len(actualSig); i++ {
		eq = eq | (int(actualSig[i]) ^ int(expectedSig[i]))
	}
	if eq != 0 {
		return "", errVerificationFailure
	}
	return a[2], nil
}
Пример #13
0
// VerifyValue extracts a value from a string created by SignValue. An error is
// returned if the expiration time has elapsed or the signature is not correct.
func VerifyValue(secret, context string, signedValue string) (string, os.Error) {
	a := strings.SplitN(signedValue, "~", 3)
	if len(a) != 3 {
		return "", errVerificationFailure
	}
	expiration, err := strconv.Btoi64(a[1], 16)
	if err != nil || expiration < time.Seconds() {
		return "", errVerificationFailure
	}
	expectedSig := signature(secret, context, a[1], a[2])
	actualSig := a[0]
	if len(actualSig) != len(expectedSig) {
		return "", errVerificationFailure
	}
	// Constant time compare
	var v byte
	for i := 0; i < len(actualSig); i++ {
		v |= actualSig[i] ^ expectedSig[i]
	}
	if subtle.ConstantTimeByteEq(v, 0) != 1 {
		return "", errVerificationFailure
	}
	return a[2], nil
}
Пример #14
0
func resolve(tag string, in string) (rtag string, out interface{}) {
	tag = shortTag(tag)
	if !resolvableTag(tag) {
		return tag, in
	}

	defer func() {
		if tag != "" && tag != rtag {
			panic("Can't decode " + rtag + " '" + in + "' as a " + tag)
		}
	}()

	if in == "" {
		return "!!null", nil
	}

	c := resolveTable[in[0]]
	if c == 0 {
		// It's a string for sure. Nothing to do.
		return "!!str", in
	}

	// Handle things we can lookup in a map.
	if item, ok := resolveMap[in]; ok {
		return item.tag, item.value
	}

	switch c {
	case 'M':
		// We've already checked the map above.

	case '.':
		// Not in the map, so maybe a normal float.
		floatv, err := strconv.Atof64(in)
		if err == nil {
			return "!!float", floatv
		}
		// XXX Handle base 60 floats here (WTF!)

	case 'D', 'S':
		// Int, float, or timestamp.
		for i := 0; i != len(in); i++ {
			if in[i] == '_' {
				in = strings.Replace(in, "_", "", -1)
				break
			}
		}
		intv, err := strconv.Btoi64(in, 0)
		if err == nil {
			if intv == int64(int(intv)) {
				return "!!int", int(intv)
			} else {
				return "!!int", intv
			}
		}
		floatv, err := strconv.Atof64(in)
		if err == nil {
			return "!!float", floatv
		}
		if strings.HasPrefix(in, "0b") {
			intv, err := strconv.Btoi64(in[2:], 2)
			if err == nil {
				return "!!int", int(intv)
			}
		} else if strings.HasPrefix(in, "-0b") {
			intv, err := strconv.Btoi64(in[3:], 2)
			if err == nil {
				return "!!int", -int(intv)
			}
		}
		// XXX Handle timestamps here.

	case '<':
		// XXX Handle merge (<<) here.

	default:
		panic("resolveTable item not yet handled: " +
			string([]byte{c}) + " (with " + in + ")")
	}
	return "!!str", in
}
Пример #15
0
func (f fieldInt32) parse(fstr string) os.Error {
	i, err := strconv.Btoi64(fstr, 0)
	*f.ptr = int32(i)
	return err
}
Пример #16
0
func (self *Symbol) isInt() bool {
	_, err := strconv.Btoi64(string(*self), 0)
	return err == nil
}
Пример #17
0
func newNumber(text string, typ itemType) (*NumberNode, os.Error) {
	n := &NumberNode{NodeType: NodeNumber, Text: text}
	switch typ {
	case itemCharConstant:
		rune, _, tail, err := strconv.UnquoteChar(text[1:], text[0])
		if err != nil {
			return nil, err
		}
		if tail != "'" {
			return nil, fmt.Errorf("malformed character constant: %s", text)
		}
		n.Int64 = int64(rune)
		n.IsInt = true
		n.Uint64 = uint64(rune)
		n.IsUint = true
		n.Float64 = float64(rune) // odd but those are the rules.
		n.IsFloat = true
		return n, nil
	case itemComplex:
		// fmt.Sscan can parse the pair, so let it do the work.
		if _, err := fmt.Sscan(text, &n.Complex128); err != nil {
			return nil, err
		}
		n.IsComplex = true
		n.simplifyComplex()
		return n, nil
	}
	// Imaginary constants can only be complex unless they are zero.
	if len(text) > 0 && text[len(text)-1] == 'i' {
		f, err := strconv.Atof64(text[:len(text)-1])
		if err == nil {
			n.IsComplex = true
			n.Complex128 = complex(0, f)
			n.simplifyComplex()
			return n, nil
		}
	}
	// Do integer test first so we get 0x123 etc.
	u, err := strconv.Btoui64(text, 0) // will fail for -0; fixed below.
	if err == nil {
		n.IsUint = true
		n.Uint64 = u
	}
	i, err := strconv.Btoi64(text, 0)
	if err == nil {
		n.IsInt = true
		n.Int64 = i
		if i == 0 {
			n.IsUint = true // in case of -0.
			n.Uint64 = u
		}
	}
	// If an integer extraction succeeded, promote the float.
	if n.IsInt {
		n.IsFloat = true
		n.Float64 = float64(n.Int64)
	} else if n.IsUint {
		n.IsFloat = true
		n.Float64 = float64(n.Uint64)
	} else {
		f, err := strconv.Atof64(text)
		if err == nil {
			n.IsFloat = true
			n.Float64 = f
			// If a floating-point extraction succeeded, extract the int if needed.
			if !n.IsInt && float64(int64(f)) == f {
				n.IsInt = true
				n.Int64 = int64(f)
			}
			if !n.IsUint && float64(uint64(f)) == f {
				n.IsUint = true
				n.Uint64 = uint64(f)
			}
		}
	}
	if !n.IsInt && !n.IsUint && !n.IsFloat {
		return nil, fmt.Errorf("illegal number syntax: %q", text)
	}
	return n, nil
}
Пример #18
0
// ReadRequest reads and parses a request from b.
func ReadRequest(b *bufio.Reader) (req *Request, err os.Error) {
	req = new(Request)

	// First line: GET /index.html HTTP/1.0
	var s string
	if s, err = readLine(b); err != nil {
		return nil, err
	}

	var f []string
	if f = strings.Split(s, " ", 3); len(f) < 3 {
		return nil, &badStringError{"malformed HTTP request", s}
	}
	req.Method, req.RawURL, req.Proto = f[0], f[1], f[2]
	var ok bool
	if req.ProtoMajor, req.ProtoMinor, ok = parseHTTPVersion(req.Proto); !ok {
		return nil, &badStringError{"malformed HTTP version", req.Proto}
	}

	if req.URL, err = ParseURL(req.RawURL); err != nil {
		return nil, err
	}

	// Subsequent lines: Key: value.
	nheader := 0
	req.Header = make(map[string]string)
	for {
		var key, value string
		if key, value, err = readKeyValue(b); err != nil {
			return nil, err
		}
		if key == "" {
			break
		}
		if nheader++; nheader >= maxHeaderLines {
			return nil, ErrHeaderTooLong
		}

		key = CanonicalHeaderKey(key)

		// RFC 2616 says that if you send the same header key
		// multiple times, it has to be semantically equivalent
		// to concatenating the values separated by commas.
		oldvalue, present := req.Header[key]
		if present {
			req.Header[key] = oldvalue + "," + value
		} else {
			req.Header[key] = value
		}
	}

	// RFC2616: Must treat
	//	GET /index.html HTTP/1.1
	//	Host: www.google.com
	// and
	//	GET http://www.google.com/index.html HTTP/1.1
	//	Host: doesntmatter
	// the same.  In the second case, any Host line is ignored.
	req.Host = req.URL.Host
	if v, present := req.Header["Host"]; present {
		if req.Host == "" {
			req.Host = v
		}
		req.Header["Host"] = "", false
	}

	// RFC2616: Should treat
	//	Pragma: no-cache
	// like
	//	Cache-Control: no-cache
	if v, present := req.Header["Pragma"]; present && v == "no-cache" {
		if _, presentcc := req.Header["Cache-Control"]; !presentcc {
			req.Header["Cache-Control"] = "no-cache"
		}
	}

	// Determine whether to hang up after sending the reply.
	if req.ProtoMajor < 1 || (req.ProtoMajor == 1 && req.ProtoMinor < 1) {
		req.Close = true
	} else if v, present := req.Header["Connection"]; present {
		// TODO: Should split on commas, toss surrounding white space,
		// and check each field.
		if v == "close" {
			req.Close = true
		}
	}

	// Pull out useful fields as a convenience to clients.
	if v, present := req.Header["Referer"]; present {
		req.Referer = v
		req.Header["Referer"] = "", false
	}
	if v, present := req.Header["User-Agent"]; present {
		req.UserAgent = v
		req.Header["User-Agent"] = "", false
	}

	// TODO: Parse specific header values:
	//	Accept
	//	Accept-Encoding
	//	Accept-Language
	//	Authorization
	//	Cache-Control
	//	Connection
	//	Date
	//	Expect
	//	From
	//	If-Match
	//	If-Modified-Since
	//	If-None-Match
	//	If-Range
	//	If-Unmodified-Since
	//	Max-Forwards
	//	Proxy-Authorization
	//	Referer [sic]
	//	TE (transfer-codings)
	//	Trailer
	//	Transfer-Encoding
	//	Upgrade
	//	User-Agent
	//	Via
	//	Warning

	// A message body exists when either Content-Length or Transfer-Encoding
	// headers are present. Transfer-Encoding trumps Content-Length.
	if v, present := req.Header["Transfer-Encoding"]; present && v == "chunked" {
		req.Body = &body{Reader: newChunkedReader(b), hdr: req, r: b, closing: req.Close}
	} else if v, present := req.Header["Content-Length"]; present {
		length, err := strconv.Btoi64(v, 10)
		if err != nil {
			return nil, &badStringError{"invalid Content-Length", v}
		}
		// TODO: limit the Content-Length. This is an easy DoS vector.
		req.Body = &body{Reader: io.LimitReader(b, length), closing: req.Close}
	}

	return req, nil
}
Пример #19
0
func (f fieldInt64) parse(fstr string) (err os.Error) {
	*f.ptr, err = strconv.Btoi64(fstr, 0)
	return err
}
Пример #20
0
func (i *intValue) Set(s string) bool {
	v, err := strconv.Btoi64(s, 0)
	*i = intValue(v)
	return err == nil
}
Пример #21
0
Файл: parse.go Проект: ssrl/go
func newNumber(text string, isComplex bool) (*numberNode, os.Error) {
	n := &numberNode{nodeType: nodeNumber, text: text}
	if isComplex {
		// fmt.Sscan can parse the pair, so let it do the work.
		if _, err := fmt.Sscan(text, &n.complex128); err != nil {
			return nil, err
		}
		n.isComplex = true
		n.simplifyComplex()
		return n, nil
	}
	// Imaginary constants can only be complex unless they are zero.
	if len(text) > 0 && text[len(text)-1] == 'i' {
		f, err := strconv.Atof64(text[:len(text)-1])
		if err == nil {
			n.isComplex = true
			n.complex128 = complex(0, f)
			n.simplifyComplex()
			return n, nil
		}
	}
	// Do integer test first so we get 0x123 etc.
	u, err := strconv.Btoui64(text, 0) // will fail for -0; fixed below.
	if err == nil {
		n.isUint = true
		n.uint64 = u
	}
	i, err := strconv.Btoi64(text, 0)
	if err == nil {
		n.isInt = true
		n.int64 = i
		if i == 0 {
			n.isUint = true // in case of -0.
			n.uint64 = u
		}
	}
	// If an integer extraction succeeded, promote the float.
	if n.isInt {
		n.isFloat = true
		n.float64 = float64(n.int64)
	} else if n.isUint {
		n.isFloat = true
		n.float64 = float64(n.uint64)
	} else {
		f, err := strconv.Atof64(text)
		if err == nil {
			n.isFloat = true
			n.float64 = f
			// If a floating-point extraction succeeded, extract the int if needed.
			if !n.isInt && float64(int64(f)) == f {
				n.isInt = true
				n.int64 = int64(f)
			}
			if !n.isUint && float64(uint64(f)) == f {
				n.isUint = true
				n.uint64 = uint64(f)
			}
		}
	}
	if !n.isInt && !n.isUint && !n.isFloat {
		return nil, fmt.Errorf("illegal number syntax: %q", text)
	}
	return n, nil
}