void NormalizeString (DPL::Optional<DPL::String>& txt)
{
    if (!!txt) {
        std::string tmp = DPL::ToUTF8String(*txt);
        const xmlChar* str = reinterpret_cast<const xmlChar*>(tmp.c_str());
        if (!xmlCheckUTF8(str)) {
            LogError("Not valid UTF8");
            return;
        }

        int i = 0;
        xmlChar* c;
        while ((c = const_cast<xmlChar*>(xmlUTF8Strpos(str, i))) != NULL) {
            if (!IsSpace(c)) {
                break;
            }
            ++i;
        }

        xmlChar* tmpnew = xmlUTF8Strndup(c, xmlUTF8Strlen(c) + 1);
        bool first = false;
        xmlChar* s = tmpnew;
        while ((c = const_cast<xmlChar*>(xmlUTF8Strpos(str, i))) != NULL) {
            if (IsSpace(c)) {
                first = true;
                ++i;
            } else {
                if (c[0] == 0x0) {
                    break;
                }
                if (first) {
                    xmlChar space[6] = { 0x20 };
                    CopyChar(s, space);
                    s += xmlUTF8Size(s);
                    first = false;
                }
                CopyChar(s, c);
                s += xmlUTF8Size(s);
                ++i;
            }
        }
        s[0] = 0x0;
        txt = DPL::FromUTF8String(reinterpret_cast<char*>(tmpnew));
        xmlFree(tmpnew);
    }
}
Beispiel #2
0
/**
 * xmlUTF8Charcmp:
 * @utf1: pointer to first UTF8 char
 * @utf2: pointer to second UTF8 char
 *
 * compares the two UCS4 values
 *
 * returns result of the compare as with xmlStrncmp
 */
int
xmlUTF8Charcmp(const xmlChar *utf1, const xmlChar *utf2) {

    if (utf1 == NULL ) {
        if (utf2 == NULL)
            return 0;
        return -1;
    }
    return xmlStrncmp(utf1, utf2, xmlUTF8Size(utf1));
}
bool CopyChar(xmlChar* out,
        xmlChar* in)
{
    int size = xmlUTF8Size(in);
    switch (size) {
    case 6:
        out[5] = in[5];
    case 5:
        out[4] = in[4];
    case 4:
        out[3] = in[3];
    case 3:
        out[2] = in[2];
    case 2:
        out[1] = in[1];
    case 1:
        out[0] = in[0];
        return true;

    default:
        return false;
    }
}
Beispiel #4
0
/**
 * exsltStrReplaceFunction:
 * @ctxt: an XPath parser context
 * @nargs: the number of arguments
 *
 * Takes a string, and two node sets and returns the string with all strings in
 * the first node set replaced by all strings in the second node set.
 */
