# Path: bdec/constraints.py
# class Equals(Constraint):
#     type = '!='
# 
#     def check(self, entry, value, context):
#         expected = self.limit.evaluate(context)
#         if isinstance(expected, int):
#             if int(value) !=  expected:
#                 raise ConstraintError(entry, int(value), '==', expected)
#         elif isinstance(expected, Data):
#             # We aren't checking the length here, only the value (for example,
#             # the expected value for a variable length binary field will have
#             # a different length than the actual). Only compare the bits that
#             # are valid.
#             length_diff = len(value) - len(expected)
#             if length_diff > 0:
#                 # The expected value is shorter than the actual, so grow it.
#                 expected = Data('\x00' * (length_diff / 8 + 1), 0, length_diff) + expected
#             elif length_diff < 0:
#                 # The expected value is longer than the actual, so shrink it.
#                 shorter = expected.copy()
#                 leading = shorter.pop(-length_diff)
#                 if not int(leading):
#                     # We can safely pop the leading bits.
#                     expected = shorter
#             if value !=  expected:
#                 raise ConstraintError(entry, value, '==', expected)
#         else:
#             if value !=  expected:
#                 raise ConstraintError(entry, value, '==', expected)
# 
#     def range(self):
#         limit = _limit(self.limit)
#         return Range(limit, limit)
# 
# class Maximum(Constraint):
#     type = '>'
# 
#     def check(self, entry, value, context):
#         expected = self.limit.evaluate(context)
#         if isinstance(value, basestring):
#             # It is useful to check the bounds of a text character...
#             value = ord(value)
#         if int(value) > expected:
#             raise ConstraintError(entry, int(value), '<=', expected)
# 
#     def range(self):
#         return Range(None, _limit(self.limit))
# 
# class Minimum(Constraint):
#     type = '<'
# 
#     def check(self, entry, value, context):
#         expected = self.limit.evaluate(context)
#         if isinstance(value, basestring):
#             # It is useful to check the bounds of a text character...
#             value = ord(value)
#         if int(value) < expected:
#             raise ConstraintError(entry, int(value), '>=', expected)
# 
#     def range(self):
#         return Range(_limit(self.limit), None)
# 
# Path: bdec/inspect/param.py
# class UnknownReferenceError(BadReferenceError):
#     def __init__(self, entry, name, entries=[]):
#         BadReferenceError.__init__(self, entry, entries)
#         self.name = name
# 
#     def __str__(self):
#         return "%s references unknown entry '%s'!" % (self.entry, self.name)


import operator
import bdec.choice as chc
import bdec.data as dt
import bdec.entry as ent
import bdec.expression as expr
import bdec.field as fld
import bdec.sequence as seq
import bdec.sequenceof as sof
from bdec.constraints import Equals, Maximum, Minimum
from bdec.inspect.param import UnknownReferenceError
from collections import defaultdict
            options = []
            for i in range(min, max + 1):
                options.append(fld.Field(entry.name, entry.length,
                    format=fld.Field.INTEGER, constraints=[Equals(i)]))
            self.entry = chc.Choice('mock %s' % entry.name, options)
        else:
            self.entry = entry
        self._parent = parent
        self._parent_offset = parent_offset

        # Walk the parent chain to ensure we're not in a cyclic loop
        while parent is not None:
            if parent.entry is entry:
                # We've found a cyclic loop. Don't both returning any more
                # data; just say it's unknown.
                self.data = _UnknownData()
                break
            parent = parent._parent
        else:
            self.data = self._create_data()

    def _next(self, offset):
        """Walk up the tree looking for the next child.
        
        Takes the child offset to walk into. """
        if isinstance(self.entry, seq.Sequence):
            if offset < len(self.entry.children):
                # There are still child items in this sequence, so return the
                # next child.
                return [_ProtocolStream(self.entry.children[offset].entry, self, offset)]

