Ejemplo n.º 1
0
static void
process_bijiben_html_content (BijiLazyDeserializer *self,
                              xmlTextReaderPtr      reader)
{
  BijiLazyDeserializerPrivate *priv = self->priv;
  int ret;
  gchar *sane_html;

  sane_html = (gchar*) xmlTextReaderReadInnerXml (reader);

  priv->inner = xmlReaderForMemory (sane_html,
                                    strlen(sane_html),
                                    "", "UTF-8", 0);

  ret = xmlTextReaderRead (priv->inner);

  /* Make the GString grow as we read */
  while (ret == 1)
  {
    process_bijiben_node (self);
    ret = xmlTextReaderRead (priv->inner);
  }

  biji_note_obj_set_raw_text (priv->note, priv->raw_text->str);
  biji_note_obj_set_html (priv->note, sane_html);

  xmlFree (BAD_CAST sane_html);
}
Ejemplo n.º 2
0
gchar *
xml_reader_read_inner_xml (XmlReader *reader)
{
  g_return_val_if_fail (XML_IS_READER (reader), FALSE);

  RETURN_STRDUP_AND_XMLFREE (xmlTextReaderReadInnerXml (reader->xml));
}
Ejemplo n.º 3
0
/*
  xar_process_subdocument - check TOC for xml subdocument. If found, extract and
                            scan in memory.
  Parameters:
     reader - xmlTextReaderPtr
     ctx - pointer to cli_ctx
  Returns:
     CL_SUCCESS - subdoc found and clean scan (or virus found and SCAN_ALL), or no subdocument
     other - error return code from cli_mem_scandesc()
*/
static int xar_scan_subdocuments(xmlTextReaderPtr reader, cli_ctx *ctx)
{
    int rc = CL_SUCCESS, subdoc_len, fd;
    xmlChar * subdoc;
    const xmlChar *name;
    char * tmpname;

    while (xmlTextReaderRead(reader) == 1) {
        name = xmlTextReaderConstLocalName(reader);
        if (name == NULL) {
            cli_dbgmsg("cli_scanxar: xmlTextReaderConstLocalName() no name.\n");
            rc = CL_EFORMAT;
            break;
        }
        if (xmlStrEqual(name, (const xmlChar *)"toc") &&
                xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT)
            return CL_SUCCESS;
        if (xmlStrEqual(name, (const xmlChar *)"subdoc") &&
                xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) {
            subdoc = xmlTextReaderReadInnerXml(reader);
            if (subdoc == NULL) {
                cli_dbgmsg("cli_scanxar: no content in subdoc element.\n");
                xmlTextReaderNext(reader);
                continue;
            }
            subdoc_len = xmlStrlen(subdoc);
            cli_dbgmsg("cli_scanxar: in-memory scan of xml subdocument, len %i.\n", subdoc_len);
            rc = cli_mem_scandesc(subdoc, subdoc_len, ctx);
            if (rc == CL_VIRUS && SCAN_ALL)
                rc = CL_SUCCESS;

            /* make a file to leave if --leave-temps in effect */
            if(ctx->engine->keeptmp) {
                if ((rc = cli_gentempfd(ctx->engine->tmpdir, &tmpname, &fd)) != CL_SUCCESS) {
                    cli_dbgmsg("cli_scanxar: Can't create temporary file for subdocument.\n");
                } else {
                    cli_dbgmsg("cli_scanxar: Writing subdoc to temp file %s.\n", tmpname);
                    if (cli_writen(fd, subdoc, subdoc_len) < 0) {
                        cli_dbgmsg("cli_scanxar: cli_writen error writing subdoc temporary file.\n");
                        rc = CL_EWRITE;
                    }
                    rc = xar_cleanup_temp_file(ctx, fd, tmpname);
                }
            }

            xmlFree(subdoc);
            if (rc != CL_SUCCESS)
                return rc;
            xmlTextReaderNext(reader);
        }
    }
    return rc;
}
Ejemplo n.º 4
0
/* reader:read_inner_xml() */
static int xmlreader_read_inner_xml(lua_State *L) {
  xmlreader xr = check_xmlreader(L, 1);
  char *str = (char*)xmlTextReaderReadInnerXml(xr);
  if (str) {
    lua_pushstring(L, str);
    xmlFree(str);
    return 1;
  } else {
    lua_pushnil(L);
    lua_pushstring(L, ERR_MSG);
    return 2;
  }
}
Ejemplo n.º 5
0
/*
 * call-seq:
 *    reader.read_inner_xml -> data
 *
 * Read the contents of the current node, including child nodes and markup.
 *
 * Return a string containing the XML content, or nil if the current node is
 * neither an element nor attribute, or has no child nodes.
 */
