本文整理汇总了Golang中bytes.Copy函数的典型用法代码### 示例。如果您正苦于以下问题:Golang Copy函数的具体用法?Golang Copy怎么用?Golang Copy使用的例子?那么恭喜您, 这里精选的函数代码### 示例或许可以为您提供帮助。

在下文中一共展示了Copy函数的20个代码### 示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码### 示例。

示例1: marshal

func (m *clientHelloMsg) marshal() []byte {
    if m.raw != nil {
        return m.raw
    }

    length := 2 + 32 + 1 + len(m.sessionId) + 2 + len(m.cipherSuites)*2 + 1 + len(m.compressionMethods)
    x := make([]byte, 4+length)
    x[0] = typeClientHello
    x[1] = uint8(length >> 16)
    x[2] = uint8(length >> 8)
    x[3] = uint8(length)
    x[4] = m.major
    x[5] = m.minor
    bytes.Copy(x[6:38], m.random)
    x[38] = uint8(len(m.sessionId))
    bytes.Copy(x[39:39+len(m.sessionId)], m.sessionId)
    y := x[39+len(m.sessionId) : len(x)]
    y[0] = uint8(len(m.cipherSuites) >> 7)
    y[1] = uint8(len(m.cipherSuites) << 1)
    for i, suite := range m.cipherSuites {
        y[2+i*2] = uint8(suite >> 8)
        y[3+i*2] = uint8(suite)
    }
    z := y[2+len(m.cipherSuites)*2 : len(y)]
    z[0] = uint8(len(m.compressionMethods))
    bytes.Copy(z[1:len(z)], m.compressionMethods)
    m.raw = x

    return x
}

开发者ID:edisonwsk,项目名称:golang-on-cygwin,代码行数:30,代码来源:handshake_messages.go

示例2: main

func main() {
    in = bufio.NewReader(os.Stdin)
    buf := make([]byte, 100*1024)
    top := 0
    for {
        line, err := in.ReadSlice('\n')
        if err != nil {
            break
        }
        if line[0] == '>' {
            if top > 0 {
                output(buf[0:top])
                top = 0
            }
            os.Stdout.Write(line)
            continue
        }
        line = line[0 : len(line)-1] // drop newline
        if top+len(line) > len(buf) {
            nbuf := make([]byte, 2*len(buf)+1024*(100+len(line)))
            bytes.Copy(nbuf, buf[0:top])
            buf = nbuf
        }
        bytes.Copy(buf[top:len(buf)], line)
        top += len(line)
    }
    output(buf[0:top])
}

开发者ID:8l,项目名称:go-learn,代码行数:28,代码来源:reverse-complement.go

示例3: EncryptOAEP

// EncryptOAEP encrypts the given message with RSA-OAEP.
// The message must be no longer than the length of the public modulus less
// twice the hash length plus 2.
func EncryptOAEP(hash hash.Hash, rand io.Reader, pub *PublicKey, msg []byte, label []byte) (out []byte, err os.Error) {
    hash.Reset()
    k := (pub.N.Len() + 7) / 8
    if len(msg) > k-2*hash.Size()-2 {
        err = MessageTooLongError{}
        return
    }

    hash.Write(label)
    lHash := hash.Sum()
    hash.Reset()

    em := make([]byte, k)
    seed := em[1 : 1+hash.Size()]
    db := em[1+hash.Size() : len(em)]

    bytes.Copy(db[0:hash.Size()], lHash)
    db[len(db)-len(msg)-1] = 1
    bytes.Copy(db[len(db)-len(msg):len(db)], msg)

    _, err = io.ReadFull(rand, seed)
    if err != nil {
        return
    }

    mgf1XOR(db, hash, seed)
    mgf1XOR(seed, hash, db)

    m := new(big.Int)
    m.SetBytes(em)
    c := encrypt(new(big.Int), pub, m)
    out = c.Bytes()
    return
}

开发者ID:8l,项目名称:go-learn,代码行数:37,代码来源:rsa.go

示例4: finishedSum

// finishedSum calculates the contents of the verify_data member of a Finished
// message given the MD5 and SHA1 hashes of a set of handshake messages.
func finishedSum(md5, sha1, label, masterSecret []byte) []byte {
    seed := make([]byte, len(md5)+len(sha1))
    bytes.Copy(seed, md5)
    bytes.Copy(seed[len(md5):len(seed)], sha1)
    out := make([]byte, finishedVerifyLength)
    pRF11(out, masterSecret, label, seed)
    return out
}

