2 * W32 disk modules identifier for acquiration installation utility
3 * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; exactly version 2 of June 1991 is required
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "captivemodid.h" /* self */
23 #include <glib/gmessages.h>
24 #include <libxml/xmlreader.h>
25 #include <glib/ghash.h>
28 #include <libgnomevfs/gnome-vfs-file-size.h>
29 #include <openssl/md5.h>
30 #include <openssl/bn.h>
31 #include <openssl/crypto.h>
32 #include <glib/gstrfuncs.h>
34 #include "../../libcaptive/include/captive/libxml.h"
36 #include <captive/macros.h>
39 gchar *calc_md5(gconstpointer base,size_t length)
41 unsigned char md5_bin[1+128/8]; /* 128 bits==16 bytes; '1+' for leading stub to prevent shorter output of BN_bn2hex() */
46 /* already done above */
47 /* Calculate MD5 sum and convert it to hex string: */
48 MD5(base,length,md5_bin+1);
49 md5_bin[0]=0xFF; /* stub to prevent shorter output of BN_bn2hex() */
50 bignum=BN_bin2bn(md5_bin,1+128/8,NULL);
51 hex=BN_bn2hex(bignum);
52 g_assert(strlen(hex)==2*(1+128/8));
57 g_assert(strlen(r)==32);
59 g_assert(isxdigit(*gs));
61 g_assert(isxdigit(*gs));
67 /* map: GINT_TO_POINTER(captivemodid_module.length) -> !=NULL */
68 static GHashTable *module_valid_length_hash;
70 static void module_valid_length_hash_init(void)
72 if (module_valid_length_hash)
74 module_valid_length_hash=g_hash_table_new(g_direct_hash,g_direct_equal);
77 /* map: (const xmlChar *)md5 -> (struct captivemodid_module *) */
78 static GHashTable *module_md5_hash;
80 static void module_md5_hash_init(void)
84 module_md5_hash=g_hash_table_new(g_str_hash,g_str_equal);
87 /* map: (const xmlChar *)type -> (gpointer)GINT_TO_POINTER(priority) */
88 /* We remove entry for module with already the best priority found,
89 * therefore captivemodid_module_type_best_priority_lookup() will return
90 * 'G_MININT' afterwards.
92 static GHashTable *module_type_best_priority_hash;
94 static void module_type_best_priority_hash_init(void)
96 if (module_type_best_priority_hash)
98 module_type_best_priority_hash=g_hash_table_new(g_str_hash,g_str_equal);
101 static void captivemodid_load_module(struct captivemodid_module *module)
103 struct captivemodid_module *module_md5_conflict;
104 gpointer valid_length_value_gpointer;
106 module_md5_hash_init();
107 if ((module_md5_conflict=g_hash_table_lookup(module_md5_hash,module->md5))) {
108 g_warning(_("Ignoring module \"%s\" as it has MD5 conflict with: %s"),
109 module->id,module_md5_conflict->id);
112 g_hash_table_insert(module_md5_hash,(/* de-const */ xmlChar *)module->md5,module);
114 module_valid_length_hash_init();
115 if (!g_hash_table_lookup_extended(module_valid_length_hash,GINT_TO_POINTER(module->length),
117 &valid_length_value_gpointer)) /* value */
118 g_hash_table_insert(module_valid_length_hash,GINT_TO_POINTER(module->length),GINT_TO_POINTER(module->cabinet_used));
120 /* Conflicting 'cabinet_used' values for single 'cabinet size'? */
121 if (valid_length_value_gpointer && GPOINTER_TO_INT(valid_length_value_gpointer)!=module->cabinet_used)
122 g_hash_table_insert(module_valid_length_hash,GINT_TO_POINTER(module->length),NULL);
125 if (strcmp((const char *)module->type,"cabinet")) {
126 if (module->priority>captivemodid_module_type_best_priority_lookup(module->type)) {
127 module_type_best_priority_hash_init();
128 g_hash_table_insert(module_type_best_priority_hash,
129 (/* de-const */ xmlChar *)module->type,GINT_TO_POINTER(module->priority));
134 gboolean captivemodid_module_length_is_valid(GnomeVFSFileSize file_size)
138 if ((GnomeVFSFileSize)(file_size_gint=file_size)!=file_size) /* Size too big to be valid. */
140 module_valid_length_hash_init();
141 return g_hash_table_lookup_extended(module_valid_length_hash,GINT_TO_POINTER(file_size_gint),
146 gint captivemodid_cabinet_length_to_used(gint cabinet_length)
148 gpointer valid_length_value_gpointer;
150 if (!g_hash_table_lookup_extended(module_valid_length_hash,GINT_TO_POINTER(cabinet_length),
152 &valid_length_value_gpointer)) /* value */
154 return GPOINTER_TO_INT(valid_length_value_gpointer);
157 struct captivemodid_module *captivemodid_module_md5_lookup(const gchar *file_md5)
159 g_return_val_if_fail(file_md5!=NULL,NULL);
161 module_md5_hash_init();
162 return g_hash_table_lookup(module_md5_hash,file_md5);
165 gint captivemodid_module_type_best_priority_lookup(const xmlChar *module_type)
170 g_return_val_if_fail(module_type!=NULL,G_MININT);
172 module_type_best_priority_hash_init();
173 errbool=g_hash_table_lookup_extended(module_type_best_priority_hash,
174 module_type, /* lookup_key */
176 &r_gpointer); /* value */
180 return GPOINTER_TO_INT(r_gpointer);
183 /* Returns: TRUE if all modules were found. */
184 gboolean captivemodid_module_type_best_priority_found(const xmlChar *module_type)
188 g_return_val_if_fail(module_type!=NULL,FALSE);
190 module_type_best_priority_hash_init();
191 errbool=g_hash_table_remove(module_type_best_priority_hash,module_type);
192 g_assert(errbool==TRUE);
194 return !g_hash_table_size(module_type_best_priority_hash);
197 static xmlChar *captivemodid_load_module_xml_get_attr
198 (const gchar *captivemodid_pathname,xmlTextReader *xml_reader,const gchar *attr_name)
202 if (!(r=xmlTextReaderGetAttribute(xml_reader,BAD_CAST attr_name))) {
203 /* FIXME: File line identification? */
204 g_warning(_("%s: Undefined attributes: %s"),captivemodid_pathname,attr_name);
210 static long captivemodid_load_module_xml_get_attr_l
211 (const gchar *captivemodid_pathname,xmlTextReader *xml_reader,const gchar *attr_name,long num_min,long num_max)
217 g_return_val_if_fail(num_min-1<num_min,-1);
218 g_return_val_if_fail(num_min<=num_max,num_min-1);
219 g_return_val_if_fail(LONG_MIN<num_min,LONG_MIN);
220 g_return_val_if_fail(num_max<LONG_MAX,num_min-1);
222 if (!(string=captivemodid_load_module_xml_get_attr(captivemodid_pathname,xml_reader,attr_name)))
224 r=strtol((const char *)string,&ends,0);
226 if (r<num_min || r>num_max) {
227 g_warning(_("%s: Numer of out range %ld..%ld: %ld"),captivemodid_pathname,num_min,num_max,r);
233 static void captivemodid_load_module_xml(const gchar *captivemodid_pathname,xmlTextReader *xml_reader)
235 struct captivemodid_module *module;
236 xmlChar *cabinet_used_string;
238 captive_new0(module);
239 if (!(module->type=captivemodid_load_module_xml_get_attr(captivemodid_pathname,xml_reader,"type")))
240 goto fail_free_module;
241 if (!(module->md5 =captivemodid_load_module_xml_get_attr(captivemodid_pathname,xml_reader,"md5")))
242 goto fail_free_module;
243 if (strlen((const char *)module->md5)!=strspn((const char *)module->md5,"0123456789abcdef")) {
244 g_warning(_("%s: Attribute 'md5' can be only lower-cased hexstring: %s"),captivemodid_pathname,module->md5);
245 goto fail_free_module;
247 if (strlen((const char *)module->md5)!=32) {
248 g_warning(_("%s: Attribute 'md5' length must be 32: %s"),captivemodid_pathname,module->md5);
249 goto fail_free_module;
251 if (!(module->id =captivemodid_load_module_xml_get_attr(captivemodid_pathname,xml_reader,"id")))
252 goto fail_free_module;
253 if (0>=(module->length=captivemodid_load_module_xml_get_attr_l(
254 captivemodid_pathname,xml_reader,"length",1,G_MAXINT-1)))
255 goto fail_free_module;
256 if (!(cabinet_used_string=xmlTextReaderGetAttribute(xml_reader,BAD_CAST "cabinet_used")))
257 module->cabinet_used=0;
259 xmlFree(cabinet_used_string);
260 if (0>=(module->cabinet_used=captivemodid_load_module_xml_get_attr_l(
261 captivemodid_pathname,xml_reader,"cabinet_used",1,G_MAXINT-1)))
262 goto fail_free_module;
264 if (G_MININT>=(module->priority=captivemodid_load_module_xml_get_attr_l(captivemodid_pathname,xml_reader,"priority",
265 G_MININT+1,G_MAXINT-1)))
266 goto fail_free_module;
267 captivemodid_load_module(module);
271 xmlFree((xmlChar *)module->type);
272 xmlFree((xmlChar *)module->md5);
273 xmlFree((xmlChar *)module->id);
277 static void captivemodid_load_foreach
278 (const xmlChar *type /* key */,gpointer priority_gpointer /* value */,gpointer user_data /* unused */)
280 g_return_if_fail(type!=NULL);
282 g_return_if_fail(captivemodid_module_best_priority_notify!=NULL);
284 (*captivemodid_module_best_priority_notify)((const gchar *)type);
287 void (*captivemodid_module_best_priority_notify)(const gchar *module_type);
289 gboolean captivemodid_load(const gchar *captivemodid_pathname)
291 xmlTextReader *xml_reader;
293 if (!(xml_reader=xmlNewTextReaderFilename(captivemodid_pathname)))
295 while (1==xmlTextReaderRead(xml_reader)) {
296 switch (xmlTextReaderNodeType(xml_reader)) {
298 case CAPTIVE_XML_TEXT_READER_NODE_TYPE_COMMENT:
301 case CAPTIVE_XML_TEXT_READER_NODE_TYPE_SIGNIFICANT_WHITESPACE:
304 case CAPTIVE_XML_TEXT_READER_NODE_TYPE_TEXT: /* Even empty nodes have some '#text'. */
307 case CAPTIVE_XML_TEXT_READER_NODE_TYPE_END: /* We do not track tag ends. */
310 case CAPTIVE_XML_TEXT_READER_NODE_TYPE_START: {
311 const xmlChar *xml_name;
313 xml_name=xmlTextReaderName(xml_reader);
314 /**/ if (!xmlStrcmp(xml_name,BAD_CAST "modid")) { /* root tag */
316 else if (!xmlStrcmp(xml_name,BAD_CAST "module"))
317 captivemodid_load_module_xml(captivemodid_pathname,xml_reader);
318 else g_warning(_("%s: Unknown ELEMENT node: %s"),captivemodid_pathname,xml_name);
319 xmlFree((xmlChar *)xml_name);
322 default: g_assert_not_reached();
325 xmlFreeTextReader(xml_reader);
327 if (captivemodid_module_best_priority_notify) {
328 g_hash_table_foreach(module_type_best_priority_hash,
329 (GHFunc)captivemodid_load_foreach,NULL);