Exemple #1
0
/**
 * xmlStrncatNew:
 * @str1:  first xmlChar string
 * @str2:  second xmlChar string
 * @len:  the len of @str2 or < 0
 *
 * same as xmlStrncat, but creates a new string.  The original
 * two strings are not freed. If @len is < 0 then the length
 * will be calculated automatically.
 *
 * Returns a new xmlChar * or NULL
 */
xmlChar *
xmlStrncatNew(const xmlChar *str1, const xmlChar *str2, int len)
{
  int size;
  xmlChar *ret;

  if (len < 0)
    len = xmlStrlen(str2);
  if ((str2 == NULL) || (len == 0))
    return(xmlStrdup(str1));
  if (str1 == NULL)
    return(xmlStrndup(str2, len));

  size = xmlStrlen(str1);
  ret = (xmlChar *) xmlMalloc((size + len + 1) * sizeof(xmlChar));
  if (ret == NULL)
  {
    xmlErrMemory(NULL, NULL);
    return(xmlStrndup(str1, size));
  }
  memcpy(ret, str1, size * sizeof(xmlChar));
  memcpy(&ret[size], str2, len * sizeof(xmlChar));
  ret[size + len] = 0;
  return(ret);
}
Exemple #2
0
/**
 * xmlStrndup:
 * @cur:  the input xmlChar *
 * @len:  the len of @cur
 *
 * a strndup for array of xmlChar's
 *
 * Returns a new xmlChar * or NULL
 */
xmlChar *
xmlStrndup(const xmlChar *cur, intptr_t len) {
    xmlChar *ret;
    
	static int ictr = 0;
	ictr++;

//	if( ictr == 294 ) //126 )
//	{
//		while(ictr>0)
//		{
//			Sleep(0);
//			ictr--;	
//		}
//	}
    if ((cur == NULL) || (len < 0)) return(NULL);
    ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
    if (ret == NULL) {
        xmlErrMemory(NULL, NULL);
        return(NULL);
    }
    memcpy(ret, cur, len * sizeof(xmlChar));
	//printf( "%d\n", ictr );
	fflush(stdout);
    ret[len] = 0;
    return(ret);
}
Exemple #3
0
/**
 * xmlStrndup:
 * @cur:  the input xmlChar *
 * @len:  the len of @cur
 *
 * a strndup for array of xmlChar's
 *
 * Returns a new xmlChar * or NULL
 */
xmlChar *
xmlStrndup(const xmlChar *cur, int len) {
    xmlChar *ret;

    if ((cur == NULL) || (len < 0)) return(NULL);
    ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
    if (ret == NULL) {
        xmlErrMemory(NULL, NULL);
        return(NULL);
    }
    memcpy(ret, cur, len * sizeof(xmlChar));
    ret[len] = 0;
    return(ret);
}
XMLPUBFUNEXPORT xmlChar *
xmlCharStrndup(const char *cur, int len) {
    int i;
    xmlChar *ret;

    if ((cur == NULL) || (len < 0)) return(NULL);
    ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
    if (ret == NULL) {
        xmlErrMemory(NULL, NULL);
        return(NULL);
    }
    for (i = 0;i < len;i++)
        ret[i] = (xmlChar) cur[i];
    ret[len] = 0;
    return(ret);
}
Exemple #5
0
/**
 * xmlEscapeFormatString:
 * @msg:  a pointer to the string in which to escape '%' characters.
 * Must be a heap-allocated buffer created by libxml2 that may be
 * returned, or that may be freed and replaced.
 *
 * Replaces the string pointed to by 'msg' with an escaped string.
 * Returns the same string with all '%' characters escaped.
 */
xmlChar *
xmlEscapeFormatString(xmlChar **msg)
{
    xmlChar *msgPtr = NULL;
    xmlChar *result = NULL;
    xmlChar *resultPtr = NULL;
    size_t count = 0;
    size_t msgLen = 0;
    size_t resultLen = 0;

    if (!msg || !*msg)
        return(NULL);

    for (msgPtr = *msg; *msgPtr != '\0'; ++msgPtr) {
        ++msgLen;
        if (*msgPtr == '%')
            ++count;
    }

    if (count == 0)
        return(*msg);

    resultLen = msgLen + count + 1;
    result = (xmlChar *) xmlMallocAtomic(resultLen * sizeof(xmlChar));
    if (result == NULL) {
        /* Clear *msg to prevent format string vulnerabilities in
           out-of-memory situations. */
        xmlFree(*msg);
        *msg = NULL;
        xmlErrMemory(NULL, NULL);
        return(NULL);
    }

    for (msgPtr = *msg, resultPtr = result; *msgPtr != '\0'; ++msgPtr, ++resultPtr) {
        *resultPtr = *msgPtr;
        if (*msgPtr == '%')
            *(++resultPtr) = '%';
    }
    result[resultLen - 1] = '\0';

    xmlFree(*msg);
    *msg = result;

    return *msg;
}
Exemple #6
0
xmlChar *
xmlStrncat(xmlChar *cur, const xmlChar *add, int len) {
    int size;
    xmlChar *ret;

    if ((add == NULL) || (len == 0))
        return(cur);
    if (cur == NULL)
        return(xmlStrndup(add, len));

    size = xmlStrlen(cur);
    ret = (xmlChar *) xmlRealloc(cur, (size + len + 1) * sizeof(xmlChar));
    if (ret == NULL) {
        xmlErrMemory(NULL, NULL);
        return(cur);
    }
    memcpy(&ret[size], add, len * sizeof(xmlChar));
    ret[size + len] = 0;
    return(ret);
}
/**
 * xmlStrncat:
 * @param cur the original xmlChar* array
 * @param add the xmlChar* array added
 * @param len the length of add
 *
 * a strncat for array of xmlChar's, it will extend cur with the len
 * first bytes of add.
 *
 * Returns a new xmlChar*, the original cur is reallocated if needed
 * and should not be freed
 *
 * OOM: possible --> OOM flag is set  
 */
XMLPUBFUNEXPORT xmlChar*
xmlStrncat(xmlChar* cur, const xmlChar* add, int len)
{
    int size;
    xmlChar* ret;

    if ((add == NULL) || (len == 0))
        return(cur);
    if (cur == NULL)
        return(xmlStrndup(add, len));

    size = xmlStrlen(cur);
    // DONE: Fix xmlRealloc: Nothing to fix!
    ret = (xmlChar*) xmlRealloc(cur, (size + len + 1) * sizeof(xmlChar));
    if (!ret) {
        xmlErrMemory(NULL, NULL);
        return(cur);
    }
    memcpy(&ret[size], add, len * sizeof(xmlChar));
    ret[size + len] = 0;
    return(ret);
}