开发者ID:edisonwsk,项目名称:golang-on-cygwin,代码行数:10,代码来源:prf.go

示例5: WriteHeader

// WriteHeader writes hdr and prepares to accept the file's contents.
// WriteHeader calls Flush if it is not the first header.
func (tw *Writer) WriteHeader(hdr *Header) os.Error {
    if tw.err == nil {
        tw.Flush()
    }
    if tw.err != nil {
        return tw.err
    }

    tw.nb = int64(hdr.Size)
    tw.pad = -tw.nb & (blockSize - 1) // blockSize is a power of two

    header := make([]byte, blockSize)
    s := slicer(header)

    // TODO(dsymonds): handle names longer than 100 chars
    bytes.Copy(s.next(100), strings.Bytes(hdr.Name))

    tw.octal(s.next(8), hdr.Mode)                       // 100:108
    tw.numeric(s.next(8), hdr.Uid)                      // 108:116
    tw.numeric(s.next(8), hdr.Gid)                      // 116:124
    tw.numeric(s.next(12), hdr.Size)                    // 124:136
    tw.numeric(s.next(12), hdr.Mtime)                   // 136:148
    s.next(8)                                           // chksum (148:156)
    s.next(1)[0] = hdr.Typeflag                         // 156:157
    s.next(100)                                         // linkname (157:257)
    bytes.Copy(s.next(8), strings.Bytes("ustar\x0000")) // 257:265
    tw.cString(s.next(32), hdr.Uname)                   // 265:297
    tw.cString(s.next(32), hdr.Gname)                   // 297:329
    tw.numeric(s.next(8), hdr.Devmajor)                 // 329:337
    tw.numeric(s.next(8), hdr.Devminor)                 // 337:345

    // Use the GNU magic instead of POSIX magic if we used any GNU extensions.
    if tw.usedBinary {
        bytes.Copy(header[257:265], strings.Bytes("ustar  \x00"))
    }

    // The chksum field is terminated by a NUL and a space.
    // This is different from the other octal fields.
    chksum, _ := checksum(header)
    tw.octal(header[148:155], chksum)
    header[155] = ' '

    if tw.err != nil {
        // problem with header; probably integer too big for a field.
        return tw.err
    }

    _, tw.err = tw.w.Write(header)

    return tw.err
}

开发者ID:8l,项目名称:go-learn,代码行数:53,代码来源:writer.go

示例6: processHandshakeRecord

func (p *recordProcessor) processHandshakeRecord(data []byte) {
    if p.handshakeBuf == nil {
        p.handshakeBuf = data
    } else {
        if len(p.handshakeBuf) > maxHandshakeMsg {
            p.error(alertInternalError)
            return
        }
        newBuf := make([]byte, len(p.handshakeBuf)+len(data))
        bytes.Copy(newBuf, p.handshakeBuf)
        bytes.Copy(newBuf[len(p.handshakeBuf):len(newBuf)], data)
        p.handshakeBuf = newBuf
    }

    for len(p.handshakeBuf) >= 4 {
        handshakeLen := int(p.handshakeBuf[1])<<16 |
            int(p.handshakeBuf[2])<<8 |
            int(p.handshakeBuf[3])
        if handshakeLen+4 > len(p.handshakeBuf) {
            break
        }

        bytes := p.handshakeBuf[0 : handshakeLen+4]
        p.handshakeBuf = p.handshakeBuf[handshakeLen+4 : len(p.handshakeBuf)]
        if bytes[0] == typeFinished {
            // Special case because Finished is synchronous: the
            // handshake handler has to tell us if it's ok to start
            // forwarding application data.
            m := new(finishedMsg)
            if !m.unmarshal(bytes) {
                p.error(alertUnexpectedMessage)
            }
            p.handshakeChan <- m
            var ok bool
            p.connState, ok = (<-p.controlChan).(ConnectionState)
            if !ok || p.connState.Error != 0 {
                p.shutdown = true
                return
            }
        } else {
            msg, ok := parseHandshakeMsg(bytes)
            if !ok {
                p.error(alertUnexpectedMessage)
                return
            }
            p.handshakeChan <- msg
        }
    }
}

开发者ID:edisonwsk,项目名称:golang-on-cygwin,代码行数:49,代码来源:record_process.go

示例7: RepeatFasta