static void
exsltStrReplaceFunction (xmlXPathParserContextPtr ctxt, int nargs) {
    int i, i_empty, n, slen0, rlen0, *slen, *rlen;
    void *mem = NULL;
    const xmlChar *src, *start;
    xmlChar *string, *search_str = NULL, *replace_str = NULL;
    xmlChar **search, **replace;
    xmlNodeSetPtr search_set = NULL, replace_set = NULL;
    xmlBufferPtr buf;

    if (nargs  != 3) {
        xmlXPathSetArityError(ctxt);
        return;
    }

    /* get replace argument */

    if (!xmlXPathStackIsNodeSet(ctxt))
        replace_str = xmlXPathPopString(ctxt);
    else
        replace_set = xmlXPathPopNodeSet(ctxt);

    if (xmlXPathCheckError(ctxt))
        goto fail_replace;

    /* get search argument */

    if (!xmlXPathStackIsNodeSet(ctxt)) {
        search_str = xmlXPathPopString(ctxt);
        n = 1;
    }
    else {
        search_set = xmlXPathPopNodeSet(ctxt);
        n = search_set != NULL ? search_set->nodeNr : 0;
    }

    if (xmlXPathCheckError(ctxt))
        goto fail_search;

    /* get string argument */

    string = xmlXPathPopString(ctxt);
    if (xmlXPathCheckError(ctxt))
        goto fail_string;

    /* check for empty search node list */

    if (n <= 0) {
        exsltStrReturnString(ctxt, string, xmlStrlen(string));
        goto done_empty_search;
    }

    /* allocate memory for string pointer and length arrays */

    if (n == 1) {
        search = &search_str;
        replace = &replace_str;
        slen = &slen0;
        rlen = &rlen0;
    }
    else {
        mem = xmlMalloc(2 * n * (sizeof(const xmlChar *) + sizeof(int)));
        if (mem == NULL) {
            xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR);
            goto fail_malloc;
        }
        search = (xmlChar **) mem;
        replace = search + n;
        slen = (int *) (replace + n);
        rlen = slen + n;
    }

    /* process arguments */

    i_empty = -1;

    for (i=0; i<n; ++i) {
        if (search_set != NULL) {
            search[i] = xmlXPathCastNodeToString(search_set->nodeTab[i]);
            if (search[i] == NULL) {
                n = i;
                goto fail_process_args;
            }
        }

        slen[i] = xmlStrlen(search[i]);
        if (i_empty < 0 && slen[i] == 0)
            i_empty = i;

        if (replace_set != NULL) {
            if (i < replace_set->nodeNr) {
                replace[i] = xmlXPathCastNodeToString(replace_set->nodeTab[i]);
                if (replace[i] == NULL) {
                    n = i + 1;
                    goto fail_process_args;
                }
            }
            else
                replace[i] = NULL;
        }
        else {
            if (i == 0)
                replace[i] = replace_str;
            else
                replace[i] = NULL;
        }

        if (replace[i] == NULL)
            rlen[i] = 0;
        else
            rlen[i] = xmlStrlen(replace[i]);
    }

    if (i_empty >= 0 && rlen[i_empty] == 0)
        i_empty = -1;

    /* replace operation */

    buf = xmlBufferCreate();
    if (buf == NULL) {
        xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR);
        goto fail_buffer;
    }
    src = string;
    start = string;

    while (*src != 0) {
        int max_len = 0, i_match = 0;

        for (i=0; i<n; ++i) {
            if (*src == search[i][0] &&
                slen[i] > max_len &&
                xmlStrncmp(src, search[i], slen[i]) == 0)
            {
                i_match = i;
                max_len = slen[i];
            }
        }

        if (max_len == 0) {
            if (i_empty >= 0 && start < src) {
                if (xmlBufferAdd(buf, start, src - start) ||
                    xmlBufferAdd(buf, replace[i_empty], rlen[i_empty]))
                {
                    xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR);
                    goto fail_buffer_add;
                }
                start = src;
            }

            src += xmlUTF8Size(src);
        }
        else {
            if ((start < src &&
                 xmlBufferAdd(buf, start, src - start)) ||
                (rlen[i_match] &&
                 xmlBufferAdd(buf, replace[i_match], rlen[i_match])))
            {
                xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR);
                goto fail_buffer_add;
            }

            src += slen[i_match];
            start = src;
        }
    }

    if (start < src && xmlBufferAdd(buf, start, src - start)) {
        xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR);
        goto fail_buffer_add;
    }

    /* create result node set */

    exsltStrReturnString(ctxt, xmlBufferContent(buf), xmlBufferLength(buf));

    /* clean up */

fail_buffer_add:
    xmlBufferFree(buf);

fail_buffer:
fail_process_args:
    if (search_set != NULL) {
        for (i=0; i<n; ++i)
            xmlFree(search[i]);
    }
    if (replace_set != NULL) {
        for (i=0; i<n; ++i) {
            if (replace[i] != NULL)
                xmlFree(replace[i]);
        }
    }

    if (mem != NULL)
        xmlFree(mem);

fail_malloc:
done_empty_search:
    xmlFree(string);

fail_string:
    if (search_set != NULL)
        xmlXPathFreeNodeSet(search_set);
    else
        xmlFree(search_str);

fail_search:
    if (replace_set != NULL)
        xmlXPathFreeNodeSet(replace_set);
    else
        xmlFree(replace_str);

fail_replace:
    return;
}
Beispiel #5
0
/**
 * exsltStrTokenizeFunction:
 * @ctxt: an XPath parser context
 * @nargs: the number of arguments
 *
 * Splits up a string on the characters of the delimiter string and returns a
 * node set of token elements, each containing one token from the string.
 */
