102 if (!filename || !*filename || !buffer || !length ||
104 const int descriptor =
open(filename, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
106 struct stat before, after;
109 if (fstat(
descriptor, &before) || !S_ISREG(before.st_mode) ||
114 if (
count < 0 && errno == EINTR)
continue;
115 if (
count < 0)
goto done;
116 if (
count == 0)
break;
117 used += (size_t)
count;
120 fstat(
descriptor, &after) || after.st_size != before.st_size ||
121 after.st_mtim.tv_sec != before.st_mtim.tv_sec ||
122 after.st_mtim.tv_nsec != before.st_mtim.tv_nsec ||
123 memchr(buffer,
'\0', used))
goto done;
136 "schema",
"filename_prefix",
"width",
"height",
"fx",
"fy",
"cx",
"cy",
137 "mount_roll_deg",
"mount_pitch_deg",
"mount_yaw_deg"
141 if (!
profile || !
read_file(filename, buffer,
sizeof(buffer), &length))
return false;
144 char prefix[65] = {0};
145 uint64_t width = 0, height = 0;
148 char *limit = buffer + length;
149 while (line < limit) {
150 char *next = memchr(line,
'\n', (
size_t)(limit - line));
151 if (next) *next++ =
'\0';
153 char *entry =
trim(line);
155 if (!*entry || *entry ==
'#')
continue;
156 char *separator = strchr(entry,
'=');
157 if (!separator)
return false;
159 const char *key =
trim(entry);
160 const char *value =
trim(separator);
166 seen |= UINT32_C(1) << field;
168 if (strcmp(value,
"PANGEA_CAMERA_V1") != 0)
return false;
169 }
else if (field == 1) {
170 const size_t prefix_length = strlen(value);
171 if (prefix_length == 0 || prefix_length > 64)
return false;
172 for (
size_t index = 0; index < prefix_length; ++index) {
173 const unsigned char character = (
unsigned char)value[index];
174 if (!((character >=
'A' && character <=
'Z') ||
175 (character >=
'a' && character <=
'z') ||
176 (character >=
'0' && character <=
'9') || character ==
'_' ||
177 character ==
'-'))
return false;
179 memcpy(prefix, value, prefix_length + 1);
180 }
else if (field == 2 || field == 3) {
182 if (!
parse_uint(value, &parsed) || parsed < 2 || parsed > 4096)
184 if (field == 2) width = parsed;
185 else height = parsed;
186 }
else if (!
parse_real(value, &values[field])) {
192 const unsigned camera_id = prefix[1] !=
'\0' ? 0 :
193 prefix[0] ==
'c' ? 1 : prefix[0] ==
'a' ? 2 :
194 prefix[0] ==
'l' ? 3 : 0;
195 if (seen != required || !prefix[0] || !width || !height)
return false;
198 .width = (unsigned)width, .height = (
unsigned)height,
199 .fx = values[4], .fy = values[5], .cx = values[6], .cy = values[7],
200 .mount_roll_deg = values[8], .mount_pitch_deg = values[9], .mount_yaw_deg = values[10]
203 .filename_prefix = {0}
205 memcpy(
result.filename_prefix, prefix,
sizeof(prefix));
static bool read_file(const char *filename, char *buffer, size_t capacity, size_t *length)
Read one bounded, text-only snapshot of a profile.