File: json-utf8.c

package info (click to toggle)
certmonger 0.79.21-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,428 kB
  • sloc: ansic: 59,314; sh: 6,767; xml: 2,486; makefile: 919; python: 564
file content (115 lines) | stat: -rw-r--r-- 2,874 bytes parent folder | download | duplicates (5)
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
/*
 * Copyright (C) 2015,2017 Red Hat, Inc.
 * 
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include "../../src/config.h"

#include <sys/types.h>
#include <errno.h>
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include <talloc.h>

#include "../../src/json.h"

int
main(int argc, char **argv)
{
	uint32_t point, point2;
	char buf[7];
	int n, o;

	for (point = 0; point < 0x5000000; point++) {
		if ((point >= 0xd800) && (point <= 0xdfff)) {
			continue;
		}
		n = cm_json_point_to_utf8_length(point);
		switch (n) {
		case 1:
			if (point > 0x7f) {
				fprintf(stderr, "error at point %lu: %d\n",
					(unsigned long) point, n);
				return n;
			}
			break;
		case 2:
			if ((point < 0x80) || (point > 0x7ff)) {
				fprintf(stderr, "error at point %lu: %d\n",
					(unsigned long) point, n);
				return n;
			}
			break;
		case 3:
			if ((point < 0x800) || (point > 0xffff)) {
				fprintf(stderr, "error at point %lu: %d\n",
					(unsigned long) point, n);
				return n;
			}
			break;
		case 4:
			if ((point < 0x10000) || (point > 0x1fffff)) {
				fprintf(stderr, "error at point %lu: %d\n",
					(unsigned long) point, n);
				return n;
			}
			break;
		case 5:
			if ((point < 0x200000) || (point > 0x3ffffff)) {
				fprintf(stderr, "error at point %lu: %d\n",
					(unsigned long) point, n);
				return n;
			}
			break;
		case 6:
			if ((point < 0x4000000) || (point > 0x7fffffff)) {
				fprintf(stderr, "error at point %lu: %d\n",
					(unsigned long) point, n);
				return n;
			}
			break;
		default:
			fprintf(stderr, "error at point %lu: %d\n",
				(unsigned long) point, n);
			return 7;
			break;
		}
		memset(buf, '\0', sizeof(buf));
		o = cm_json_point_to_utf8(point, buf, sizeof(buf));
		if (o != n) {
			fprintf(stderr, "error at encoding of %lu: %d\n",
				(unsigned long) point, o);
			return 8;
		}
		o = cm_json_utf8_to_point(buf, &point2);
		if (o != n) {
			fprintf(stderr, "error at decoding of %s (%lu): %d\n",
				buf, (unsigned long) point, o);
			return 8;
		}
		if (point2 != point) {
			fprintf(stderr, "decode mismatch: expected \"%s\" to be %lu, got %lu\n",
				buf, (unsigned long) point, (unsigned long) point2);
			return 9;
		}
	}
	return 0;
}