// RepeatFasta prints the characters of the byte slice s. When it
// reaches the end of the slice, it goes back to the beginning.
// It stops after generating count characters.
// After each WIDTH characters it prints a newline.
// It assumes that WIDTH <= len(s) + 1.
func RepeatFasta(s []byte, count int) {
    pos := 0;
    s2 := make([]byte, len(s) + WIDTH);
    bytes.Copy(s2, s);
    bytes.Copy(s2[len(s):len(s2)], s);
    for count > 0 {
        line := min(WIDTH, count);
        out.Write(s2[pos:pos+line]);
        out.WriteByte('\n');
        pos += line;
        if pos >= len(s) {
            pos -= len(s);
        }
        count -= line;
    }
}

开发者ID:8l,项目名称:go-learn,代码行数:21,代码来源:fasta.go

示例8: Read

func (tls *Conn) Read(p []byte) (int, os.Error) {
    if len(tls.readBuf) == 0 {
        if tls.eof {
            return 0, os.EOF
        }

        var timeoutChan chan bool
        if tls.readTimeout > 0 {
            timeoutChan = make(chan bool)
            go timeout(timeoutChan, tls.readTimeout)
        }

        select {
        case b := <-tls.readChan:
            tls.readBuf = b
        case <-timeoutChan:
            return 0, os.EAGAIN
        }

        // TLS distinguishes between orderly closes and truncations. An
        // orderly close is represented by a zero length slice.
        if closed(tls.readChan) {
            return 0, io.ErrUnexpectedEOF
        }
        if len(tls.readBuf) == 0 {
            tls.eof = true
            return 0, os.EOF
        }
    }

    n := bytes.Copy(p, tls.readBuf)
    tls.readBuf = tls.readBuf[n:len(tls.readBuf)]
    return n, nil
}

开发者ID:edisonwsk,项目名称:golang-on-cygwin,代码行数:34,代码来源:tls.go

示例9: recv

func (r *msgReceiver) recv() (*msg, os.Error) {
    // Init pointers to buffers where syscall recvmsg can write.
    r.iov.base = &r.data[0]
    r.iov.len = int32(len(r.data))
    r.hdr.iov = &r.iov
    r.hdr.niov = 1
    r.hdr.desc = &r.desc[0]
    r.hdr.ndesc = int32(len(r.desc))
    n, _, e := syscall.Syscall(syscall.SYS_IMC_RECVMSG, uintptr(r.fd), uintptr(unsafe.Pointer(&r.hdr)), 0)
    if e != 0 {
        return nil, os.NewSyscallError("imc_recvmsg", int(e))
    }

    // Make a copy of the data so that the next recvmsg doesn't
    // smash it.  The system call did not update r.iov.len.  Instead it
    // returned the total byte count as n.
    m := new(msg)
    m.rdata = make([]byte, n)
    bytes.Copy(m.rdata, &r.data)

    // Make a copy of the desc too.
    // The system call *did* update r.hdr.ndesc.
    if r.hdr.ndesc > 0 {
        m.rdesc = make([]int32, r.hdr.ndesc)
        for i := range m.rdesc {
            m.rdesc[i] = r.desc[i]
        }
    }

    return m, nil
}

开发者ID:edisonwsk,项目名称:golang-on-cygwin,代码行数:31,代码来源:msg.go

示例10: fillWindow

func (d *deflater) fillWindow(index int) (int, os.Error) {
    wSize := d.windowMask + 1
    if index >= wSize+wSize-(minMatchLength+maxMatchLength) {
        // shift the window by wSize
        bytes.Copy(d.window, d.window[wSize:2*wSize])
        index -= wSize
        d.windowEnd -= wSize
        if d.blockStart >= wSize {
            d.blockStart -= wSize
        } else {
            d.blockStart = math.MaxInt32
        }
        for i, h := range d.hashHead {
            d.hashHead[i] = max(h-wSize, -1)
        }
        for i, h := range d.hashPrev {
            d.hashPrev[i] = max(h-wSize, -1)
        }
    }
    var count int
    var err os.Error
    count, err = io.ReadAtLeast(d.r, d.window[d.windowEnd:len(d.window)], 1)
    d.windowEnd += count
    if err == os.EOF {
        return index, nil
    }
    return index, err
}

开发者ID:edisonwsk,项目名称:golang-on-cygwin,代码行数:28,代码来源:deflate.go

示例11: Read

