/* * Read xmlDocument from server */ static xmlDoc * js_document_read (xmlParserCtxtPtr ctxt, js_session_t *jsp, const char *url, const char *encoding, int options) { xmlParserInputBufferPtr input; xmlParserInputPtr stream; xmlDoc *docp; if (jsp == NULL || ctxt == NULL || jsp->js_state == JSS_DEAD) return NULL; xmlCtxtReset(ctxt); input = js_buffer_create(jsp, XML_CHAR_ENCODING_NONE); if (input == NULL) return NULL; input->closecallback = NULL; stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); if (stream == NULL) { xmlFreeParserInputBuffer(input); return NULL; } inputPush(ctxt, stream); xmlCtxtUseOptions(ctxt, options); xmlCharEncodingHandlerPtr hdlr; if (encoding && ((hdlr = xmlFindCharEncodingHandler(encoding)) != NULL)) xmlSwitchToEncoding(ctxt, hdlr); if (url != NULL && ctxt->input != NULL && ctxt->input->filename == NULL) ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) url); /* * All right. The stage is set, time to open the curtain and let * the show begin. */ xmlParseDocument(ctxt); docp = ctxt->myDoc; ctxt->myDoc = NULL; if (docp && !ctxt->wellFormed) { xmlFreeDoc(docp); docp = NULL; } return docp; }
static void testDocumentRangeByte1(xmlParserCtxtPtr ctxt, char *document, int len, char *data, int forbid1, int forbid2) { int i; xmlDocPtr res; for (i = 0;i <= 0xFF;i++) { lastError = 0; xmlCtxtReset(ctxt); data[0] = i; res = xmlReadMemory(document, len, "test", NULL, 0); if ((i == forbid1) || (i == forbid2)) { if ((lastError == 0) || (res != NULL)) fprintf(stderr, "Failed to detect invalid char for Byte 0x%02X: %c\n", i, i); } else if ((i == '<') || (i == '&')) { if ((lastError == 0) || (res != NULL)) fprintf(stderr, "Failed to detect illegal char %c for Byte 0x%02X\n", i, i); } else if (((i < 0x20) || (i >= 0x80)) && (i != 0x9) && (i != 0xA) && (i != 0xD)) { if ((lastError != XML_ERR_INVALID_CHAR) && (res != NULL)) fprintf(stderr, "Failed to detect invalid char for Byte 0x%02X\n", i); } else if (res == NULL) { fprintf(stderr, "Failed to parse valid char for Byte 0x%02X : %c\n", i, i); } if (res != NULL) xmlFreeDoc(res); } }
//! Reset parser state. //----------------------------------------------------------------------- static void reset(const parser_type &p) { xmlCtxtReset(p); }
static void testDocumentRangeByte2(xmlParserCtxtPtr ctxt, char *document, int len, char *data) { int i, j; xmlDocPtr res; for (i = 0x80;i <= 0xFF;i++) { for (j = 0;j <= 0xFF;j++) { lastError = 0; xmlCtxtReset(ctxt); data[0] = i; data[1] = j; res = xmlReadMemory(document, len, "test", NULL, 0); /* if first bit of first char is set, then second bit must too */ if ((i & 0x80) && ((i & 0x40) == 0)) { if ((lastError == 0) || (res != NULL)) fprintf(stderr, "Failed to detect invalid char for Bytes 0x%02X 0x%02X\n", i, j); } /* * if first bit of first char is set, then second char first * bits must be 10 */ else if ((i & 0x80) && ((j & 0xC0) != 0x80)) { if ((lastError == 0) || (res != NULL)) fprintf(stderr, "Failed to detect invalid char for Bytes 0x%02X 0x%02X\n", i, j); } /* * if using a 2 byte encoding then the value must be greater * than 0x80, i.e. one of bits 5 to 1 of i must be set */ else if ((i & 0x80) && ((i & 0x1E) == 0)) { if ((lastError == 0) || (res != NULL)) fprintf(stderr, "Failed to detect invalid char for Bytes 0x%02X 0x%02X\n", i, j); } /* * if third bit of first char is set, then the sequence would need * at least 3 bytes, but we give only 2 ! */ else if ((i & 0xE0) == 0xE0) { if ((lastError == 0) || (res != NULL)) fprintf(stderr, "Failed to detect invalid char for Bytes 0x%02X 0x%02X 0x00\n", i, j); } /* * We should see no error in remaning cases */ else if ((lastError != 0) || (res == NULL)) { fprintf(stderr, "Failed to parse document for Bytes 0x%02X 0x%02X\n", i, j); } if (res != NULL) xmlFreeDoc(res); } } }