implantisomd5和checkisomd5

以前每做出一个ISO都会同时生成一个md5值文件,防止复制过程中数据损坏;现使用implantisomd5和checkisomd5两个工具,可以将md5值嵌入ISO中并直接效验,方便不少。

用法:

implantisomd5 xxx.iso
checkisomd5 xxx.iso

$ implantisomd5 test2.iso
Inserting md5sum into iso image...
md5 = 1f7419e5fc8157e3c2a79a8023816454
Inserting fragment md5sums into iso image...
fragmd5 = ded8b32776d8ea11f436df6f6b4171
frags = 20
Setting supported flag to 0


$ checkisomd5 test2.iso
Press [Esc] to abort check.
The media check is complete, the result is: PASS.
It is OK to use this media.


只能植入一次
$ implantisomd5 test2.iso
ERROR: Application data has been used - not implanting md5sum!


实验:

$ mkdir iso
$ echo 'Hello World!' > iso/test
$ mkisofs -J -T -o test.iso iso/
$ cp test.iso test2.iso
$ implantisomd5 test2.iso
$ hexdump -C test.iso > test-dump
$ hexdump -C test2.iso > test2-dump
$ diff test-dump  test2-dump 

结果如下:
33,34c33,46
< 00008370  20 01 00 20 20 20 20 20  20 20 20 20 20 20 20 20  | ..             |
< 00008380  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
---
> 00008370  20 01 00 49 53 4f 20 4d  44 35 53 55 4d 20 3d 20  | ..ISO MD5SUM = |
> 00008380  31 66 37 34 31 39 65 35  66 63 38 31 35 37 65 33  |1f7419e5fc8157e3|
> 00008390  63 32 61 37 39 61 38 30  32 33 38 31 36 34 35 34  |c2a79a8023816454|
> 000083a0  3b 53 4b 49 50 53 45 43  54 4f 52 53 20 3d 20 31  |;SKIPSECTORS = 1|
> 000083b0  35 3b 52 48 4c 49 53 4f  53 54 41 54 55 53 3d 30  |5;RHLISOSTATUS=0|
> 000083c0  3b 46 52 41 47 4d 45 4e  54 20 53 55 4d 53 20 3d  |;FRAGMENT SUMS =|
> 000083d0  20 64 65 64 38 62 33 32  37 37 36 64 38 65 61 31  | ded8b32776d8ea1|
> 000083e0  31 66 34 33 36 64 66 36  66 36 62 34 31 37 31 3b  |1f436df6f6b4171;|
> 000083f0  46 52 41 47 4d 45 4e 54  20 43 4f 55 4e 54 20 3d  |FRAGMENT COUNT =|
> 00008400  20 32 30 3b 54 48 49 53  20 49 53 20 4e 4f 54 20  | 20;THIS IS NOT |
> 00008410  54 48 45 20 53 41 4d 45  20 41 53 20 52 55 4e 4e  |THE SAME AS RUNN|
> 00008420  49 4e 47 20 4d 44 35 53  55 4d 20 4f 4e 20 54 48  |ING MD5SUM ON TH|
> 00008430  49 53 20 49 53 4f 21 21  20 20 20 20 20 20 20 20  |IS ISO!!        |
> 00008440  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |



注意最后一句话:" THIS IS NOT THE SAME AS RUNNING MD5SUM ON THIS ISO!!"
$ md5sum test.iso
79771b464de80779e78b126ba0e918a7  test.iso
可见并不是简单将原ISO的md5值插入而已。


代码分析:

$ rpm -qf `which implantisomd5`
isomd5sum-1.0.7-1.fc15.i686

$ yumdownloader --source isomd5sum-1.0.7-1.fc15
isomd5sum-1.0.7-1.fc15.src.rpm                           |  24 kB     00:00  

$ rpm -ivh isomd5sum-1.0.7-1.fc15.src.rpm
   1:isomd5sum              warning: user mockbuild does not exist - using root
########################################### [100%]

$ rpmbuild -bp ~/rpmbuild/SPECS/isomd5sum.spec
Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.mwSxql
+ umask 022
+ cd /home/eric/rpmbuild/BUILD
+ LANG=C
+ export LANG
+ unset DISPLAY
+ cd /home/eric/rpmbuild/BUILD
+ rm -rf isomd5sum-1.0.7
+ /usr/bin/bzip2 -dc /home/eric/rpmbuild/SOURCES/isomd5sum-1.0.7.tar.bz2
+ /bin/tar -xf -
+ STATUS=0
+ '[' 0 -ne 0 ']'
+ cd isomd5sum-1.0.7
+ /bin/chmod -Rf a+rX,u+w,g-w,o-w .
+ exit 0

$ cd ~/rpmbuild/BUILD/isomd5sum-1.0.7/

# 在libimplantisomd5.c文件中找到核心代码,说明读取<16扇区 ~ END>; 根据ISO9660规范,扇区0~15未定义,全0;
    /* read up to 15 sectors from end, due to problems reading last few */
    /* sectors on burned CDs                                            */
    while (total < isosize - SKIPSECTORS*2048) {

# DUMP中的内容和注释一致
SKIPSECTORS = 15

# 测试是否读取<16扇区 ~ END>内容再计算MD5
2048*16=32768=0x8000
$ tail -c +32769 test.iso > test-sum.iso
$ md5sum test-sum.iso
91cccb33d25a976bd88911b71ce630a4  test-sum.iso

# 结果与预期不符,仔细查看while代码,跟进MD5_Final()函数,发现原来是用自己写的算法计算SUM。

# 插入位置为Primary Volume Descriptor的 APPDATA区域(偏移883=0x373; 0x8000+0x373=0x8373)

参考:

http://alumnus.caltech.edu/~pje/iso9660.html

你可能感兴趣的:(c,算法,application,工具,Descriptor,代码分析)