# Path: xclib/utf8.py
# def unutf8(u, opts='strict'):
#     if opts == 'illegal':
#         try:
#             return u.decode('utf-8', 'strict')
#         except UnicodeError:
#             dec = u.decode('utf-8', 'ignore')
#             logging.error('Illegal UTF-8 sequence: %r' % dec)
#             sys.stderr.write('Illegal UTF-8 sequence: %r\n' % dec)
#             traceback.print_exc()
#             return 'illegal-utf8-sequence-' + dec
#     else:
#         return u.decode('utf-8', opts)


import sys
import logging
from struct import pack, unpack
from xclib.utf8 import unutf8

# Message formats described in `../doc/Protocol.md`

class saslauthd_io:
    @classmethod
    def read_request(cls, infd, outfd):
        try:
            infd = infd.buffer
        except AttributeError:
            pass
        field_no = 0
        fields = [None, None, None, None]
        length_field = infd.read(2)
        while len(length_field) == 2:
            (size,) = unpack('>H', length_field)
            val = infd.read(size)
            if len(val) != size:
               logging.warn('premature EOF while reading field %d: %d != %d' % (field_no, len(val), size))
               return
            fields[field_no] = val
            field_no = (field_no + 1) % 4
            if field_no == 0:
                logging.debug('from_saslauthd got %r, %r, %r, %r' % tuple(fields))

# Path: xclib/utf8.py
# def unutf8(u, opts='strict'):
#     if opts == 'illegal':
#         try:
#             return u.decode('utf-8', 'strict')
#         except UnicodeError:
#             dec = u.decode('utf-8', 'ignore')
#             logging.error('Illegal UTF-8 sequence: %r' % dec)
#             sys.stderr.write('Illegal UTF-8 sequence: %r\n' % dec)
#             traceback.print_exc()
#             return 'illegal-utf8-sequence-' + dec
#     else:
#         return u.decode('utf-8', opts)


# Path: xclib/saslauthd_io.py
import sys
import logging
from struct import pack, unpack
from xclib.utf8 import unutf8


# Message formats described in `../doc/Protocol.md`

class saslauthd_io:
    @classmethod
    def read_request(cls, infd, outfd):
        try:
            infd = infd.buffer
        except AttributeError:
            pass
        field_no = 0
        fields = [None, None, None, None]
        length_field = infd.read(2)
        while len(length_field) == 2:
            (size,) = unpack('>H', length_field)
            val = infd.read(size)
            if len(val) != size:
               logging.warn('premature EOF while reading field %d: %d != %d' % (field_no, len(val), size))
               return
            fields[field_no] = val
            field_no = (field_no + 1) % 4
            if field_no == 0:
                logging.debug('from_saslauthd got %r, %r, %r, %r' % tuple(fields))

                yield ('auth', unutf8(fields[0], 'illegal'), unutf8(fields[3], 'illegal'), unutf8(fields[1], 'illegal'))