static void
exsltStrTokenizeFunction(xmlXPathParserContextPtr ctxt, int nargs)
{
    xsltTransformContextPtr tctxt;
    xmlChar *str, *delimiters, *cur;
    const xmlChar *token, *delimiter;
    xmlNodePtr node;
    xmlDocPtr container;
    xmlXPathObjectPtr ret = NULL;
    int clen;

    if ((nargs < 1) || (nargs > 2)) {
        xmlXPathSetArityError(ctxt);
        return;
    }

    if (nargs == 2) {
        delimiters = xmlXPathPopString(ctxt);
        if (xmlXPathCheckError(ctxt))
            return;
    } else {
        delimiters = xmlStrdup((const xmlChar *) "\t\r\n ");
    }
    if (delimiters == NULL)
        return;

    str = xmlXPathPopString(ctxt);
    if (xmlXPathCheckError(ctxt) || (str == NULL)) {
        xmlFree(delimiters);
        return;
    }

    /* Return a result tree fragment */
    tctxt = xsltXPathGetTransformContext(ctxt);
    if (tctxt == NULL) {
        xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
	      "exslt:tokenize : internal error tctxt == NULL\n");
	goto fail;
    }

    container = xsltCreateRVT(tctxt);
    if (container != NULL) {
        xsltRegisterLocalRVT(tctxt, container);
        ret = xmlXPathNewNodeSet(NULL);
        if (ret != NULL) {
            for (cur = str, token = str; *cur != 0; cur += clen) {
	        clen = xmlUTF8Size(cur);
		if (*delimiters == 0) {	/* empty string case */
		    xmlChar ctmp;
		    ctmp = *(cur+clen);
		    *(cur+clen) = 0;
                    node = xmlNewDocRawNode(container, NULL,
                                       (const xmlChar *) "token", cur);
		    xmlAddChild((xmlNodePtr) container, node);
		    xmlXPathNodeSetAddUnique(ret->nodesetval, node);
                    *(cur+clen) = ctmp; /* restore the changed byte */
                    token = cur + clen;
                } else for (delimiter = delimiters; *delimiter != 0;
				delimiter += xmlUTF8Size(delimiter)) {
                    if (!xmlUTF8Charcmp(cur, delimiter)) {
                        if (cur == token) {
                            /* discard empty tokens */
                            token = cur + clen;
                            break;
                        }
                        *cur = 0;	/* terminate the token */
                        node = xmlNewDocRawNode(container, NULL,
                                           (const xmlChar *) "token", token);
			xmlAddChild((xmlNodePtr) container, node);
			xmlXPathNodeSetAddUnique(ret->nodesetval, node);
                        *cur = *delimiter; /* restore the changed byte */
                        token = cur + clen;
                        break;
                    }
                }
            }
            if (token != cur) {
		node = xmlNewDocRawNode(container, NULL,
				    (const xmlChar *) "token", token);
                xmlAddChild((xmlNodePtr) container, node);
	        xmlXPathNodeSetAddUnique(ret->nodesetval, node);
            }
	    /*
	     * Mark it as a function result in order to avoid garbage
	     * collecting of tree fragments
	     */
	    xsltExtensionInstructionResultRegister(tctxt, ret);
        }
    }

fail:
    if (str != NULL)
        xmlFree(str);
    if (delimiters != NULL)
        xmlFree(delimiters);
    if (ret != NULL)
        valuePush(ctxt, ret);
    else
        valuePush(ctxt, xmlXPathNewNodeSet(NULL));
}
bool IsSpace(const xmlChar* str)
{
    int charlen = xmlUTF8Size(str);

    switch (charlen) {
    case 1:
        switch (*str) {
        case 0x09:
        case 0x0a:
        case 0x0b:
        case 0x0c:
        case 0x0d:
        case 0x20:
            return true;

        default:
            return false;
        }

    case 2:
        switch (*(str + 1)) {
        case 0x85:
        case 0xa0:
            return true;

        default:
            return false;
        }

    case 3:
        switch (*str) {
        case 0xe1:
        {
            unsigned char c2 = *(str + 1);
            unsigned char c3 = *(str + 2);
            if ((c2 == 0x9a && c3 == 0x80) || (c2 == 0xa0 && c3 == 0x8e)) {
                return true;
            } else {
                return false;
            }
        }

        case 0xe2:
            switch (*(str + 1)) {
            case 0x80:
                switch (*(str + 2)) {
                case 0x80:
                case 0x81:
                case 0x82:
                case 0x83:
                case 0x84:
                case 0x85:
                case 0x86:
                case 0x87:
                case 0x88:
                case 0x89:
                case 0x8a:
                case 0xa8:
                case 0xa9:
                case 0xaf:
                    return true;
                }
            case 0x81:
                if (*(str + 2) == 0x9f) {
                    return true;
                } else {
                    return false;
                }

            default:
                return false;
            }
        case 0xe3:
            if (*(str + 1) == 0x80 && *(str + 2) == 0x80) {
                return true;
            } else {
                return false;
            }

        default:
            return false;
        }

    default:
        return false;
    }
}
/**
 * print_element_names:
 * @a_node: the initial xml node to consider.
 *
 * Prints the names of the all the xml elements
 * that are siblings or children of a given xml node.
 */
