bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenstatic bool field_find(const char *const *names, const char *name,
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen unsigned int *idx_r)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen unsigned int i;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenint dsync_deserializer_init(const char *name, const char *const *required_fields,
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen const char **error_r)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen pool = pool_alloconly_create("dsync deserializer", 1024);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen deserializer = p_new(pool, struct dsync_deserializer, 1);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen deserializer->keys = (void *)p_strsplit_tabescaped(pool, header_line);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen deserializer->required_field_count = required_count =
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen dup_required_fields = p_new(pool, const char *, required_count + 1);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen p_new(pool, unsigned int, required_count + 1);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen for (i = 0; i < required_count; i++) {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen if (!field_find(deserializer->keys, required_fields[i],
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen "Header missing required field %s",
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen deserializer->required_fields = dup_required_fields;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenvoid dsync_deserializer_deinit(struct dsync_deserializer **_deserializer)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen struct dsync_deserializer *deserializer = *_deserializer;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenint dsync_deserializer_decode_begin(struct dsync_deserializer *deserializer,
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen struct dsync_deserializer_decoder **decoder_r,
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen const char **error_r)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen unsigned int i;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen pool = pool_alloconly_create("dsync deserializer decode", 1024);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen decoder = p_new(pool, struct dsync_deserializer_decoder, 1);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen /* fix NULLs */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen /* see if all required fields exist */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen for (i = 0; i < deserializer->required_field_count; i++) {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen unsigned int ridx = deserializer->required_field_indexes[i];
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen if (ridx >= decoder->values_count || values[ridx] == NULL) {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen *error_r = t_strdup_printf("Missing required field %s",
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainendsync_deserializer_find_field(struct dsync_deserializer *deserializer,
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen unsigned int i;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen for (i = 0; deserializer->keys[i] != NULL; i++) {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen if (strcmp(deserializer->keys[i], key) == 0) {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenbool dsync_deserializer_decode_try(struct dsync_deserializer_decoder *decoder,
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen if (!dsync_deserializer_find_field(decoder->deserializer, key, &idx) ||
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainendsync_deserializer_decode_get(struct dsync_deserializer_decoder *decoder,
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen const char *key)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen if (!dsync_deserializer_decode_try(decoder, key, &value)) {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainendsync_deserializer_decoder_get_name(struct dsync_deserializer_decoder *decoder)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenvoid dsync_deserializer_decode_finish(struct dsync_deserializer_decoder **_decoder)