def decode(input, output, header = <NUM_LIT:0>):
if a2b_qp is not None:<EOL><INDENT>data = input.read()<EOL>odata = a2b_qp(data, header = header)<EOL>output.write(odata)<EOL>return<EOL><DEDENT>new = '<STR_LIT>'<EOL>while <NUM_LIT:1>:<EOL><INDENT>line = input.readline()<EOL>if not line: break<EOL>i, n = <NUM_LIT:0>, len(line)<EOL>if n > <NUM_LIT:0> and line[n-<NUM_LIT:1>] == '<STR_LIT:\n>':<EOL><INDENT>partial = <NUM_LIT:0>; n = n-<NUM_LIT:1><EOL>while n > <NUM_LIT:0> and line[n-<NUM_LIT:1>] in "<STR_LIT>":<EOL><INDENT>n = n-<NUM_LIT:1><EOL><DEDENT><DEDENT>else:<EOL><INDENT>partial = <NUM_LIT:1><EOL><DEDENT>while i < n:<EOL><INDENT>c = line[i]<EOL>if c == '<STR_LIT:_>' and header:<EOL><INDENT>new = new + '<STR_LIT:U+0020>'; i = i+<NUM_LIT:1><EOL><DEDENT>elif c != ESCAPE:<EOL><INDENT>new = new + c; i = i+<NUM_LIT:1><EOL><DEDENT>elif i+<NUM_LIT:1> == n and not partial:<EOL><INDENT>partial = <NUM_LIT:1>; break<EOL><DEDENT>elif i+<NUM_LIT:1> < n and line[i+<NUM_LIT:1>] == ESCAPE:<EOL><INDENT>new = new + ESCAPE; i = i+<NUM_LIT:2><EOL><DEDENT>elif i+<NUM_LIT:2> < n and ishex(line[i+<NUM_LIT:1>]) and ishex(line[i+<NUM_LIT:2>]):<EOL><INDENT>new = new + chr(unhex(line[i+<NUM_LIT:1>:i+<NUM_LIT:3>])); i = i+<NUM_LIT:3><EOL><DEDENT>else: <EOL><INDENT>new = new + c; i = i+<NUM_LIT:1><EOL><DEDENT><DEDENT>if not partial:<EOL><INDENT>output.write(new + '<STR_LIT:\n>')<EOL>new = '<STR_LIT>'<EOL><DEDENT><DEDENT>if new:<EOL><INDENT>output.write(new)<EOL><DEDENT>
Read 'input', apply quoted-printable decoding, and write to 'output'.
    'input' and 'output' are files with readline() and write() methods.
    If 'header' is true, decode underscore as space (per RFC 1522).