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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import cStringIO
  5. import struct
  6. import sys
  7. if sys.hexversion >= 33751040:
  8.     import encodings.idna as encodings
  9.  
  10. import dns.exception as dns
  11. NAMERELN_NONE = 0
  12. NAMERELN_SUPERDOMAIN = 1
  13. NAMERELN_SUBDOMAIN = 2
  14. NAMERELN_EQUAL = 3
  15. NAMERELN_COMMONANCESTOR = 4
  16.  
  17. class EmptyLabel(dns.exception.SyntaxError):
  18.     pass
  19.  
  20.  
  21. class BadEscape(dns.exception.SyntaxError):
  22.     pass
  23.  
  24.  
  25. class BadPointer(dns.exception.FormError):
  26.     pass
  27.  
  28.  
  29. class BadLabelType(dns.exception.FormError):
  30.     pass
  31.  
  32.  
  33. class NeedAbsoluteNameOrOrigin(dns.exception.DNSException):
  34.     pass
  35.  
  36.  
  37. class NameTooLong(dns.exception.FormError):
  38.     pass
  39.  
  40.  
  41. class LabelTooLong(dns.exception.SyntaxError):
  42.     pass
  43.  
  44.  
  45. class AbsoluteConcatenation(dns.exception.DNSException):
  46.     pass
  47.  
  48.  
  49. class NoParent(dns.exception.DNSException):
  50.     pass
  51.  
  52. _escaped = {
  53.     '"': True,
  54.     '(': True,
  55.     ')': True,
  56.     '.': True,
  57.     ';': True,
  58.     '\\': True,
  59.     '@': True,
  60.     '$': True }
  61.  
  62. def _escapify(label):
  63.     text = ''
  64.     for c in label:
  65.         if c in _escaped:
  66.             text += '\\' + c
  67.             continue
  68.         if ord(c) > 32 and ord(c) < 127:
  69.             text += c
  70.             continue
  71.         text += '\\%03d' % ord(c)
  72.     
  73.     return text
  74.  
  75.  
  76. def _validate_labels(labels):
  77.     l = len(labels)
  78.     total = 0
  79.     i = -1
  80.     j = 0
  81.     for label in labels:
  82.         ll = len(label)
  83.         total += ll + 1
  84.         if ll > 63:
  85.             raise LabelTooLong
  86.         ll > 63
  87.         if i < 0 and label == '':
  88.             i = j
  89.         
  90.         j += 1
  91.     
  92.     if total > 255:
  93.         raise NameTooLong
  94.     total > 255
  95.     if i >= 0 and i != l - 1:
  96.         raise EmptyLabel
  97.     i != l - 1
  98.  
  99.  
  100. class Name(object):
  101.     __slots__ = [
  102.         'labels']
  103.     
  104.     def __init__(self, labels):
  105.         super(Name, self).__setattr__('labels', tuple(labels))
  106.         _validate_labels(self.labels)
  107.  
  108.     
  109.     def __setattr__(self, name, value):
  110.         raise TypeError, "object doesn't support attribute assignment"
  111.  
  112.     
  113.     def is_absolute(self):
  114.         if len(self.labels) > 0:
  115.             pass
  116.         return self.labels[-1] == ''
  117.  
  118.     
  119.     def is_wild(self):
  120.         if len(self.labels) > 0:
  121.             pass
  122.         return self.labels[0] == '*'
  123.  
  124.     
  125.     def __hash__(self):
  126.         h = 0x0L
  127.         for label in self.labels:
  128.             for c in label:
  129.                 h += (h << 3) + ord(c.lower())
  130.             
  131.         
  132.         return int(h % sys.maxint)
  133.  
  134.     
  135.     def fullcompare(self, other):
  136.         sabs = self.is_absolute()
  137.         oabs = other.is_absolute()
  138.         if sabs != oabs:
  139.             if sabs:
  140.                 return (NAMERELN_NONE, 1, 0)
  141.             return (NAMERELN_NONE, -1, 0)
  142.         sabs != oabs
  143.         l1 = len(self.labels)
  144.         l2 = len(other.labels)
  145.         ldiff = l1 - l2
  146.         if ldiff < 0:
  147.             l = l1
  148.         else:
  149.             l = l2
  150.         order = 0
  151.         nlabels = 0
  152.         namereln = NAMERELN_NONE
  153.         while l > 0:
  154.             l -= 1
  155.             l1 -= 1
  156.             l2 -= 1
  157.             label1 = self.labels[l1].lower()
  158.             label2 = other.labels[l2].lower()
  159.             if label1 < label2:
  160.                 order = -1
  161.                 if nlabels > 0:
  162.                     namereln = NAMERELN_COMMONANCESTOR
  163.                 
  164.                 return (namereln, order, nlabels)
  165.             if label1 > label2:
  166.                 order = 1
  167.                 if nlabels > 0:
  168.                     namereln = NAMERELN_COMMONANCESTOR
  169.                 
  170.                 return (namereln, order, nlabels)
  171.             nlabels += 1
  172.             continue
  173.             label1 > label2
  174.         order = ldiff
  175.         if ldiff < 0:
  176.             namereln = NAMERELN_SUPERDOMAIN
  177.         elif ldiff > 0:
  178.             namereln = NAMERELN_SUBDOMAIN
  179.         else:
  180.             namereln = NAMERELN_EQUAL
  181.         return (namereln, order, nlabels)
  182.  
  183.     
  184.     def is_subdomain(self, other):
  185.         (nr, o, nl) = self.fullcompare(other)
  186.         if nr == NAMERELN_SUBDOMAIN or nr == NAMERELN_EQUAL:
  187.             return True
  188.         return False
  189.  
  190.     
  191.     def is_superdomain(self, other):
  192.         (nr, o, nl) = self.fullcompare(other)
  193.         if nr == NAMERELN_SUPERDOMAIN or nr == NAMERELN_EQUAL:
  194.             return True
  195.         return False
  196.  
  197.     
  198.     def canonicalize(self):
  199.         return []([ x.lower() for x in self.labels ])
  200.  
  201.     
  202.     def __eq__(self, other):
  203.         if isinstance(other, Name):
  204.             return self.fullcompare(other)[1] == 0
  205.         return False
  206.  
  207.     
  208.     def __ne__(self, other):
  209.         if isinstance(other, Name):
  210.             return self.fullcompare(other)[1] != 0
  211.         return True
  212.  
  213.     
  214.     def __lt__(self, other):
  215.         if isinstance(other, Name):
  216.             return self.fullcompare(other)[1] < 0
  217.         return NotImplemented
  218.  
  219.     
  220.     def __le__(self, other):
  221.         if isinstance(other, Name):
  222.             return self.fullcompare(other)[1] <= 0
  223.         return NotImplemented
  224.  
  225.     
  226.     def __ge__(self, other):
  227.         if isinstance(other, Name):
  228.             return self.fullcompare(other)[1] >= 0
  229.         return NotImplemented
  230.  
  231.     
  232.     def __gt__(self, other):
  233.         if isinstance(other, Name):
  234.             return self.fullcompare(other)[1] > 0
  235.         return NotImplemented
  236.  
  237.     
  238.     def __repr__(self):
  239.         return '<DNS name ' + self.__str__() + '>'
  240.  
  241.     
  242.     def __str__(self):
  243.         return self.to_text(False)
  244.  
  245.     
  246.     def to_text(self, omit_final_dot = False):
  247.         if len(self.labels) == 0:
  248.             return '@'
  249.         if len(self.labels) == 1 and self.labels[0] == '':
  250.             return '.'
  251.         s = '.'.join(map(_escapify, l))
  252.         return s
  253.  
  254.     
  255.     def to_unicode(self, omit_final_dot = False):
  256.         if len(self.labels) == 0:
  257.             return u'@'
  258.         if len(self.labels) == 1 and self.labels[0] == '':
  259.             return u'.'
  260.         s = []([ encodings.idna.ToUnicode(_escapify(x)) for x in l ])
  261.         return s
  262.  
  263.     
  264.     def to_digestable(self, origin = None):
  265.         if not self.is_absolute():
  266.             if origin is None or not origin.is_absolute():
  267.                 raise NeedAbsoluteNameOrOrigin
  268.             not origin.is_absolute()
  269.             labels = list(self.labels)
  270.             labels.extend(list(origin.labels))
  271.         else:
  272.             labels = self.labels
  273.         dlabels = [ '%s%s' % (chr(len(x)), x.lower()) for x in labels ]
  274.         return ''.join(dlabels)
  275.  
  276.     
  277.     def to_wire(self, file = None, compress = None, origin = None):
  278.         if file is None:
  279.             file = cStringIO.StringIO()
  280.             want_return = True
  281.         else:
  282.             want_return = False
  283.         if not self.is_absolute():
  284.             if origin is None or not origin.is_absolute():
  285.                 raise NeedAbsoluteNameOrOrigin
  286.             not origin.is_absolute()
  287.             labels = list(self.labels)
  288.             labels.extend(list(origin.labels))
  289.         else:
  290.             labels = self.labels
  291.         i = 0
  292.         for label in labels:
  293.             n = Name(labels[i:])
  294.             i += 1
  295.             if compress is not None:
  296.                 pos = compress.get(n)
  297.             else:
  298.                 pos = None
  299.             if pos is not None:
  300.                 value = 49152 + pos
  301.                 s = struct.pack('!H', value)
  302.                 file.write(s)
  303.                 break
  304.                 continue
  305.             if compress is not None and len(n) > 1:
  306.                 pos = file.tell()
  307.                 if pos < 49152:
  308.                     compress[n] = pos
  309.                 
  310.             
  311.             l = len(label)
  312.             file.write(chr(l))
  313.             if l > 0:
  314.                 file.write(label)
  315.                 continue
  316.         
  317.         if want_return:
  318.             return file.getvalue()
  319.  
  320.     
  321.     def __len__(self):
  322.         return len(self.labels)
  323.  
  324.     
  325.     def __getitem__(self, index):
  326.         return self.labels[index]
  327.  
  328.     
  329.     def __getslice__(self, start, stop):
  330.         return self.labels[start:stop]
  331.  
  332.     
  333.     def __add__(self, other):
  334.         return self.concatenate(other)
  335.  
  336.     
  337.     def __sub__(self, other):
  338.         return self.relativize(other)
  339.  
  340.     
  341.     def split(self, depth):
  342.         l = len(self.labels)
  343.         if depth == 0:
  344.             return (self, dns.name.empty)
  345.         if depth == l:
  346.             return (dns.name.empty, self)
  347.         if depth < 0 or depth > l:
  348.             raise ValueError, 'depth must be >= 0 and <= the length of the name'
  349.         depth > l
  350.         return (Name(self[:-depth]), Name(self[-depth:]))
  351.  
  352.     
  353.     def concatenate(self, other):
  354.         if self.is_absolute() and len(other) > 0:
  355.             raise AbsoluteConcatenation
  356.         len(other) > 0
  357.         labels = list(self.labels)
  358.         labels.extend(list(other.labels))
  359.         return Name(labels)
  360.  
  361.     
  362.     def relativize(self, origin):
  363.         if origin is not None and self.is_subdomain(origin):
  364.             return Name(self[:-len(origin)])
  365.         return self
  366.  
  367.     
  368.     def derelativize(self, origin):
  369.         if not self.is_absolute():
  370.             return self.concatenate(origin)
  371.         return self
  372.  
  373.     
  374.     def choose_relativity(self, origin = None, relativize = True):
  375.         if origin:
  376.             if relativize:
  377.                 return self.relativize(origin)
  378.             return self.derelativize(origin)
  379.         origin
  380.         return self
  381.  
  382.     
  383.     def parent(self):
  384.         if self == root or self == empty:
  385.             raise NoParent
  386.         self == empty
  387.         return Name(self.labels[1:])
  388.  
  389.  
  390. root = Name([
  391.     ''])
  392. empty = Name([])
  393.  
  394. def from_unicode(text, origin = root):
  395.     if not isinstance(text, unicode):
  396.         raise ValueError, 'input to from_unicode() must be a unicode string'
  397.     isinstance(text, unicode)
  398.     if not origin is None or isinstance(origin, Name):
  399.         raise ValueError, 'origin must be a Name or None'
  400.     isinstance(origin, Name)
  401.     labels = []
  402.     label = u''
  403.     escaping = False
  404.     edigits = 0
  405.     total = 0
  406.     if text == u'@':
  407.         text = u''
  408.     
  409.     if text:
  410.         if text == u'.':
  411.             return Name([
  412.                 ''])
  413.         for c in text:
  414.             if escaping:
  415.                 if edigits == 0:
  416.                     if c.isdigit():
  417.                         total = int(c)
  418.                         edigits += 1
  419.                     else:
  420.                         label += c
  421.                         escaping = False
  422.                 elif not c.isdigit():
  423.                     raise BadEscape
  424.                 
  425.                 total *= 10
  426.                 total += int(c)
  427.                 edigits += 1
  428.                 if edigits == 3:
  429.                     escaping = False
  430.                     label += chr(total)
  431.                 
  432.             edigits == 3
  433.             if c == u'.' and c == u'πÇé' and c == u'∩╝Ä' or c == u'∩╜í':
  434.                 if len(label) == 0:
  435.                     raise EmptyLabel
  436.                 len(label) == 0
  437.                 labels.append(encodings.idna.ToASCII(label))
  438.                 label = u''
  439.                 continue
  440.             if c == u'\\':
  441.                 escaping = True
  442.                 edigits = 0
  443.                 total = 0
  444.                 continue
  445.             label += c
  446.         
  447.         if escaping:
  448.             raise BadEscape
  449.         escaping
  450.         if len(label) > 0:
  451.             labels.append(encodings.idna.ToASCII(label))
  452.         else:
  453.             labels.append('')
  454.     
  455.     if (len(labels) == 0 or labels[-1] != '') and origin is not None:
  456.         labels.extend(list(origin.labels))
  457.     
  458.     return Name(labels)
  459.  
  460.  
  461. def from_text(text, origin = root):
  462.     if not isinstance(text, str):
  463.         if isinstance(text, unicode) and sys.hexversion >= 33751040:
  464.             return from_unicode(text, origin)
  465.         raise ValueError, 'input to from_text() must be a string'
  466.     isinstance(text, str)
  467.     if not origin is None or isinstance(origin, Name):
  468.         raise ValueError, 'origin must be a Name or None'
  469.     isinstance(origin, Name)
  470.     labels = []
  471.     label = ''
  472.     escaping = False
  473.     edigits = 0
  474.     total = 0
  475.     if text == '@':
  476.         text = ''
  477.     
  478.     if text:
  479.         if text == '.':
  480.             return Name([
  481.                 ''])
  482.         for c in text:
  483.             if escaping:
  484.                 if edigits == 0:
  485.                     if c.isdigit():
  486.                         total = int(c)
  487.                         edigits += 1
  488.                     else:
  489.                         label += c
  490.                         escaping = False
  491.                 elif not c.isdigit():
  492.                     raise BadEscape
  493.                 
  494.                 total *= 10
  495.                 total += int(c)
  496.                 edigits += 1
  497.                 if edigits == 3:
  498.                     escaping = False
  499.                     label += chr(total)
  500.                 
  501.             edigits == 3
  502.             if c == '.':
  503.                 if len(label) == 0:
  504.                     raise EmptyLabel
  505.                 len(label) == 0
  506.                 labels.append(label)
  507.                 label = ''
  508.                 continue
  509.             if c == '\\':
  510.                 escaping = True
  511.                 edigits = 0
  512.                 total = 0
  513.                 continue
  514.             label += c
  515.         
  516.         if escaping:
  517.             raise BadEscape
  518.         escaping
  519.         if len(label) > 0:
  520.             labels.append(label)
  521.         else:
  522.             labels.append('')
  523.     
  524.     if (len(labels) == 0 or labels[-1] != '') and origin is not None:
  525.         labels.extend(list(origin.labels))
  526.     
  527.     return Name(labels)
  528.  
  529.  
  530. def from_wire(message, current):
  531.     if not isinstance(message, str):
  532.         raise ValueError, 'input to from_wire() must be a byte string'
  533.     isinstance(message, str)
  534.     labels = []
  535.     biggest_pointer = current
  536.     hops = 0
  537.     count = ord(message[current])
  538.     current += 1
  539.     cused = 1
  540.     while count != 0:
  541.         if count < 64:
  542.             labels.append(message[current:current + count])
  543.             current += count
  544.             if hops == 0:
  545.                 cused += count
  546.             
  547.         elif count >= 192:
  548.             current = (count & 63) * 256 + ord(message[current])
  549.             if hops == 0:
  550.                 cused += 1
  551.             
  552.             if current >= biggest_pointer:
  553.                 raise BadPointer
  554.             current >= biggest_pointer
  555.             biggest_pointer = current
  556.             hops += 1
  557.         else:
  558.             raise BadLabelType
  559.         count = count < 64(message[current])
  560.         current += 1
  561.         if hops == 0:
  562.             cused += 1
  563.             continue
  564.     labels.append('')
  565.     return (Name(labels), cused)
  566.  
  567.