libtld 2.0.14
A library to determine the Top-Level Domain name of any Internet URI.
tld_file.h
Go to the documentation of this file.
1/* TLD library -- TLD, domain name, and sub-domain extraction
2 * Copyright (c) 2011-2025 Made to Order Software Corp. All Rights Reserved
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#ifndef LIB_TLD_FILE_H
24#define LIB_TLD_FILE_H
54// C
55//
56#include <stdint.h>
57
58
59#ifdef __cplusplus
60
61#include <iostream>
62
63extern "C" {
64#endif
65
66
67#define TLD_FILE_VERSION_MAJOR 1
68#define TLD_FILE_VERSION_MINOR 0
69
70#define TLD_HUNK(a, b, c, d) ((uint32_t)((a)|((b)<<8)|((c)<<16)|((d)<<24)))
71
72#define TLD_MAGIC TLD_HUNK('R','I','F','F')
73#define TLD_TLDS TLD_HUNK('T','L','D','S')
74#define TLD_HEADER TLD_HUNK('H','E','A','D')
75#define TLD_DESCRIPTIONS TLD_HUNK('D','E','S','C')
76#define TLD_TAGS TLD_HUNK('T','A','G','S')
77#define TLD_STRING_OFFSETS TLD_HUNK('S','O','F','F')
78#define TLD_STRING_LENGTHS TLD_HUNK('S','L','E','N')
79#define TLD_STRINGS TLD_HUNK('S','T','R','S')
80
81
82
83
85{
86 uint32_t f_riff; // 'RIFF' (Reversed IFF)
87 uint32_t f_size; // total size of this file - 8
88 uint32_t f_type; // 'TLDS'
89};
90
91
93{
94 uint32_t f_name;
95 uint32_t f_size;
96};
97
98
100{
101 // WARNING: do not change the version position
102 // anything else may change based on that information
103 //
104 uint8_t f_version_major; // 1.0
105 uint8_t f_version_minor;
106 uint8_t f_pad0;
107 uint8_t f_tld_max_level;
108
109 uint16_t f_tld_start_offset;
110 uint16_t f_tld_end_offset;
111
112 int64_t f_created_on;
113};
114
115
117{
118 uint8_t f_status;
120 uint16_t f_exception_apply_to; // index of tld_description this exception applies to
121
122 uint16_t f_start_offset; // next level or -1 (65535)
123 uint16_t f_end_offset;
124
125 uint16_t f_tld; // string ID
126
127 uint16_t f_tags; // offset in tld_tag table
128 uint16_t f_tags_count;
129};
130
131
133{
134 uint32_t f_tag_name; // string ID
135 uint32_t f_tag_value; // string ID
136};
137
138
140{
141 uint32_t f_string_offset; // offset in STRS
142};
143
144
146{
147 uint16_t f_string_length; // corresponding length
148};
149
150
152{
153 struct tld_header * f_header;
154 uint32_t f_descriptions_count;
155 struct tld_description * f_descriptions;
156 uint32_t f_tags_size; // WARNING: this is the number of uint32_t, not tld_tag
157 uint32_t * f_tags; // these are tld_tags which may be merged at any level (a tag id may be odd)
158 uint32_t f_strings_count;
159 struct tld_string_offset * f_string_offsets;
160 struct tld_string_length * f_string_lengths;
161 char * f_strings;
162 char * f_strings_end;
163};
164
165
166enum tld_file_error
167{
168 TLD_FILE_ERROR_NONE = 0,
169 TLD_FILE_ERROR_INVALID_POINTER,
170 TLD_FILE_ERROR_POINTER_PRESENT,
171 TLD_FILE_ERROR_CANNOT_OPEN_FILE,
172 TLD_FILE_ERROR_CANNOT_READ_FILE,
173 TLD_FILE_ERROR_UNRECOGNIZED_FILE,
174 TLD_FILE_ERROR_INVALID_FILE_SIZE,
175 TLD_FILE_ERROR_OUT_OF_MEMORY,
176 TLD_FILE_ERROR_INVALID_HUNK_SIZE,
177 TLD_FILE_ERROR_INVALID_STRUCTURE_SIZE,
178 TLD_FILE_ERROR_INVALID_ARRAY_SIZE,
179 TLD_FILE_ERROR_UNSUPPORTED_VERSION,
180 TLD_FILE_ERROR_MISSING_HUNK,
181 TLD_FILE_ERROR_HUNK_FOUND_TWICE,
182};
183
184
185enum tld_file_error tld_file_load(const char * filename, struct tld_file ** file);
186const char * tld_file_errstr(enum tld_file_error err);
187const struct tld_description * tld_file_description(struct tld_file const * file, uint32_t id);
188const struct tld_tag * tld_file_tag(struct tld_file const * file, uint32_t id);
189const char * tld_file_string(struct tld_file const * file, uint32_t id, uint32_t * length);
190char * tld_file_to_json(struct tld_file const * file);
191void tld_file_free(struct tld_file ** file);
192
193
194#ifdef __cplusplus
195
196enum tld_file_error tld_file_load_stream(tld_file ** file, std::istream & in);
197
199{
200public:
201 auto_free_string(char * s)
202 : f_string(s)
203 {
204 }
205 auto_free_string(auto_free_string const &) = delete;
206 auto_free_string & operator = (auto_free_string const &) = delete;
208 {
209 free(f_string);
210 }
211private:
212 char * f_string = nullptr;
213};
214
216{
217public:
219 : f_file(f)
220 {
221 }
222 auto_free_tld_file(auto_free_tld_file const &) = delete;
223 auto_free_tld_file & operator = (auto_free_tld_file const &) = delete;
225 {
226 tld_file_free(f_file);
227 }
228private:
229 tld_file ** f_file = nullptr;
230};
231
232}
233#endif
234
235#endif
236//#ifndef LIB_TLD_FILE_H
237// vim: ts=4 sw=4 et
[internal] The description of one TLD.
Definition tld_file.h:117
uint16_t f_end_offset
The last offset of a list of TLDs.
Definition tld_file.h:123
uint16_t f_tags_count
The number of tags defined by this TLD.
Definition tld_file.h:128
uint16_t f_start_offset
The first offset of a list of TLDs.
Definition tld_file.h:122
uint16_t f_tld
The actual TLD of this entry.
Definition tld_file.h:125
uint16_t f_exception_apply_to
This TLD is an exception of the "apply to" TLD.
Definition tld_file.h:120
uint16_t f_tags
The tags of this TLD.
Definition tld_file.h:127
uint8_t f_exception_level
This entry is an exception representing a TLD at this specified level.
Definition tld_file.h:119
uint8_t f_status
The status of this TLD.
Definition tld_file.h:118
char * tld_file_to_json(tld_file const *file)
Transform a tld_file to a JSON string.
Definition tld_file.cpp:414

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.