...

Source file src/github.com/google/certificate-transparency-go/fixchain/containers.go

Documentation: github.com/google/certificate-transparency-go/fixchain

     1  // Copyright 2016 Google LLC. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package fixchain
    16  
    17  import (
    18  	"sync"
    19  
    20  	"github.com/google/certificate-transparency-go/x509"
    21  )
    22  
    23  type dedupedChain struct {
    24  	certs []*x509.Certificate
    25  }
    26  
    27  func (d *dedupedChain) addCert(cert *x509.Certificate) {
    28  	// Check that the certificate isn't being added twice.
    29  	for _, c := range d.certs {
    30  		if c.Equal(cert) {
    31  			return
    32  		}
    33  	}
    34  	d.certs = append(d.certs, cert)
    35  }
    36  
    37  func (d *dedupedChain) addCertToFront(cert *x509.Certificate) {
    38  	// Check that the certificate isn't being added twice.
    39  	for _, c := range d.certs {
    40  		if c.Equal(cert) {
    41  			return
    42  		}
    43  	}
    44  	d.certs = append([]*x509.Certificate{cert}, d.certs...)
    45  }
    46  
    47  func newDedupedChain(chain []*x509.Certificate) *dedupedChain {
    48  	d := &dedupedChain{}
    49  	for _, cert := range chain {
    50  		d.addCert(cert)
    51  	}
    52  	return d
    53  }
    54  
    55  type lockedMap struct {
    56  	m map[[hashSize]byte]bool
    57  	sync.RWMutex
    58  }
    59  
    60  func newLockedMap() *lockedMap {
    61  	return &lockedMap{m: make(map[[hashSize]byte]bool)}
    62  }
    63  
    64  func (m *lockedMap) get(hash [hashSize]byte) bool {
    65  	m.RLock()
    66  	defer m.RUnlock()
    67  	return m.m[hash]
    68  }
    69  
    70  func (m *lockedMap) set(hash [hashSize]byte, b bool) {
    71  	m.Lock()
    72  	defer m.Unlock()
    73  	m.m[hash] = b
    74  }
    75  

View as plain text