2 * User options handling code of libcaptive
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 "captive/options.h" /* self */
23 #include <glib/gmessages.h>
24 #include "captive/macros.h"
25 #include <glib/gstrfuncs.h>
27 #include "captive/rtl-file.h"
28 #include <sys/types.h>
32 #include <openssl/md5.h>
33 #include <openssl/bn.h>
34 #include <openssl/crypto.h>
38 gboolean captive_options_module_load(struct captive_options_module *options_module,const gchar *pathname_utf8)
40 g_return_val_if_fail(options_module!=NULL,FALSE);
41 g_return_val_if_fail(pathname_utf8!=NULL,FALSE);
44 options_module->type=CAPTIVE_OPTIONS_MODULE_TYPE_PE32;
45 options_module->pathname_utf8=g_strdup(pathname_utf8);
46 options_module->u.pe32.mapped=TRUE;
47 options_module->u.pe32.base=captive_rtl_file_mmap(
48 &options_module->u.pe32.length, /* lenp */
49 pathname_utf8, /* path */
50 O_RDONLY, /* open_flags */
51 PROT_READ, /* mmap_prot */
52 MAP_SHARED); /* mmap_flags */
53 /* FIXME: convert errno instead of STATUS_INSUFFICIENT_RESOURCES */
54 g_return_val_if_fail(options_module->u.pe32.base!=NULL,FALSE);
56 if (options_module->u.pe32.length>=2
57 && (('M'<<8U)|('Z'<<0U))==GUINT16_FROM_BE(*(const guint16 *)options_module->u.pe32.base)) {
58 unsigned char md5_bin[1+128/8]; /* 128 bits==16 bytes; '1+' for leading stub to prevent shorter output of BN_bn2hex() */
62 /* already done above */
63 /* Calculate MD5 sum and convert it to hex string: */
64 MD5(options_module->u.pe32.base,options_module->u.pe32.length,md5_bin+1);
65 md5_bin[0]=0xFF; /* stub to prevent shorter output of BN_bn2hex() */
66 bignum=BN_bin2bn(md5_bin,1+128/8,NULL);
67 hex=BN_bn2hex(bignum);
68 g_assert(strlen(hex)==2*(1+128/8));
69 options_module->u.pe32.md5=g_strdup(hex+2);
72 /* BN_bn2hex() returns uppercased string: */
73 g_assert(strlen(options_module->u.pe32.md5)==32);
74 for (s=options_module->u.pe32.md5;*s;s++) {
75 g_assert(isxdigit(*s));
77 g_assert(isxdigit(*s));
81 captive_rtl_file_munmap(options_module->u.pe32.base);
82 options_module->type=CAPTIVE_OPTIONS_MODULE_TYPE_GMODULE;
83 options_module->u.gmodule.pathname=g_strdup(pathname_utf8);
90 void captive_options_module_copy(struct captive_options_module *dest,const struct captive_options_module *src)
92 g_return_if_fail(dest!=NULL);
93 g_return_if_fail(src!=NULL);
95 dest->pathname_utf8=g_strdup(src->pathname_utf8);
99 case CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY: g_assert_not_reached();
101 case CAPTIVE_OPTIONS_MODULE_TYPE_PE32:
102 dest->u.pe32.base=g_memdup(src->u.pe32.base,src->u.pe32.length);
103 dest->u.pe32.length=src->u.pe32.length;
104 dest->u.pe32.mapped=FALSE;
105 dest->u.pe32.md5=g_strdup(src->u.pe32.md5);
108 case CAPTIVE_OPTIONS_MODULE_TYPE_GMODULE:
109 dest->u.gmodule.pathname=g_strdup(src->u.gmodule.pathname);
112 default: g_assert_not_reached();
117 void captive_options_module_free(struct captive_options_module *options_module)
119 g_return_if_fail(options_module!=NULL);
121 g_free(options_module->pathname_utf8);
123 switch (options_module->type) {
124 case CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY:
127 case CAPTIVE_OPTIONS_MODULE_TYPE_PE32:
128 if (options_module->u.pe32.mapped)
129 captive_rtl_file_munmap(options_module->u.pe32.base);
131 g_free(options_module->u.pe32.base);
132 g_free(options_module->u.pe32.md5);
135 case CAPTIVE_OPTIONS_MODULE_TYPE_GMODULE:
136 g_free(options_module->u.gmodule.pathname);
139 default: g_assert_not_reached();