func (d *decoder) Read(p []byte) (n int, err os.Error) {
    if d.err != nil {
        return 0, d.err
    }

    // Use leftover decoded output from last read.
    if len(d.out) > 0 {
        n = bytes.Copy(p, d.out)
        d.out = d.out[n:len(d.out)]
        return n, nil
    }

    // Read a chunk.
    nn := len(p) / 3 * 4
    if nn < 4 {
        nn = 4
    }
    if nn > len(d.buf) {
        nn = len(d.buf)
    }
    nn, d.err = io.ReadAtLeast(d.r, d.buf[d.nbuf:nn], 4-d.nbuf)
    d.nbuf += nn
    if d.nbuf < 4 {
        return 0, d.err
    }

    // Decode chunk into p, or d.out and then p if p is too small.
    nr := d.nbuf / 4 * 4
    nw := d.nbuf / 4 * 3
    if nw > len(p) {
        nw, d.end, d.err = d.enc.decode(&d.outbuf, d.buf[0:nr])
        d.out = d.outbuf[0:nw]
        n = bytes.Copy(p, d.out)
        d.out = d.out[n:len(d.out)]
    } else {
        n, d.end, d.err = d.enc.decode(p, d.buf[0:nr])
    }
    d.nbuf -= nr
    for i := 0; i < d.nbuf; i++ {
        d.buf[i] = d.buf[i+nr]
    }

    if d.err == nil {
        d.err = err
    }
    return n, d.err
}

开发者ID:edisonwsk,项目名称:golang-on-cygwin,代码行数:47,代码来源:base64.go

示例12: pRF11

// pRF11 implements the TLS 1.1 pseudo-random function, as defined in RFC 4346, section 5.
func pRF11(result, secret, label, seed []byte) {
    hashSHA1 := sha1.New()
    hashMD5 := md5.New()

    labelAndSeed := make([]byte, len(label)+len(seed))
    bytes.Copy(labelAndSeed, label)
    bytes.Copy(labelAndSeed[len(label):len(labelAndSeed)], seed)

    s1, s2 := splitPreMasterSecret(secret)
    pHash(result, s1, labelAndSeed, hashMD5)
    result2 := make([]byte, len(result))
    pHash(result2, s2, labelAndSeed, hashSHA1)

    for i, b := range result2 {
        result[i] ^= b
    }
}

开发者ID:edisonwsk,项目名称:golang-on-cygwin,代码行数:18,代码来源:prf.go

示例13: Read

func (r *reader) Read(p []byte) (n int, err os.Error) {
    b := *r;
    if len(b) == 0 && len(p) > 0 {
        return 0, os.EOF
    }
    n = bytes.Copy(p, b);
    *r = b[n:len(b)];
    return;
}

开发者ID:edisonwsk,项目名称:golang-on-cygwin,代码行数:9,代码来源:event.go

示例14: leftPad

// leftPad returns a new slice of length size. The contents of input are right
// aligned in the new slice.
func leftPad(input []byte, size int) (out []byte) {
    n := len(input)
    if n > size {
        n = size
    }
    out = make([]byte, size)
    bytes.Copy(out[len(out)-n:len(out)], input)
    return
}

开发者ID:8l,项目名称:go-learn,代码行数:11,代码来源:rsa.go

示例15: keysFromPreMasterSecret11

// keysFromPreMasterSecret generates the connection keys from the pre master
// secret, given the lengths of the MAC and cipher keys, as defined in RFC
// 4346, section 6.3.
func keysFromPreMasterSecret11(preMasterSecret, clientRandom, serverRandom []byte, macLen, keyLen int) (masterSecret, clientMAC, serverMAC, clientKey, serverKey []byte) {
    var seed [tlsRandomLength * 2]byte
    bytes.Copy(seed[0:len(clientRandom)], clientRandom)
    bytes.Copy(seed[len(clientRandom):len(seed)], serverRandom)
    masterSecret = make([]byte, masterSecretLength)
    pRF11(masterSecret, preMasterSecret, masterSecretLabel, seed[0:len(seed)])

    bytes.Copy(seed[0:len(clientRandom)], serverRandom)
    bytes.Copy(seed[len(serverRandom):len(seed)], clientRandom)

    n := 2*macLen + 2*keyLen
    keyMaterial := make([]byte, n)
    pRF11(keyMaterial, masterSecret, keyExpansionLabel, seed[0:len(seed)])
    clientMAC = keyMaterial[0:macLen]
    serverMAC = keyMaterial[macLen : macLen*2]
    clientKey = keyMaterial[macLen*2 : macLen*2+keyLen]
    serverKey = keyMaterial[macLen*2+keyLen : len(keyMaterial)]
    return
}