# Path: bdec/constraints.py
# class Equals(Constraint):
#     type = '!='
# 
#     def check(self, entry, value, context):
#         expected = self.limit.evaluate(context)
#         if isinstance(expected, int):
#             if int(value) !=  expected:
#                 raise ConstraintError(entry, int(value), '==', expected)
#         elif isinstance(expected, Data):
#             # We aren't checking the length here, only the value (for example,
#             # the expected value for a variable length binary field will have
#             # a different length than the actual). Only compare the bits that
#             # are valid.
#             length_diff = len(value) - len(expected)
#             if length_diff > 0:
#                 # The expected value is shorter than the actual, so grow it.
#                 expected = Data('\x00' * (length_diff / 8 + 1), 0, length_diff) + expected
#             elif length_diff < 0:
#                 # The expected value is longer than the actual, so shrink it.
#                 shorter = expected.copy()
#                 leading = shorter.pop(-length_diff)
#                 if not int(leading):
#                     # We can safely pop the leading bits.
#                     expected = shorter
#             if value !=  expected:
#                 raise ConstraintError(entry, value, '==', expected)
#         else:
#             if value !=  expected:
#                 raise ConstraintError(entry, value, '==', expected)
# 
#     def range(self):
#         limit = _limit(self.limit)
#         return Range(limit, limit)
# 
# class Maximum(Constraint):
#     type = '>'
# 
#     def check(self, entry, value, context):
#         expected = self.limit.evaluate(context)
#         if isinstance(value, basestring):
#             # It is useful to check the bounds of a text character...
#             value = ord(value)
#         if int(value) > expected:
#             raise ConstraintError(entry, int(value), '<=', expected)
# 
#     def range(self):
#         return Range(None, _limit(self.limit))
# 
# class Minimum(Constraint):
#     type = '<'
# 
#     def check(self, entry, value, context):
#         expected = self.limit.evaluate(context)
#         if isinstance(value, basestring):
#             # It is useful to check the bounds of a text character...
#             value = ord(value)
#         if int(value) < expected:
#             raise ConstraintError(entry, int(value), '>=', expected)
# 
#     def range(self):
#         return Range(_limit(self.limit), None)
# 
# Path: bdec/inspect/param.py
# class UnknownReferenceError(BadReferenceError):
#     def __init__(self, entry, name, entries=[]):
#         BadReferenceError.__init__(self, entry, entries)
#         self.name = name
# 
#     def __str__(self):
#         return "%s references unknown entry '%s'!" % (self.entry, self.name)


# Path: bdec/inspect/chooser.py
import operator
import bdec.choice as chc
import bdec.data as dt
import bdec.entry as ent
import bdec.expression as expr
import bdec.field as fld
import bdec.sequence as seq
import bdec.sequenceof as sof
from bdec.constraints import Equals, Maximum, Minimum
from bdec.inspect.param import UnknownReferenceError
from collections import defaultdict

            options = []
            for i in range(min, max + 1):
                options.append(fld.Field(entry.name, entry.length,
                    format=fld.Field.INTEGER, constraints=[Equals(i)]))
            self.entry = chc.Choice('mock %s' % entry.name, options)
        else:
            self.entry = entry
        self._parent = parent
        self._parent_offset = parent_offset

        # Walk the parent chain to ensure we're not in a cyclic loop
        while parent is not None:
            if parent.entry is entry:
                # We've found a cyclic loop. Don't both returning any more
                # data; just say it's unknown.
                self.data = _UnknownData()
                break
            parent = parent._parent
        else:
            self.data = self._create_data()

    def _next(self, offset):
        """Walk up the tree looking for the next child.
        
        Takes the child offset to walk into. """
        if isinstance(self.entry, seq.Sequence):
            if offset < len(self.entry.children):
                # There are still child items in this sequence, so return the
                # next child.
                return [_ProtocolStream(self.entry.children[offset].entry, self, offset)]

            assert offset == len(self.entry.children)