| 1 | /* |
| 2 | Copyright 2014 Google Inc. All rights reserved. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | // Package bytesource provides a rand.Source64 that is determined by a slice of bytes. |
| 18 | package bytesource |
| 19 | |
| 20 | import ( |
| 21 | "bytes" |
| 22 | "encoding/binary" |
| 23 | "io" |
| 24 | "math/rand" |
| 25 | ) |
| 26 | |
| 27 | // ByteSource implements rand.Source64 determined by a slice of bytes. The random numbers are |
| 28 | // generated from each 8 bytes in the slice, until the last bytes are consumed, from which a |
| 29 | // fallback pseudo random source is created in case more random numbers are required. |
| 30 | // It also exposes a `bytes.Reader` API, which lets callers consume the bytes directly. |
| 31 | type ByteSource struct { |
| 32 | *bytes.Reader |
| 33 | fallback rand.Source |
| 34 | } |
| 35 | |
| 36 | // New returns a new ByteSource from a given slice of bytes. |
| 37 | func New(input []byte) *ByteSource { |
| 38 | s := &ByteSource{ |
| 39 | Reader: bytes.NewReader(input), |
| 40 | fallback: rand.NewSource(0), |
| 41 | } |
| 42 | if len(input) > 0 { |
| 43 | s.fallback = rand.NewSource(int64(s.consumeUint64())) |
| 44 | } |
| 45 | return s |
| 46 | } |
| 47 | |
| 48 | func (s *ByteSource) Uint64() uint64 { |
| 49 | // Return from input if it was not exhausted. |
| 50 | if s.Len() > 0 { |
| 51 | return s.consumeUint64() |
| 52 | } |
| 53 | |
| 54 | // Input was exhausted, return random number from fallback (in this case fallback should not be |
| 55 | // nil). Try first having a Uint64 output (Should work in current rand implementation), |
| 56 | // otherwise return a conversion of Int63. |
| 57 | if s64, ok := s.fallback.(rand.Source64); ok { |
| 58 | return s64.Uint64() |
| 59 | } |
| 60 | return uint64(s.fallback.Int63()) |
| 61 | } |
| 62 | |
| 63 | func (s *ByteSource) Int63() int64 { |
| 64 | return int64(s.Uint64() >> 1) |
| 65 | } |
| 66 | |
| 67 | func (s *ByteSource) Seed(seed int64) { |
| 68 | s.fallback = rand.NewSource(seed) |
| 69 | s.Reader = bytes.NewReader(nil) |
| 70 | } |
| 71 | |
| 72 | // consumeUint64 reads 8 bytes from the input and convert them to a uint64. It assumes that the the |
| 73 | // bytes reader is not empty. |
| 74 | func (s *ByteSource) consumeUint64() uint64 { |
| 75 | var bytes [8]byte |
| 76 | _, err := s.Read(bytes[:]) |
| 77 | if err != nil && err != io.EOF { |
| 78 | panic("failed reading source") // Should not happen. |
| 79 | } |
| 80 | return binary.BigEndian.Uint64(bytes[:]) |
| 81 | } |