FFmpeg
Loading...
Searching...
No Matches
utf8.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2013 Stefano Sabatini
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <stdio.h>
22
23#include "libavutil/avstring.h"
24#include "libavutil/file.h"
25
26static const uint8_t vectors[] =
27 "A\n"
28 "\xC3\xA9\n" /* U+00E9 */
29 "\xE2\x82\xAC\n" /* U+20AC */
30 "\xF0\x9F\x98\x80\n" /* U+1F600 */
31 "\xF4\x8F\xBF\xBF\n" /* U+10FFFF */
32 "\xF4\x90\x80\x80\n" /* U+110000 */
33 "\xF8\x88\x80\x80\x80\n" /* 5 byte form of U+200000 */
34 "\xC0\x80\n" /* overlong NUL */
35 "\xE0\x80\x80\n" /* overlong NUL */
36 "\xED\xA0\x80\n" /* U+D800 surrogate */
37 "\xEF\xBF\xBF\n" /* U+FFFF */
38 "\x80\n" /* stray continuation */
39 "\xFE\n" /* invalid lead byte */
40 "\xC3" "A\n" /* missing continuation */
41 "\xE2\x82"; /* truncated */
42
43static void print_sequence(const char *p, int l, int indent)
44{
45 int i;
46 for (i = 0; i < l; i++)
47 printf("%02X", (uint8_t)p[i]);
48 printf("%*s", indent-l*2, "");
49}
50
51static void decode(const uint8_t *p, const uint8_t *endp)
52{
53 uint32_t code;
54
55 while (p < endp) {
56 int l, r;
57 const uint8_t *p0 = p;
58 code = UINT32_MAX;
59 r = av_utf8_decode(&code, &p, endp, 0);
60 l = (int)(p-p0);
61 print_sequence(p0, l, 20);
62 if (code != UINT32_MAX) {
63 printf("%-10d 0x%-10X %-5d ", code, code, l);
64 if (r >= 0) {
65 if (*p0 == '\n') printf("\\n\n");
66 else printf ("%.*s\n", l, p0);
67 } else {
68 printf("invalid code range\n");
69 }
70 } else {
71 printf("invalid sequence\n");
72 }
73 }
74}
75
76int main(int argc, char **argv)
77{
78 if (argc > 1) {
79 uint8_t *file_buf;
80 size_t file_buf_size;
81 int ret = av_file_map(argv[1], &file_buf, &file_buf_size, 0, NULL);
82 if (ret < 0)
83 return 1;
84 decode(file_buf, file_buf + file_buf_size);
85 av_file_unmap(file_buf, file_buf_size);
86 } else {
87 decode(vectors, vectors + sizeof(vectors) - 1);
88 }
89
90 return 0;
91}
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define NULL
Definition coverity.c:32
__device__ int printf(const char *,...)
int main
Definition dovi_rpuenc.c:38
Misc file utilities.
int av_utf8_decode(int32_t *codep, const uint8_t **bufp, const uint8_t *buf_end, unsigned int flags)
Read and decode a single UTF-8 code point (character) from the buffer in *buf, and update *buf to poi...
Definition avstring.c:367
#define r
Definition input.c:42
void av_file_unmap(uint8_t *bufptr, size_t size)
Unmap or free the buffer bufptr created by av_file_map().
Definition file.c:175
int av_file_map(const char *filename, uint8_t **bufptr, size_t *size, int log_offset, void *log_ctx)
Read the file with name filename, and put its content in a newly allocated buffer or map it with mmap...
Definition file.c:114
const uint8_t * code
Definition spdifenc.c:433
static const uint8_t vectors[]
Definition utf8.c:26
static void decode(const uint8_t *p, const uint8_t *endp)
Definition utf8.c:51
static void print_sequence(const char *p, int l, int indent)
Definition utf8.c:43