1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-21 19:55:07 +00:00
serenity/Userland/Libraries/LibIMAP/Parser.h
Tim Ledbetter 4b995542c4 LibIMAP: Make parsing of atom data fallible
We now return an error where `parse_atom()` would have previously
returned an empty StringView. This is consistent with RFC3501, which
says that an atom consists of one or more characters.

This prevents a few cases where parsing an invalid atom could lead to
an infinite loop.
2023-11-08 09:36:37 +01:00

74 lines
2.2 KiB
C++

/*
* Copyright (c) 2021, Kyle Pereira <hey@xylepereira.me>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/ByteBuffer.h>
#include <AK/Result.h>
#include <LibIMAP/Objects.h>
namespace IMAP {
class Client;
struct ParseStatus {
bool successful;
Optional<Response> response;
};
class Parser {
public:
ParseStatus parse(ByteBuffer&& buffer, bool expecting_tag);
private:
static MailboxFlag parse_mailbox_flag(StringView);
ErrorOr<ParseStatus> try_parse(ByteBuffer&& buffer, bool expecting_tag);
ErrorOr<void> consume(StringView);
bool consume_if(StringView);
StringView consume_while(Function<bool(u8)> should_consume);
StringView consume_until_end_of_line();
bool at_end() { return m_position >= m_buffer.size(); }
ErrorOr<unsigned> parse_number();
Optional<unsigned> try_parse_number();
ErrorOr<void> parse_response_done();
ErrorOr<void> parse_untagged();
ErrorOr<void> parse_capability_response();
ErrorOr<StringView> parse_atom();
ErrorOr<StringView> parse_quoted_string();
ErrorOr<StringView> parse_literal_string();
ErrorOr<StringView> parse_string();
ErrorOr<StringView> parse_astring();
ErrorOr<StringView> parse_nstring();
ErrorOr<ResponseStatus> parse_status();
ErrorOr<ListItem> parse_list_item();
ErrorOr<FetchCommand::DataItem> parse_fetch_data_item();
ErrorOr<FetchResponseData> parse_fetch_response();
ErrorOr<Vector<Address>> parse_address_list();
ErrorOr<Address> parse_address();
ErrorOr<HashMap<DeprecatedString, DeprecatedString>> parse_body_fields_params();
ErrorOr<BodyStructure> parse_body_structure();
ErrorOr<BodyStructure> parse_one_part_body();
ErrorOr<BodyExtension> parse_body_extension();
ErrorOr<Tuple<DeprecatedString, HashMap<DeprecatedString, DeprecatedString>>> parse_disposition();
ErrorOr<Vector<DeprecatedString>> parse_langs();
ErrorOr<Envelope> parse_envelope();
template<typename T>
ErrorOr<Vector<T>> parse_list(T (*converter)(StringView));
// To retain state if parsing is not finished
ByteBuffer m_buffer;
SolidResponse m_response;
unsigned m_position { 0 };
bool m_incomplete { false };
};
}