File: textdet.c

package info (click to toggle)
clamav 0.98.7+dfsg-0+deb6u2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 60,204 kB
  • ctags: 49,129
  • sloc: cpp: 267,090; ansic: 152,211; sh: 35,196; python: 2,630; makefile: 2,220; perl: 1,690; pascal: 1,218; lisp: 184; csh: 117; xml: 38; asm: 32; exp: 4
file content (204 lines) | stat: -rw-r--r-- 6,118 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * Text detection based on ascmagic.c from the file(1) utility.
 * Portions Copyright (C) 2008 Sourcefire, Inc.
 * Maintained by Tomasz Kojm <tkojm@clamav.net>
 *
 * Copyright (c) Ian F. Darwin 1986-1995.
 * Software written by Ian F. Darwin and others;
 * maintained 1995-present by Christos Zoulas and others.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice immediately at the beginning of the file, without modification,
 *    this list of conditions, and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif

#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <ctype.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "clamav.h"
#include "filetypes.h"
#include "textdet.h"
#include "others.h"

#define F 0   /* character never appears in text */
#define T 1   /* character appears in plain ASCII text */
#define I 2   /* character appears in ISO-8859 text */
#define X 3   /* character appears in non-ISO extended ASCII (Mac, IBM PC) */

static char text_chars[256] = {
	/*                  BEL BS HT LF    FF CR    */
	F, F, F, F, F, F, F, T, T, T, T, F, T, T, F, F,  /* 0x0X */
        /*                              ESC          */
	F, F, F, F, F, F, F, F, F, F, F, T, F, F, F, F,  /* 0x1X */
	T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0x2X */
	T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0x3X */
	T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0x4X */
	T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0x5X */
	T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0x6X */
	T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, F,  /* 0x7X */
	/*            NEL                            */
	X, X, X, X, X, T, X, X, X, X, X, X, X, X, X, X,  /* 0x8X */
	X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,  /* 0x9X */
	I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I,  /* 0xaX */
	I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I,  /* 0xbX */
	I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I,  /* 0xcX */
	I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I,  /* 0xdX */
	I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I,  /* 0xeX */
	I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I   /* 0xfX */
};

static int td_isascii(const unsigned char *buf, unsigned int len)
{
	unsigned int i;

    for(i = 0; i < len; i++)
	if(text_chars[buf[i]] == F)
	    return 0;

    return 1;
}

static int td_isutf8(const unsigned char *buf, unsigned int len)
{
	unsigned int i, j, gotone = 0;


    for(i = 0; i < len; i++) {
	if((buf[i] & 0x80) == 0) {  /* 0xxxxxxx is plain ASCII */
	    /*
	     * Even if the whole file is valid UTF-8 sequences,
	     * still reject it if it uses weird control characters.
	     */
	    if(text_chars[buf[i]] != T)
		return 0;

	} else if((buf[i] & 0x40) == 0) { /* 10xxxxxx never 1st byte */
	    return 0;
	} else {			   /* 11xxxxxx begins UTF-8 */
		unsigned int following;

	    if((buf[i] & 0x20) == 0) {		/* 110xxxxx */
		/* c = buf[i] & 0x1f; */
		following = 1;
	    } else if((buf[i] & 0x10) == 0) {	/* 1110xxxx */
		/* c = buf[i] & 0x0f; */
		following = 2;
	    } else if((buf[i] & 0x08) == 0) {	/* 11110xxx */
		/* c = buf[i] & 0x07; */
		following = 3;
	    } else if((buf[i] & 0x04) == 0) {	/* 111110xx */
		/* c = buf[i] & 0x03; */
		following = 4;
	    } else if((buf[i] & 0x02) == 0) {	/* 1111110x */
		/* c = buf[i] & 0x01; */
		following = 5;
	    } else {
		return 0;
	    }

	    for(j = 0; j < following; j++) {
		if(++i >= len)
		    return gotone;

		if((buf[i] & 0x80) == 0 || (buf[i] & 0x40))
		    return 0;

		/* c = (c << 6) + (buf[i] & 0x3f); */
	    }

	    gotone = 1;
	}
    }

    return gotone;
}

static int td_isutf16(const unsigned char *buf, unsigned int len)
{
	unsigned int be = 1, nobom = 0, i, c, bad = 0, high = 0;


    if(len < 2)
	return 0;

    if(buf[0] == 0xff && buf[1] == 0xfe)
	be = 0;
    else if(buf[0] == 0xfe && buf[1] == 0xff)
	be = 1;
    else
	nobom = 1;

    for(i = 2; i + 1 < len; i += 2) {
	if(be)
	    c = buf[i + 1] + 256 * buf[i];
	else
	    c = buf[i] + 256 * buf[i + 1];

	if(c == 0xfffe)
	    return 0;

	if(c < 128 && text_chars[c] != T) {
	    if(nobom)
		return 0;
	    else
		bad++;
	} else if (c >= 128) {
        high++;
    }
    }

    //   if (nobom && high >= len / 4)
    //        return 0;

    if(!nobom && bad >= len / 2)
	return 0;

    return 1 + be;
}

cli_file_t cli_texttype(const unsigned char *buf, unsigned int len)
{
	int ret;

    if(td_isascii(buf, len)) {
	cli_dbgmsg("Recognized ASCII text\n");
	return CL_TYPE_TEXT_ASCII;
    } else if(td_isutf8(buf, len)) {
	cli_dbgmsg("Recognized UTF-8 character data\n");
	return CL_TYPE_TEXT_UTF8;
    } else if((ret = td_isutf16(buf, len))) {
	cli_dbgmsg("Recognized %s character data\n", (ret == 1) ? "UTF-16LE" : "UTF-16BE");
	return (ret == 1) ? CL_TYPE_TEXT_UTF16LE : CL_TYPE_TEXT_UTF16BE;
    } else {
	cli_dbgmsg("Recognized binary data\n");
	return CL_TYPE_BINARY_DATA;
    }
}