+ return buf;
+}
+
+static xmlNodePtr __media_svc_find_node(xmlNodePtr node, const char *key)
+{
+ xmlNodePtr tmp = NULL;
+
+ media_svc_retvm_if(!node, NULL, "node is NULL");
+ media_svc_retvm_if(!key, NULL, "key is NULL");
+
+ for (tmp = node->children; tmp; tmp = tmp->next) {
+ if (xmlIsBlankNode(tmp))
+ continue;
+
+ if (g_str_has_suffix((gchar *)tmp->name, key))
+ return tmp;
+ }
+
+ return NULL;
+}
+
+static char * __media_svc_remove_escape_c(const char *value)
+{
+ int start = -1;
+ int end = 0;
+ int len, i;
+
+ media_svc_retv_if(!value, NULL);
+
+ len = strlen(value);
+
+ for (i = 0; i < len; i++) {
+ if (value[i] != 10 && value[i] != 32) { // 10='\n' 32=' '
+ if (start == -1)
+ start = i;
+
+ end = i;
+ }
+ }
+
+ end = end - start + 1;
+
+ return g_strndup(value + start, end);
+}
+
+static char * __media_svc_find_and_get_value(xmlNodePtr node, const char *key)
+{
+ xmlNodePtr tmp = NULL;
+ char *tmp_res = NULL;
+ char *res = NULL;
+
+ media_svc_retvm_if(!node, NULL, "node is NULL");
+ media_svc_retvm_if(!key, NULL, "key is NULL");
+
+ for (tmp = node->children; tmp; tmp = tmp->next) {
+ if (xmlIsBlankNode(tmp))
+ continue;
+
+ if (tmp->children) {
+ tmp_res = __media_svc_find_and_get_value(tmp, key);
+ if (tmp_res) {
+ res = __media_svc_remove_escape_c(tmp_res);
+ xmlFree(tmp_res);
+ return res;
+ }
+ }
+
+ if (g_str_has_suffix((gchar *)tmp->name, key))
+ return (char *)xmlNodeGetContent(tmp);
+ }
+
+ return NULL;
+}
+
+static gboolean __media_svc_get_epub_root_file(zip_t *z, char **opf_file)
+{
+ gchar *buf = NULL;
+ gchar *tmp_buf = NULL;
+ xmlDocPtr doc = NULL;
+ xmlNodePtr node = NULL;
+
+ media_svc_retvm_if(!z, FALSE, "z is NULL");
+ media_svc_retvm_if(!opf_file, FALSE, "opf_file is NULL");
+
+ buf = __media_svc_get_zipfile_string(z, "META-INF/container.xml");
+ media_svc_retvm_if(!buf, FALSE, "buf is NULL");
+
+ tmp_buf = g_strrstr(buf, ">");
+ if (tmp_buf)
+ *(tmp_buf + 1) = '\0';
+
+ doc = xmlParseDoc((const xmlChar *)buf);
+ g_free(buf);
+ media_svc_retvm_if(!doc, FALSE, "doc is NULL");
+
+ node = xmlDocGetRootElement(doc);
+ node = __media_svc_find_node(node, "rootfiles");
+ node = __media_svc_find_node(node, "rootfile");
+
+ *opf_file = (char *)xmlGetProp(node, (const xmlChar *)"full-path");
+ media_svc_sec_debug("OPF [%s]", *opf_file);
+ xmlFreeDoc(doc);
+
+ return TRUE;
+}
+
+static gboolean __media_svc_get_xml_metadata(const xmlChar *buffer, gboolean is_pdf, media_svc_content_info_s *content_info)
+{
+ xmlDocPtr doc = NULL;
+ xmlNodePtr root = NULL;
+
+ media_svc_retvm_if(!buffer, FALSE, "buffer is NULL");
+ media_svc_retvm_if(!content_info, FALSE, "content_info is NULL");
+
+ doc = xmlParseDoc(buffer);
+ media_svc_retv_if(!doc, FALSE);
+
+ root = xmlDocGetRootElement(doc);
+ if (!root) {
+ xmlFreeDoc(doc);
+ return FALSE;
+ }
+
+ content_info->media_meta.title = __media_svc_find_and_get_value(root, "title");
+ if (is_pdf && !content_info->media_meta.title) {
+ xmlFreeDoc(doc);
+ return FALSE;
+ }
+
+ content_info->media_meta.artist = __media_svc_find_and_get_value(root, "creator");
+ if (!content_info->media_meta.artist)
+ content_info->media_meta.artist = __media_svc_find_and_get_value(root, "author");
+ content_info->media_meta.genre = __media_svc_find_and_get_value(root, "subject");
+ content_info->media_meta.copyright = __media_svc_find_and_get_value(root, "publisher");
+ content_info->media_meta.recorded_date = __media_svc_find_and_get_value(root, "date");
+
+ xmlFreeDoc(doc);
+
+ return TRUE;
+}
+
+static int __media_svc_get_epub_metadata(media_svc_content_info_s *content_info)
+{
+ int err = 0;
+ zip_t *z = NULL;
+ gchar *buf = NULL;
+ char *opf_path = NULL;
+
+ media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
+
+ //1. open epub
+ z = zip_open(content_info->path, ZIP_RDONLY, &err);
+ media_svc_retvm_if(err == -1, MS_MEDIA_ERR_INTERNAL, "zip_open failed");
+
+ //2. find and read opf file
+ if (!__media_svc_get_epub_root_file(z, &opf_path)) {
+ media_svc_error("__media_svc_get_epub_root_file failed");
+ zip_close(z);
+ return MS_MEDIA_ERR_INTERNAL;
+ }
+
+ //3. get metadata
+ buf = __media_svc_get_zipfile_string(z, opf_path);
+ xmlFree(opf_path);
+ zip_close(z);
+ media_svc_retvm_if(!buf, MS_MEDIA_ERR_INTERNAL, "__media_svc_get_zipfile_string failed");
+
+ if (!__media_svc_get_xml_metadata((const xmlChar *)buf, FALSE, content_info))
+ media_svc_error("__media_svc_get_xml_metadata failed");
+
+ g_free(buf);