-
Notifications
You must be signed in to change notification settings - Fork 5
/
text.c
168 lines (150 loc) · 4.77 KB
/
text.c
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
/* $OpenBSD: text.c,v 1.3 2017/04/18 14:16:48 nicm Exp $ */
/*
* Copyright (c) 2015 Nicholas Marriott <nicm@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <ctype.h>
#include <string.h>
#include "file.h"
#include "magic.h"
#include "xmalloc.h"
static const char *text_words[][3] = {
{ "msgid", "PO (gettext message catalogue)", "text/x-po" },
{ "dnl", "M4 macro language pre-processor", "text/x-m4" },
{ "import", "Java program", "text/x-java" },
{ "\"libhdr\"", "BCPL program", "text/x-bcpl" },
{ "\"LIBHDR\"", "BCPL program", "text/x-bcpl" },
{ "//", "C++ program", "text/x-c++" },
{ "virtual", "C++ program", "text/x-c++" },
{ "class", "C++ program", "text/x-c++" },
{ "public:", "C++ program", "text/x-c++" },
{ "private:", "C++ program", "text/x-c++" },
{ "/*", "C program", "text/x-c" },
{ "#include", "C program", "text/x-c" },
{ "char", "C program", "text/x-c" },
{ "The", "English", "text/plain" },
{ "the", "English", "text/plain" },
{ "double", "C program", "text/x-c" },
{ "extern", "C program", "text/x-c" },
{ "float", "C program", "text/x-c" },
{ "struct", "C program", "text/x-c" },
{ "union", "C program", "text/x-c" },
{ "CFLAGS", "make commands", "text/x-makefile" },
{ "LDFLAGS", "make commands", "text/x-makefile" },
{ "all:", "make commands", "text/x-makefile" },
{ ".PRECIOUS", "make commands", "text/x-makefile" },
{ ".ascii", "assembler program", "text/x-asm" },
{ ".asciiz", "assembler program", "text/x-asm" },
{ ".byte", "assembler program", "text/x-asm" },
{ ".even", "assembler program", "text/x-asm" },
{ ".globl", "assembler program", "text/x-asm" },
{ ".text", "assembler program", "text/x-asm" },
{ "clr", "assembler program", "text/x-asm" },
{ "(input", "Pascal program", "text/x-pascal" },
{ "program", "Pascal program", "text/x-pascal" },
{ "record", "Pascal program", "text/x-pascal" },
{ "dcl", "PL/1 program", "text/x-pl1" },
{ "Received:", "mail", "text/x-mail" },
{ ">From", "mail", "text/x-mail" },
{ "Return-Path:", "mail", "text/x-mail" },
{ "Cc:", "mail", "text/x-mail" },
{ "Newsgroups:", "news", "text/x-news" },
{ "Path:", "news", "text/x-news" },
{ "Organization:", "news", "text/x-news" },
{ "href=", "HTML document", "text/html" },
{ "HREF=", "HTML document", "text/html" },
{ "<body", "HTML document", "text/html" },
{ "<BODY", "HTML document", "text/html" },
{ "<html", "HTML document", "text/html" },
{ "<HTML", "HTML document", "text/html" },
{ "<!--", "HTML document", "text/html" },
{ NULL, NULL, NULL }
};
static int
text_is_ascii(u_char c)
{
const char cc[] = "\007\010\011\012\014\015\033";
if (c == '\0')
return (0);
if (strchr(cc, c) != NULL)
return (1);
return (c > 31 && c < 127);
}
static int
text_is_latin1(u_char c)
{
if (c >= 160)
return (1);
return (text_is_ascii(c));
}
static int
text_is_extended(u_char c)
{
if (c >= 128)
return (1);
return (text_is_ascii(c));
}
static int
text_try_test(const void *base, size_t size, int (*f)(u_char))
{
const u_char *data = base;
size_t offset;
for (offset = 0; offset < size; offset++) {
if (!f(data[offset]))
return (0);
}
return (1);
}
const char *
text_get_type(const void *base, size_t size)
{
if (text_try_test(base, size, text_is_ascii))
return ("ASCII");
if (text_try_test(base, size, text_is_latin1))
return ("ISO-8859");
if (text_try_test(base, size, text_is_extended))
return ("Non-ISO extended-ASCII");
return (NULL);
}
const char *
text_try_words(const void *base, size_t size, int flags)
{
const char *cp, *end, *next, *word;
size_t wordlen;
u_int i;
end = (const char *)base + size;
for (cp = base; cp != end; /* nothing */) {
while (cp != end && isspace((u_char)*cp))
cp++;
next = cp;
while (next != end && !isspace((u_char)*next))
next++;
for (i = 0; /* nothing */; i++) {
word = text_words[i][0];
if (word == NULL)
break;
wordlen = strlen(word);
if ((size_t)(next - cp) != wordlen)
continue;
if (memcmp(cp, word, wordlen) != 0)
continue;
if (flags & MAGIC_TEST_MIME)
return (text_words[i][2]);
return (text_words[i][1]);
}
cp = next;
}
return (NULL);
}