def segment_range_to_fragment_range(segment_start, segment_end,
    segment_size, fragment_size):
    """
    Takes a byterange spanning some segments and converts that into a
    byterange spanning the corresponding fragments within their fragment
    archives.

    Handles prefix, suffix, and fully-specified byte ranges.

    :param segment_start: first byte of the first segment
    :param segment_end: last byte of the last segment
    :param segment_size: size of an EC segment, in bytes
    :param fragment_size: size of an EC fragment, in bytes

    :returns: a 2-tuple (frag_start, frag_end) where

      * frag_start is the first byte of the first fragment, or None if this
        is a suffix byte range

      * frag_end is the last byte of the last fragment, or None if this is a
        prefix byte range
    """
    fragment_start = (segment_start // segment_size * fragment_size if 
        segment_start is not None else None)
    fragment_end = None if segment_end is None else (segment_end + 1
        ) // segment_size * fragment_size if segment_start is None else (
        segment_end + 1) // segment_size * fragment_size - 1
    return fragment_start, fragment_end
