summaryrefslogtreecommitdiff
path: root/isutf8.c
blob: 88bb38b36f639fc0617a45869c1bb67f82a094e7 (plain)
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
/*
 * isutf8.c - do the input files look like valid utf-8 byte streams?
 * 
 * Copyright (C) 2005  Lars Wirzenius
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <getopt.h>

#define VERSION "1.0"

/* 
 * I'm pretty sure there is a fancy trick to do this without a loop,
 * but I'm too tired to figure it out now. --liw
 */
static int high_ones(int c)
{
    int n;

    for (n = 0; (c & 0x80) == 0x80; c <<= 1)
        ++n;    
    return n;
}

static int is_utf8_byte_stream(FILE *file, char *filename, int quiet)
{
    int c, n, remaining_bytes;
    unsigned long line, col;
    
    remaining_bytes = 0;
    line = 1;
    col = 1;
    while ((c = getc(file)) != EOF) {
        n = high_ones(c);
        if (remaining_bytes > 0) {
            if (n == 1) {
                --remaining_bytes;
                if (remaining_bytes == 0)
                    ++col;
            } else
                goto error;
        } else if (n == 0) {
            /* 7-bit character, skip, but adjust position */
            if (c == '\n') {
                ++line;
                col = 1;
            } else
                ++col;
        } else if (n == 1)
            goto error; /* wrong place for continuation byte */
        else
            remaining_bytes = n - 1; /* start of multi-byte sequence */
    }
    if (remaining_bytes > 0)
        goto error;
    return 1;
    
error:
    if (!quiet) {
        printf("%s: line %lu, col %lu: invalid UTF-8 code\n", 
               filename, line, col);
    }
    return 0;
}

static void usage(const char *program_name)
{
    printf("Usage: %s [-hq] [--help] [--quiet] [file ...]\n", program_name);
    printf("Check whether input files are valid UTF-8.\n");
    printf("This is version %s.\n", VERSION);
}

int main(int argc, char **argv)
{
    int i, ok;
    FILE *file;

    int quiet;
    struct option options[] = {
        { "help", no_argument, NULL, 'h' },
        { "quiet", no_argument, &quiet, 1 },
    };
    int opt;
    
    quiet = 0;
    
    while ((opt = getopt_long(argc, argv, "hq", options, NULL)) != -1) {
        switch (opt) {
        case 0:
            break;
            
        case 'h':
            usage(argv[0]);
            exit(0);
            break;
            
        case 'q':
            quiet = 1;
            break;

        case '?':
            exit(EXIT_FAILURE);

        default:
            abort();
        }
    }

    if (optind == argc)
        ok = is_utf8_byte_stream(stdin, "stdin", quiet);
    else {
        ok = 1;
        for (i = optind; i < argc; ++i) {
            file = fopen(argv[i], "r");
            if (file == NULL) {
                fprintf(stderr, "isutf8: %s: error %d: %s\n", 
                        argv[i], errno, strerror(errno));
                ok = 0;
            } else {
                ok = is_utf8_byte_stream(file, argv[i], quiet) && ok;
                (void) fclose(file);
            }
        }
    }
    
    if (ok)
        exit(0);
    exit(EXIT_FAILURE);
}