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>
35 #include <captive/macros.h>
38 gchar *calc_md5(gconstpointer base,size_t length)
40 unsigned char md5_bin[1+128/8]; /* 128 bits==16 bytes; '1+' for leading stub to prevent shorter output of BN_bn2hex() */
45 /* already done above */
46 /* Calculate MD5 sum and convert it to hex string: */
47 MD5(base,length,md5_bin+1);
48 md5_bin[0]=0xFF; /* stub to prevent shorter output of BN_bn2hex() */
49 bignum=BN_bin2bn(md5_bin,1+128/8,NULL);
50 hex=BN_bn2hex(bignum);
51 g_assert(strlen(hex)==2*(1+128/8));
56 g_assert(strlen(r)==32);
58 g_assert(isxdigit(*gs));
60 g_assert(isxdigit(*gs));
66 /* map: GINT_TO_POINTER(captivemodid_module.length) -> !=NULL */
67 static GHashTable *module_valid_length_hash;
69 static void module_valid_length_hash_init(void)
71 if (module_valid_length_hash)
73 module_valid_length_hash=g_hash_table_new(g_direct_hash,g_direct_equal);
76 /* map: (const xmlChar *)md5 -> (struct captivemodid_module *) */
77 static GHashTable *module_md5_hash;
79 static void module_md5_hash_init(void)
83 module_md5_hash=g_hash_table_new(g_str_hash,g_str_equal);
86 /* map: (const xmlChar *)type -> (gpointer)GINT_TO_POINTER(priority) */
87 /* We remove entry for module with already the best priority found,
88 * therefore captivemodid_module_type_best_priority_lookup() will return
89 * 'G_MININT' afterwards.
91 static GHashTable *module_type_best_priority_hash;
93 static void module_type_best_priority_hash_init(void)
95 if (module_type_best_priority_hash)
97 module_type_best_priority_hash=g_hash_table_new(g_str_hash,g_str_equal);
100 static void captivemodid_load_module(struct captivemodid_module *module)
102 struct captivemodid_module *module_md5_conflict;
104 module_md5_hash_init();
105 if ((module_md5_conflict=g_hash_table_lookup(module_md5_hash,module->md5))) {
106 g_warning(_("Ignoring module \"%s\" as it has MD5 conflict with: %s"),
107 module->id,module_md5_conflict->id);
110 g_hash_table_insert(module_md5_hash,(/* de-const */ xmlChar *)module->md5,module);
112 module_valid_length_hash_init();
113 g_hash_table_insert(module_valid_length_hash,GINT_TO_POINTER(module->length),module_valid_length_hash);
115 if (strcmp(module->type,"cabinet")) {
116 if (module->priority>captivemodid_module_type_best_priority_lookup(module->type)) {
117 module_type_best_priority_hash_init();
118 g_hash_table_insert(module_type_best_priority_hash,
119 (/* de-const */ xmlChar *)module->type,GINT_TO_POINTER(module->priority));
124 gboolean captivemodid_module_length_is_valid(GnomeVFSFileSize file_size)
128 if ((GnomeVFSFileSize)(file_size_gint=file_size)!=file_size) /* Size too big to be valid. */
130 module_valid_length_hash_init();
131 return !!g_hash_table_lookup(module_valid_length_hash,GINT_TO_POINTER(file_size_gint));
134 struct captivemodid_module *captivemodid_module_md5_lookup(const gchar *file_md5)
136 g_return_val_if_fail(file_md5!=NULL,NULL);
138 module_md5_hash_init();
139 return g_hash_table_lookup(module_md5_hash,file_md5);
142 gint captivemodid_module_type_best_priority_lookup(const xmlChar *module_type)
147 g_return_val_if_fail(module_type!=NULL,G_MININT);
149 module_type_best_priority_hash_init();
150 errbool=g_hash_table_lookup_extended(module_type_best_priority_hash,
151 module_type, /* lookup_key */
153 &r_gpointer); /* value */
157 return GPOINTER_TO_INT(r_gpointer);
160 /* Returns: TRUE if all modules were found. */
161 gboolean captivemodid_module_type_best_priority_found(const xmlChar *module_type)
165 g_return_val_if_fail(module_type!=NULL,FALSE);
167 module_type_best_priority_hash_init();
168 errbool=g_hash_table_remove(module_type_best_priority_hash,module_type);
169 g_assert(errbool==TRUE);
171 return !g_hash_table_size(module_type_best_priority_hash);
174 static xmlChar *captivemodid_load_module_xml_get_attr
175 (const gchar *captivemodid_pathname,xmlTextReader *xml_reader,const gchar *attr_name)
179 if (!(r=xmlTextReaderGetAttribute(xml_reader,attr_name))) {
180 /* FIXME: File line identification? */
181 g_warning(_("%s: Undefined attributes: %s"),captivemodid_pathname,attr_name);
187 static long captivemodid_load_module_xml_get_attr_l
188 (const gchar *captivemodid_pathname,xmlTextReader *xml_reader,const gchar *attr_name,long num_min,long num_max)
194 g_return_val_if_fail(num_min-1<num_min,-1);
195 g_return_val_if_fail(num_min<=num_max,num_min-1);
196 g_return_val_if_fail(LONG_MIN<num_min,LONG_MIN);
197 g_return_val_if_fail(num_max<LONG_MAX,num_min-1);
199 if (!(string=captivemodid_load_module_xml_get_attr(captivemodid_pathname,xml_reader,attr_name)))
201 r=strtol(string,&ends,0);
203 if (r<num_min || r>num_max) {
204 g_warning(_("%s: Numer of out range %ld..%ld: %ld"),captivemodid_pathname,num_min,num_max,r);
210 static void captivemodid_load_module_xml(const gchar *captivemodid_pathname,xmlTextReader *xml_reader)
212 struct captivemodid_module *module;
214 captive_new0(module);
215 if (!(module->type=captivemodid_load_module_xml_get_attr(captivemodid_pathname,xml_reader,"type")))
216 goto fail_free_module;
217 if (!(module->md5 =captivemodid_load_module_xml_get_attr(captivemodid_pathname,xml_reader,"md5")))
218 goto fail_free_module;
219 if (strlen(module->md5)!=strspn(module->md5,"0123456789abcdef")) {
220 g_warning(_("%s: Attribute 'md5' can be only lower-cased hexstring: %s"),captivemodid_pathname,module->md5);
221 goto fail_free_module;
223 if (strlen(module->md5)!=32) {
224 g_warning(_("%s: Attribute 'md5' length must be 32: %s"),captivemodid_pathname,module->md5);
225 goto fail_free_module;
227 if (!(module->id =captivemodid_load_module_xml_get_attr(captivemodid_pathname,xml_reader,"id")))
228 goto fail_free_module;
229 if (0>=(module->length=captivemodid_load_module_xml_get_attr_l(captivemodid_pathname,xml_reader,"length",1,G_MAXINT-1)))
230 goto fail_free_module;
231 if (G_MININT>=(module->priority=captivemodid_load_module_xml_get_attr_l(captivemodid_pathname,xml_reader,"priority",
232 G_MININT+1,G_MAXINT-1)))
233 goto fail_free_module;
234 captivemodid_load_module(module);
238 xmlFree((xmlChar *)module->type);
239 xmlFree((xmlChar *)module->md5);
240 xmlFree((xmlChar *)module->id);
244 static void captivemodid_load_foreach
245 (const xmlChar *type /* key */,gpointer priority_gpointer /* value */,gpointer user_data /* unused */)
247 g_return_if_fail(type!=NULL);
249 g_return_if_fail(captivemodid_module_best_priority_notify!=NULL);
251 (*captivemodid_module_best_priority_notify)(type);
254 void (*captivemodid_module_best_priority_notify)(const gchar *module_type);
256 gboolean captivemodid_load(const gchar *captivemodid_pathname)
258 xmlTextReader *xml_reader;
260 if (!(xml_reader=xmlNewTextReaderFilename(captivemodid_pathname)))
262 while (1==xmlTextReaderRead(xml_reader)) {
263 switch (xmlTextReaderNodeType(xml_reader)) {
265 case XML_READER_TYPE_COMMENT:
268 case XML_READER_TYPE_SIGNIFICANT_WHITESPACE:
271 case XML_READER_TYPE_TEXT: /* Even empty nodes have some '#text'. */
274 case XML_READER_TYPE_END_ELEMENT: /* We do not track tag ends. */
277 case XML_READER_TYPE_ELEMENT: {
278 const xmlChar *xml_name;
280 xml_name=xmlTextReaderName(xml_reader);
281 /**/ if (!xmlStrcmp(xml_name,"modid")) { /* root tag */
283 else if (!xmlStrcmp(xml_name,"module"))
284 captivemodid_load_module_xml(captivemodid_pathname,xml_reader);
285 else g_warning(_("%s: Unknown ELEMENT node: %s"),captivemodid_pathname,xml_name);
286 xmlFree((xmlChar *)xml_name);
289 default: g_assert_not_reached();
292 xmlFreeTextReader(xml_reader);
294 if (captivemodid_module_best_priority_notify) {
295 g_hash_table_foreach(module_type_best_priority_hash,
296 (GHFunc)captivemodid_load_foreach,NULL);