static VALUE rxml_reader_read_inner_xml(VALUE self)
{
  VALUE result = Qnil;
  xmlTextReaderPtr xReader = rxml_text_reader_get(self);

  xmlChar *xml = xmlTextReaderReadInnerXml(xReader);

  if (xml)
  {
    const xmlChar *xencoding = xmlTextReaderConstEncoding(xReader);
    result = rxml_new_cstr((const char*) xml, xencoding);
    xmlFree(xml);
  }

  return result;
}
Ejemplo n.º 6
0
/*
 * call-seq:
 *   inner_xml
 *
 * Read the contents of the current node, including child nodes and markup.
 * Returns a utf-8 encoded string.
 */
static VALUE inner_xml(VALUE self)
{
  xmlTextReaderPtr reader;
  xmlChar* value;
  VALUE str;

  Data_Get_Struct(self, xmlTextReader, reader);

  value = xmlTextReaderReadInnerXml(reader);

  str = Qnil;
  if(value) {
    str = NOKOGIRI_STR_NEW2((char*)value);
    xmlFree(value);
  }

  return str;
}
Ejemplo n.º 7
0
static int parse_data(xmlTextReaderPtr reader, int32_t **gidsadr, size_t gidscount) {
	char *value, *inner_xml;

	if (!(value = (char*)xmlTextReaderGetAttribute(reader, (xmlChar*)"encoding"))) { /* encoding */
		tmx_err(E_MISSEL, "xml parser: missing 'encoding' attribute in the 'data' element");
		return 0;
	}

	if (!(inner_xml = (char*)xmlTextReaderReadInnerXml(reader))) {
		tmx_err(E_XDATA, "xml parser: missing content in the 'data' element");
		tmx_free_func(value);
		return 0;
	}

	if (!strcmp(value, "base64")) {
		tmx_free_func(value);
		if (!(value = (char*)xmlTextReaderGetAttribute(reader, (xmlChar*)"compression"))) { /* compression */
			tmx_err(E_MISSEL, "xml parser: missing 'compression' attribute in the 'data' element");
			goto cleanup;
		}
		if (strcmp(value, "zlib") && strcmp(value, "gzip")) {
			tmx_err(E_ENCCMP, "xml parser: unsupported data compression: '%s'", value); /* unsupported compression */
			goto cleanup;
		}
		if (!data_decode(str_trim(inner_xml), B64Z, gidscount, gidsadr)) goto cleanup;

	} else if (!strcmp(value, "xml")) {
		tmx_err(E_ENCCMP, "xml parser: unimplemented data encoding: XML");
		goto cleanup;
	} else if (!strcmp(value, "csv")) {
		if (!data_decode(str_trim(inner_xml), CSV, gidscount, gidsadr)) goto cleanup;
	} else {
		tmx_err(E_ENCCMP, "xml parser: unknown data encoding: %s", value);
		goto cleanup;
	}
	tmx_free_func(value);
	tmx_free_func(inner_xml);
	return 1;

cleanup:
	tmx_free_func(value);
	tmx_free_func(inner_xml);
	return 0;
}
Ejemplo n.º 8
0
/*
 * call-seq:
 *    reader.read_inner_xml -> data
 *
 * Read the contents of the current node, including child nodes and markup.
 *
 * Return a string containing the XML content, or nil if the current node is
 * neither an element nor attribute, or has no child nodes.
 */
