/*
  This file is part of Anastasis
  Copyright (C) 2026 Anastasis SARL

  Anastasis is free software; you can redistribute it and/or modify it under the
  terms of the GNU Lesser General Public License as published by the Free Software
  Foundation; either version 3, or (at your option) any later version.

  Anastasis 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 Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License along with
  Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file cli/anastasis-cli-common.h
 * @brief helpers shared by the Anastasis command line tools
 * @author Christian Grothoff
 */
#ifndef ANASTASIS_CLI_COMMON_H
#define ANASTASIS_CLI_COMMON_H

#include <jansson.h>
#include <stdio.h>


/**
 * Largest reducer state we are willing to read from a stream.  Matches the
 * 16 MB upload limit the provider backend applies to recovery documents, so
 * anything bigger could not have come from (or be sent to) a provider anyway.
 */
#define ANASTASIS_CLI_MAX_STATE_SIZE (16 * 1024 * 1024)


/**
 * Read a JSON value from @a f, refusing inputs larger than
 * #ANASTASIS_CLI_MAX_STATE_SIZE.  Unlike json_loadf(), this cannot be made to
 * consume unbounded memory by a caller that keeps writing to our stdin.  If
 * @a f is a regular file we simply map it and check st_size against the limit;
 * otherwise the limit bounds how much we read from the stream.
 *
 * @param f stream to read from, read until EOF
 * @param[out] error set to the parse (or size) error on failure
 * @return the parsed JSON, or NULL on error
 */
json_t *
ANASTASIS_CLI_json_load_capped (FILE *f,
                                json_error_t *error);


#endif
