def parse_data_url(data_url):
    """
    Parses a data URL and returns its components.
    Data URLs are defined as follows::
       dataurl    := "data:" [ mediatype ] [ ";base64" ] "," data
       mediatype  := [ type "/" subtype ] *( ";" parameter )
       data       := *urlchar
       parameter  := attribute "=" value
    This specific implementation is limited to data urls of the following
    format::
        dataurl := "data:" type "/" subtype ";base64" "," data
    Here an example::
      'data:image/jpg;base64,/9j/4SXNRXhpZg...AUAFFABRQB//Z'
    References:
    - http://tools.ietf.org/html/rfc2397
    - http://www.askapache.com/online-tools/base64-image-converter/
    :param str data_url: A data URL.
    :return: Components of the data URL,
             e.g. ('image', 'png', 'base64', '/9j/4S')
    :rtype: tuple (datatype, subtype, encoding, data)
    :raises: ValueError if data URL doesn't follow spec.
    """
    if not data_url.startswith('data:'):
        raise ValueError("Not a data URL: " + data_url[:40])
    try:
        header, data = data_url.split(',')
        header = header.replace('data:', '')
        mediatype, encoding = header.rsplit(';', 1)
        datatype, subtype = mediatype.split('/')
    except BaseException:
        raise ValueError("Data URL not in correct format: " + data_url[:40])
    return datatype, subtype, encoding, data