void* CMSEXPORT cmsReadTag(cmsHPROFILE hProfile, cmsTagSignature sig)
{
    _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
    cmsIOHANDLER* io = Icc ->IOhandler;
    cmsTagTypeHandler* TypeHandler;
    cmsTagDescriptor*  TagDescriptor;
    cmsTagTypeSignature BaseType;
    cmsUInt32Number Offset, TagSize;
    cmsUInt32Number ElemCount;
    int n;

	n = _cmsSearchTag(Icc, sig, TRUE);
	if (n < 0) return NULL;                 // Not found, return NULL


	// If the element is already in memory, return the pointer
	if (Icc -> TagPtrs[n]) {

		if (Icc ->TagSaveAsRaw[n]) return NULL;  // We don't support read raw tags as cooked
		return Icc -> TagPtrs[n];
	}

	// We need to read it. Get the offset and size to the file
    Offset    = Icc -> TagOffsets[n];
    TagSize   = Icc -> TagSizes[n];

    // Seek to its location
    if (!io -> Seek(io, Offset))
            return NULL;

    // Search for support on this tag
    TagDescriptor = _cmsGetTagDescriptor(sig);
    if (TagDescriptor == NULL) return NULL;     // Unsupported.

    // if supported, get type and check if in list
    BaseType = _cmsReadTypeBase(io);
    if (BaseType == 0) return NULL;

    if (!IsTypeSupported(TagDescriptor, BaseType)) return NULL;

    TagSize  -= 8;                      // Alredy read by the type base logic

    // Get type handler
    TypeHandler = _cmsGetTagTypeHandler(BaseType);
    if (TypeHandler == NULL) return NULL;


    // Read the tag
    Icc -> TagTypeHandlers[n] = TypeHandler;

    TypeHandler ->ContextID = Icc ->ContextID;
    TypeHandler ->ICCVersion = Icc ->Version;
    Icc -> TagPtrs[n] = TypeHandler ->ReadPtr(TypeHandler, io, &ElemCount, TagSize);

    // The tag type is supported, but something wrong happend and we cannot read the tag.
    // let know the user about this (although it is just a warning)
    if (Icc -> TagPtrs[n] == NULL) {

        char String[5];

        _cmsTagSignature2String(String, sig);
        cmsSignalError(Icc ->ContextID, cmsERROR_CORRUPTION_DETECTED, "Corrupted tag '%s'", String);
        return NULL;
    }

    // This is a weird error that may be a symptom of something more serious, the number of
    // stored item is actually less than the number of required elements.
    if (ElemCount < TagDescriptor ->ElemCount) {

        char String[5];

        _cmsTagSignature2String(String, sig);
        cmsSignalError(Icc ->ContextID, cmsERROR_CORRUPTION_DETECTED, "'%s' Inconsistent number of items: expected %d, got %d",
                                                             String, TagDescriptor ->ElemCount, ElemCount);
    }


    // Return the data
    return Icc -> TagPtrs[n];
}
void* CMSEXPORT cmsReadTag(cmsHPROFILE hProfile, cmsTagSignature sig)
{
    _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
    cmsIOHANDLER* io = Icc ->IOhandler;
    cmsTagTypeHandler* TypeHandler;
    cmsTagDescriptor*  TagDescriptor;
    cmsTagTypeSignature BaseType;
    cmsUInt32Number Offset, TagSize;
    cmsUInt32Number ElemCount;
    int n;

	n = _cmsSearchTag(Icc, sig, TRUE);
	if (n < 0) return NULL;                  


	 
	if (Icc -> TagPtrs[n]) {

		if (Icc ->TagSaveAsRaw[n]) return NULL;   
		return Icc -> TagPtrs[n];
	}

	 
    Offset    = Icc -> TagOffsets[n];
    TagSize   = Icc -> TagSizes[n];

     
    if (!io -> Seek(io, Offset))
            return NULL;

     
    TagDescriptor = _cmsGetTagDescriptor(sig);
    if (TagDescriptor == NULL) return NULL;      

     
    BaseType = _cmsReadTypeBase(io);
    if (BaseType == 0) return NULL;

    if (!IsTypeSupported(TagDescriptor, BaseType)) return NULL;

    TagSize  -= 8;                       

     
    TypeHandler = _cmsGetTagTypeHandler(BaseType);
    if (TypeHandler == NULL) return NULL;


     
    Icc -> TagTypeHandlers[n] = TypeHandler;

    TypeHandler ->ContextID = Icc ->ContextID;
    TypeHandler ->ICCVersion = Icc ->Version;
    Icc -> TagPtrs[n] = TypeHandler ->ReadPtr(TypeHandler, io, &ElemCount, TagSize);

     
     
    if (Icc -> TagPtrs[n] == NULL) {

        char String[5];

        _cmsTagSignature2String(String, sig);
        cmsSignalError(Icc ->ContextID, cmsERROR_CORRUPTION_DETECTED, "Corrupted tag '%s'", String);
        return NULL;
    }

     
     
    if (ElemCount < TagDescriptor ->ElemCount) {

        char String[5];

        _cmsTagSignature2String(String, sig);
        cmsSignalError(Icc ->ContextID, cmsERROR_CORRUPTION_DETECTED, "'%s' Inconsistent number of items: expected %d, got %d",
                                                             String, TagDescriptor ->ElemCount, ElemCount);
    }


     
    return Icc -> TagPtrs[n];
}