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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import cStringIO
  5. import struct
  6. import random
  7. import time
  8. import dns.exception as dns
  9. import dns.tsig as dns
  10. QUESTION = 0
  11. ANSWER = 1
  12. AUTHORITY = 2
  13. ADDITIONAL = 3
  14.  
  15. class Renderer(object):
  16.     
  17.     def __init__(self, id = None, flags = 0, max_size = 65535, origin = None):
  18.         self.output = cStringIO.StringIO()
  19.         if id is None:
  20.             self.id = random.randint(0, 65535)
  21.         else:
  22.             self.id = id
  23.         self.flags = flags
  24.         self.max_size = max_size
  25.         self.origin = origin
  26.         self.compress = { }
  27.         self.section = QUESTION
  28.         self.counts = [
  29.             0,
  30.             0,
  31.             0,
  32.             0]
  33.         self.output.write('\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
  34.         self.mac = ''
  35.  
  36.     
  37.     def _rollback(self, where):
  38.         self.output.seek(where)
  39.         self.output.truncate()
  40.         keys_to_delete = []
  41.         for k, v in self.compress.iteritems():
  42.             if v >= where:
  43.                 keys_to_delete.append(k)
  44.                 continue
  45.         
  46.         for k in keys_to_delete:
  47.             del self.compress[k]
  48.         
  49.  
  50.     
  51.     def _set_section(self, section):
  52.         if self.section != section:
  53.             if self.section > section:
  54.                 raise dns.exception.FormError
  55.             self.section > section
  56.             self.section = section
  57.         
  58.  
  59.     
  60.     def add_question(self, qname, rdtype, rdclass = dns.rdataclass.IN):
  61.         self._set_section(QUESTION)
  62.         before = self.output.tell()
  63.         qname.to_wire(self.output, self.compress, self.origin)
  64.         self.output.write(struct.pack('!HH', rdtype, rdclass))
  65.         after = self.output.tell()
  66.         if after >= self.max_size:
  67.             self._rollback(before)
  68.             raise dns.exception.TooBig
  69.         after >= self.max_size
  70.         self.counts[QUESTION] += 1
  71.  
  72.     
  73.     def add_rrset(self, section, rrset, **kw):
  74.         self._set_section(section)
  75.         before = self.output.tell()
  76.         n = rrset.to_wire(self.output, self.compress, self.origin, **kw)
  77.         after = self.output.tell()
  78.         if after >= self.max_size:
  79.             self._rollback(before)
  80.             raise dns.exception.TooBig
  81.         after >= self.max_size
  82.         self.counts[section] += n
  83.  
  84.     
  85.     def add_rdataset(self, section, name, rdataset, **kw):
  86.         self._set_section(section)
  87.         before = self.output.tell()
  88.         n = rdataset.to_wire(name, self.output, self.compress, self.origin, **kw)
  89.         after = self.output.tell()
  90.         if after >= self.max_size:
  91.             self._rollback(before)
  92.             raise dns.exception.TooBig
  93.         after >= self.max_size
  94.         self.counts[section] += n
  95.  
  96.     
  97.     def add_edns(self, edns, ednsflags, payload):
  98.         self._set_section(ADDITIONAL)
  99.         before = self.output.tell()
  100.         self.output.write(struct.pack('!BHHIH', 0, dns.rdatatype.OPT, payload, ednsflags, 0))
  101.         after = self.output.tell()
  102.         if after >= self.max_size:
  103.             self._rollback(before)
  104.             raise dns.exception.TooBig
  105.         after >= self.max_size
  106.         self.counts[ADDITIONAL] += 1
  107.  
  108.     
  109.     def add_tsig(self, keyname, secret, fudge, id, tsig_error, other_data, request_mac):
  110.         self._set_section(ADDITIONAL)
  111.         before = self.output.tell()
  112.         s = self.output.getvalue()
  113.         (tsig_rdata, self.mac, ctx) = dns.tsig.hmac_md5(s, keyname, secret, int(time.time()), fudge, id, tsig_error, other_data, request_mac)
  114.         keyname.to_wire(self.output, self.compress, self.origin)
  115.         self.output.write(struct.pack('!HHIH', dns.rdatatype.TSIG, dns.rdataclass.ANY, 0, 0))
  116.         rdata_start = self.output.tell()
  117.         self.output.write(tsig_rdata)
  118.         after = self.output.tell()
  119.         if after >= self.max_size:
  120.             self._rollback(before)
  121.             raise dns.exception.TooBig
  122.         after >= self.max_size
  123.         self.output.seek(rdata_start - 2)
  124.         self.output.write(struct.pack('!H', after - rdata_start))
  125.         self.counts[ADDITIONAL] += 1
  126.         self.output.seek(10)
  127.         self.output.write(struct.pack('!H', self.counts[ADDITIONAL]))
  128.         self.output.seek(0, 2)
  129.  
  130.     
  131.     def write_header(self):
  132.         self.output.seek(0)
  133.         self.output.write(struct.pack('!HHHHHH', self.id, self.flags, self.counts[0], self.counts[1], self.counts[2], self.counts[3]))
  134.         self.output.seek(0, 2)
  135.  
  136.     
  137.     def get_wire(self):
  138.         return self.output.getvalue()
  139.  
  140.  
  141.