def shorten_lines(lines, maxlen):
    """Generate a list of shared prefixes and a map of long string -> encoded string."""
    prefixes = []
    mapping = {}
    for line in sorted(lines, key=lambda line: -len(line)):
        if len(line) <= maxlen:
            break
        for i, prefix in enumerate(prefixes):
            if line.startswith(prefix):
                break
        else:
            i = len(prefixes)
            prefixcode = '\x00%i\x00' % i
            prefix = line[:-(maxlen - len(prefixcode))]
            prefixes.append(prefix)
        mapping[line] = '\x00%i\x00%s' % (i, line[len(prefix):])
    return prefixes, mapping