static VALUE rxml_reader_read_inner_xml(VALUE self)
{
  const xmlChar *result = xmlTextReaderReadInnerXml(rxml_text_reader_get(self));
  return (result == NULL ? Qnil : rb_str_new2((const char*)result));
}
Ejemplo n.º 9
0
/* Common XML format for both Bijiben / Tomboy */
static void
processNode (BijiLazyDeserializer *self) 
{
  xmlTextReaderPtr r = self->priv->r;
  BijiNoteObj * n = self->priv->note;
  xmlChar   *name;
  GdkRGBA   *color;
  gchar     *tag;
  GString   *norm;
  gchar     *debug;

  name = xmlTextReaderName (r);

  if ( g_strcmp0((gchar*)name,"title") == 0 )
    biji_process_string (r, (BijiReaderFunc*) biji_note_obj_set_title, n);

  if ( g_strcmp0((gchar*)name,"text") == 0 )
  {
    if (self->priv->type == BIJIBEN_1)
    {
      self->priv->content = (gchar*) xmlTextReaderReadInnerXml (r);
      process_bijiben_html_content (self);
    }

    else if (self->priv->type == TOMBOY_1 ||
             self->priv->type == TOMBOY_2 ||
             self->priv->type == TOMBOY_3 )
    {
      self->priv->content = (gchar*) xmlTextReaderReadInnerXml (r);
      process_tomboy_xml_content (self);
    }
  }

  if (g_strcmp0 ((gchar*) name, "last-change-date") == 0)
    biji_process_string (r, (BijiReaderFunc*) biji_note_obj_set_last_change_date, n); 

  if (g_strcmp0 ((gchar*) name, "last-metadata-change-date") == 0)
    biji_process_string (r, (BijiReaderFunc*) biji_note_obj_set_last_metadata_change_date, n); 

  if (g_strcmp0 ((gchar*) name, "create-date") == 0)
    biji_process_string (r, (BijiReaderFunc*) biji_note_obj_set_create_date, n); 

  if (g_strcmp0 ((gchar*) name, "color") == 0 )  
  {
    color = g_new (GdkRGBA,1);
    debug = (gchar*) xmlTextReaderReadString (r);

    if ( gdk_rgba_parse (color,debug))
    {
      biji_note_obj_set_rgba (n, color);
    }
    else
    {
      g_warning ("color invalid:%s",debug);
    }

    free (debug);

  }

  if ( g_strcmp0((gchar*)name,"tag") == 0 )  
  {
    tag = (gchar*) xmlTextReaderReadString(r);

    if (g_str_has_prefix (tag,"system:template"))
    {
      note_obj_set_is_template(n,TRUE);
    }

    else if (g_str_has_prefix (tag,"system:notebook:"))
    {
      norm = g_string_new (tag);
      g_string_erase (norm,0,16);
      _biji_note_obj_set_tags (n, g_list_prepend((GList*)_biji_note_obj_get_tags(n),
                               g_string_free (norm,FALSE)));
    }

    free (tag);
  }

  xmlFree(name);
}
Ejemplo n.º 10
0
int cli_scanxdp(cli_ctx *ctx)
{
#if HAVE_LIBXML2
    xmlTextReaderPtr reader = NULL;
    fmap_t *map = *(ctx->fmap);
    const char *buf;
    const xmlChar *name, *value;
    char *decoded;
    size_t decodedlen;
    int rc = CL_SUCCESS;
    char *dumpname;
    size_t i;
    
    buf = (const char *)fmap_need_off_once(map, map->offset, map->len);
    if (!(buf))
        return CL_EREAD;

    if (ctx->engine->keeptmp) {
        dumpname = dump_xdp(ctx, buf, map->len);
        if (dumpname)
            free(dumpname);
    }

    /*
     * Since a PDF file can contain embedded XDP documents,
     * it's possible that the filetyping code matched an embedded XDP document.
     * If that's the case, then xmlReaderForMemory will throw an error. For now,
     * silently ignore the error and return CL_SUCCESS so the filetyping code can
     * continue on.
     */
    reader = xmlReaderForMemory(buf, (int)(map->len), "noname.xml", NULL, CLAMAV_MIN_XMLREADER_FLAGS);
    if (!(reader))
        return CL_SUCCESS;

    while (xmlTextReaderRead(reader) == 1) {
        name = xmlTextReaderConstLocalName(reader);
        if (!(name))
            continue;

        if (!strcmp((const char *)name, "chunk") && xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) {
            value = xmlTextReaderReadInnerXml(reader);
            if (value) {
                decoded = cl_base64_decode((char *)value, strlen((const char *)value), NULL, &decodedlen, 0);
                if (decoded) {
                    unsigned int shouldscan=0;

                    if (decodedlen > 5) {
                        for (i=0; i < MIN(MAGIC_BUFFER_SIZE, decodedlen-5); i++) {
                            if (decoded[i] != '%')
                                continue;

                            if (decoded[i+1] == 'P' || decoded[i+1] == 'p') {
                                if (decoded[i+2] == 'D' || decoded[i+2] == 'd') {
                                    if (decoded[i+3] == 'F' || decoded[i+3] == 'f') {
                                        if (decoded[i+4] == '-') {
                                            shouldscan=1;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    if (!shouldscan) {
                        free(decoded);
                        xmlFree((void *)value);
                        break;
                    }

                    rc = cli_mem_scandesc(decoded, decodedlen, ctx);
                    free(decoded);
                    if (rc != CL_SUCCESS || rc == CL_BREAK) {
                        xmlFree((void *)value);
                        break;
                    }
                }

                xmlFree((void *)value);
            }
        }
    }

    xmlFreeTextReader(reader);

    return rc;
#else
    return CL_SUCCESS;
#endif
}
Ejemplo n.º 11
0
char *oscap_get_xml(xmlTextReaderPtr reader)
{
	return (char *)xmlTextReaderReadInnerXml(reader);
}
Ejemplo n.º 12
0
/* Common XML format for both Bijiben / Tomboy */
static void
processNode (BijiLazyDeserializer *self) 
{
  xmlTextReaderPtr r = self->priv->r;
  BijiNoteObj * n = self->priv->note;
  xmlChar   *name;
  GdkRGBA    color;
  gchar     *tag, *color_str;
  GString   *norm;

  name = xmlTextReaderName (r);

  if ( g_strcmp0((gchar*)name,"title") == 0 )
    biji_process_string (r, (BijiReaderFunc*) biji_note_obj_set_title, n);

  if ( g_strcmp0((gchar*)name,"text") == 0 )
  {
    if (self->priv->type == BIJIBEN_1)
    {
      process_bijiben_html_content (self, r);
    }

    else if (self->priv->type == TOMBOY_1 ||
             self->priv->type == TOMBOY_2 ||
             self->priv->type == TOMBOY_3 )
    {
      self->priv->content = (gchar*) xmlTextReaderReadInnerXml (r);
      process_tomboy_xml_content (self);
    }
  }

  if (g_strcmp0 ((gchar*) name, "last-change-date") == 0)
  {
    gchar *result = (gchar*) xmlTextReaderReadString (r);
    biji_note_obj_set_mtime (n, iso8601_to_gint64 (result));
    free (result);
  }

  if (g_strcmp0 ((gchar*) name, "last-metadata-change-date") == 0)
  {
    gchar *result = (gchar*) xmlTextReaderReadString (r);
    biji_note_obj_set_last_metadata_change_date (n, iso8601_to_gint64 (result));
    free (result);
  }

  if (g_strcmp0 ((gchar*) name, "create-date") == 0)
  {
    gchar *result = (gchar*) xmlTextReaderReadString (r);
    biji_note_obj_set_create_date (n, iso8601_to_gint64 (result));
    free (result);
  }

  if (g_strcmp0 ((gchar*) name, "color") == 0 )  
  {
    color_str = (gchar*) xmlTextReaderReadString (r);

    if (gdk_rgba_parse (&color, color_str))
      biji_note_obj_set_rgba (n, &color);

    else
      g_warning ("color invalid:%s", color_str);

    free (color_str);
  }

  if ( g_strcmp0((gchar*)name,"tag") == 0 )  
  {
    tag = (gchar*) xmlTextReaderReadString(r);

    if (g_str_has_prefix (tag,"system:template"))
    {
      note_obj_set_is_template(n,TRUE);
    }

    else if (g_str_has_prefix (tag,"system:notebook:"))
    {
      norm = g_string_new (tag);
      g_string_erase (norm,0,16);
      biji_item_add_notebook (BIJI_ITEM (n), NULL, norm->str);
      g_string_free (norm, TRUE);
    }

    free (tag);
  }

  xmlFree(name);
}
Ejemplo n.º 13
0
bool xccdf_benchmark_parse(struct xccdf_item * benchmark, xmlTextReaderPtr reader)
{
	XCCDF_ASSERT_ELEMENT(reader, XCCDFE_BENCHMARK);
	assert(benchmark != NULL);
	if (benchmark->type != XCCDF_BENCHMARK)
		return false;

	xccdf_benchmark_set_schema_version(XBENCHMARK(benchmark), xccdf_detect_version_parser(reader));

	if (!xccdf_item_process_attributes(benchmark, reader)) {
		xccdf_benchmark_free(XBENCHMARK(benchmark));
		return false;
	}
	benchmark->sub.benchmark.style = xccdf_attribute_copy(reader, XCCDFA_STYLE);
	benchmark->sub.benchmark.style_href = xccdf_attribute_copy(reader, XCCDFA_STYLE_HREF);
    benchmark->sub.benchmark.lang = (char *) xmlTextReaderXmlLang(reader);
	if (xccdf_attribute_has(reader, XCCDFA_RESOLVED))
		benchmark->item.flags.resolved = xccdf_attribute_get_bool(reader, XCCDFA_RESOLVED);

	int depth = oscap_element_depth(reader) + 1;

	while (oscap_to_start_element(reader, depth)) {
		struct xccdf_model *parsed_model;

		switch (xccdf_element_get(reader)) {
		case XCCDFE_NOTICE:
				oscap_list_add(benchmark->sub.benchmark.notices, xccdf_notice_new_parse(reader));
				break;
		case XCCDFE_FRONT_MATTER:
				oscap_list_add(benchmark->sub.benchmark.front_matter, oscap_text_new_parse(XCCDF_TEXT_HTMLSUB, reader));
			break;
		case XCCDFE_REAR_MATTER:
				oscap_list_add(benchmark->sub.benchmark.rear_matter, oscap_text_new_parse(XCCDF_TEXT_HTMLSUB, reader));
			break;
		case XCCDFE_PLATFORM:
			oscap_list_add(benchmark->item.platforms, xccdf_attribute_copy(reader, XCCDFA_IDREF));
			break;
		case XCCDFE_MODEL:
			parsed_model = xccdf_model_new_xml(reader);

			// we won't add the implied default scoring model, it is already in the benchmark
			if (strcmp(xccdf_model_get_system(parsed_model), "urn:xccdf:scoring:default") != 0)
				assume_ex(xccdf_benchmark_add_model(XBENCHMARK(benchmark), parsed_model), false);
			else
				xccdf_model_free(parsed_model);

			break;
		case XCCDFE_PLAIN_TEXT:{
				const char *id = xccdf_attribute_get(reader, XCCDFA_ID);
				char *data = (char *)xmlTextReaderReadInnerXml(reader);
				if (id)
					oscap_list_add(benchmark->sub.benchmark.plain_texts,
							xccdf_plain_text_new_fill(id,
							data == NULL ? "" : data));
				xmlFree(data);
				break;
			}
		case XCCDFE_CPE_LIST:{
			struct cpe_parser_ctx *ctx = cpe_parser_ctx_from_reader(reader);
			xccdf_benchmark_set_cpe_list(XBENCHMARK(benchmark), cpe_dict_model_parse(ctx));
			cpe_parser_ctx_free(ctx);
			break;
			}
		case XCCDFE_CPE2_PLATFORMSPEC:
			xccdf_benchmark_set_cpe_lang_model(XBENCHMARK(benchmark), cpe_lang_model_parse(reader));
			break;
		case XCCDFE_PROFILE:
			oscap_list_add(benchmark->sub.benchmark.profiles, xccdf_profile_parse(reader, benchmark));
			break;
		case XCCDFE_GROUP:
		case XCCDFE_RULE:
			xccdf_content_parse(reader, benchmark);
			break;
		case XCCDFE_VALUE:
			oscap_list_add(benchmark->sub.benchmark.values, xccdf_value_parse(reader, benchmark));
			break;
		case XCCDFE_TESTRESULT:
			assume_ex(xccdf_benchmark_add_result(XBENCHMARK(benchmark), xccdf_result_new_parse(reader)), false);
			break;
		default:
			if (!xccdf_item_process_element(benchmark, reader))
				dW("Encountered an unknown element '%s' while parsing XCCDF benchmark.",
				   xmlTextReaderConstLocalName(reader));
		}
		xmlTextReaderRead(reader);
	}

	return true;
}
	CInterfaceDefinition *CInterfaceDefinitionLoader::load( const unsigned char &buffer, unsigned int buffer_size )
	{
		assert( !m_interface_definition && !m_reader && m_element_path.empty() );

		m_interface_definition = new CInterfaceDefinition;

		xmlInitParser();

		m_reader = xmlReaderForMemory( (char *) &buffer, buffer_size, NULL, NULL, 0 );
		if( !m_reader )
		{
			INTEGRA_TRACE_ERROR << "unable to read file";
			cleanup();
			return NULL;
		}

		int depth = -1;
		bool handled_element_content = false;
		char *inner_xml = NULL; 

		while( xmlTextReaderRead( m_reader ) )
		{
			switch( xmlTextReaderNodeType( m_reader ) )
			{
				case XML_READER_TYPE_TEXT:
					{
						char *element_value = ( char * ) xmlTextReaderValue( m_reader );
						if( handle_element_value( element_value ) != CError::SUCCESS )
						{
							xmlFree( element_value );
							cleanup();
							return NULL;
						}
						xmlFree( element_value );
						handled_element_content = true;

						store_map_entries();
					}
					break;

				case XML_READER_TYPE_ELEMENT:
					{
						const char *element_name = ( const char * ) xmlTextReaderConstName( m_reader );
						int new_depth = xmlTextReaderDepth( m_reader );

						while( depth >= new_depth )
						{
							pop_element_name();
							depth--;
						}

						if( depth < new_depth )
						{
							push_element_name( element_name );
							depth++;
						}

						assert( depth == new_depth );

						if( handle_element() != CError::SUCCESS )
						{
							cleanup();
							return NULL;
						}

						if( xmlTextReaderHasAttributes( m_reader ) )
						{
							if( handle_element_attributes() != CError::SUCCESS )
							{
								cleanup();
								return NULL;
							}
						}

						if( xmlTextReaderIsEmptyElement( m_reader ) )
						{
							handle_element_value( "" );
							handled_element_content = true;
						}
						else
						{
							handled_element_content = false;
							inner_xml = ( char * ) xmlTextReaderReadInnerXml( m_reader );
						}
					}

					break;

				case XML_READER_TYPE_END_ELEMENT:
					if( !handled_element_content && inner_xml )
					{
						handle_element_value( inner_xml );
					}
					if( inner_xml )
					{
						xmlFree( inner_xml );
						inner_xml = NULL;
					}
					handled_element_content = true;
					break;

				default:
					break;
			}
		}

		CError CError = do_sanity_check();
		if( CError != CError::SUCCESS )
		{
			INTEGRA_TRACE_ERROR << "sanity check failed";
			cleanup();
			return NULL;
		}

		m_interface_definition->propagate_defaults();

		INTEGRA_TRACE_VERBOSE << "Loaded ok: " << m_interface_definition->get_interface_info().get_name();
		CInterfaceDefinition *loaded_interface = m_interface_definition;
		m_interface_definition = NULL;
		cleanup();

		return loaded_interface;
	}