/[dtapublic]/sf_code/esrgpcpj/gfsw/gfsw.c
ViewVC logotype

Contents of /sf_code/esrgpcpj/gfsw/gfsw.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 26 - (show annotations) (download)
Sat Oct 8 06:57:57 2016 UTC (8 years, 1 month ago) by dashley
File MIME type: text/plain
File size: 93336 byte(s)
Initial commit.
1 /* $Header: /cvsroot/esrg/sfesrg/esrgpcpj/gfsw/gfsw.c,v 1.30 2009/11/29 02:16:04 dtashley Exp $ */
2
3 #include <windows.h>
4 #include <commdlg.h>
5
6 #include <assert.h>
7 #include <process.h>
8 #include <stdio.h>
9
10 #include "resource.h"
11
12 #include "bstrfunc.h"
13 #include "build_config.h"
14 #include "crchashfuncs.h"
15 #include "esrg_md5.h"
16 #include "esrg_sha512.h"
17
18 //--------------------------------------------------------------------------------
19 //Copyright 2001 David T. Ashley
20 //-------------------------------------------------------------------------------------------------
21 //This source code and any program in which it is compiled/used is provided under the GNU GENERAL
22 //PUBLIC LICENSE, Version 3, full license text below.
23 //-------------------------------------------------------------------------------------------------
24 // GNU GENERAL PUBLIC LICENSE
25 // Version 3, 29 June 2007
26 //
27 // Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
28 // Everyone is permitted to copy and distribute verbatim copies
29 // of this license document, but changing it is not allowed.
30 //
31 // Preamble
32 //
33 // The GNU General Public License is a free, copyleft license for
34 //software and other kinds of works.
35 //
36 // The licenses for most software and other practical works are designed
37 //to take away your freedom to share and change the works. By contrast,
38 //the GNU General Public License is intended to guarantee your freedom to
39 //share and change all versions of a program--to make sure it remains free
40 //software for all its users. We, the Free Software Foundation, use the
41 //GNU General Public License for most of our software; it applies also to
42 //any other work released this way by its authors. You can apply it to
43 //your programs, too.
44 //
45 // When we speak of free software, we are referring to freedom, not
46 //price. Our General Public Licenses are designed to make sure that you
47 //have the freedom to distribute copies of free software (and charge for
48 //them if you wish), that you receive source code or can get it if you
49 //want it, that you can change the software or use pieces of it in new
50 //free programs, and that you know you can do these things.
51 //
52 // To protect your rights, we need to prevent others from denying you
53 //these rights or asking you to surrender the rights. Therefore, you have
54 //certain responsibilities if you distribute copies of the software, or if
55 //you modify it: responsibilities to respect the freedom of others.
56 //
57 // For example, if you distribute copies of such a program, whether
58 //gratis or for a fee, you must pass on to the recipients the same
59 //freedoms that you received. You must make sure that they, too, receive
60 //or can get the source code. And you must show them these terms so they
61 //know their rights.
62 //
63 // Developers that use the GNU GPL protect your rights with two steps:
64 //(1) assert copyright on the software, and (2) offer you this License
65 //giving you legal permission to copy, distribute and/or modify it.
66 //
67 // For the developers' and authors' protection, the GPL clearly explains
68 //that there is no warranty for this free software. For both users' and
69 //authors' sake, the GPL requires that modified versions be marked as
70 //changed, so that their problems will not be attributed erroneously to
71 //authors of previous versions.
72 //
73 // Some devices are designed to deny users access to install or run
74 //modified versions of the software inside them, although the manufacturer
75 //can do so. This is fundamentally incompatible with the aim of
76 //protecting users' freedom to change the software. The systematic
77 //pattern of such abuse occurs in the area of products for individuals to
78 //use, which is precisely where it is most unacceptable. Therefore, we
79 //have designed this version of the GPL to prohibit the practice for those
80 //products. If such problems arise substantially in other domains, we
81 //stand ready to extend this provision to those domains in future versions
82 //of the GPL, as needed to protect the freedom of users.
83 //
84 // Finally, every program is threatened constantly by software patents.
85 //States should not allow patents to restrict development and use of
86 //software on general-purpose computers, but in those that do, we wish to
87 //avoid the special danger that patents applied to a free program could
88 //make it effectively proprietary. To prevent this, the GPL assures that
89 //patents cannot be used to render the program non-free.
90 //
91 // The precise terms and conditions for copying, distribution and
92 //modification follow.
93 //
94 // TERMS AND CONDITIONS
95 //
96 // 0. Definitions.
97 //
98 // "This License" refers to version 3 of the GNU General Public License.
99 //
100 // "Copyright" also means copyright-like laws that apply to other kinds of
101 //works, such as semiconductor masks.
102 //
103 // "The Program" refers to any copyrightable work licensed under this
104 //License. Each licensee is addressed as "you". "Licensees" and
105 //"recipients" may be individuals or organizations.
106 //
107 // To "modify" a work means to copy from or adapt all or part of the work
108 //in a fashion requiring copyright permission, other than the making of an
109 //exact copy. The resulting work is called a "modified version" of the
110 //earlier work or a work "based on" the earlier work.
111 //
112 // A "covered work" means either the unmodified Program or a work based
113 //on the Program.
114 //
115 // To "propagate" a work means to do anything with it that, without
116 //permission, would make you directly or secondarily liable for
117 //infringement under applicable copyright law, except executing it on a
118 //computer or modifying a private copy. Propagation includes copying,
119 //distribution (with or without modification), making available to the
120 //public, and in some countries other activities as well.
121 //
122 // To "convey" a work means any kind of propagation that enables other
123 //parties to make or receive copies. Mere interaction with a user through
124 //a computer network, with no transfer of a copy, is not conveying.
125 //
126 // An interactive user interface displays "Appropriate Legal Notices"
127 //to the extent that it includes a convenient and prominently visible
128 //feature that (1) displays an appropriate copyright notice, and (2)
129 //tells the user that there is no warranty for the work (except to the
130 //extent that warranties are provided), that licensees may convey the
131 //work under this License, and how to view a copy of this License. If
132 //the interface presents a list of user commands or options, such as a
133 //menu, a prominent item in the list meets this criterion.
134 //
135 // 1. Source Code.
136 //
137 // The "source code" for a work means the preferred form of the work
138 //for making modifications to it. "Object code" means any non-source
139 //form of a work.
140 //
141 // A "Standard Interface" means an interface that either is an official
142 //standard defined by a recognized standards body, or, in the case of
143 //interfaces specified for a particular programming language, one that
144 //is widely used among developers working in that language.
145 //
146 // The "System Libraries" of an executable work include anything, other
147 //than the work as a whole, that (a) is included in the normal form of
148 //packaging a Major Component, but which is not part of that Major
149 //Component, and (b) serves only to enable use of the work with that
150 //Major Component, or to implement a Standard Interface for which an
151 //implementation is available to the public in source code form. A
152 //"Major Component", in this context, means a major essential component
153 //(kernel, window system, and so on) of the specific operating system
154 //(if any) on which the executable work runs, or a compiler used to
155 //produce the work, or an object code interpreter used to run it.
156 //
157 // The "Corresponding Source" for a work in object code form means all
158 //the source code needed to generate, install, and (for an executable
159 //work) run the object code and to modify the work, including scripts to
160 //control those activities. However, it does not include the work's
161 //System Libraries, or general-purpose tools or generally available free
162 //programs which are used unmodified in performing those activities but
163 //which are not part of the work. For example, Corresponding Source
164 //includes interface definition files associated with source files for
165 //the work, and the source code for shared libraries and dynamically
166 //linked subprograms that the work is specifically designed to require,
167 //such as by intimate data communication or control flow between those
168 //subprograms and other parts of the work.
169 //
170 // The Corresponding Source need not include anything that users
171 //can regenerate automatically from other parts of the Corresponding
172 //Source.
173 //
174 // The Corresponding Source for a work in source code form is that
175 //same work.
176 //
177 // 2. Basic Permissions.
178 //
179 // All rights granted under this License are granted for the term of
180 //copyright on the Program, and are irrevocable provided the stated
181 //conditions are met. This License explicitly affirms your unlimited
182 //permission to run the unmodified Program. The output from running a
183 //covered work is covered by this License only if the output, given its
184 //content, constitutes a covered work. This License acknowledges your
185 //rights of fair use or other equivalent, as provided by copyright law.
186 //
187 // You may make, run and propagate covered works that you do not
188 //convey, without conditions so long as your license otherwise remains
189 //in force. You may convey covered works to others for the sole purpose
190 //of having them make modifications exclusively for you, or provide you
191 //with facilities for running those works, provided that you comply with
192 //the terms of this License in conveying all material for which you do
193 //not control copyright. Those thus making or running the covered works
194 //for you must do so exclusively on your behalf, under your direction
195 //and control, on terms that prohibit them from making any copies of
196 //your copyrighted material outside their relationship with you.
197 //
198 // Conveying under any other circumstances is permitted solely under
199 //the conditions stated below. Sublicensing is not allowed; section 10
200 //makes it unnecessary.
201 //
202 // 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
203 //
204 // No covered work shall be deemed part of an effective technological
205 //measure under any applicable law fulfilling obligations under article
206 //11 of the WIPO copyright treaty adopted on 20 December 1996, or
207 //similar laws prohibiting or restricting circumvention of such
208 //measures.
209 //
210 // When you convey a covered work, you waive any legal power to forbid
211 //circumvention of technological measures to the extent such circumvention
212 //is effected by exercising rights under this License with respect to
213 //the covered work, and you disclaim any intention to limit operation or
214 //modification of the work as a means of enforcing, against the work's
215 //users, your or third parties' legal rights to forbid circumvention of
216 //technological measures.
217 //
218 // 4. Conveying Verbatim Copies.
219 //
220 // You may convey verbatim copies of the Program's source code as you
221 //receive it, in any medium, provided that you conspicuously and
222 //appropriately publish on each copy an appropriate copyright notice;
223 //keep intact all notices stating that this License and any
224 //non-permissive terms added in accord with section 7 apply to the code;
225 //keep intact all notices of the absence of any warranty; and give all
226 //recipients a copy of this License along with the Program.
227 //
228 // You may charge any price or no price for each copy that you convey,
229 //and you may offer support or warranty protection for a fee.
230 //
231 // 5. Conveying Modified Source Versions.
232 //
233 // You may convey a work based on the Program, or the modifications to
234 //produce it from the Program, in the form of source code under the
235 //terms of section 4, provided that you also meet all of these conditions:
236 //
237 // a) The work must carry prominent notices stating that you modified
238 // it, and giving a relevant date.
239 //
240 // b) The work must carry prominent notices stating that it is
241 // released under this License and any conditions added under section
242 // 7. This requirement modifies the requirement in section 4 to
243 // "keep intact all notices".
244 //
245 // c) You must license the entire work, as a whole, under this
246 // License to anyone who comes into possession of a copy. This
247 // License will therefore apply, along with any applicable section 7
248 // additional terms, to the whole of the work, and all its parts,
249 // regardless of how they are packaged. This License gives no
250 // permission to license the work in any other way, but it does not
251 // invalidate such permission if you have separately received it.
252 //
253 // d) If the work has interactive user interfaces, each must display
254 // Appropriate Legal Notices; however, if the Program has interactive
255 // interfaces that do not display Appropriate Legal Notices, your
256 // work need not make them do so.
257 //
258 // A compilation of a covered work with other separate and independent
259 //works, which are not by their nature extensions of the covered work,
260 //and which are not combined with it such as to form a larger program,
261 //in or on a volume of a storage or distribution medium, is called an
262 //"aggregate" if the compilation and its resulting copyright are not
263 //used to limit the access or legal rights of the compilation's users
264 //beyond what the individual works permit. Inclusion of a covered work
265 //in an aggregate does not cause this License to apply to the other
266 //parts of the aggregate.
267 //
268 // 6. Conveying Non-Source Forms.
269 //
270 // You may convey a covered work in object code form under the terms
271 //of sections 4 and 5, provided that you also convey the
272 //machine-readable Corresponding Source under the terms of this License,
273 //in one of these ways:
274 //
275 // a) Convey the object code in, or embodied in, a physical product
276 // (including a physical distribution medium), accompanied by the
277 // Corresponding Source fixed on a durable physical medium
278 // customarily used for software interchange.
279 //
280 // b) Convey the object code in, or embodied in, a physical product
281 // (including a physical distribution medium), accompanied by a
282 // written offer, valid for at least three years and valid for as
283 // long as you offer spare parts or customer support for that product
284 // model, to give anyone who possesses the object code either (1) a
285 // copy of the Corresponding Source for all the software in the
286 // product that is covered by this License, on a durable physical
287 // medium customarily used for software interchange, for a price no
288 // more than your reasonable cost of physically performing this
289 // conveying of source, or (2) access to copy the
290 // Corresponding Source from a network server at no charge.
291 //
292 // c) Convey individual copies of the object code with a copy of the
293 // written offer to provide the Corresponding Source. This
294 // alternative is allowed only occasionally and noncommercially, and
295 // only if you received the object code with such an offer, in accord
296 // with subsection 6b.
297 //
298 // d) Convey the object code by offering access from a designated
299 // place (gratis or for a charge), and offer equivalent access to the
300 // Corresponding Source in the same way through the same place at no
301 // further charge. You need not require recipients to copy the
302 // Corresponding Source along with the object code. If the place to
303 // copy the object code is a network server, the Corresponding Source
304 // may be on a different server (operated by you or a third party)
305 // that supports equivalent copying facilities, provided you maintain
306 // clear directions next to the object code saying where to find the
307 // Corresponding Source. Regardless of what server hosts the
308 // Corresponding Source, you remain obligated to ensure that it is
309 // available for as long as needed to satisfy these requirements.
310 //
311 // e) Convey the object code using peer-to-peer transmission, provided
312 // you inform other peers where the object code and Corresponding
313 // Source of the work are being offered to the general public at no
314 // charge under subsection 6d.
315 //
316 // A separable portion of the object code, whose source code is excluded
317 //from the Corresponding Source as a System Library, need not be
318 //included in conveying the object code work.
319 //
320 // A "User Product" is either (1) a "consumer product", which means any
321 //tangible personal property which is normally used for personal, family,
322 //or household purposes, or (2) anything designed or sold for incorporation
323 //into a dwelling. In determining whether a product is a consumer product,
324 //doubtful cases shall be resolved in favor of coverage. For a particular
325 //product received by a particular user, "normally used" refers to a
326 //typical or common use of that class of product, regardless of the status
327 //of the particular user or of the way in which the particular user
328 //actually uses, or expects or is expected to use, the product. A product
329 //is a consumer product regardless of whether the product has substantial
330 //commercial, industrial or non-consumer uses, unless such uses represent
331 //the only significant mode of use of the product.
332 //
333 // "Installation Information" for a User Product means any methods,
334 //procedures, authorization keys, or other information required to install
335 //and execute modified versions of a covered work in that User Product from
336 //a modified version of its Corresponding Source. The information must
337 //suffice to ensure that the continued functioning of the modified object
338 //code is in no case prevented or interfered with solely because
339 //modification has been made.
340 //
341 // If you convey an object code work under this section in, or with, or
342 //specifically for use in, a User Product, and the conveying occurs as
343 //part of a transaction in which the right of possession and use of the
344 //User Product is transferred to the recipient in perpetuity or for a
345 //fixed term (regardless of how the transaction is characterized), the
346 //Corresponding Source conveyed under this section must be accompanied
347 //by the Installation Information. But this requirement does not apply
348 //if neither you nor any third party retains the ability to install
349 //modified object code on the User Product (for example, the work has
350 //been installed in ROM).
351 //
352 // The requirement to provide Installation Information does not include a
353 //requirement to continue to provide support service, warranty, or updates
354 //for a work that has been modified or installed by the recipient, or for
355 //the User Product in which it has been modified or installed. Access to a
356 //network may be denied when the modification itself materially and
357 //adversely affects the operation of the network or violates the rules and
358 //protocols for communication across the network.
359 //
360 // Corresponding Source conveyed, and Installation Information provided,
361 //in accord with this section must be in a format that is publicly
362 //documented (and with an implementation available to the public in
363 //source code form), and must require no special password or key for
364 //unpacking, reading or copying.
365 //
366 // 7. Additional Terms.
367 //
368 // "Additional permissions" are terms that supplement the terms of this
369 //License by making exceptions from one or more of its conditions.
370 //Additional permissions that are applicable to the entire Program shall
371 //be treated as though they were included in this License, to the extent
372 //that they are valid under applicable law. If additional permissions
373 //apply only to part of the Program, that part may be used separately
374 //under those permissions, but the entire Program remains governed by
375 //this License without regard to the additional permissions.
376 //
377 // When you convey a copy of a covered work, you may at your option
378 //remove any additional permissions from that copy, or from any part of
379 //it. (Additional permissions may be written to require their own
380 //removal in certain cases when you modify the work.) You may place
381 //additional permissions on material, added by you to a covered work,
382 //for which you have or can give appropriate copyright permission.
383 //
384 // Notwithstanding any other provision of this License, for material you
385 //add to a covered work, you may (if authorized by the copyright holders of
386 //that material) supplement the terms of this License with terms:
387 //
388 // a) Disclaiming warranty or limiting liability differently from the
389 // terms of sections 15 and 16 of this License; or
390 //
391 // b) Requiring preservation of specified reasonable legal notices or
392 // author attributions in that material or in the Appropriate Legal
393 // Notices displayed by works containing it; or
394 //
395 // c) Prohibiting misrepresentation of the origin of that material, or
396 // requiring that modified versions of such material be marked in
397 // reasonable ways as different from the original version; or
398 //
399 // d) Limiting the use for publicity purposes of names of licensors or
400 // authors of the material; or
401 //
402 // e) Declining to grant rights under trademark law for use of some
403 // trade names, trademarks, or service marks; or
404 //
405 // f) Requiring indemnification of licensors and authors of that
406 // material by anyone who conveys the material (or modified versions of
407 // it) with contractual assumptions of liability to the recipient, for
408 // any liability that these contractual assumptions directly impose on
409 // those licensors and authors.
410 //
411 // All other non-permissive additional terms are considered "further
412 //restrictions" within the meaning of section 10. If the Program as you
413 //received it, or any part of it, contains a notice stating that it is
414 //governed by this License along with a term that is a further
415 //restriction, you may remove that term. If a license document contains
416 //a further restriction but permits relicensing or conveying under this
417 //License, you may add to a covered work material governed by the terms
418 //of that license document, provided that the further restriction does
419 //not survive such relicensing or conveying.
420 //
421 // If you add terms to a covered work in accord with this section, you
422 //must place, in the relevant source files, a statement of the
423 //additional terms that apply to those files, or a notice indicating
424 //where to find the applicable terms.
425 //
426 // Additional terms, permissive or non-permissive, may be stated in the
427 //form of a separately written license, or stated as exceptions;
428 //the above requirements apply either way.
429 //
430 // 8. Termination.
431 //
432 // You may not propagate or modify a covered work except as expressly
433 //provided under this License. Any attempt otherwise to propagate or
434 //modify it is void, and will automatically terminate your rights under
435 //this License (including any patent licenses granted under the third
436 //paragraph of section 11).
437 //
438 // However, if you cease all violation of this License, then your
439 //license from a particular copyright holder is reinstated (a)
440 //provisionally, unless and until the copyright holder explicitly and
441 //finally terminates your license, and (b) permanently, if the copyright
442 //holder fails to notify you of the violation by some reasonable means
443 //prior to 60 days after the cessation.
444 //
445 // Moreover, your license from a particular copyright holder is
446 //reinstated permanently if the copyright holder notifies you of the
447 //violation by some reasonable means, this is the first time you have
448 //received notice of violation of this License (for any work) from that
449 //copyright holder, and you cure the violation prior to 30 days after
450 //your receipt of the notice.
451 //
452 // Termination of your rights under this section does not terminate the
453 //licenses of parties who have received copies or rights from you under
454 //this License. If your rights have been terminated and not permanently
455 //reinstated, you do not qualify to receive new licenses for the same
456 //material under section 10.
457 //
458 // 9. Acceptance Not Required for Having Copies.
459 //
460 // You are not required to accept this License in order to receive or
461 //run a copy of the Program. Ancillary propagation of a covered work
462 //occurring solely as a consequence of using peer-to-peer transmission
463 //to receive a copy likewise does not require acceptance. However,
464 //nothing other than this License grants you permission to propagate or
465 //modify any covered work. These actions infringe copyright if you do
466 //not accept this License. Therefore, by modifying or propagating a
467 //covered work, you indicate your acceptance of this License to do so.
468 //
469 // 10. Automatic Licensing of Downstream Recipients.
470 //
471 // Each time you convey a covered work, the recipient automatically
472 //receives a license from the original licensors, to run, modify and
473 //propagate that work, subject to this License. You are not responsible
474 //for enforcing compliance by third parties with this License.
475 //
476 // An "entity transaction" is a transaction transferring control of an
477 //organization, or substantially all assets of one, or subdividing an
478 //organization, or merging organizations. If propagation of a covered
479 //work results from an entity transaction, each party to that
480 //transaction who receives a copy of the work also receives whatever
481 //licenses to the work the party's predecessor in interest had or could
482 //give under the previous paragraph, plus a right to possession of the
483 //Corresponding Source of the work from the predecessor in interest, if
484 //the predecessor has it or can get it with reasonable efforts.
485 //
486 // You may not impose any further restrictions on the exercise of the
487 //rights granted or affirmed under this License. For example, you may
488 //not impose a license fee, royalty, or other charge for exercise of
489 //rights granted under this License, and you may not initiate litigation
490 //(including a cross-claim or counterclaim in a lawsuit) alleging that
491 //any patent claim is infringed by making, using, selling, offering for
492 //sale, or importing the Program or any portion of it.
493 //
494 // 11. Patents.
495 //
496 // A "contributor" is a copyright holder who authorizes use under this
497 //License of the Program or a work on which the Program is based. The
498 //work thus licensed is called the contributor's "contributor version".
499 //
500 // A contributor's "essential patent claims" are all patent claims
501 //owned or controlled by the contributor, whether already acquired or
502 //hereafter acquired, that would be infringed by some manner, permitted
503 //by this License, of making, using, or selling its contributor version,
504 //but do not include claims that would be infringed only as a
505 //consequence of further modification of the contributor version. For
506 //purposes of this definition, "control" includes the right to grant
507 //patent sublicenses in a manner consistent with the requirements of
508 //this License.
509 //
510 // Each contributor grants you a non-exclusive, worldwide, royalty-free
511 //patent license under the contributor's essential patent claims, to
512 //make, use, sell, offer for sale, import and otherwise run, modify and
513 //propagate the contents of its contributor version.
514 //
515 // In the following three paragraphs, a "patent license" is any express
516 //agreement or commitment, however denominated, not to enforce a patent
517 //(such as an express permission to practice a patent or covenant not to
518 //sue for patent infringement). To "grant" such a patent license to a
519 //party means to make such an agreement or commitment not to enforce a
520 //patent against the party.
521 //
522 // If you convey a covered work, knowingly relying on a patent license,
523 //and the Corresponding Source of the work is not available for anyone
524 //to copy, free of charge and under the terms of this License, through a
525 //publicly available network server or other readily accessible means,
526 //then you must either (1) cause the Corresponding Source to be so
527 //available, or (2) arrange to deprive yourself of the benefit of the
528 //patent license for this particular work, or (3) arrange, in a manner
529 //consistent with the requirements of this License, to extend the patent
530 //license to downstream recipients. "Knowingly relying" means you have
531 //actual knowledge that, but for the patent license, your conveying the
532 //covered work in a country, or your recipient's use of the covered work
533 //in a country, would infringe one or more identifiable patents in that
534 //country that you have reason to believe are valid.
535 //
536 // If, pursuant to or in connection with a single transaction or
537 //arrangement, you convey, or propagate by procuring conveyance of, a
538 //covered work, and grant a patent license to some of the parties
539 //receiving the covered work authorizing them to use, propagate, modify
540 //or convey a specific copy of the covered work, then the patent license
541 //you grant is automatically extended to all recipients of the covered
542 //work and works based on it.
543 //
544 // A patent license is "discriminatory" if it does not include within
545 //the scope of its coverage, prohibits the exercise of, or is
546 //conditioned on the non-exercise of one or more of the rights that are
547 //specifically granted under this License. You may not convey a covered
548 //work if you are a party to an arrangement with a third party that is
549 //in the business of distributing software, under which you make payment
550 //to the third party based on the extent of your activity of conveying
551 //the work, and under which the third party grants, to any of the
552 //parties who would receive the covered work from you, a discriminatory
553 //patent license (a) in connection with copies of the covered work
554 //conveyed by you (or copies made from those copies), or (b) primarily
555 //for and in connection with specific products or compilations that
556 //contain the covered work, unless you entered into that arrangement,
557 //or that patent license was granted, prior to 28 March 2007.
558 //
559 // Nothing in this License shall be construed as excluding or limiting
560 //any implied license or other defenses to infringement that may
561 //otherwise be available to you under applicable patent law.
562 //
563 // 12. No Surrender of Others' Freedom.
564 //
565 // If conditions are imposed on you (whether by court order, agreement or
566 //otherwise) that contradict the conditions of this License, they do not
567 //excuse you from the conditions of this License. If you cannot convey a
568 //covered work so as to satisfy simultaneously your obligations under this
569 //License and any other pertinent obligations, then as a consequence you may
570 //not convey it at all. For example, if you agree to terms that obligate you
571 //to collect a royalty for further conveying from those to whom you convey
572 //the Program, the only way you could satisfy both those terms and this
573 //License would be to refrain entirely from conveying the Program.
574 //
575 // 13. Use with the GNU Affero General Public License.
576 //
577 // Notwithstanding any other provision of this License, you have
578 //permission to link or combine any covered work with a work licensed
579 //under version 3 of the GNU Affero General Public License into a single
580 //combined work, and to convey the resulting work. The terms of this
581 //License will continue to apply to the part which is the covered work,
582 //but the special requirements of the GNU Affero General Public License,
583 //section 13, concerning interaction through a network will apply to the
584 //combination as such.
585 //
586 // 14. Revised Versions of this License.
587 //
588 // The Free Software Foundation may publish revised and/or new versions of
589 //the GNU General Public License from time to time. Such new versions will
590 //be similar in spirit to the present version, but may differ in detail to
591 //address new problems or concerns.
592 //
593 // Each version is given a distinguishing version number. If the
594 //Program specifies that a certain numbered version of the GNU General
595 //Public License "or any later version" applies to it, you have the
596 //option of following the terms and conditions either of that numbered
597 //version or of any later version published by the Free Software
598 //Foundation. If the Program does not specify a version number of the
599 //GNU General Public License, you may choose any version ever published
600 //by the Free Software Foundation.
601 //
602 // If the Program specifies that a proxy can decide which future
603 //versions of the GNU General Public License can be used, that proxy's
604 //public statement of acceptance of a version permanently authorizes you
605 //to choose that version for the Program.
606 //
607 // Later license versions may give you additional or different
608 //permissions. However, no additional obligations are imposed on any
609 //author or copyright holder as a result of your choosing to follow a
610 //later version.
611 //
612 // 15. Disclaimer of Warranty.
613 //
614 // THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
615 //APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
616 //HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
617 //OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
618 //THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
619 //PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
620 //IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
621 //ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
622 //
623 // 16. Limitation of Liability.
624 //
625 // IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
626 //WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
627 //THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
628 //GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
629 //USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
630 //DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
631 //PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
632 //EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
633 //SUCH DAMAGES.
634 //
635 // 17. Interpretation of Sections 15 and 16.
636 //
637 // If the disclaimer of warranty and limitation of liability provided
638 //above cannot be given local legal effect according to their terms,
639 //reviewing courts shall apply local law that most closely approximates
640 //an absolute waiver of all civil liability in connection with the
641 //Program, unless a warranty or assumption of liability accompanies a
642 //copy of the Program in return for a fee.
643 //
644 // END OF TERMS AND CONDITIONS
645 //
646 // How to Apply These Terms to Your New Programs
647 //
648 // If you develop a new program, and you want it to be of the greatest
649 //possible use to the public, the best way to achieve this is to make it
650 //free software which everyone can redistribute and change under these terms.
651 //
652 // To do so, attach the following notices to the program. It is safest
653 //to attach them to the start of each source file to most effectively
654 //state the exclusion of warranty; and each file should have at least
655 //the "copyright" line and a pointer to where the full notice is found.
656 //
657 // <one line to give the program's name and a brief idea of what it does.>
658 // Copyright (C) <year> <name of author>
659 //
660 // This program is free software: you can redistribute it and/or modify
661 // it under the terms of the GNU General Public License as published by
662 // the Free Software Foundation, either version 3 of the License, or
663 // (at your option) any later version.
664 //
665 // This program is distributed in the hope that it will be useful,
666 // but WITHOUT ANY WARRANTY; without even the implied warranty of
667 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
668 // GNU General Public License for more details.
669 //
670 // You should have received a copy of the GNU General Public License
671 // along with this program. If not, see <http://www.gnu.org/licenses/>.
672 //
673 //Also add information on how to contact you by electronic and paper mail.
674 //
675 // If the program does terminal interaction, make it output a short
676 //notice like this when it starts in an interactive mode:
677 //
678 // <program> Copyright (C) <year> <name of author>
679 // This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
680 // This is free software, and you are welcome to redistribute it
681 // under certain conditions; type `show c' for details.
682 //
683 //The hypothetical commands `show w' and `show c' should show the appropriate
684 //parts of the General Public License. Of course, your program's commands
685 //might be different; for a GUI interface, you would use an "about box".
686 //
687 // You should also get your employer (if you work as a programmer) or school,
688 //if any, to sign a "copyright disclaimer" for the program, if necessary.
689 //For more information on this, and how to apply and follow the GNU GPL, see
690 //<http://www.gnu.org/licenses/>.
691 //
692 // The GNU General Public License does not permit incorporating your program
693 //into proprietary programs. If your program is a subroutine library, you
694 //may consider it more useful to permit linking proprietary applications with
695 //the library. If this is what you want to do, use the GNU Lesser General
696 //Public License instead of this License. But first, please read
697 //<http://www.gnu.org/philosophy/why-not-lgpl.html>.
698 //-------------------------------------------------------------------------------------------------
699 //--------------------------------------------------------------------------------
700 /*
701 KNOWN DEFECTS
702 a)When window has the focus, edit control does not get page
703 ups and downs and arrows. This means one cannot really use the widget
704 without the mouse. Need to redirect these keystrokes to the
705 edit control.
706
707 b)The OPEN dialog box does not remember previous paths. I believe this
708 is an option I can set with a little storage--need to investigate.
709 In any case, very annoying when each time get thrown back into a different
710 directory than where I last opened files.
711
712 c)Does not correctly handle file sharing (Dan Parks, Dave Ashley).
713 For example, if one tries to signature a file which is currently
714 open in a word processor, it won't do it. Need to get sharing
715 behavior right.
716
717 SUGGESTIONS FOR FUTURE ENHANCEMENT:
718
719 a)Should sort files before beginning the signaturing (the drag'n'drop
720 interface delivers them in an unpredictable order).
721
722 b)Should accomodate more than 200 files (Cory Pratt). (Limiting factor
723 at present time is size of edit window display buffer.)
724
725 c)Should include a SAVE AS option (Raul Selgado).
726
727 d)Should include a print option (Dave Ashley).
728 */
729
730
731 #define GFSW_MAXFILES (200)
732 //Maximum number of files that will be processed and displayed.
733 //Limited to avoid filling up the edit control's buffer.
734
735 #define GFSW_DISPBUFLEN (55000)
736 //The point at which we say the edit control buffer has too
737 //many characters in it.
738
739 #define GFSW_FILE_READ_BUF_SIZE (0x10000)
740 //The number of bytes for each read buffer. It is difficult to know
741 //how to size this value. On one hand, larger is better, as it
742 //enables the disk to operate in blocks. On the other hand, too
743 //large is probably not a good idea, as it will take a lot of
744 //time to fill the buffer. Suspect that 16K-64K is about the
745 //optimum, but may go larger, anyway.
746
747 static char GFSW_dialog_file_open_buffer[15000];
748 //Buffer from the "open files" dialog box, for it to store results
749 //in.
750
751 static char GFSW_dispbuf[GFSW_DISPBUFLEN+1];
752 //The buffer which holds the contents of the edit control (the main control, the
753 //text box). This is a mirror buffer, probably not a very efficient way of doing
754 //things.
755
756 static int GFSW_use_sound = 1;
757 //Remembers the state of the "use sound" option. Default is YES.
758
759 static int GFSW_always_on_top = 1;
760 //Remembers whether the main window should always be on top.
761 //This can be annoying in some applications, so it can be turned off.
762
763 #define WM_USER_EV_CHILD_THREAD_PROGRESS (WM_USER + 1)
764 //The child thread has progress to report (updated information that
765 //can be used by the parent).
766
767 #define WM_USER_EV_CHILD_THREAD_ENDING (WM_USER + 2)
768 //The child thread is reporting that it is ending. After this event is
769 //posted for the parent, there may be some delay until the thread is actually
770 //terminated, so additional OS interaction is required to know the thread
771 //is actually dead.
772
773 static char GFSW_szAppName[] = "FileHash";
774 //The application name.
775
776 static WNDPROC GFSW_old_win_edit_handler = NULL;
777 //The old edit control handler. This is used to remember the function pointer,
778 //in order to subclass the behavior, because we must catch the drop files event.
779
780 static volatile int GFSW_thread_abort_request = 0;
781 //This is set TRUE by the main thread if the child thread
782 //should abort. This means the application will terminate
783 //(this is the only reason for setting this flag). This
784 //variable is set by the main thread and read by the
785 //child thread.
786
787 static DWORD GFSW_parent_thread_id = 0;
788 //The thread ID of the parent obtained using the
789 //GetCurrentThreadId() call. The child needs to know this
790 //in order to post a message.
791
792 static HWND GFSW_parent_thread_main_window_handle = 0;
793 //Window handle of main window.
794
795 static int GFSW_child_thread_is_active = 0;
796 //Set TRUE when the child thread is kicked off. Set
797 //FALSE when a message from the child thread is received
798 //indicating that the thread has terminated and when the
799 //thread is confirmed dead. This variable is used only
800 //by the main thread.
801
802 static HANDLE GFSW_child_handle = 0;
803 //The handle of the child process. This is only valid
804 //if a thread is active. This variable is used only
805 //by the main thread to check on status.
806
807 static unsigned char GFSW_file_read_buf[GFSW_FILE_READ_BUF_SIZE];
808 //File read buffer.
809
810 struct GFSW_FileInfoStructPerFile
811 {
812 volatile char fpath[MAX_PATH + 1];
813 //The full path to the file, which should be recognized by the operating system.
814 //If this string is empty, it is a signal that no file is in this slot.
815 volatile int is_directory_valid;
816 //TRUE if the is_directory flag is assigned by the child process and so the
817 //parent can rely on it.
818 volatile int is_directory_error;
819 //TRUE if the child process could not obtain directory information.
820 volatile int is_directory;
821 //TRUE if the file is actually a directory. This means it cannot be processed.
822 volatile int file_size_valid;
823 //TRUE if file size has been assigned by child (or an attempt to assign it was made).
824 volatile int file_size_error;
825 //There was an error in the attempt to obtain file size.
826 volatile unsigned __int64 file_size;
827 //The number of bytes in the file.
828 volatile int crc32_valid;
829 //TRUE if the CRC32 has been assigned, or an attempt was made to assign it. This
830 //means the child has filled in the string.
831 volatile char crc32[100];
832 //A string representation of the CRC-32, or else an error message from the child.
833 volatile int md5_valid;
834 //TRUE if the md5 is valid or an attempt was made.
835 volatile char md5[100];
836 //A string representation of the MD5 or else an error message from the child.
837 volatile int sha512_valid;
838 //TRUE if the SHA512 is valid or an attempt was made.
839 volatile char sha512[200];
840 //A string representation of the SHA512 or else an error message from the child.
841 };
842
843 struct GFSW_FileInfoStruct
844 {
845 volatile struct GFSW_FileInfoStructPerFile file_info[GFSW_MAXFILES];
846 volatile int terminated_normally;
847 volatile int aborted;
848 };
849
850
851 volatile struct GFSW_FileInfoStruct GFSW_file_info;
852 //This is the major structure that holds file information. Here are the three
853 //roles it fills:
854 //
855 // a)The parent thread fills in this information in preparation for starting
856 // up the child thread.
857 //
858 // b)The child thread fills in the information (file size, CRC, MD5) as it
859 // goes down this list, and notifies the parent thread with an event,
860 // which causes the updating of displayed information.
861 //
862 // c)After the child thread is absolutely complete, another event is send
863 // to indicate this.
864
865
866 //Function prototypes, to be sure no strange accidents.
867 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow);
868 LRESULT CALLBACK GFSW_ReplacementEditProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
869 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
870 static unsigned __stdcall GFSW_ChildThreadFunc(void *pArguments);
871 static void GFSW_ChildThreadHardSyncStop(void);
872 static void GFSW_ChildThreadStart(void);
873 static void GFSW_MakeGenericSound(void);
874
875
876
877 //Main function for Windows.
878 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
879 PSTR szCmdLine, int iCmdShow)
880 {
881 HWND hwnd ;
882 HMENU hmenu;
883 MSG msg ;
884 WNDCLASS wndclass ;
885
886 //Store our current parent thread ID. The child process will need
887 //to know this to post a message. This is expected to be
888 //invariant for the life of the program.
889 GFSW_parent_thread_id = GetCurrentThreadId();
890
891 //Zero out the file information block.
892 memset((char *)&GFSW_file_info, 0, sizeof(GFSW_file_info));
893
894 wndclass.style = CS_HREDRAW | CS_VREDRAW ;
895 wndclass.lpfnWndProc = WndProc ;
896 wndclass.cbClsExtra = 0 ;
897 wndclass.cbWndExtra = 0 ;
898 wndclass.hInstance = hInstance ;
899 wndclass.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_ICON)) ;
900 wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
901 wndclass.hbrBackground = (HBRUSH) (COLOR_BTNFACE+1) ;
902 wndclass.lpszMenuName = NULL ;
903 wndclass.lpszClassName = GFSW_szAppName ;
904
905 if (!RegisterClass (&wndclass))
906 {
907 MessageBox (NULL,
908 TEXT ("Error code from RegisterClass() Win32 API function. \nMost probable cause is incompatible operating system."),
909 GFSW_szAppName,
910 MB_ICONERROR) ;
911 return 0 ;
912 }
913
914 //We need to do some voodoo to figure out how large to make the main window.
915 //The feeling is that the window should be as small as possible to display the
916 //results from an individual file (the most common application), and if the user
917 //wants it bigger s/he can resize it. Four lines are required (file name, file
918 //size, file CRC-32, and file MD5), so that needs to be the height. A file
919 //MD5 is 128 bits = 32 characters. In addition, there is the constraint that we
920 //cannot go beyond the width and height of a screen. The guess below is quite
921 //crude, assuming that the edit box has a larger font, etc.
922 {
923 int initial_width = 0;
924 int initial_height = 0;
925
926 //Get a crude measure of how big text is.
927 initial_width = LOWORD(GetDialogBaseUnits());
928 initial_height = HIWORD(GetDialogBaseUnits());
929
930 //Dope it up linearly.
931 initial_width = (initial_width * 1500)/10;
932 initial_height = (initial_height * 150)/10;
933
934 {
935 int temp;
936
937 //Be sure nothing is bigger than 2/3 screen dimensions.
938 temp = (GetSystemMetrics(SM_CXFULLSCREEN) * 2) / 3;
939 if (initial_width > temp)
940 initial_width = temp;
941 temp = (GetSystemMetrics(SM_CYFULLSCREEN) * 2) / 3;
942 if (initial_height > temp)
943 initial_height = temp;
944 }
945
946 //Load up the menu resources. I have verified in documentation that this will be destroyed
947 //automatically.
948 hmenu = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_GFSW));
949
950 //Create the window. The "Ex" call is used to make it a topmost window.
951 hwnd = CreateWindowEx (
952 WS_EX_TOPMOST,
953 GFSW_szAppName, // window class name
954 TEXT ("FileHash v0.1"), // window caption
955 WS_OVERLAPPEDWINDOW, // window style
956 CW_USEDEFAULT, // initial x position
957 CW_USEDEFAULT, // initial y position
958 initial_width, // initial x size
959 initial_height, // initial y size
960 NULL, // parent window handle
961 hmenu, // window menu handle
962 hInstance, // program instance handle
963 NULL) ; // creation parameters
964 }
965
966
967 GFSW_parent_thread_main_window_handle = hwnd;
968
969 //This is standard code from Petzold's book. Don't know _exactly_ what it does, but
970 //every Windows program seems to have it.
971 ShowWindow (hwnd, iCmdShow) ;
972 UpdateWindow (hwnd) ;
973
974 while (GetMessage (&msg, NULL, 0, 0))
975 {
976 TranslateMessage (&msg) ;
977 DispatchMessage (&msg) ;
978 }
979
980 return msg.wParam ;
981 }
982
983
984 //This is the subclass WndProc. This had to be done to allow the edit
985 //control to catch the WM_DROPFILES event.
986 LRESULT CALLBACK GFSW_ReplacementEditProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
987 {
988 static int iDeltaPerLine = 120, iAccumDelta = 0;
989 //These are for mouse wheel logic. It may happen in the future that more
990 //precise mice without notches are introduced.
991
992 //We need to watch for the drop target notification message, and take action
993 //in that case.
994 switch(message)
995 {
996 case WM_DROPFILES:
997 {
998 int iFiles;
999 int i;
1000 HDROP hDropInfo = (HANDLE)wParam;
1001
1002 //Do a hard sync stop on the child thread. This should not take
1003 //very long. A new drag'n'drop event will just cause restart
1004 //with the new set of files.
1005 GFSW_ChildThreadHardSyncStop();
1006
1007 //Zero out the file information area and the text buffer mirror.
1008 memset((char *)&GFSW_file_info, 0, sizeof(GFSW_file_info));
1009 GFSW_dispbuf[0] = 0;
1010
1011 // Get the number of files and folders that were dropped.
1012 iFiles = DragQueryFile (hDropInfo, (DWORD)(-1), (LPSTR)NULL, 0);
1013
1014 //Copy the paths to the right buffer area.
1015 for (i=0; (i<iFiles) && (i<GFSW_MAXFILES); i++)
1016 {
1017 DragQueryFile (hDropInfo, i, (LPSTR)(GFSW_file_info.file_info[i].fpath), MAX_PATH);
1018 }
1019
1020 //Start up the child thread.
1021 GFSW_ChildThreadStart();
1022
1023 break;
1024 }
1025 case WM_MOUSEWHEEL:
1026 {
1027 iAccumDelta += (short) HIWORD (wParam);
1028
1029 while (iAccumDelta >= iDeltaPerLine)
1030 {
1031 SendMessage(hwnd, WM_VSCROLL, SB_PAGEUP, 0);
1032 iAccumDelta -= iDeltaPerLine;
1033 //iAccumDelta = 0;
1034 }
1035
1036 while (iAccumDelta <= -iDeltaPerLine)
1037 {
1038 SendMessage(hwnd, WM_VSCROLL, SB_PAGEDOWN, 0);
1039 iAccumDelta += iDeltaPerLine;
1040 //iAccumDelta = 0;
1041 }
1042
1043 break;
1044 }
1045 }
1046
1047 //Call the old Window procedure.
1048 return (CallWindowProc(GFSW_old_win_edit_handler, hwnd, message, wParam, lParam));
1049 }
1050
1051
1052 //Stages the display buffer in preparation for outputting it out
1053 //to the edit child window. This function generally interacts
1054 //with the file information data structure even while the
1055 //child thread is running. This is OK, as race conditions are
1056 //covered. Essentially, as the child thread proceeds, it updates
1057 //data then afterwards flags it as valid. This function
1058 //never proceeds into invalid data.
1059 static void GFSW_StageDisplayBuffer(void)
1060 {
1061 int dispbufidx;
1062 int nfiles;
1063 int len;
1064 int i;
1065 char buf[2*MAX_PATH+200];
1066
1067 //Count the number of files queued, and put that in the buffer.
1068 dispbufidx = 0;
1069 nfiles=0;
1070 for (i=0; i<GFSW_MAXFILES; i++)
1071 {
1072 if (GFSW_file_info.file_info[i].fpath[0])
1073 {
1074 nfiles++;
1075 }
1076 else
1077 {
1078 break;
1079 }
1080 }
1081 sprintf_s(buf, sizeof(buf), "Files to signature: %d.\x0d\x0a", nfiles);
1082 len = strlen(buf);
1083 if ((len + dispbufidx + 2) < GFSW_DISPBUFLEN)
1084 {
1085 strcpy_s(GFSW_dispbuf+dispbufidx, sizeof(GFSW_dispbuf) - dispbufidx,buf);
1086 dispbufidx += len;
1087 }
1088
1089 //Loop through and output the information for each file.
1090 for (i=0; i<GFSW_MAXFILES; i++)
1091 {
1092 if ((GFSW_file_info.file_info[i].fpath[0]) && (GFSW_file_info.file_info[i].is_directory_valid))
1093 {
1094 sprintf_s(buf, sizeof(buf), "--------------------\x0d\x0a");
1095 len = strlen(buf);
1096 if ((len + dispbufidx + 2) < GFSW_DISPBUFLEN)
1097 {
1098 strcpy_s(GFSW_dispbuf+dispbufidx, sizeof(GFSW_dispbuf)-dispbufidx, buf);
1099 dispbufidx += len;
1100 }
1101
1102 sprintf_s(buf, sizeof(buf), "[%03d/%03d] %s\x0d\x0a", i+1, nfiles, GFSW_file_info.file_info[i].fpath);
1103 len = strlen(buf);
1104 if ((len + dispbufidx + 2) < GFSW_DISPBUFLEN)
1105 {
1106 strcpy_s(GFSW_dispbuf+dispbufidx, sizeof(GFSW_dispbuf)-dispbufidx, buf);
1107 dispbufidx += len;
1108 }
1109
1110 //If the file is actually a directory or there was an error, we can't
1111 //do further processing on it.
1112 if (GFSW_file_info.file_info[i].is_directory_error)
1113 {
1114 sprintf_s(buf, sizeof(buf), "\tError obtaining file attributes--cannot process this file.\x0d\x0a");
1115 len = strlen(buf);
1116 if ((len + dispbufidx + 2) < GFSW_DISPBUFLEN)
1117 {
1118 strcpy_s(GFSW_dispbuf+dispbufidx, sizeof(GFSW_dispbuf)-dispbufidx, buf);
1119 dispbufidx += len;
1120 }
1121 }
1122 else if (GFSW_file_info.file_info[i].is_directory)
1123 {
1124 sprintf_s(buf, sizeof(buf), "\tThis item is a directory and will not be signatured.\x0d\x0a");
1125 len = strlen(buf);
1126 if ((len + dispbufidx + 2) < GFSW_DISPBUFLEN)
1127 {
1128 strcpy_s(GFSW_dispbuf+dispbufidx, sizeof(GFSW_dispbuf)-dispbufidx, buf);
1129 dispbufidx += len;
1130 }
1131 }
1132 else
1133 {
1134 //This is not a directory. We can print the rest of the
1135 //information.
1136 //
1137 //First, the file size.
1138 if (GFSW_file_info.file_info[i].file_size_valid)
1139 {
1140 char buf2[100];
1141
1142 buf2[0] = 0; //Just in case conversion doesn't go right.
1143 sprintf_s(buf2, sizeof(buf2), "%I64u", GFSW_file_info.file_info[i].file_size);
1144 BSTRFUNC_commanate(buf2);
1145 sprintf_s(buf, sizeof(buf), "\tSize:\t%s\x0d\x0a", buf2);
1146
1147 len = strlen(buf);
1148 if ((len + dispbufidx + 2) < GFSW_DISPBUFLEN)
1149 {
1150 strcpy_s(GFSW_dispbuf+dispbufidx, sizeof(GFSW_dispbuf)-dispbufidx, buf);
1151 dispbufidx += len;
1152 }
1153 }
1154
1155 //Now, the CRC-32.
1156 if (GFSW_file_info.file_info[i].crc32_valid)
1157 {
1158 sprintf_s(buf, sizeof(buf), "\tCRC-32:\t%s\x0d\x0a", GFSW_file_info.file_info[i].crc32);
1159 len = strlen(buf);
1160 if ((len + dispbufidx + 2) < GFSW_DISPBUFLEN)
1161 {
1162 strcpy_s(GFSW_dispbuf+dispbufidx, sizeof(GFSW_dispbuf)-dispbufidx, buf);
1163 dispbufidx += len;
1164 }
1165 }
1166
1167 //Now, the MD-5.
1168 if (GFSW_file_info.file_info[i].md5_valid)
1169 {
1170 sprintf_s(buf, sizeof(buf), "\tMD-5:\t%s\x0d\x0a", GFSW_file_info.file_info[i].md5);
1171 len = strlen(buf);
1172 if ((len + dispbufidx + 2) < GFSW_DISPBUFLEN)
1173 {
1174 strcpy_s(GFSW_dispbuf+dispbufidx, sizeof(GFSW_dispbuf)-dispbufidx, buf);
1175 dispbufidx += len;
1176 }
1177 }
1178
1179 //Now, the SHA512.
1180 if (GFSW_file_info.file_info[i].sha512_valid)
1181 {
1182 sprintf_s(buf, sizeof(buf), "\tSHA-512:\t%s\x0d\x0a", GFSW_file_info.file_info[i].sha512);
1183 len = strlen(buf);
1184 if ((len + dispbufidx + 2) < GFSW_DISPBUFLEN)
1185 {
1186 strcpy_s(GFSW_dispbuf+dispbufidx, sizeof(GFSW_dispbuf)-dispbufidx, buf);
1187 dispbufidx += len;
1188 }
1189 }
1190 }
1191 }
1192 else
1193 {
1194 break;
1195 }
1196
1197 }
1198
1199 //Process the termination condition information.
1200 if (GFSW_file_info.aborted)
1201 {
1202 sprintf_s(buf, sizeof(buf), "-------------------- FILE SIGNATURE ABORTED --------------------");
1203 }
1204 else if (GFSW_file_info.terminated_normally)
1205 {
1206 sprintf_s(buf, sizeof(buf), "-------------------- FILE SIGNATURE COMPLETE --------------------");
1207 }
1208 else
1209 {
1210 sprintf_s(buf, sizeof(buf), "-------------------- FILE SIGNATURE IN PROGRESS -------------------");
1211 }
1212
1213 len = strlen(buf);
1214 if ((len + dispbufidx + 2) < GFSW_DISPBUFLEN)
1215 {
1216 strcpy_s(GFSW_dispbuf+dispbufidx, sizeof(GFSW_dispbuf)-dispbufidx, buf);
1217 dispbufidx += len;
1218 }
1219 }
1220
1221
1222 //Pushes the display buffer out to the edit cotrol.
1223 static int GFSW_UpdateEditControl(HWND te_wind)
1224 {
1225 SetWindowText(te_wind, GFSW_dispbuf);
1226
1227 //Must set the buffer position (i.e. the display position) to the end.
1228 //The "/10" assumes that there can't be more than 8 chars or so per line--
1229 //couldn't find a way to go to the end automatically.
1230 SendMessage(te_wind,EM_LINESCROLL,0,GFSW_DISPBUFLEN/8);
1231
1232 return(0);
1233 }
1234
1235
1236 //This is the Windows procedure and event loop for the
1237 //main Window.
1238 //
1239 LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
1240 {
1241 HDC hdc ;
1242 PAINTSTRUCT ps ;
1243 static RECT rect ;
1244 static HWND te_wind = 0;
1245
1246 switch (message)
1247 {
1248 case WM_CREATE:
1249 {
1250 //Need to get the size of our current window so we can create the
1251 //child same size.
1252 //
1253 GetClientRect (hwnd, &rect);
1254
1255 te_wind = CreateWindow ("EDIT", // window class name
1256 "", // window caption
1257 WS_CHILD|WS_VISIBLE|WS_BORDER|ES_MULTILINE|ES_AUTOVSCROLL|ES_WANTRETURN, // window style
1258 0, // initial x position
1259 0, // initial y position
1260 rect.right, // initial x size
1261 rect.bottom, // initial y size
1262 hwnd, // parent window handle
1263 (HMENU) 1, // window menu handle
1264 ((LPCREATESTRUCT)lParam)->hInstance, // program instance handle
1265 NULL) ; // creation parameters
1266 //Print initial usage instructions.
1267 SetWindowText(te_wind, "To calculate CRC-32 cyclic redundancy codes, MD-5 message digests, "
1268 "and SHA-512 message digests, just "
1269 "drag and drop files into this box (from Windows Explorer "
1270 "or a similar application), or use the FILE->OPEN "
1271 "menu selection. Text can be freely selected and "
1272 "copied from this window (select the desired text "
1273 "and use the right mouse button for a popup menu "
1274 "which includes COPY).");
1275
1276 //Tell Windows that this child is a fair drop target for files.
1277 DragAcceptFiles (te_wind, TRUE);
1278
1279 //We need to subclass the child window, which is to define a new wndproc
1280 //for it.
1281 GFSW_old_win_edit_handler = (WNDPROC) SetWindowLong(te_wind, GWL_WNDPROC, (long) GFSW_ReplacementEditProc);
1282
1283 return 0 ;
1284 }
1285 case WM_SIZE:
1286 {
1287 rect.left = 0;
1288 rect.top = 0;
1289 rect.right = LOWORD(lParam);
1290 rect.bottom = HIWORD(lParam);
1291 MoveWindow(te_wind, 0, 0, rect.right, rect.bottom, TRUE);
1292 return 0;
1293 }
1294 case WM_PAINT:
1295 {
1296 hdc = BeginPaint (hwnd, &ps) ;
1297
1298 GetClientRect (hwnd, &rect) ;
1299
1300 EndPaint (hwnd, &ps) ;
1301 return 0 ;
1302 }
1303 case WM_DESTROY:
1304 {
1305 PostQuitMessage (0) ;
1306 GFSW_ChildThreadHardSyncStop();
1307 return 0 ;
1308 }
1309 //This is a home-spun event which means that there are files to be processed.
1310 //This is sent once when the drop is initially done and then each time a file
1311 //is processed another event is used to continue the process, until finally
1312 //there are no more files to do.
1313 case WM_USER_EV_CHILD_THREAD_PROGRESS:
1314 {
1315 //Just recalc the mirror buffer and redisplay it.
1316 GFSW_StageDisplayBuffer();
1317 GFSW_UpdateEditControl(te_wind);
1318 return 0;
1319 }
1320 case WM_USER_EV_CHILD_THREAD_ENDING:
1321 {
1322 HMENU hMenu;
1323
1324 //Just recalc the mirror buffer and redisplay it.
1325 GFSW_StageDisplayBuffer();
1326 GFSW_UpdateEditControl(te_wind);
1327 GFSW_MakeGenericSound();
1328
1329 hMenu = GetMenu(GFSW_parent_thread_main_window_handle);
1330
1331 EnableMenuItem(hMenu, ID_ACTIONS_HALTSIGOP, MF_GRAYED);
1332
1333 return 0;
1334 }
1335 case WM_COMMAND:
1336 {
1337 switch(LOWORD(wParam))
1338 {
1339 case ID_FILE_OPEN_FOR_SIG:
1340 {
1341 OPENFILENAME ofn;
1342 static char szFilter[] = "All Files (*.*)\0*.*\0\0";
1343
1344 //Set up for the dialog.
1345 GFSW_dialog_file_open_buffer[0] = 0; //Be sure that initial name zero'd out.
1346 GFSW_dialog_file_open_buffer[1] = 0;
1347
1348 ofn.lStructSize = sizeof(OPENFILENAME);
1349 ofn.hwndOwner = hwnd ;
1350 ofn.hInstance = NULL ;
1351 ofn.lpstrFilter = szFilter;
1352 ofn.lpstrCustomFilter = NULL ;
1353 ofn.nMaxCustFilter = 0 ;
1354 ofn.nFilterIndex = 0 ;
1355 ofn.lpstrFile = GFSW_dialog_file_open_buffer;
1356 ofn.nMaxFile = sizeof(GFSW_dialog_file_open_buffer) - 1;
1357 ofn.lpstrFileTitle = NULL;
1358 ofn.nMaxFileTitle = 0;
1359 ofn.lpstrInitialDir = NULL ;
1360 ofn.lpstrTitle = "Open File(s) For Signature Calculation" ;
1361 ofn.Flags = OFN_ALLOWMULTISELECT
1362 | OFN_FILEMUSTEXIST
1363 | OFN_HIDEREADONLY
1364 | OFN_NODEREFERENCELINKS
1365 | OFN_PATHMUSTEXIST
1366 | OFN_EXPLORER
1367 | OFN_SHAREAWARE;
1368 ofn.nFileOffset = 0 ;
1369 ofn.nFileExtension = 0 ;
1370 ofn.lpstrDefExt = NULL;
1371 ofn.lCustData = 0L ;
1372 ofn.lpfnHook = NULL ;
1373 ofn.lpTemplateName = NULL ;
1374
1375 //Zero out the dialog return area buffer.
1376 GFSW_dialog_file_open_buffer[0] = 0;
1377 GFSW_dialog_file_open_buffer[1] = 0;
1378
1379 if (GetOpenFileName (&ofn))
1380 {
1381 int dstarridx=0;
1382 int srccharidx=0;
1383
1384 //There was success and something specified. This means we must
1385 //kill the child thread if it is running.
1386 //Do a hard sync stop on the child thread. This should not take
1387 //very long. A new drag'n'drop event will just cause restart
1388 //with the new set of files.
1389 GFSW_ChildThreadHardSyncStop();
1390
1391 //Zero out the file information area and the text buffer mirror.
1392 memset((char *)&GFSW_file_info, 0, sizeof(GFSW_file_info));
1393 GFSW_dispbuf[0] = 0;
1394
1395 dstarridx=0;
1396 srccharidx=0;
1397
1398 //Advance to first string terminator.
1399 while (GFSW_dialog_file_open_buffer[srccharidx] != 0)
1400 srccharidx++;
1401
1402 //In the special case where the double-null is encountered immediately, there is
1403 //only one file, and full path is specified.
1404 //
1405 if (!GFSW_dialog_file_open_buffer[srccharidx+1])
1406 {
1407 strcpy_s((char *)GFSW_file_info.file_info[0].fpath, sizeof(GFSW_file_info.file_info[0].fpath), GFSW_dialog_file_open_buffer);
1408 }
1409 else
1410 {
1411 //This is the multiple file case.
1412 while ((dstarridx < GFSW_MAXFILES) && (GFSW_dialog_file_open_buffer[srccharidx+1]))
1413 {
1414 int len;
1415
1416 srccharidx++;
1417 strcpy_s((char *)GFSW_file_info.file_info[dstarridx].fpath, sizeof(GFSW_file_info.file_info[dstarridx].fpath), GFSW_dialog_file_open_buffer);
1418
1419 //Watch out for adding two backslashes. This can happen if the path
1420 //contains only a drive letter ("c:\", for example) so that the OS
1421 //adds a path on.
1422 len = strlen(GFSW_dialog_file_open_buffer);
1423 if ((len != 0) && (GFSW_dialog_file_open_buffer[len-1]=='\\'))
1424 {
1425 //Do nothing. Watch out in modifying this. The "len != 0" test
1426 //above is to prevent an access violation in the second test--watch
1427 //out when modifying this if-then-else construct.
1428 }
1429 else
1430 {
1431 strcat_s((char *)GFSW_file_info.file_info[dstarridx].fpath, sizeof(GFSW_file_info.file_info[dstarridx].fpath), "\\");
1432 }
1433 strcat_s((char *)GFSW_file_info.file_info[dstarridx].fpath, sizeof(GFSW_file_info.file_info[dstarridx].fpath), GFSW_dialog_file_open_buffer+srccharidx);
1434 while (GFSW_dialog_file_open_buffer[srccharidx] != 0)
1435 srccharidx++;
1436 dstarridx++;
1437 }
1438 }
1439
1440 //Start up the child thread.
1441 GFSW_ChildThreadStart();
1442
1443 //Redisplay the edit buffer.
1444 GFSW_StageDisplayBuffer();
1445 GFSW_UpdateEditControl(te_wind);
1446 }
1447
1448 return 0;
1449 }
1450 case ID_FILE_EXIT:
1451 {
1452 SendMessage(hwnd, WM_CLOSE, 0, 0);
1453 return 0;
1454 }
1455 case ID_ACTIONS_HALTSIGOP:
1456 {
1457 GFSW_ChildThreadHardSyncStop();
1458 GFSW_MakeGenericSound();
1459 GFSW_StageDisplayBuffer();
1460 GFSW_UpdateEditControl(te_wind);
1461 return 0;
1462 }
1463 case ID_OPTIONS_USE_SOUND:
1464 {
1465 if (GFSW_use_sound)
1466 {
1467 HMENU hMenu;
1468
1469 hMenu = GetMenu(GFSW_parent_thread_main_window_handle);
1470 GFSW_use_sound = 0;
1471 CheckMenuItem(hMenu, ID_OPTIONS_USE_SOUND, MF_UNCHECKED);
1472 }
1473 else
1474 {
1475 HMENU hMenu;
1476
1477 hMenu = GetMenu(GFSW_parent_thread_main_window_handle);
1478 GFSW_use_sound = 1;
1479 CheckMenuItem(hMenu, ID_OPTIONS_USE_SOUND, MF_CHECKED);
1480 }
1481 return 0;
1482 }
1483 case ID_OPTIONS_WINDOWALWAYSONTOP:
1484 {
1485 if (GFSW_always_on_top)
1486 {
1487 HMENU hMenu;
1488
1489 hMenu = GetMenu(GFSW_parent_thread_main_window_handle);
1490 GFSW_always_on_top = 0;
1491 CheckMenuItem(hMenu, ID_OPTIONS_WINDOWALWAYSONTOP, MF_UNCHECKED);
1492 SetWindowPos(GFSW_parent_thread_main_window_handle,
1493 HWND_NOTOPMOST,
1494 0,
1495 0,
1496 0,
1497 0,
1498 SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
1499 }
1500 else
1501 {
1502 HMENU hMenu;
1503
1504 hMenu = GetMenu(GFSW_parent_thread_main_window_handle);
1505 GFSW_always_on_top = 1;
1506 CheckMenuItem(hMenu, ID_OPTIONS_WINDOWALWAYSONTOP, MF_CHECKED);
1507 SetWindowPos(GFSW_parent_thread_main_window_handle,
1508 HWND_TOPMOST,
1509 0,
1510 0,
1511 0,
1512 0,
1513 SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
1514 }
1515 return 0;
1516 }
1517 case ID_HELP_ABOUT:
1518 {
1519 char *msg;
1520 char *first_line = "FileHash (GUI file message digest widget)\n\n";
1521 char *second_line = "[Built on " __DATE__ " at " __TIME__ "]\n\n";
1522 char *third_line = "Please contact Dave Ashley (DASHLEY@GMAIL.COM) with any issues.";
1523
1524 msg = malloc(strlen(first_line)+strlen(second_line)+strlen(third_line)+1);
1525
1526 assert(msg != NULL);
1527
1528 if (msg)
1529 {
1530 strcpy_s(msg, strlen(first_line)+strlen(second_line)+strlen(third_line)+1, first_line);
1531 strcat_s(msg, strlen(first_line)+strlen(second_line)+strlen(third_line)+1, second_line);
1532 strcat_s(msg, strlen(first_line)+strlen(second_line)+strlen(third_line)+1, third_line);
1533 MessageBox (hwnd,
1534 msg,
1535 GFSW_szAppName,
1536 MB_OK|MB_ICONINFORMATION) ;
1537 free(msg);
1538 }
1539 return 0 ;
1540 }
1541 case ID_HELP_HELP:
1542 {
1543 MessageBox (hwnd,
1544 "This program was written by Dave Ashley\n"
1545 "(DASHLEY@GMAIL.COM). Please feel free\n"
1546 "to correspond by e-mail.",
1547 GFSW_szAppName,
1548 MB_OK|MB_ICONINFORMATION) ;
1549 return 0 ;
1550 }
1551 }
1552 break;
1553 }
1554 }
1555
1556 return DefWindowProc (hwnd, message, wParam, lParam) ;
1557 }
1558
1559
1560 //This is the child thread which is deployed in order to calculate the CRC-32
1561 //and MD5 of a single file. This is done because a single-threaded approach
1562 //does not allow the GUI to continue to receive Windows events, and it
1563 //clogs up the desktop with an unmoveable unresponsive blob. This child
1564 //thread keeps its eye on the abort flag, and will terminate if it sees that.
1565 //It takes its input from the top of the list of filenames, and returns
1566 //the CRC-32 and MD5, whic take some time to compute.
1567 static unsigned __stdcall GFSW_ChildThreadFunc(void *pArguments)
1568 {
1569 int fidx;
1570 //The index of the table entry we're currently processing.
1571 DWORD file_attributes;
1572 //The returned file attributes from the OS.
1573 HANDLE hFile = (HANDLE) -1;
1574 //The file handle used to get size and to open for reading. This being
1575 //not the error value signals that it is valid and will need to be closed.
1576 DWORD file_size=0;
1577 //File size obtained from a call to the GetFileSize() function.
1578 DWORD file_size_high=0;
1579 //High 32 bits of same.
1580 DWORD last_error=0;
1581 //Result of the GetLastError() function. This is required to properly decode
1582 //the file size when very large files are involved.
1583 DWORD rc = 0;
1584 //Return code from file seek.
1585 BOOL rv = 0;
1586
1587 struct CRCHASHFUNCS_Crc32StateStruct crc32s;
1588 struct ESRG_MD5_Md5StateStruct md5s;
1589 struct ESRG_MD5_Md5ResultStruct md5r;
1590 struct ESRG_SHA512_Sha512StateStruct sha512s;
1591 struct ESRG_SHA512_Sha512ResultStruct sha512r;
1592 int exit_flag = 0;
1593 DWORD bytes_read = 0;
1594 unsigned crc32;
1595
1596
1597 //We have been invoked. We now just signature files until the list is
1598 //done or until we are asked to gracefully abort.
1599 for (
1600 fidx = 0;
1601 (fidx<GFSW_MAXFILES) && (GFSW_file_info.file_info[fidx].fpath[0] != 0) && (!GFSW_thread_abort_request);
1602 fidx++
1603 )
1604 {
1605 //Set any values here that are critical to have right in case we must abort.
1606 hFile = (HANDLE) -1;
1607 //This has to be the error value. If we abort, need to know whether
1608 //to try to close this.
1609
1610 /********************************************************************************/
1611 /********************************************************************************/
1612 /******* F I L E A T T R I B U T E S ***************************************/
1613 /********************************************************************************/
1614 /********************************************************************************/
1615 file_attributes = GetFileAttributes((char *)GFSW_file_info.file_info[fidx].fpath);
1616 if (file_attributes == 0xFFFFFFFF)
1617 {
1618 GFSW_file_info.file_info[fidx].is_directory_error = 1;
1619 //Signal trouble trying to figure out if this is a directory. This also
1620 //signals that the OS does not understand the file path, so can't do much.
1621 }
1622 else if (file_attributes & FILE_ATTRIBUTE_DIRECTORY)
1623 {
1624 GFSW_file_info.file_info[fidx].is_directory = 1;
1625 //This is a directory.
1626 }
1627
1628 //In any case, the is_directory information is now valid.
1629 GFSW_file_info.file_info[fidx].is_directory_valid = 1;
1630
1631 /********************************************************************************/
1632 /********************************************************************************/
1633 /******* F I L E S I Z E ***************************************************/
1634 /********************************************************************************/
1635 /********************************************************************************/
1636 //If this is a directory or if there was an error getting information, we
1637 //cannot go forward with finding file size information.
1638 if (GFSW_file_info.file_info[fidx].is_directory || GFSW_file_info.file_info[fidx].is_directory_error)
1639 {
1640 GFSW_file_info.file_info[fidx].file_size_error = 1;
1641 }
1642 else
1643 {
1644 hFile = CreateFile ((char *)GFSW_file_info.file_info[fidx].fpath,
1645 GENERIC_READ,
1646 FILE_SHARE_READ | FILE_SHARE_WRITE,
1647 NULL,
1648 OPEN_EXISTING,
1649 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
1650 (HANDLE)NULL);
1651 //Try to open the file to look at it. The documentation
1652 //of the CreateFile() call is misleading in the MSDN help.
1653 //A web reference said that the flag combination above,
1654 //
1655 // FILE_SHARE_READ | FILE_SHARE_WRITE,
1656 //
1657 //means "exclude none", i.e. allow others to have file open for both read
1658 //and write. This seems to be the right combination to allow
1659 //the signaturing of documents that are open with Microsoft
1660 //Word or other applications. I have no idea what will happen
1661 //if the file size changes while the file is open, but I
1662 //assume that the EOF will become true or there will be an error,
1663 //either of which will terminate processing of the file.
1664 if (hFile == (HANDLE) -1)
1665 {
1666 //Open error. Can't go further.
1667 GFSW_file_info.file_info[fidx].file_size_error = 1;
1668 }
1669 else
1670 {
1671 //Try to obtain the file size.
1672 file_size = GetFileSize(hFile, &file_size_high);
1673
1674 //Try to get the error flags, which we may need.
1675 last_error = GetLastError();
1676
1677 //There is some voodoo involved with file size and the
1678 //error code. This is documented in the GetFileSize() docs.
1679 if ((file_size == 0xFFFFFFFF) && (last_error != NO_ERROR))
1680 {
1681 GFSW_file_info.file_info[fidx].file_size_error = 1;
1682 }
1683 else
1684 {
1685 unsigned __int64 lo, hi, composite;
1686 //If we're here, we have a clean size. Convert it and display it.
1687
1688 //Play the bit games to get the size.
1689 lo = (unsigned __int64)file_size;
1690 hi = (unsigned __int64)file_size_high;
1691 composite = lo + (hi << 32);
1692 GFSW_file_info.file_info[fidx].file_size = composite;
1693 }
1694 }
1695 }
1696
1697 //The file size information is now valid in any case.
1698 GFSW_file_info.file_info[fidx].file_size_valid = 1;
1699
1700 //At this point there is potentially progress to report to
1701 //the main thread. We will sit and spin until the main
1702 //thread will accept a posted event announcing progress
1703 //or until we are aborted.
1704 while (!PostMessage(GFSW_parent_thread_main_window_handle,
1705 WM_USER_EV_CHILD_THREAD_PROGRESS,
1706 0,
1707 0)
1708 &&
1709 !GFSW_thread_abort_request)
1710 {
1711 Sleep(1000); //Wait 1 second until trying again.
1712 //1000ms is a long time for a modern computer,
1713 //and this should be OK to ease the load if
1714 //the system is seriously busy or something.
1715 }
1716
1717 /********************************************************************************/
1718 /********************************************************************************/
1719 /******* F I L E C R C - 3 2, M D - 5, A N D S H A - 5 1 2 *******/
1720 /********************************************************************************/
1721 /********************************************************************************/
1722 if (hFile == (HANDLE) -1)
1723 {
1724 //File handle signals an error condition. We cannot
1725 //even read the file for CRC-32, MD-5, and SHA-512. Just set info
1726 //and boogie.
1727 strcpy_s((char *)GFSW_file_info.file_info[fidx].crc32, sizeof(GFSW_file_info.file_info[fidx].crc32), "INVALID FILE HANDLE");
1728 strcpy_s((char *)GFSW_file_info.file_info[fidx].md5, sizeof(GFSW_file_info.file_info[fidx].md5), "INVALID FILE HANDLE");
1729 strcpy_s((char *)GFSW_file_info.file_info[fidx].sha512, sizeof(GFSW_file_info.file_info[fidx].sha512), "INVALID FILE HANDLE");
1730 }
1731 else
1732 {
1733 //This is the meat of the matter where we read the file contents and do
1734 //the hash calculations. The assumption made is the the buffer size
1735 //will be somewhere in the range of 16K-1M, so that the time to process
1736 //that much data is fairly small. This should be enough granularity
1737 //for a graceful voluntary thread abort.
1738 //
1739 //Rewind the file to positon 0.
1740 rc = SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
1741 if (rc == 0xFFFFFFFF)
1742 {
1743 strcpy_s((char *)GFSW_file_info.file_info[fidx].crc32, sizeof(GFSW_file_info.file_info[fidx].crc32), "FILE SEEK ERROR");
1744 strcpy_s((char *)GFSW_file_info.file_info[fidx].md5, sizeof(GFSW_file_info.file_info[fidx].md5), "FILE SEEK ERROR");
1745 strcpy_s((char *)GFSW_file_info.file_info[fidx].sha512, sizeof(GFSW_file_info.file_info[fidx].sha512), "FILE SEEK ERROR");
1746 goto file_read_loop_endpoint;
1747 }
1748
1749 //Initialize the CRC-32 and MD-5 state blocks.
1750 CRCHASHFUNCS_Crc32StateStructOpen(&crc32s);
1751 ESRG_MD5_Md5StateStructOpen(&md5s);
1752 ESRG_SHA512_Sha512StateStructOpen(&sha512s);
1753
1754 //Iterate through, reading the file and processing the blocks. In the case
1755 //of an abort, we assign the strings appropriately.
1756
1757 exit_flag = 0;
1758 while (!exit_flag)
1759 {
1760 rv = ReadFile(hFile, GFSW_file_read_buf, GFSW_FILE_READ_BUF_SIZE, &bytes_read, NULL);
1761
1762 //Add whatever has been read to the running calculation.
1763 if (bytes_read)
1764 {
1765 CRCHASHFUNCS_Crc32StateStructAddData(&crc32s, GFSW_file_read_buf, bytes_read);
1766 ESRG_MD5_Md5StateStructAddData (&md5s, GFSW_file_read_buf, bytes_read);
1767 ESRG_SHA512_Sha512StateStructAddData(&sha512s, GFSW_file_read_buf, bytes_read);
1768 }
1769
1770 //Process error conditions.
1771 if (!rv)
1772 {
1773 strcpy_s((char *)GFSW_file_info.file_info[fidx].crc32, sizeof(GFSW_file_info.file_info[fidx].crc32), "FILE READ ERROR");
1774 strcpy_s((char *)GFSW_file_info.file_info[fidx].md5, sizeof(GFSW_file_info.file_info[fidx].md5), "FILE READ ERROR");
1775 strcpy_s((char *)GFSW_file_info.file_info[fidx].sha512, sizeof(GFSW_file_info.file_info[fidx].sha512), "FILE READ ERROR");
1776 goto file_read_loop_endpoint;
1777 }
1778
1779 if (!bytes_read)
1780 {
1781 //This is the no-error abort when we hit the end of the file naturally.
1782 exit_flag = 1;
1783 }
1784 else if (GFSW_thread_abort_request)
1785 {
1786 strcpy_s((char *)GFSW_file_info.file_info[fidx].crc32, sizeof(GFSW_file_info.file_info[fidx].crc32), "OPERATION ABORTED");
1787 strcpy_s((char *)GFSW_file_info.file_info[fidx].md5, sizeof(GFSW_file_info.file_info[fidx].md5), "OPERATION ABORTED");
1788 strcpy_s((char *)GFSW_file_info.file_info[fidx].sha512, sizeof(GFSW_file_info.file_info[fidx].sha512), "OPERATION ABORTED");
1789 goto file_read_loop_endpoint;
1790 }
1791 }
1792
1793 //This is the natural fallthrough when there was no error.
1794 //
1795 //Close up the CRC-32 structure.
1796 crc32 = CRCHASHFUNCS_Crc32Extract(&crc32s);
1797 CRCHASHFUNCS_Crc32StateStructClose(&crc32s);
1798
1799 //Close up the MD-5 structure.
1800 ESRG_MD5_Md5StateStructClose(&md5s, &md5r);
1801
1802 //Close up the SHA-512 structure.
1803 ESRG_SHA512_Sha512StateStructClose(&sha512s, &sha512r);
1804
1805 //Form up a CRC-32 string.
1806 sprintf_s((char *)GFSW_file_info.file_info[fidx].crc32, sizeof(GFSW_file_info.file_info[fidx].crc32), "%8.8X", crc32);
1807
1808 //Form up an MD-5 string.
1809 strcpy_s((char *)GFSW_file_info.file_info[fidx].md5, sizeof(GFSW_file_info.file_info[fidx].md5), md5r.md5_chars);
1810
1811 //Form up an SHA-512 string.
1812 strcpy_s((char *)GFSW_file_info.file_info[fidx].sha512, sizeof(GFSW_file_info.file_info[fidx].sha512), sha512r.sha512_chars);
1813
1814 //Report progress to parent thread.
1815 while (!PostMessage(GFSW_parent_thread_main_window_handle,
1816 WM_USER_EV_CHILD_THREAD_PROGRESS,
1817 0,
1818 0))
1819 {
1820 Sleep(1000); //Wait 1 second until trying again.
1821 }
1822
1823 file_read_loop_endpoint: ;
1824 }
1825
1826 //In any case both CRC32 and MD5 info filled in.
1827 GFSW_file_info.file_info[fidx].crc32_valid = 1;
1828 GFSW_file_info.file_info[fidx].md5_valid = 1;
1829 GFSW_file_info.file_info[fidx].sha512_valid = 1;
1830
1831 //If the file handle is valid (i.e. we opened the file) we must
1832 //close it, regardless of cause.
1833 if (hFile != (HANDLE)-1)
1834 {
1835 BOOL close_result;
1836
1837 close_result = CloseHandle(hFile);
1838
1839 if (!close_result)
1840 {
1841 strcpy_s((char *)GFSW_file_info.file_info[fidx].crc32, sizeof(GFSW_file_info.file_info[fidx].crc32), "FILE HANDLE CLOSE ERROR");
1842 strcpy_s((char *)GFSW_file_info.file_info[fidx].md5, sizeof(GFSW_file_info.file_info[fidx].md5), "FILE HANDLE CLOSE ERROR");
1843 strcpy_s((char *)GFSW_file_info.file_info[fidx].sha512, sizeof(GFSW_file_info.file_info[fidx].sha512), "FILE HANDLE CLOSE ERROR");
1844 }
1845 }
1846 } // End for() each table entry loop.
1847
1848
1849 //Report completion to parent thread.
1850 if (GFSW_thread_abort_request)
1851 GFSW_file_info.aborted = 1;
1852 else
1853 GFSW_file_info.terminated_normally = 1;
1854
1855 while (!PostMessage(GFSW_parent_thread_main_window_handle,
1856 WM_USER_EV_CHILD_THREAD_ENDING,
1857 0,
1858 0))
1859 {
1860 Sleep(1000); //Wait 1 second until trying again.
1861 }
1862
1863 return 0;
1864 }
1865
1866
1867 //This function does a hard sync stop on the child thread if it is running.
1868 //If not running, returns immediately. Because the time to CRC32 or MD5 a
1869 //single buffer is negligible, it shouldn't take long for the thread to
1870 //gracefully end itself.
1871 static void GFSW_ChildThreadHardSyncStop(void)
1872 {
1873 if (GFSW_child_thread_is_active)
1874 {
1875 HMENU hMenu;
1876
1877 GFSW_thread_abort_request = 1;
1878
1879 WaitForSingleObject(GFSW_child_handle, INFINITE);
1880
1881 //The thread is done.
1882 GFSW_child_thread_is_active = 0;
1883 GFSW_thread_abort_request = 0;
1884 GFSW_child_handle = 0;
1885
1886 hMenu = GetMenu(GFSW_parent_thread_main_window_handle);
1887
1888 EnableMenuItem(hMenu, ID_ACTIONS_HALTSIGOP, MF_GRAYED);
1889 }
1890 }
1891
1892
1893 //Starts up a child thread.
1894 static void GFSW_ChildThreadStart(void)
1895 {
1896 unsigned thread_id;
1897 //We just discard this.
1898
1899 //We don't dare start up our single child thread unless none is already running.
1900 //In the release version, such an error is just ignored, but in the debug version,
1901 //hard stop.
1902 assert(!GFSW_child_thread_is_active);
1903
1904 if (!GFSW_child_thread_is_active)
1905 {
1906 //Test compilation of second thread invocation.
1907 GFSW_child_handle = (HANDLE) _beginthreadex(NULL,
1908 0,
1909 &GFSW_ChildThreadFunc,
1910 NULL,
1911 0,
1912 &thread_id);
1913 if (!GFSW_child_handle)
1914 {
1915 //This is an error return from _beginthreadex.
1916 assert(0);
1917 }
1918 else
1919 {
1920 HMENU hMenu;
1921
1922 GFSW_child_thread_is_active = 1;
1923
1924 hMenu = GetMenu(GFSW_parent_thread_main_window_handle);
1925
1926 EnableMenuItem(hMenu, ID_ACTIONS_HALTSIGOP, MF_ENABLED);
1927 }
1928 }
1929 }
1930
1931
1932 //Makes a generic sound to alert user. Honors the GFSW_use_sound state.
1933 //
1934 static void GFSW_MakeGenericSound(void)
1935 {
1936 if (GFSW_use_sound)
1937 {
1938 MessageBeep(MB_ICONEXCLAMATION);
1939 }
1940 }
1941
1942
1943 /******************************************************************************
1944 ** $Log: gfsw.c,v $
1945 ** Revision 1.30 2009/11/29 02:16:04 dtashley
1946 ** Passed final review.
1947 **
1948 ** Revision 1.29 2009/11/28 18:17:03 dtashley
1949 ** Development edits.
1950 **
1951 ** Revision 1.28 2009/11/28 02:00:09 dtashley
1952 ** SHA-512 function calls added.
1953 **
1954 ** Revision 1.27 2009/11/28 00:09:41 dtashley
1955 ** Addition of SHA512 module.
1956 **
1957 ** Revision 1.26 2009/11/27 22:30:37 dtashley
1958 ** Warnings from newer version of Visual Studio addressed.
1959 **
1960 ** Revision 1.25 2002/06/24 05:01:12 dtashley
1961 ** Additional documentation added for CreateFile() call.
1962 **
1963 ** Revision 1.24 2002/06/23 07:11:55 dtashley
1964 ** Defect where would not CRC/MD5 files which were open in an application
1965 ** has been corrected.
1966 **
1967 ** Revision 1.23 2002/05/21 06:11:07 dtashley
1968 ** Addition of version information including date and time compiled.
1969 ** Feedback from Dan Parks, Cory Pratt, and Raul Selgado moved to
1970 ** top of file and more tersely stated.
1971 **
1972 ** Revision 1.22 2002/05/19 05:16:36 dtashley
1973 ** Additional usability by Cory Pratt added.
1974 **
1975 ** Revision 1.21 2002/05/15 20:07:47 dtashley
1976 ** Feedback from Dan Parks, Dick McKamie, and Raul Selgado added. This
1977 ** feedback represents things to do.
1978 **
1979 ** Revision 1.20 2002/05/15 04:56:23 dtashley
1980 ** Minor issue with two contiguous backslashes appearing in file paths
1981 ** corrected.
1982 **
1983 ** Revision 1.19 2002/05/14 09:50:36 dtashley
1984 ** Nearing completion of initial release of GFSW.
1985 **
1986 ** Revision 1.18 2002/05/14 07:32:51 dtashley
1987 ** Safety checkin.
1988 **
1989 ** Revision 1.17 2002/05/06 07:11:24 dtashley
1990 ** Safety checkin.
1991 **
1992 ** Revision 1.16 2002/05/04 09:07:51 dtashley
1993 ** Substantial strides towards completion.
1994 **
1995 ** Revision 1.15 2002/05/03 09:42:04 dtashley
1996 ** Completion of work on laptop. Current solution has trouble when doing
1997 ** an exit while child thread running--need to see if this also happens
1998 ** on Win2k and resolve. Near completion.
1999 **
2000 ** Revision 1.14 2002/05/03 07:31:16 dtashley
2001 ** Preparing to work on laptop again.
2002 **
2003 ** Revision 1.13 2002/05/03 05:08:28 dtashley
2004 ** Work complete on laptop.
2005 **
2006 ** Revision 1.12 2002/05/03 04:31:30 dtashley
2007 ** Checkins before working on laptop computer.
2008 **
2009 ** Revision 1.11 2002/05/03 00:32:23 dtashley
2010 ** Checkins before working on laptop computer.
2011 **
2012 ** Revision 1.10 2002/05/02 12:08:59 dtashley
2013 ** Edits on laptop towards multithreading.
2014 **
2015 ** Revision 1.9 2002/05/02 04:06:25 dtashley
2016 ** MD5 module working correctly, pending review of code.
2017 **
2018 ** Revision 1.8 2002/05/01 21:51:44 dtashley
2019 ** Completion of MD5 implementation. Implementation does not work correctly,
2020 ** and need to debug. Noticed in MD5 result that first hex digit of MD5 digest
2021 ** is always "8"--this is definitely a problem as this can't happen in practice.
2022 ** Checking in for baseline to debug.
2023 **
2024 ** Revision 1.7 2002/04/30 05:31:21 dtashley
2025 ** Minor mouse wheel handling change. Suspect that somehow integer counts
2026 ** were getting out of sync. May need to resolve later.
2027 **
2028 ** Revision 1.6 2002/04/30 05:03:42 dtashley
2029 ** Final checkin before addition of MD5.
2030 **
2031 ** Revision 1.5 2002/04/30 00:03:12 dtashley
2032 ** Code straightened up--ready for line-by-line review.
2033 **
2034 ** Revision 1.4 2002/04/29 19:43:54 dtashley
2035 ** Safety checkin.
2036 **
2037 ** Revision 1.3 2002/04/29 06:18:04 dtashley
2038 ** Evening safety checkin.
2039 **
2040 ** Revision 1.2 2002/04/28 06:15:48 dtashley
2041 ** Safety checkin.
2042 **
2043 ** Revision 1.1 2002/04/28 05:37:39 dtashley
2044 ** Initial checkin.
2045 *******************************************************************************
2046 ** End of GFSW.C. */

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25