首页
社区
课程
招聘
[原创]使用hash匹配mime。
发表于: 2013-9-23 14:49 7466

[原创]使用hash匹配mime。

2013-9-23 14:49
7466
我错了,向伟大的技术道歉。
先看一段网上的mime匹配代码,接下来我们对这段代码稍微的优化一下。
const char *get_mime_type(const char *name) {
	char *ext = strrchr((char*)name, '.');
	if (!ext)
		return NULL;
	if (strcmp(ext, ".html") == 0 || strcmp(ext, ".htm") == 0)
		return "text/html";
	if (strcmp(ext, ".jpg") == 0 || strcmp(ext, ".jpeg") == 0)
		return "image/jpeg";
	if (strcmp(ext, ".gif") == 0)
		return "image/gif";
	if (strcmp(ext, ".png") == 0)
		return "image/png";
	if (strcmp(ext, ".css") == 0)
		return "text/css";
	if (strcmp(ext, ".au") == 0)
		return "audio/basic";
	if (strcmp(ext, ".wav") == 0)
		return "audio/wav";
	if (strcmp(ext, ".avi") == 0)
		return "video/x-msvideo";
	if (strcmp(ext, ".mpeg") == 0 || strcmp(ext, ".mpg") == 0)
		return "video/mpeg";
	if (strcmp(ext, ".mp3") == 0)
		return "audio/mpeg";
	return NULL;
}

下面这段代码是根据hash来匹配mime原型,请看大屏幕。
namespace vws {
const char* vws_http_mime::get_ext_name(const char* filename) {
	const char* ext = strrchr(filename, '.');
	if (!ext) {
		return (char*) 0;
	} else {
		return ext;
	}

}

const char* vws_http_mime::to_lower_string(const char* string) {
	static char full[PATH_MAX];
	int i = 0;
	for (i = 0; *string; i++) {
		full[i] = tolower(*string++);
	}
	full[i] = 0;
	return full;
}

const char* vws_http_mime::find_ext_name_mime(const char* filename) {
	const char* ext = vws_http_mime::get_ext_name(filename);
	const char* ext_low = vws_http_mime::to_lower_string(ext);
	unsigned long value = vws_crc32::calculate((unsigned char*) ext_low,
			strlen(ext_low));
	if (sizeof(value) == 8) // 64 bit
	{
		value &= 0x00000000ffffffff;
	}
	switch (value) {
	case 0x81ee4181:
	case 0x5f3d3c9e:
	case 0x0b2b7803:
		return ("text/html");
	case 0xe62678db:
		return ("text/css");
	case 0xaf1c0660:
		return ("text/xml");
	case 0x3cdfce37:
		return ("image/gif");
	case 0x73e57256:
	case 0xd800c4ea:
		return ("image/jpeg");
	case 0x9b8c1153:
		return ("application/x-javascript");
	case 0xb9d75c1f:
		return ("application/atom+xml");
	case 0xfbc2b19c:
		return ("application/rss+xml");
	case 0xb5f167fb:
		return ("text/mathml");
	case 0x82df8146:
		return ("text/plain");
	case 0x12d0b640:
		return ("text/vnd.sun.j2me.app-descriptor");
	case 0xa440415d:
		return ("text/vnd.wap.wml");
	case 0xb8851199:
		return ("text/x-component");
	case 0x1df0dd93:
		return ("image/png");
	case 0x22bfd31e:
	case 0x8cfea950:
		return ("image/tiff");
	case 0x853e85c5:
		return ("image/vnd.wap.wbmp");
	case 0xb572b313:
		return ("image/x-icon");
	case 0x0c41fb35:
		return ("image/x-jng");
	case 0xaaac7c89:
		return ("image/x-ms-bmp");
	case 0x9dadfb93:
	case 0x0b4918aa:
		return ("image/svg+xml");
	case 0x07e98f8f:
		return ("image/webp");
	case 0xe6040311:
	case 0xf2fa3332:
	case 0xed58442c:
		return ("application/java-archive");
	case 0x4f972c30:
		return ("application/mac-binhex40");
	case 0x18a92367:
		return ("application/msword");
	case 0x9018058f:
		return ("application/pdf");
	case 0x2ba1eb88:
	case 0xc98657aa:
	case 0x851a31e2:
		return ("application/postscript");
	case 0xd95ec3b0:
		return ("application/rtf");
	case 0x3b0f3ad4:
		return ("application/vnd.ms-excel");
	case 0x4d0fa392:
		return ("application/vnd.ms-powerpoint");
	case 0x13c7b267:
		return ("application/vnd.wap.wmlc");
	case 0xb17c1b49:
		return ("application/vnd.google-earth.kml+xml");
	case 0x45a8ae18:
		return ("application/vnd.google-earth.kmz");
	case 0xed458aee:
		return ("application/x-7z-compressed");
	case 0xb8e536c5:
		return ("application/x-cocoa");
	case 0x82964b41:
		return ("application/x-java-archive-diff");
	case 0x374cc2a2:
		return ("application/x-java-jnlp-file");
	case 0xce9e7ac3:
		return ("application/x-makeself");
	case 0xa6a9e67d:
	case 0xd1aed6eb:
		return ("application/x-perl");
	case 0xfcea44d7:
	case 0x9775c196:
		return ("application/x-pilot");
	case 0xf431f1d9:
		return ("application/x-rar-compressed");
	case 0x2ae0df3c:
		return ("application/x-redhat-package-manager");
	case 0x15211f34:
		return ("application/x-sea");
	case 0xf3b1fa44:
		return ("application/x-shockwave-flash");
	case 0xd449b4d3:
		return ("application/x-stuffit");
	case 0x3885d28a:
	case 0x5ca1b6da:
		return ("application/x-tcl");
	case 0x88f6eb1f:
	case 0x1ed1ed46:
	case 0x6159dc39:
		return ("application/x-x509-ca-cert");
	case 0x201a9ef3:
		return ("application/x-xpinstall");
	case 0x7cfb4912:
		return ("application/xhtml+xml");
	case 0xdcf54b45:
		return ("application/zip");
	case 0x34cf84ee:
	case 0xf58b68f3:
	case 0xa33b6d35:
		return ("application/octet-stream");
	case 0x9541fb7b:
		return ("application/octet-stream");
	case 0x2df285fc:
		return ("application/octet-stream");
	case 0x9ab8cc97:
		return ("application/octet-stream");
	case 0xffb0a142:
	case 0x252a16af:
		return ("application/octet-stream");
	case 0x11daacab:
	case 0x75b1046b:
	case 0x16b768b2:
		return ("application/octet-stream");
	case 0xdf462acd:
	case 0x0366ce56:
	case 0xe7c66926:
		return ("audio/midi");
	case 0xb1494782:
		return ("audio/mpeg");
	case 0xdb488297:
		return ("audio/ogg");
	case 0xa039fd5b:
		return ("audio/x-m4a");
	case 0xea2ef842:
		return ("audio/x-realaudio");
	case 0x68efe458:
	case 0x3d3dd084:
		return ("video/3gpp");
	case 0x2f2dd221:
		return ("video/mp4");
	case 0xee324aef:
	case 0xdd4fd26f:
		return ("video/mpeg");
	case 0x7aa5fc03:
		return ("video/quicktime");
	case 0x64efe356:
		return ("video/webm");
	case 0x5ddd4021:
		return ("video/x-flv");
	case 0x23ea789c:
		return ("video/x-m4v");
	case 0x090eedb0:
		return ("video/x-mng");
	case 0x7270753d:
	case 0x887f485e:
		return ("video/x-ms-asf");
	case 0x5922b827:
		return ("video/x-ms-wmv");
	case 0x65b7a18a:
		return ("video/x-msvideo");
	default:
		break;
	}
	return ("text/unknown");
}
}

