home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1654 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  4.6 KB  |  148 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import cStringIO
  5. import dns.exception as dns
  6. import dns.rdata as dns
  7. import dns.rdatatype as dns
  8. import dns.name as dns
  9.  
  10. class NSEC(dns.rdata.Rdata):
  11.     __slots__ = [
  12.         'next',
  13.         'windows']
  14.     
  15.     def __init__(self, rdclass, rdtype, next, windows):
  16.         super(NSEC, self).__init__(rdclass, rdtype)
  17.         self.next = next
  18.         self.windows = windows
  19.  
  20.     
  21.     def to_text(self, origin = None, relativize = True, **kw):
  22.         next = self.next.choose_relativity(origin, relativize)
  23.         for window, bitmap in self.windows:
  24.             bits = []
  25.             for i in xrange(0, len(bitmap)):
  26.                 byte = ord(bitmap[i])
  27.                 for j in xrange(0, 8):
  28.                     if byte & 128 >> j:
  29.                         bits.append(dns.rdatatype.to_text(window * 256 + i * 8 + j))
  30.                         continue
  31.                 
  32.             
  33.             text = ' '.join(bits)
  34.         
  35.         return '%s %s' % (next, text)
  36.  
  37.     
  38.     def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
  39.         next = tok.get_name()
  40.         next = next.choose_relativity(origin, relativize)
  41.         rdtypes = []
  42.         while None:
  43.             (ttype, value) = tok.get()
  44.             if ttype == dns.tokenizer.EOL or ttype == dns.tokenizer.EOF:
  45.                 break
  46.             
  47.             nrdtype = dns.rdatatype.from_text(value)
  48.             if nrdtype == 0:
  49.                 raise dns.exception.SyntaxError, 'NSEC with bit 0'
  50.             if nrdtype > 65535:
  51.                 raise dns.exception.SyntaxError, 'NSEC with bit > 65535'
  52.             nrdtype > 65535
  53.             rdtypes.append(nrdtype)
  54.             continue
  55.             rdtypes.sort()
  56.             window = 0
  57.             octets = 0
  58.             prior_rdtype = 0
  59.             bitmap = [
  60.                 '\x00'] * 32
  61.             windows = []
  62.             for nrdtype in rdtypes:
  63.                 if nrdtype == prior_rdtype:
  64.                     continue
  65.                 
  66.                 prior_rdtype = nrdtype
  67.                 new_window = nrdtype // 256
  68.                 if new_window != window:
  69.                     windows.append((window, ''.join(bitmap[0:octets])))
  70.                     bitmap = [
  71.                         '\x00'] * 32
  72.                     window = new_window
  73.                 
  74.                 offset = nrdtype % 256
  75.                 byte = offset / 8
  76.                 bit = offset % 8
  77.                 octets = byte + 1
  78.                 bitmap[byte] = chr(ord(bitmap[byte]) | 128 >> bit)
  79.             
  80.         windows.append((window, ''.join(bitmap[0:octets])))
  81.         return cls(rdclass, rdtype, next, windows)
  82.  
  83.     from_text = classmethod(from_text)
  84.     
  85.     def to_wire(self, file, compress = None, origin = None):
  86.         self.next.to_wire(file, None, origin)
  87.         for window, bitmap in self.windows:
  88.             file.write(chr(window))
  89.             file.write(chr(len(bitmap)))
  90.             file.write(bitmap)
  91.         
  92.  
  93.     
  94.     def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
  95.         (next, cused) = dns.name.from_wire(wire[:current + rdlen], current)
  96.         current += cused
  97.         rdlen -= cused
  98.         windows = []
  99.         while rdlen > 0:
  100.             if rdlen < 3:
  101.                 raise dns.exception.FormError, 'NSEC too short'
  102.             rdlen < 3
  103.             window = ord(wire[current])
  104.             octets = ord(wire[current + 1])
  105.             if octets == 0 or octets > 32:
  106.                 raise dns.exception.FormError, 'bad NSEC octets'
  107.             octets > 32
  108.             current += 2
  109.             rdlen -= 2
  110.             if rdlen < octets:
  111.                 raise dns.exception.FormError, 'bad NSEC bitmap length'
  112.             rdlen < octets
  113.             bitmap = wire[current:current + octets]
  114.             current += octets
  115.             rdlen -= octets
  116.             windows.append((window, bitmap))
  117.         if origin is not None:
  118.             next = next.relativize(origin)
  119.         
  120.         return cls(rdclass, rdtype, next, windows)
  121.  
  122.     from_wire = classmethod(from_wire)
  123.     
  124.     def choose_relativity(self, origin = None, relativize = True):
  125.         self.next = self.next.choose_relativity(origin, relativize)
  126.  
  127.     
  128.     def _cmp(self, other):
  129.         v = cmp(self.next, other.next)
  130.         if v == 0:
  131.             b1 = cStringIO.StringIO()
  132.             for window, bitmap in self.windows:
  133.                 b1.write(chr(window))
  134.                 b1.write(chr(len(bitmap)))
  135.                 b1.write(bitmap)
  136.             
  137.             b2 = cStringIO.StringIO()
  138.             for window, bitmap in other.windows:
  139.                 b2.write(chr(window))
  140.                 b2.write(chr(len(bitmap)))
  141.                 b2.write(bitmap)
  142.             
  143.             v = cmp(b1.getvalue(), b2.getvalue())
  144.         
  145.         return v
  146.  
  147.  
  148.