def b2a_qp(data, quotetabs=False, istext=True, header=False):
MAXLINESIZE = <NUM_LIT><EOL>lf = data.find('<STR_LIT:\n>')<EOL>crlf = lf > <NUM_LIT:0> and data[lf-<NUM_LIT:1>] == '<STR_LIT:\r>'<EOL>inp = <NUM_LIT:0><EOL>linelen = <NUM_LIT:0><EOL>odata = []<EOL>while inp < len(data):<EOL><INDENT>c = data[inp]<EOL>if (c > '<STR_LIT>' or<EOL>c == '<STR_LIT:=>' or<EOL>(header and c == '<STR_LIT:_>') or<EOL>(c == '<STR_LIT:.>' and linelen == <NUM_LIT:0> and (inp+<NUM_LIT:1> == len(data) or<EOL>data[inp+<NUM_LIT:1>] == '<STR_LIT:\n>' or<EOL>data[inp+<NUM_LIT:1>] == '<STR_LIT:\r>')) or<EOL>(not istext and (c == '<STR_LIT:\r>' or c == '<STR_LIT:\n>')) or<EOL>((c == '<STR_LIT:\t>' or c == '<STR_LIT:U+0020>') and (inp + <NUM_LIT:1> == len(data))) or<EOL>(c <= '<STR_LIT:U+0020>' and c != '<STR_LIT:\r>' and c != '<STR_LIT:\n>' and<EOL>(quotetabs or (not quotetabs and (c != '<STR_LIT:\t>' and c != '<STR_LIT:U+0020>'))))):<EOL><INDENT>linelen += <NUM_LIT:3><EOL>if linelen >= MAXLINESIZE:<EOL><INDENT>odata.append('<STR_LIT:=>')<EOL>if crlf: odata.append('<STR_LIT:\r>')<EOL>odata.append('<STR_LIT:\n>')<EOL>linelen = <NUM_LIT:3><EOL><DEDENT>odata.append('<STR_LIT:=>' + two_hex_digits(ord(c)))<EOL>inp += <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>if (istext and<EOL>(c == '<STR_LIT:\n>' or (inp+<NUM_LIT:1> < len(data) and c == '<STR_LIT:\r>' and<EOL>data[inp+<NUM_LIT:1>] == '<STR_LIT:\n>'))):<EOL><INDENT>linelen = <NUM_LIT:0><EOL>if (len(odata) > <NUM_LIT:0> and<EOL>(odata[-<NUM_LIT:1>] == '<STR_LIT:U+0020>' or odata[-<NUM_LIT:1>] == '<STR_LIT:\t>')):<EOL><INDENT>ch = ord(odata[-<NUM_LIT:1>])<EOL>odata[-<NUM_LIT:1>] = '<STR_LIT:=>'<EOL>odata.append(two_hex_digits(ch))<EOL><DEDENT>if crlf: odata.append('<STR_LIT:\r>')<EOL>odata.append('<STR_LIT:\n>')<EOL>if c == '<STR_LIT:\r>':<EOL><INDENT>inp += <NUM_LIT:2><EOL><DEDENT>else:<EOL><INDENT>inp += <NUM_LIT:1><EOL><DEDENT><DEDENT>else:<EOL><INDENT>if (inp + <NUM_LIT:1> < len(data) and<EOL>data[inp+<NUM_LIT:1>] != '<STR_LIT:\n>' and<EOL>(linelen + <NUM_LIT:1>) >= MAXLINESIZE):<EOL><INDENT>odata.append('<STR_LIT:=>')<EOL>if crlf: odata.append('<STR_LIT:\r>')<EOL>odata.append('<STR_LIT:\n>')<EOL>linelen = <NUM_LIT:0><EOL><DEDENT>linelen += <NUM_LIT:1><EOL>if header and c == '<STR_LIT:U+0020>':<EOL><INDENT>c = '<STR_LIT:_>'<EOL><DEDENT>odata.append(c)<EOL>inp += <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>return '<STR_LIT>'.join(odata)<EOL>
quotetabs=True means that tab and space characters are always
       quoted.
       istext=False means that \r and \n are treated as regular characters
       header=True encodes space characters with '_' and requires
       real '_' characters to be quoted.