讲解完毕,谢谢观看。

[课程]Android-CTF解题方法汇总!

收藏
免费 5
支持
分享
最新回复 (9)
雪    币: 243
活跃值: (169)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
一般用多模匹配
2013-9-23 15:39
0
雪    币: 278
活跃值: (709)
能力值: ( LV15,RANK:520 )
在线值:
发帖
回帖
粉丝
3
不懂,求解。
2013-9-23 15:57
0
雪    币: 167
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
如果考虑概率和易维护性就不会优化,这段代码有很多可以改进。
2013-9-23 16:19
0
雪    币: 962
活跃值: (1681)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
你去测一下 根本不比原来的快.......
要用hash值也会直接用c语言版的hashmap, 而不会像这样直接硬编码
2013-9-23 16:19
0
雪    币: 278
活跃值: (709)
能力值: ( LV15,RANK:520 )
在线值:
发帖
回帖
粉丝
6
错觉,省事。
2013-9-23 17:04
0
雪    币: 278
活跃值: (709)
能力值: ( LV15,RANK:520 )
在线值:
发帖
回帖
粉丝
7
下次改正。
2013-9-23 17:05
0
雪    币: 406
活跃值: (164)
能力值: ( LV12,RANK:250 )
在线值:
发帖
回帖
粉丝
8
经这样一改,可维护性和可读性大大地降低了
2013-9-23 18:53
0
雪    币: 247
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
9
try this way:
(little  endian)

const char *get_mime_type(const char *name) {
char *ext = strrchr((char*)name, '.');
if (!ext)
return NULL;
int val = *(int*)ext;
if (val == 'mth.')
return "text/html";
if (val == 'gpj.' || val == 'epj.')
return "image/jpeg";
if (val == 'fig.')
return "image/gif";
//...
return NULL;
}
2013-9-24 21:14
0
雪    币: 278
活跃值: (709)
能力值: ( LV15,RANK:520 )
在线值:
发帖
回帖
粉丝
10
膜拜一下,这样也可以,第一次见。
2013-9-24 22:55
0
游客
登录 | 注册 方可回帖
返回
//