+
+#ifdef HAVE_XML_STOPPARSER
+/* Stop the parser if an entity declaration is hit. */
+static void xmpp_entity_declaration(void *userData, const XML_Char *entityName,
+ int is_parameter_entity, const XML_Char *value,
+ int value_length, const XML_Char *base,
+ const XML_Char *systemId, const XML_Char *publicId,
+ const XML_Char *notationName
+) {
+<<<<<<< HEAD
+ syslog(LOG_WARNING, "Illegal entity declaration encountered; stopping parser.");
+=======
+ XMPPM_syslog(LOG_WARNING, "Illegal entity declaration encountered; stopping parser.");
+>>>>>>> 1c0b8162b0a90f2e97028a531005c11b09441498
+ XML_StopParser(XMPP->xp, XML_FALSE);
+}
+#endif
+
+static inline int XMPP_GetUtf8SequenceLength(const char *CharS, const char *CharE)
+{
+ /* if this is is migrated to strbuf, remove this copy. */
+ int n = 0;
+ unsigned char test = (1<<7);
+
+ if ((*CharS & 0xC0) != 0xC0)
+ return 1;
+
+ while ((n < 8) &&
+ ((test & ((unsigned char)*CharS)) != 0))
+ {
+ test = test >> 1;
+ n ++;
+ }
+ if ((n > 6) || ((CharE - CharS) < n))
+ n = 0;
+ return n;
+}
+
+
+/*
+ * Given a source string and a target buffer, returns the string
+ * properly escaped for insertion into an XML stream. Returns a
+ * pointer to the target buffer for convenience.
+ *
+ * BUG: this does not properly handle UTF-8
+ */
+char *xmlesc(char *buf, char *str, int bufsiz)
+{
+ char *ptr;
+ char *eiptr;
+ unsigned char ch;
+ int inlen;
+ int len = 0;
+ int IsUtf8Sequence;
+
+ if (!buf) return(NULL);
+ buf[0] = 0;
+ len = 0;
+ if (!str) {
+ return(buf);
+ }
+
+ inlen = strlen(str);
+ eiptr = str + inlen;
+
+ for (ptr=str; *ptr; ptr++) {
+ ch = *ptr;
+ if (ch == '<') {
+ strcpy(&buf[len], "<");
+ len += 4;
+ }
+ else if (ch == '>') {
+ strcpy(&buf[len], ">");
+ len += 4;
+ }
+ else if (ch == '&') {
+ strcpy(&buf[len], "&");
+ len += 5;
+ }
+ else if ((ch >= 0x20) && (ch <= 0x7F)) {
+ buf[len++] = ch;
+ buf[len] = 0;
+ }
+ else if (ch < 0x20) {
+ /* we probably shouldn't be doing this */
+ buf[len++] = '_';
+ buf[len] = 0;
+ }
+ else {
+ char oct[32];
+
+ IsUtf8Sequence = XMPP_GetUtf8SequenceLength(&buf[len], eiptr);
+ if (IsUtf8Sequence)
+ {
+ while (IsUtf8Sequence > 0){
+ buf[len] = *ptr;
+ len ++;
+ if (--IsUtf8Sequence)
+ ptr++;
+ }
+ buf[len] = '\0';
+ }
+ else
+ {
+ sprintf(oct, "&#%o;", ch);
+ strcpy(&buf[len], oct);
+ len += strlen(oct);
+ }
+ }
+ if ((len + 6) > bufsiz) {
+ return(buf);
+ }
+ }
+ return(buf);
+}
+
+
+/*
+ * We have just received a <stream> tag from the client, so send them ours
+ */