static MI_Result _PackInstance()

in Unix/wsman/wsbuf.c [1688:2226]


static MI_Result _PackInstance(
    WSBuf* buf,
    UserAgent userAgent,
    const MI_Instance* instance,
    MI_Boolean (*filterProperty)(const ZChar* name, void* data),
    void* filterPropertyData,
    const MI_ClassDecl* castToClassDecl,
    MI_Uint32 flags,
    MI_Boolean embedded,
    const ZChar* propName,
    MI_Uint32 *lastPrefixIndex,
    const ZChar* parentNSPrefix) //optional
{
    Instance* self = Instance_GetSelf( instance );
    const MI_ClassDecl* cd = castToClassDecl ? castToClassDecl : self->classDecl;
    MI_Uint32 i;
    const ZChar* name;
    const MI_Char *methodParamType = ZT("_OUTPUT");

    if ((cd->flags & MI_FLAG_METHOD) && (flags & WSMAN_MethodInParameter))
    {
        methodParamType = ZT("_INPUT");
    }

    /* Check for null arguments */
    if (!self || !buf)
        MI_RETURN(MI_RESULT_INVALID_PARAMETER);

    /* For Object & EPR, add <Item> tag */
    if (!embedded &&
        ((flags & WSMAN_ObjectAndEPRFlag)== WSMAN_ObjectAndEPRFlag))
    {
        if (MI_RESULT_OK != WSBuf_AddLit(buf,LIT(ZT("<wsman:Item>"))))
            return MI_RESULT_FAILED;
    }

    /* If object was requested */
    if ((flags & WSMAN_ObjectFlag)== WSMAN_ObjectFlag)
    {
        const ZChar* cn;
        const ZChar* elementName;
        ZChar nsPrefix[12];  // p followed by index and

        //GeneratePrefix
        {
            MI_Uint32 used = 1;
            nsPrefix[0] = ZT('p');

            if (*lastPrefixIndex)
            {
                used += Stprintf(nsPrefix + 1, 11, ZT("%d"), *lastPrefixIndex);
            }

            if (used > 11)  // should never happen
                return MI_RESULT_FAILED;

            nsPrefix[used] = ZT('\0');
            (*lastPrefixIndex)++;
        }

        if ((cd->flags & MI_FLAG_METHOD) && ((MI_MethodDecl*)cd)->propagator)
            cn = ((MI_MethodDecl*)cd)->propagator;
        else
            cn = cd->name;

        elementName = propName ? propName : cn;

#ifndef DISABLE_SHELL
        if ((flags & WSMAN_IsShellRequest) &&
                (Tcscmp(elementName, MI_T("Command")) == 0))
        {
            elementName = MI_T("CommandLine");
        }
#endif
        /* <p:propName ... type='class_Type> */
        if ((flags & WSMAN_IsCimError) == 0)
        {
            if (WSBuf_AddLit1(buf, '<') != MI_RESULT_OK ||
                WSBuf_AddStringNoEncoding(buf, parentNSPrefix ? parentNSPrefix:nsPrefix) != MI_RESULT_OK ||
                WSBuf_AddLit1(buf, ':') != MI_RESULT_OK ||
                WSBuf_AddStringNoEncoding(buf, elementName) != MI_RESULT_OK)
            {
                return MI_RESULT_FAILED;
            }

            /* If parameters, append "_OUTPUT" suffix */

#ifndef DISABLE_SHELL
            if (flags & WSMAN_IsShellResponse)
            {
                /* All shell response body instances have Response appended except the CreateInstance on the Shell */
                if (Tcscmp(elementName, MI_T("Shell")) != 0)
                {
                    if (WSBuf_AddStringNoEncoding(buf, ZT("Response")) != MI_RESULT_OK)
                    {
                        return MI_RESULT_FAILED;
                    }
                }


            }
            else if (flags & WSMAN_IsShellRequest)
            {
                if (Tcscmp(elementName, MI_T("Shell")) == 0)
                {
                    MI_Value value;
                    MI_Type type;
                    MI_Uint32 flags;

                    /* Name is a mandatory attribute */
                    if (MI_Instance_GetElement(instance, MI_T("Name"), &value, &type, &flags, 0) == MI_RESULT_OK &&
                        (flags & MI_FLAG_NULL) == 0)
                    {
                        if (WSBuf_AddStringNoEncoding(buf, MI_T(" Name=\"")) != MI_RESULT_OK ||
                            WSBuf_AddStringNoEncoding(buf, value.string) != MI_RESULT_OK ||
                            WSBuf_AddLit1(buf, '"') != MI_RESULT_OK)
                        {
                            return MI_RESULT_FAILED;
                        }
                    }

                     /* ShellId is a mandatory attribute */
                    if (MI_Instance_GetElement(instance, MI_T("ShellId"), &value, &type, &flags, 0) == MI_RESULT_OK &&
                        (flags & MI_FLAG_NULL) == 0)
                    {
                        if (WSBuf_AddStringNoEncoding(buf, MI_T(" ShellId=\"")) != MI_RESULT_OK ||
                            WSBuf_AddStringNoEncoding(buf, value.string) != MI_RESULT_OK ||
                            WSBuf_AddLit1(buf, '"') != MI_RESULT_OK)
                        {
                            return MI_RESULT_FAILED;
                        }
                    }

                }
                else if ((Tcscmp(elementName, MI_T("CommandLine")) == 0) ||
                        (Tcscmp(elementName, MI_T("Signal")) == 0))
                {
                    MI_Value value;
                    MI_Type type;
                    MI_Uint32 flags;

                      /* ShellId is a mandatory attribute */
                    if (MI_Instance_GetElement(instance, MI_T("CommandId"), &value, &type, &flags, 0) == MI_RESULT_OK &&
                        (flags & MI_FLAG_NULL) == 0)
                    {
                        if (WSBuf_AddStringNoEncoding(buf, MI_T(" CommandId=\"")) != MI_RESULT_OK ||
                            WSBuf_AddStringNoEncoding(buf, value.string) != MI_RESULT_OK ||
                            WSBuf_AddLit1(buf, '"') != MI_RESULT_OK)
                        {
                            return MI_RESULT_FAILED;
                        }
                    }
                }
            }
            else

#endif
            if (cd->flags & MI_FLAG_METHOD)
            {
                if (WSBuf_AddStringNoEncoding(buf, methodParamType) != MI_RESULT_OK)
                {
                    return MI_RESULT_FAILED;
                }
            }

#ifndef DISABLE_SHELL
            if (flags & (WSMAN_IsShellRequest | WSMAN_IsShellResponse))
            {
                if (WSBuf_AddLit(buf, LIT(ZT(" xmlns:"))) != MI_RESULT_OK ||
                        WSBuf_AddStringNoEncoding(buf, nsPrefix) != MI_RESULT_OK ||
                        WSBuf_AddLit(buf, LIT(ZT("=\"http://schemas.microsoft.com/wbem/wsman/1/windows/shell"))) != MI_RESULT_OK ||
                        WSBuf_AddLit1(buf, '"') != MI_RESULT_OK)
                {
                    return MI_RESULT_FAILED;
                }
            }
            else
#endif
            {
                if (WSBuf_AddLit(buf, LIT(ZT(" xmlns:"))) != MI_RESULT_OK ||
                        WSBuf_AddStringNoEncoding(buf, nsPrefix) != MI_RESULT_OK ||
                        WSBuf_AddLit(buf, LIT(ZT("=\"http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/"))) != MI_RESULT_OK ||
                        WSBuf_AddStringNoEncoding(buf, cn) != MI_RESULT_OK ||
                        WSBuf_AddLit1(buf, '"') != MI_RESULT_OK)
                {
                    return MI_RESULT_FAILED;
                }
                if (embedded)
                {
                    if (WSBuf_AddLit(buf,LIT(ZT(" xsi:type=\""))) != MI_RESULT_OK ||
                        WSBuf_AddStringNoEncoding(buf, nsPrefix) != MI_RESULT_OK ||
                        WSBuf_AddLit1(buf, ':') != MI_RESULT_OK ||
                        WSBuf_AddStringNoEncoding(buf,cn) != MI_RESULT_OK ||
                        MI_RESULT_OK != WSBuf_AddLit(buf,ZT("_Type\""), 6))
                    {
                        return MI_RESULT_FAILED;
                    }
                }
            }


            if (WSBuf_AddLit1(buf, '>') != MI_RESULT_OK)
                return MI_RESULT_FAILED;
        }

        /* Put properties */
        for (i = 0; i < cd->numProperties; i++)
        {
            const MI_PropertyDecl* pd = cd->properties[i];
            const void* value = (char*)self + pd->offset;
            MI_Boolean isOctetString = MI_FALSE;

            if (filterProperty &&
                (*filterProperty)(pd->name, filterPropertyData))
            {
                continue;
            }

            /* Search for OctetString qualifier */

            if (pd->type == MI_UINT8A || pd->type == MI_STRINGA)
                isOctetString = TestOctetStringQualifier(pd);

            /* Skip non-out params; rename 'MIReturn' to  'ReturnValue' */

            name = pd->name;

            if (pd->flags & MI_FLAG_PARAMETER)
            {
                if (!(pd->flags & MI_FLAG_OUT))
                    continue;

                if (name[0] == 'M' && Tcscmp(name, ZT("MIReturn")) == 0)
                {
#ifndef DISABLE_SHELL
                    if (flags & (WSMAN_IsShellRequest|WSMAN_IsShellResponse))
                        continue;
#endif

                    name = ZT("ReturnValue");
                }
            }

#ifndef DISABLE_SHELL
            if ((((flags & WSMAN_IsShellResponse) && (Tcscmp(elementName, ZT("Receive")) == 0)) ||
                 ((flags & WSMAN_IsShellRequest) && (Tcscmp(elementName, ZT("Send")) == 0))) &&
                (Tcscmp(name, ZT("Stream")) == 0))
            {
                MI_InstanceField *field = (MI_InstanceField*) value;
                MI_Instance *stream = field->value;
                MI_Value mivalue;
                MI_Type mitype;
                MI_Uint32 miflags;

                if (field->exists)
                {
                    if (WSBuf_AddLit(buf,LIT(ZT("<p:Stream"))) != MI_RESULT_OK)
                    {
                        return MI_RESULT_FAILED;
                    }

                    /* commandId is an optional attribute */
                    if (MI_Instance_GetElement(stream, MI_T("commandId"), &mivalue, &mitype, &miflags, 0) == MI_RESULT_OK &&
                        (miflags & MI_FLAG_NULL) == 0)
                    {
                        if (WSBuf_AddStringNoEncoding(buf, MI_T(" CommandId=\"")) != MI_RESULT_OK ||
                            WSBuf_AddStringNoEncoding(buf, mivalue.string) != MI_RESULT_OK ||
                            WSBuf_AddLit1(buf, '"') != MI_RESULT_OK)
                        {
                            return MI_RESULT_FAILED;
                        }
                    }

                    /* End attribute is optional and only needed if we are at endOfStream */
                    if (MI_Instance_GetElement(stream, MI_T("endOfStream"), &mivalue, &mitype, &miflags, 0) == MI_RESULT_OK &&
                        (miflags & MI_FLAG_NULL) == 0 &&
                        mivalue.boolean)
                    {
                        if (WSBuf_AddStringNoEncoding(buf, MI_T(" End=\"true\"")) != MI_RESULT_OK )
                        {
                            return MI_RESULT_FAILED;
                        }
                    }

                    /* stream name is mandatory attribute*/
                    if (MI_Instance_GetElement(stream, MI_T("streamName"), &mivalue, &mitype, &miflags, 0) == MI_RESULT_OK &&
                        (miflags & MI_FLAG_NULL) == 0 )
                    {
                        if (WSBuf_AddStringNoEncoding(buf, MI_T(" Name=\"")) != MI_RESULT_OK ||
                            WSBuf_AddStringNoEncoding(buf, mivalue.string) != MI_RESULT_OK ||
                            WSBuf_AddLit1(buf, '"') != MI_RESULT_OK)
                        {
                            return MI_RESULT_FAILED;
                        }
                    }

                    if (WSBuf_AddLit(buf,LIT(ZT(">"))) != MI_RESULT_OK)
                    {
                        return MI_RESULT_FAILED;
                    }

                    if (MI_Instance_GetElement(stream, MI_T("data"), &mivalue, &mitype, &miflags, 0) == MI_RESULT_OK &&
                        (miflags & MI_FLAG_NULL) == 0)
                    {
                        if (WSBuf_AddStringNoEncoding(buf, mivalue.string) != MI_RESULT_OK)
                        {
                            return MI_RESULT_FAILED;
                        }
                    }

                    if (WSBuf_AddLit(buf,LIT(ZT("</p:Stream>"))) != MI_RESULT_OK)
                    {
                        return MI_RESULT_FAILED;
                    }
                }
                continue;
            }
            if (flags & WSMAN_IsShellResponse)
            {
                if (Tcscmp(elementName, ZT("Receive")) == 0)
                {
                    if (Tcscmp(name, ZT("CommandState")) == 0)
                    {
                        MI_InstanceField *field = (MI_InstanceField*) value;
                        MI_Instance *commandState = field->value;
                        MI_Value mivalue;
                        MI_Type mitype;
                        MI_Uint32 miflags;

                        if (field->exists)
                        {
                            if (WSBuf_AddLit(buf,LIT(ZT("<p:CommandState"))) != MI_RESULT_OK)
                            {
                                return MI_RESULT_FAILED;
                            }

                            /* commandId is a mandatory attribute */
                            if (MI_Instance_GetElement(commandState, MI_T("commandId"), &mivalue, &mitype, &miflags, 0) == MI_RESULT_OK &&
                                (miflags & MI_FLAG_NULL) == 0)
                            {
                                if (WSBuf_AddStringNoEncoding(buf, MI_T(" CommandId=\"")) != MI_RESULT_OK ||
                                    WSBuf_AddStringNoEncoding(buf, mivalue.string) != MI_RESULT_OK ||
                                    WSBuf_AddLit1(buf, '"') != MI_RESULT_OK)
                                {
                                    return MI_RESULT_FAILED;
                                }
                            }

                            /* State is a mandatory attribute */
                            if (MI_Instance_GetElement(commandState, MI_T("state"), &mivalue, &mitype, &miflags, 0) == MI_RESULT_OK &&
                                (miflags & MI_FLAG_NULL) == 0)
                            {
                                if (WSBuf_AddStringNoEncoding(buf, MI_T(" State=\"")) != MI_RESULT_OK ||
                                    WSBuf_AddStringNoEncoding(buf, mivalue.string) != MI_RESULT_OK ||
                                    WSBuf_AddLit1(buf, '"') != MI_RESULT_OK)
                                {
                                    return MI_RESULT_FAILED;
                                }
                            }

                            /* ExitCode is optional attribute and only present if the command has finished */
                            if (MI_Instance_GetElement(commandState, MI_T("ExitCode"), &mivalue, &mitype, &miflags, 0) == MI_RESULT_OK &&
                                (miflags & MI_FLAG_NULL) == 0 )
                            {
                                if (WSBuf_AddStringNoEncoding(buf, MI_T(" ExitCode=\"")) != MI_RESULT_OK ||
                                    WSBuf_AddUint32(buf, mivalue.uint32) != MI_RESULT_OK ||
                                    WSBuf_AddLit1(buf, '"') != MI_RESULT_OK)
                                {
                                    return MI_RESULT_FAILED;
                                }
                            }
                            if (WSBuf_AddLit(buf,LIT(ZT("></p:CommandState>"))) != MI_RESULT_OK)
                            {
                                return MI_RESULT_FAILED;
                            }
                        }

                        continue;
                    }
                }
                else if ((Tcscmp(cn, ZT("Connect")) == 0) && (Tcscmp(name, ZT("connectResponseXml")) == 0))
                {
                    /* Need to add this field directly as it is already an xml element */
                    MI_StringField *field = (MI_StringField*) value;
                    if (field->exists &&
                        WSBuf_AddStringNoEncoding(buf, field->value) != MI_RESULT_OK)
                    {
                        return MI_RESULT_FAILED;
                    }
                    break;
                }
                else if ((Tcscmp(cn, ZT("CommandLine")) == 0) ||
                         (Tcscmp(cn, ZT("Signal")) == 0))
                {
                    if (Tcscmp(name, ZT("CommandId")) == 0)
                    {
                        continue; /* Already added these as selector or as attribute */
                    }
                }
            }
            else if (flags & (WSMAN_IsShellRequest))
            {
                if (Tcscmp(cn, ZT("Shell")) == 0)
                {
                    if (Tcscmp(name, ZT("CreationXml")) == 0)
                    {
                        /* Need to add this field directly as it is already an xml element */
                        MI_StringField *field = (MI_StringField*) value;
                        if (field->exists &&
                            WSBuf_AddStringNoEncoding(buf, field->value) != MI_RESULT_OK)
                        {
                            return MI_RESULT_FAILED;
                        }
                        continue;
                    }
                    else if (Tcscmp(name, ZT("InputStreams")) != 0 &&
                            Tcscmp(name, ZT("OutputStreams")) != 0 &&
                            Tcscmp(name, ZT("creationXml")) != 0 )
                    {
                        /* Only want to add streams and XML. nothing else is needed in body. */
                        continue;
                    }
                }
                else if ((Tcscmp(cn, ZT("CommandLine")) == 0) ||
                         (Tcscmp(cn, ZT("Signal")) == 0))
                {
                    if (Tcscmp(name, ZT("CommandId")) == 0)
                    {
                        continue; /* Already added these as selector or as attribute */
                    }
                }
                else if (Tcscmp(cn, ZT("Receive")) == 0)
                {
                    MI_Value mivalue;
                    MI_Uint32 type;
                    MI_Uint32 flags;
                    MI_StringField *field = (MI_StringField*) value;

                    if (Tcscmp(name, ZT("CommandId")) == 0)
                    {
                        continue; /* Already added these as selector or as attribute */
                    }
                    else if ((Tcscmp(name, ZT("DesiredStream")) == 0) &&
                            (MI_Instance_GetElement(instance, MI_T("CommandId"), &mivalue, (MI_Type *)&type, &flags, 0) == MI_RESULT_OK &&
                            (flags & MI_FLAG_NULL) == 0))
                    {
                        if (WSBuf_AddLit(buf,LIT(ZT("<p:DesiredStream CommandId=\""))) != MI_RESULT_OK ||
                            WSBuf_AddStringNoEncoding(buf, mivalue.string) != MI_RESULT_OK ||
                            WSBuf_AddLit(buf, LIT(ZT("\">"))) != MI_RESULT_OK ||
                            WSBuf_AddStringNoEncoding(buf, field->value) != MI_RESULT_OK ||
                            WSBuf_AddLit(buf, LIT(ZT("</p:DesiredStream>"))) != MI_RESULT_OK)
                        {
                            return MI_RESULT_FAILED;
                        }
                        continue;
                    }
                 }
            }
#endif

            /* Pack the field */

            if (_PackField(
                buf,
                userAgent,
                PropertyTagWriter_Prop,
                name,
                value,
                (MI_Type)pd->type,
                isOctetString,
                flags, lastPrefixIndex, nsPrefix)!= MI_RESULT_OK)
            {
                return MI_RESULT_FAILED;
            }
        }

        /* </p:elementName> */
        if ((flags & WSMAN_IsCimError) == 0)
        {
            if (WSBuf_AddLit2(buf, '<', '/') != MI_RESULT_OK ||
                WSBuf_AddStringNoEncoding(buf, parentNSPrefix ? parentNSPrefix : nsPrefix) != MI_RESULT_OK ||
                WSBuf_AddLit1(buf, ':') != MI_RESULT_OK ||
                WSBuf_AddStringNoEncoding(buf, elementName) != MI_RESULT_OK)
            {
                return MI_RESULT_FAILED;
            }

#ifndef DISABLE_SHELL
            if (flags & WSMAN_IsShellResponse)
            {
                if ( Tcscmp(elementName, MI_T("Shell")) != 0 &&
                    WSBuf_AddStringNoEncoding(buf, ZT("Response")) != MI_RESULT_OK)
                {
                    return MI_RESULT_FAILED;
                }
            }
            else
#endif
            if ((cd->flags & MI_FLAG_METHOD) &&
                WSBuf_AddStringNoEncoding(buf, methodParamType) != MI_RESULT_OK)
            {
                return MI_RESULT_FAILED;
            }

            if (WSBuf_AddLit1(buf, '>') != MI_RESULT_OK)
                return MI_RESULT_FAILED;
        }
    }

    /* If EPR was requested */
    if (!embedded &&
        ((flags & WSMAN_EPRFlag)== WSMAN_EPRFlag))
    {
        if (MI_RESULT_OK != WSBuf_AddLit(buf,LIT(ZT("<wsa:EndpointReference>")))||
            MI_RESULT_OK != _PackEPR(buf, userAgent, instance, flags)||
            MI_RESULT_OK != WSBuf_AddLit(buf,LIT(ZT("</wsa:EndpointReference>"))))
            return MI_RESULT_FAILED;
    }

    /* If EPR was requested */
    if ((flags & WSMAN_CreatedEPRFlag)== WSMAN_CreatedEPRFlag)
    {
        if (MI_RESULT_OK != WSBuf_AddLit(buf,LIT(ZT("<wxf:ResourceCreated>")))||
            MI_RESULT_OK != _PackEPR(buf, userAgent, instance, flags)||
            MI_RESULT_OK != WSBuf_AddLit(buf,LIT(ZT("</wxf:ResourceCreated>"))))
            return MI_RESULT_FAILED;
    }

    /* For Object & EPR, add </Item> tag */
    if (!embedded &&
        ((flags & WSMAN_ObjectAndEPRFlag)== WSMAN_ObjectAndEPRFlag))
    {
        if (MI_RESULT_OK != WSBuf_AddLit(buf,LIT(ZT("</wsman:Item>"))))
            return MI_RESULT_FAILED;
    }


    return MI_RESULT_OK;
}