cmsUInt32Number CMSEXPORT cmsSaveProfileToIOhandler(cmsHPROFILE hProfile, cmsIOHANDLER* io)
{
    _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
    _cmsICCPROFILE Keep;
    cmsIOHANDLER* PrevIO;
    cmsUInt32Number UsedSpace;
    cmsContext ContextID;

    memmove(&Keep, Icc, sizeof(_cmsICCPROFILE));

    ContextID = cmsGetProfileContextID(hProfile);
    PrevIO = Icc ->IOhandler = cmsOpenIOhandlerFromNULL(ContextID);
    if (PrevIO == NULL) return 0;

    // Pass #1 does compute offsets

    if (!_cmsWriteHeader(Icc, 0)) return 0;
    if (!SaveTags(Icc, &Keep)) return 0;

    UsedSpace = PrevIO ->UsedSpace;

    // Pass #2 does save to iohandler

    if (io != NULL) {
        Icc ->IOhandler = io;
        if (!SetLinks(Icc)) goto CleanUp;
        if (!_cmsWriteHeader(Icc, UsedSpace)) goto CleanUp;
        if (!SaveTags(Icc, &Keep)) goto CleanUp;
    }

    memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
    if (!cmsCloseIOhandler(PrevIO)) return 0;

    return UsedSpace;


CleanUp:
    cmsCloseIOhandler(PrevIO);
    memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
    return 0;
}
cmsUInt32Number CMSEXPORT cmsSaveProfileToIOhandler(cmsHPROFILE hProfile, cmsIOHANDLER* io)
{
    _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile;
    _cmsICCPROFILE Keep;
    cmsIOHANDLER* PrevIO;
    cmsUInt32Number UsedSpace;
    cmsContext ContextID;

    memmove(&Keep, Icc, sizeof(_cmsICCPROFILE));

    ContextID = cmsGetProfileContextID(hProfile);
    PrevIO = Icc ->IOhandler = cmsOpenIOhandlerFromNULL(ContextID);
    if (PrevIO == NULL) return 0;

     

    if (!_cmsWriteHeader(Icc, 0)) return 0;
    if (!SaveTags(Icc, &Keep)) return 0;

    UsedSpace = PrevIO ->UsedSpace;

     

    if (io != NULL) {
        Icc ->IOhandler = io;
        if (!SetLinks(Icc)) goto CleanUp;
        if (!_cmsWriteHeader(Icc, UsedSpace)) goto CleanUp;
        if (!SaveTags(Icc, &Keep)) goto CleanUp;
    }

    memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
    if (!cmsCloseIOhandler(PrevIO)) return 0;

    return UsedSpace;


CleanUp:
    cmsCloseIOhandler(PrevIO);
    memmove(Icc, &Keep, sizeof(_cmsICCPROFILE));
    return 0;
}