开发者ID:edisonwsk,项目名称:golang-on-cygwin,代码行数:22,代码来源:prf.go

示例16: grow

// Writing to msg.wdata.
func (m *msg) grow(n int) []byte {
    i := len(m.wdata)
    if i+n > cap(m.wdata) {
        a := make([]byte, i, (i+n)*2)
        bytes.Copy(a, m.wdata)
        m.wdata = a
    }
    m.wdata = m.wdata[0 : i+n]
    return m.wdata[i : i+n]
}

开发者ID:edisonwsk,项目名称:golang-on-cygwin,代码行数:11,代码来源:msg.go

示例17: Read

func (d *decoder) Read(p []byte) (n int, err os.Error) {
    if len(p) == 0 {
        return 0, nil
    }
    if d.err != nil {
        return 0, d.err
    }

    for {
        // Copy leftover output from last decode.
        if len(d.out) > 0 {
            n = bytes.Copy(p, d.out)
            d.out = d.out[n:len(d.out)]
            return
        }

        // Decode leftover input from last read.
        var nn, nsrc, ndst int
        if d.nbuf > 0 {
            ndst, nsrc, d.err = Decode(&d.outbuf, d.buf[0:d.nbuf], d.readErr != nil)
            if ndst > 0 {
                d.out = d.outbuf[0:ndst]
                d.nbuf = bytes.Copy(&d.buf, d.buf[nsrc:d.nbuf])
                continue // copy out and return
            }
        }

        // Out of input, out of decoded output.  Check errors.
        if d.err != nil {
            return 0, d.err
        }
        if d.readErr != nil {
            d.err = d.readErr
            return 0, d.err
        }

        // Read more data.
        nn, d.readErr = d.r.Read(d.buf[d.nbuf:len(d.buf)])
        d.nbuf += nn
    }
    panic("unreachable")
}

开发者ID:8l,项目名称:go-learn,代码行数:42,代码来源:ascii85.go

示例18: Read

func (d *decoder) Read(p []byte) (n int, err os.Error) {
    if len(p) == 0 {
        return 0, nil
    }

    for {
        // Copy leftover output from last decode.
        if len(d.out) > 0 {
            n = bytes.Copy(p, d.out)
            d.out = d.out[n:len(d.out)]
            return
        }

        // Out of decoded output.  Check errors.
        if d.err != nil {
            return 0, d.err
        }
        if d.readErr != nil {
            d.err = d.readErr
            return 0, d.err
        }

        // Read and decode more input.
        var nn int
        nn, d.readErr = d.r.Read(d.buf[d.nbuf:len(d.buf)])
        d.nbuf += nn

        // Send complete lines to Decode.
        nl := bytes.LastIndex(d.buf[0:d.nbuf], newline)
        if nl < 0 {
            continue
        }
        nn, d.err = Decode(&d.outbuf, d.buf[0:nl+1])
        if e, ok := d.err.(CorruptInputError); ok {
            d.err = CorruptInputError(int64(e) + d.off)
        }
        d.out = d.outbuf[0:nn]
        d.nbuf = bytes.Copy(&d.buf, d.buf[nl+1:d.nbuf])
        d.off += int64(nl + 1)
    }
    panic("unreacahable")
}

开发者ID:edisonwsk,项目名称:golang-on-cygwin,代码行数:42,代码来源:git.go

示例19: copy

func copy(v int, x, y []byte) []byte {
    if len(x) > len(y) {
        x = x[0:len(y)]
    } else {
        y = y[0:len(x)]
    }
    if v == 1 {
        bytes.Copy(x, y)
    }
    return x
}

开发者ID:8l,项目名称:go-learn,代码行数:11,代码来源:constant_time_test.go

示例20: videoPollEvent

func videoPollEvent(ev []byte) (err os.Error) {
    if srpcEnabled {
        r := bridge.share.eq.ri
        if r == bridge.share.eq.wi {
            return noEvents
        }
        bytes.Copy(ev, &bridge.share.eq.event[r])
        bridge.share.eq.ri = (r + 1) % eqsize
        return nil
    }
    return os.NewSyscallError("video_poll_event", syscall.VideoPollEvent(&ev[0]))
}

开发者ID:edisonwsk,项目名称:golang-on-cygwin,代码行数:12,代码来源:av.go

最后编辑: kuteng  文档更新时间: 2021-08-23 19:14   作者:kuteng