gchar *
read_connection(struct connection *conn, xmlNode * a_node)
{
	static gchar err[512];
	bzero(conn, sizeof(*conn));
	xmlAttr *properties = a_node->properties;

	/* Set some defaults */
	conn->allocation_type = ALLOCATION_TYPE_DHCP;
	conn->connection_type = CONNECTION_TYPE_WLAN;
	conn->phy.wlan.auth_type = AUTH_TYPE_OPEN;
	conn->phy.wlan.encryption_type = ENCRYPTION_TYPE_NONE;
	conn->phy.wlan.key_type = KEY_TYPE_ASCII;

	for(properties = a_node->properties; properties; properties = properties->next) {
		char *name = (char *)properties->name;
		char *value = (char *)properties->children->content;

		if(!strcmp(name, "type")) {
			if(!strcmp(value, "wlan"))
				conn->connection_type = CONNECTION_TYPE_WLAN;
			else if(!strcmp(value, "lan"))
				conn->connection_type = CONNECTION_TYPE_LAN;
			else {
				conn->connection_type = CONNECTION_TYPE_UNKNOWN;
				g_snprintf(err, sizeof(err), "Unrecognized connection type: %s\n", value);
				return err;
			}
		}

		else if(!strcmp(name, "hwaddr"))
			strncpy(conn->phy.wlan.hwaddr, value, sizeof(conn->phy.wlan.hwaddr)-1);

		else if(!strcmp(name, "ssid")) {
			strncpy(conn->phy.wlan.ssid, value, sizeof(conn->phy.wlan.ssid)-1);
			conn->phy.wlan.ssid_len = xmlUTF8Size(properties->name);
		}

		else if(!strcmp(name, "auth")) {
			if(!strcmp(value, "OPEN"))
				conn->phy.wlan.auth_type = AUTH_TYPE_OPEN;
			else if(!strcmp(value, "WEPAUTO"))
				conn->phy.wlan.auth_type = AUTH_TYPE_WEPAUTO;
			else if(!strcmp(value, "WPAPSK"))
				conn->phy.wlan.auth_type = AUTH_TYPE_WPAPSK;
			else if(!strcmp(value, "WPA2PSK"))
				conn->phy.wlan.auth_type = AUTH_TYPE_WPA2PSK;
			else {
				conn->phy.wlan.auth_type = AUTH_TYPE_UNKNOWN;
				g_snprintf(err, sizeof(err), "Unrecognized auth: %s\n", value);
				return err;
			}
		}

		else if(!strcmp(name, "encryption")) {
			if(!strcmp(value, "NONE"))
				conn->phy.wlan.encryption_type = ENCRYPTION_TYPE_NONE;
			else if(!strcmp(value, "WEP"))
				conn->phy.wlan.encryption_type = ENCRYPTION_TYPE_WEP;
			else if(!strcmp(value, "TKIP"))
				conn->phy.wlan.encryption_type = ENCRYPTION_TYPE_TKIP;
			else if(!strcmp(value, "AES"))
				conn->phy.wlan.encryption_type = ENCRYPTION_TYPE_AES;
			else {
				conn->phy.wlan.encryption_type = ENCRYPTION_TYPE_UNKNOWN;
				g_snprintf(err, sizeof(err), "Unrecognized encryption: %s\n", value);
				return err;
			}
		}

		else if(!strcmp(name, "encoding")) {
			if(!strcmp(value, "ascii"))
				conn->phy.wlan.key_type = KEY_TYPE_ASCII;
			else if(!strcmp(value, "hex"))
				conn->phy.wlan.key_type = KEY_TYPE_HEX;
			else {
				conn->phy.wlan.key_type = KEY_TYPE_UNKNOWN;
				g_snprintf(err, sizeof(err), "Unrecognized encoding type: %s\n", value);
				return err;
			}
		}

		else if(!strcmp(name, "key"))
			strncpy(conn->phy.wlan.key, value, sizeof(conn->phy.wlan.key)-1);

		else if(!strcmp(name, "allocation")) {
			if(!strcmp(value, "static"))
				conn->allocation_type = ALLOCATION_TYPE_STATIC;
			else if(!strcmp(value, "dhcp"))
				conn->allocation_type = ALLOCATION_TYPE_DHCP;
			else {
				conn->allocation_type = ALLOCATION_TYPE_UNKNOWN;
				g_snprintf(err, sizeof(err), "Unrecognized allocation: %s\n", value);
				return err;
			}
		}

		else if(!strcmp(name, "ip"))
			strtoip(value, &conn->ip);
		else if(!strcmp(name, "netmask"))
			strtoip(value, &conn->netmask);
		else if(!strcmp(name, "gateway"))
			strtoip(value, &conn->gateway);
		else if(!strcmp(name, "nameserver1"))
			strtoip(value, &conn->nameserver1);
		else if(!strcmp(name, "nameserver2"))
			strtoip(value, &conn->nameserver2);
		else if(!strcmp(name, "username"))
			; // Ignore
		else {
			fprintf(stderr, "Unrecognized field: %s\n", name);
		}
	}

	return NULL;
}