aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-10-11 19:24:15 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2024-10-11 22:10:42 +0200
commitc4f64cf2556c0f8597e1fbc19d7c664b8da94612 (patch)
tree67cf5324d089dc8bafa255a5580556ba44361b0d /src
parent4668156da5915c8d7a7b40edaf6617b004547bd7 (diff)
downloadnissy-core-c4f64cf2556c0f8597e1fbc19d7c664b8da94612.tar.gz
nissy-core-c4f64cf2556c0f8597e1fbc19d7c664b8da94612.zip
Added buffer sizes in API args
Diffstat (limited to 'src')
-rw-r--r--src/nissy.c99
-rw-r--r--src/nissy.h99
2 files changed, 139 insertions, 59 deletions
diff --git a/src/nissy.c b/src/nissy.c
index d37256a..f8f5d09 100644
--- a/src/nissy.c
+++ b/src/nissy.c
@@ -16,7 +16,7 @@ int parse_h48_solver(const char *, uint8_t [static 1], uint8_t [static 1]);
16STATIC int64_t write_result(cube_t, char [static 22]); 16STATIC int64_t write_result(cube_t, char [static 22]);
17STATIC bool distribution_equal(const uint64_t [static INFO_DISTRIBUTION_LEN], 17STATIC bool distribution_equal(const uint64_t [static INFO_DISTRIBUTION_LEN],
18 const uint64_t [static INFO_DISTRIBUTION_LEN], uint8_t); 18 const uint64_t [static INFO_DISTRIBUTION_LEN], uint8_t);
19STATIC bool checkdata(const void *, const tableinfo_t *); 19STATIC bool checkdata(const char *, const tableinfo_t *);
20 20
21#define GETCUBE_OPTIONS(S, F) { .option = S, .fix = F } 21#define GETCUBE_OPTIONS(S, F) { .option = S, .fix = F }
22struct { 22struct {
@@ -67,7 +67,7 @@ parse_h48_solver_error:
67} 67}
68 68
69STATIC bool 69STATIC bool
70checkdata(const void *buf, const tableinfo_t *info) 70checkdata(const char *buf, const tableinfo_t *info)
71{ 71{
72 uint64_t distr[INFO_DISTRIBUTION_LEN]; 72 uint64_t distr[INFO_DISTRIBUTION_LEN];
73 73
@@ -202,6 +202,12 @@ nissy_applymoves(
202 cube_t c, res; 202 cube_t c, res;
203 int64_t err; 203 int64_t err;
204 204
205 if (moves == NULL) {
206 LOG("Error: 'moves' argument is NULL\n");
207 err = NISSY_ERROR_NULL_POINTER;
208 goto nissy_applymoves_error;
209 }
210
205 c = readcube("B32", cube); 211 c = readcube("B32", cube);
206 212
207 if (!isconsistent(c)) { 213 if (!isconsistent(c)) {
@@ -228,7 +234,7 @@ nissy_applymoves_error:
228int64_t 234int64_t
229nissy_applytrans( 235nissy_applytrans(
230 const char cube[static 22], 236 const char cube[static 22],
231 const char *transformation, 237 const char transformation[static 12],
232 char result[static 22] 238 char result[static 22]
233) 239)
234{ 240{
@@ -267,6 +273,12 @@ nissy_frommoves(
267 cube_t res; 273 cube_t res;
268 int64_t err; 274 int64_t err;
269 275
276 if (moves == NULL) {
277 LOG("Error: 'moves' argument is NULL\n");
278 err = NISSY_ERROR_NULL_POINTER;
279 goto nissy_frommoves_error;
280 }
281
270 res = applymoves(SOLVED_CUBE, moves); 282 res = applymoves(SOLVED_CUBE, moves);
271 283
272 if (!isconsistent(res)) { 284 if (!isconsistent(res)) {
@@ -287,13 +299,32 @@ nissy_convert(
287 const char *format_in, 299 const char *format_in,
288 const char *format_out, 300 const char *format_out,
289 const char *cube_string, 301 const char *cube_string,
290 char *result 302 size_t result_size,
303 char result[result_size]
291) 304)
292{ 305{
293 cube_t c; 306 cube_t c;
294 int ret; 307 int ret;
295 int64_t err; 308 int64_t err;
296 309
310 if (format_in == NULL) {
311 LOG("Error: 'format_in' argument is NULL\n");
312 err = NISSY_ERROR_NULL_POINTER;
313 goto nissy_convert_error;
314 }
315
316 if (format_out == NULL) {
317 LOG("Error: 'format_out' argument is NULL\n");
318 err = NISSY_ERROR_NULL_POINTER;
319 goto nissy_convert_error;
320 }
321
322 if (cube_string == NULL) {
323 LOG("Error: 'cube_string' argument is NULL\n");
324 err = NISSY_ERROR_NULL_POINTER;
325 goto nissy_convert_error;
326 }
327
297 c = readcube(format_in, cube_string); 328 c = readcube(format_in, cube_string);
298 329
299 if (iserror(c)) { 330 if (iserror(c)) {
@@ -334,6 +365,11 @@ nissy_getcube(
334 int i; 365 int i;
335 cube_t c; 366 cube_t c;
336 367
368 if (options == NULL) {
369 LOG("Error: 'options' argument is NULL\n");
370 return NISSY_ERROR_NULL_POINTER;
371 }
372
337 for (i = 0; getcube_options[i].option != NULL; i++) 373 for (i = 0; getcube_options[i].option != NULL; i++)
338 if (!strcmp(options, getcube_options[i].option)) 374 if (!strcmp(options, getcube_options[i].option))
339 getcube_options[i].fix(&ep, &eo, &cp, &co); 375 getcube_options[i].fix(&ep, &eo, &cp, &co);
@@ -355,13 +391,19 @@ nissy_datasize(
355 const char *solver 391 const char *solver
356) 392)
357{ 393{
394 if (solver == NULL) {
395 LOG("Error: 'solver' argument is NULL\n");
396 return NISSY_ERROR_NULL_POINTER;
397 }
398
358 /* gendata() handles a NULL *data as a "dryrun" request */ 399 /* gendata() handles a NULL *data as a "dryrun" request */
359 return nissy_gendata(solver, NULL); 400 return nissy_gendata(solver, 0, NULL);
360} 401}
361 402
362int64_t 403int64_t
363nissy_datainfo( 404nissy_datainfo(
364 const void *data, 405 size_t data_size,
406 const char data[data_size],
365 void (*write)(const char *, ...) 407 void (*write)(const char *, ...)
366) 408)
367{ 409{
@@ -396,7 +438,8 @@ nissy_datainfo(
396 } 438 }
397 439
398 if (info.next != 0) 440 if (info.next != 0)
399 return nissy_datainfo((char *)data + info.next, write); 441 return nissy_datainfo(
442 data_size - info.next, (char *)data + info.next, write);
400 443
401 write("\n---------\n"); 444 write("\n---------\n");
402 445
@@ -406,12 +449,18 @@ nissy_datainfo(
406int64_t 449int64_t
407nissy_gendata( 450nissy_gendata(
408 const char *solver, 451 const char *solver,
409 void *data 452 size_t data_size,
453 char data[data_size]
410) 454)
411{ 455{
412 int p; 456 int p;
413 gendata_h48_arg_t arg; 457 gendata_h48_arg_t arg;
414 458
459 if (solver == NULL) {
460 LOG("Error: 'solver' argument is NULL\n");
461 return NISSY_ERROR_NULL_POINTER;
462 }
463
415 arg.buf = data; 464 arg.buf = data;
416 if (!strncmp(solver, "h48", 3)) { 465 if (!strncmp(solver, "h48", 3)) {
417 p = parse_h48_solver(solver, &arg.h, &arg.k); 466 p = parse_h48_solver(solver, &arg.h, &arg.k);
@@ -427,8 +476,8 @@ nissy_gendata(
427 476
428int64_t 477int64_t
429nissy_checkdata( 478nissy_checkdata(
430 const char *solver, 479 uint64_t data_size,
431 const void *data 480 const char data[data_size]
432) 481)
433{ 482{
434 char *buf; 483 char *buf;
@@ -454,16 +503,23 @@ nissy_solve(
454 uint8_t nissflag, 503 uint8_t nissflag,
455 int8_t minmoves, 504 int8_t minmoves,
456 int8_t maxmoves, 505 int8_t maxmoves,
457 int64_t maxsolutions, 506 int64_t maxsols,
458 int8_t optimal, 507 int8_t optimal,
459 const void *data, 508 size_t data_size,
460 char *solutions 509 const char data[data_size],
510 size_t sols_size,
511 char sols[sols_size]
461) 512)
462{ 513{
463 cube_t c; 514 cube_t c;
464 int p; 515 int p;
465 uint8_t h, k; 516 uint8_t h, k;
466 517
518 if (solver == NULL) {
519 LOG("Error: 'solver' argument is NULL\n");
520 return NISSY_ERROR_NULL_POINTER;
521 }
522
467 c = readcube_B32(cube); 523 c = readcube_B32(cube);
468 524
469 if (!isconsistent(c)) { 525 if (!isconsistent(c)) {
@@ -486,24 +542,19 @@ nissy_solve(
486 maxmoves = 20; 542 maxmoves = 20;
487 } 543 }
488 544
489 if (maxsolutions < 0) { 545 if (maxsols < 0) {
490 LOG("solve: 'maxsols' is negative, stopping\n"); 546 LOG("solve: 'maxsols' is negative, stopping\n");
491 return NISSY_ERROR_OPTIONS; 547 return NISSY_ERROR_OPTIONS;
492 } 548 }
493 549
494 if (maxsolutions == 0) { 550 if (maxsols == 0) {
495 LOG("solve: 'maxsols' is 0, returning no solution\n"); 551 LOG("solve: 'maxsols' is 0, returning no solution\n");
496 return 0; 552 return 0;
497 } 553 }
498 554
499 if (solutions == NULL) {
500 LOG("solve: return parameter 'solutions' is NULL, stopping\n");
501 return NISSY_ERROR_NULL_POINTER;
502 }
503
504 if (!strncmp(solver, "h48", 3)) { 555 if (!strncmp(solver, "h48", 3)) {
505 if (!strcmp(solver, "h48stats")) 556 if (!strcmp(solver, "h48stats"))
506 return solve_h48stats(c, maxmoves, data, solutions); 557 return solve_h48stats(c, maxmoves, data, sols);
507 558
508 p = parse_h48_solver(solver, &h, &k); 559 p = parse_h48_solver(solver, &h, &k);
509 if (p != 0) { 560 if (p != 0) {
@@ -512,13 +563,13 @@ nissy_solve(
512 } else { 563 } else {
513 return THREADS > 1 ? 564 return THREADS > 1 ?
514 solve_h48_multithread(c, minmoves, 565 solve_h48_multithread(c, minmoves,
515 maxmoves, maxsolutions, data, solutions) : 566 maxmoves, maxsols, data, sols) :
516 solve_h48(c, minmoves, 567 solve_h48(c, minmoves,
517 maxmoves, maxsolutions, data, solutions); 568 maxmoves, maxsols, data, sols);
518 } 569 }
519 } else if (!strcmp(solver, "simple")) { 570 } else if (!strcmp(solver, "simple")) {
520 return solve_simple( 571 return solve_simple(
521 c, minmoves, maxmoves, maxsolutions, optimal, solutions); 572 c, minmoves, maxmoves, maxsols, optimal, sols);
522 } else { 573 } else {
523 LOG("solve: unknown solver '%s'\n", solver); 574 LOG("solve: unknown solver '%s'\n", solver);
524 return NISSY_ERROR_INVALID_SOLVER; 575 return NISSY_ERROR_INVALID_SOLVER;
diff --git a/src/nissy.h b/src/nissy.h
index e037e3b..d4c2760 100644
--- a/src/nissy.h
+++ b/src/nissy.h
@@ -98,7 +98,7 @@ Apply the given sequence of moves on the given cube.
98 98
99Parameters: 99Parameters:
100 cube - The cube to move, in B32 format. 100 cube - The cube to move, in B32 format.
101 moves - The moves to apply to the cube. 101 moves - The moves to apply to the cube. Must be a NULL-terminated string.
102 result - The return parameter for the resulting cube, in B32 format. 102 result - The return parameter for the resulting cube, in B32 format.
103 103
104Return values: 104Return values:
@@ -108,6 +108,7 @@ Return values:
108 or due to an unknown internal error. 108 or due to an unknown internal error.
109 NISSY_ERROR_INVALID_CUBE - The given cube is invalid. 109 NISSY_ERROR_INVALID_CUBE - The given cube is invalid.
110 NISSY_ERROR_INVALID_MOVES - The given moves are invalid. 110 NISSY_ERROR_INVALID_MOVES - The given moves are invalid.
111 NISSY_ERROR_NULL_POINTER - The 'moves' argument is NULL.
111*/ 112*/
112int64_t nissy_applymoves( 113int64_t nissy_applymoves(
113 const char cube[static 22], 114 const char cube[static 22],
@@ -132,7 +133,7 @@ Return values:
132*/ 133*/
133int64_t nissy_applytrans( 134int64_t nissy_applytrans(
134 const char cube[static 22], 135 const char cube[static 22],
135 const char *transformation, 136 const char transformation[static 12],
136 char result[static 22] 137 char result[static 22]
137); 138);
138 139
@@ -140,7 +141,8 @@ int64_t nissy_applytrans(
140Apply the given moves to the solved cube. 141Apply the given moves to the solved cube.
141 142
142Parameters: 143Parameters:
143 moves - The moves to be applied to the solved cube. 144 moves - The moves to be applied to the solved cube. Must be a
145 NULL-terminated string.
144 result - Return parameter for the resulting cube, in B32 format. 146 result - Return parameter for the resulting cube, in B32 format.
145 147
146Return values: 148Return values:
@@ -148,6 +150,7 @@ Return values:
148 NISSY_WARNING_UNSOLVABLE - The resulting cube is not solvable. This is 150 NISSY_WARNING_UNSOLVABLE - The resulting cube is not solvable. This is
149 probably due to an unknown internal error. 151 probably due to an unknown internal error.
150 NISSY_ERROR_INVALID_MOVES - The given moves are invalid. 152 NISSY_ERROR_INVALID_MOVES - The given moves are invalid.
153 NISSY_ERROR_NULL_POINTER - The 'moves' argument is NULL.
151*/ 154*/
152int64_t nissy_frommoves( 155int64_t nissy_frommoves(
153 const char *moves, 156 const char *moves,
@@ -158,23 +161,26 @@ int64_t nissy_frommoves(
158Convert the given cube between the two given formats. 161Convert the given cube between the two given formats.
159 162
160Parameters: 163Parameters:
161 format_in - The input format. 164 format_in - The input format.
162 format_out - The output format. 165 format_out - The output format.
163 string - The cube, in format_in format. 166 cube_string - The cube, in format_in format.
164 result - Return parameter for the cube in format_out format. Must be 167 retult_size - The allocated size of the result array.
165 large enough to contains the cube in this format. 168 result - Return parameter for the cube in format_out format.
166 169
167Return values: 170Return values:
168 NISSY_OK - The conversion was performed succesfully. 171 NISSY_OK - The conversion was performed succesfully.
169 NISSY_ERROR_INVALID_CUBE - The given cube is invalid. 172 NISSY_ERROR_INVALID_CUBE - The given cube is invalid.
170 NISSY_ERROR_INVALID_FORMAT - At least one of the given formats is invalid. 173 NISSY_ERROR_INVALID_FORMAT - At least one of the given formats is invalid.
171 NISSY_ERROR_UNKNOWN - An unknown error occurred. 174 NISSY_ERROR_UNKNOWN - An unknown error occurred.
175 NISSY_ERROR_NULL_POINTER - At least one of 'format_in', 'format_out' or
176 'cube_string' arguments is NULL.
172*/ 177*/
173int64_t nissy_convert( 178int64_t nissy_convert(
174 const char *format_in, 179 const char *format_in,
175 const char *format_out, 180 const char *format_out,
176 const char *cube, 181 const char *cube_string,
177 char *result 182 uint64_t result_size,
183 char result[result_size]
178); 184);
179 185
180/* 186/*
@@ -214,6 +220,7 @@ Parameters:
214 220
215Return values: 221Return values:
216 NISSY_ERROR_INVALID_SOLVER - The given solver is not known. 222 NISSY_ERROR_INVALID_SOLVER - The given solver is not known.
223 NISSY_ERROR_NULL_POINTER - The 'solver' argument is null.
217 NISSY_ERROR_UNKNOWN - An unknown error occurred. 224 NISSY_ERROR_UNKNOWN - An unknown error occurred.
218 Any value >= 0 - The size of the data, in bytes. 225 Any value >= 0 - The size of the data, in bytes.
219*/ 226*/
@@ -225,55 +232,75 @@ int64_t nissy_datasize(
225Compute the data for the given solver and store it in generated_data. 232Compute the data for the given solver and store it in generated_data.
226 233
227Parameters: 234Parameters:
228 solver - The name of the solver. 235 solver - The name of the solver.
229 data - The return parameter for the generated data. Must be large enoguh 236 data_size - The size of the data buffer. It is advised to use nissy_datasize
230 to contain the whole data. It is advised to use nissy_datasize to 237 to check how much memory is needed.
231 check how much memory is needed. 238 data - The return parameter for the generated data.
232 239
233Return values: 240Return values:
234 NISSY_ERROR_INVALID_SOLVER - The given solver is not known. 241 NISSY_ERROR_INVALID_SOLVER - The given solver is not known.
242 NISSY_ERROR_NULL_POINTER - The 'solver' argument is null.
235 NISSY_ERROR_UNKNOWN - An error occurred while generating the data. 243 NISSY_ERROR_UNKNOWN - An error occurred while generating the data.
236 Any value >= 0 - The size of the data, in bytes. 244 Any value >= 0 - The size of the data, in bytes.
237*/ 245*/
238int64_t nissy_gendata( 246int64_t nissy_gendata(
239 const char *solver, 247 const char *solver,
240 void *data 248 uint64_t data_size,
249 char data[data_size]
241); 250);
242 251
243/* 252/*
244Print information on a data table via the provided callback writer. 253Print information on a data table via the provided callback writer.
245 254
246Parameters: 255Parameters:
247 data - The data 256 data_size - The size of the data buffer.
248 write - A callback writer with the same signature as printf(3). 257 data - The data to be read.
258 write - A callback writer with the same signature as printf(3).
249 259
250Return values: 260Return values:
251 NISSY_OK - The data is correct. 261 NISSY_OK - The data is correct.
252 NISSY_ERROR_DATA - The data contains errors. 262 NISSY_ERROR_DATA - The data contains errors.
253*/ 263*/
254int64_t nissy_datainfo( 264int64_t nissy_datainfo(
255 const void *data, 265 uint64_t data_size,
266 const char data[data_size],
256 void (*write)(const char *, ...) 267 void (*write)(const char *, ...)
257); 268);
258 269
259/* 270/*
271Check that the data is a valid data table for a solver.
272
273Parameters:
274 data_size - The size of the data buffer.
275 data - The data for the solver. Can be computed with gendata.
276
277Return values:
278 NISSY_OK - The data is valid.
279 NISSY_ERROR_DATA - The data is invalid.
280*/
281int64_t nissy_checkdata(
282 uint64_t data_size,
283 const char data[data_size]
284);
285
286/*
260Solve the given cube using the given solver and options. 287Solve the given cube using the given solver and options.
261 288
262Parameters: 289Parameters:
263 cube - The cube to solver, in B32 format. 290 cube - The cube to solver, in B32 format.
264 solver - The name of the solver. 291 solver - The name of the solver.
265 nissflag - The flags for NISS (linear, inverse, mixed, or combinations). 292 nissflag - The flags for NISS (linear, inverse, mixed, or combinations).
266 minmoves - The minimum number of moves for a solution. 293 minmoves - The minimum number of moves for a solution.
267 maxmoves - The maximum number of moves for a solution. 294 maxmoves - The maximum number of moves for a solution.
268 maxsolutions - The maximum number of solutions. 295 maxsols - The maximum number of solutions.
269 optimal - If set to a non-negative value, the maximum number of moves 296 optimal - If set to a non-negative value, the maximum number of moves
270 above the optimal solution length. 297 above the optimal solution length.
271 data - The data for the solver. Can be computed with gendata. 298 data_size - The size of the data buffer.
272 solutions - The return parameter for the solutions. Must be large enough 299 data - The data for the solver. Can be computed with gendata.
273 to store all found solutions. The solutions are separated by 300 sols_size - The size of the solutions buffer.
274 a '\n' (newline) and a '\0' (NULL character) terminates the 301 sols - The return parameter for the solutions. The solutions are
275 list. 302 separated by a '\n' (newline) and a '\0' (NULL character)
276 TODO: replace with callback writer. 303 terminates the list.
277 304
278Return values: 305Return values:
279 NISSY_OK - Cube solved succesfully. 306 NISSY_OK - Cube solved succesfully.
@@ -281,8 +308,8 @@ Return values:
281 NISSY_ERROR_UNSOLVABLE_CUBE - The given cube is valid, but not solvable with 308 NISSY_ERROR_UNSOLVABLE_CUBE - The given cube is valid, but not solvable with
282 the given solver. 309 the given solver.
283 NISSY_ERROR_OPTIONS - One or more of the given options are invalid. 310 NISSY_ERROR_OPTIONS - One or more of the given options are invalid.
284 NISSY_ERROR_NULL_POINTER - One of the provided pointers is null.
285 NISSY_ERROR_INVALID_SOLVER - The given solver is not known. 311 NISSY_ERROR_INVALID_SOLVER - The given solver is not known.
312 NISSY_ERROR_NULL_POINTER - The 'solver' argument is null.
286 Any value >= 0 - The number of solutions found. 313 Any value >= 0 - The number of solutions found.
287*/ 314*/
288int64_t nissy_solve( 315int64_t nissy_solve(
@@ -293,8 +320,10 @@ int64_t nissy_solve(
293 int8_t maxmoves, 320 int8_t maxmoves,
294 int64_t maxsolutions, 321 int64_t maxsolutions,
295 int8_t optimal, 322 int8_t optimal,
296 const void *data, 323 uint64_t data_size,
297 char *solutions 324 const char data[data_size],
325 uint64_t sols_size,
326 char sols[sols_size]
298); 327);
299 328
300/* 329/*

Generated with cgit - Back to sebastiano.tronto.net