vfs: check userland buffers before reading them.
[haiku.git] / src / kits / package / PackageInfoParser.h
blob53195587c7d62d78f7691dfdd93df707a7c5bd69
1 /*
2 * Copyright 2011, Oliver Tappe <zooey@hirschkaefer.de>
3 * Copyright 2016, Andrew Lindesay <apl@lindesay.co.nz>
4 * Distributed under the terms of the MIT License.
5 */
6 #ifndef PACKAGE_INFO_PARSER_H
7 #define PACKAGE_INFO_PARSER_H
10 #include <package/PackageInfo.h>
13 namespace BPackageKit {
17 * Parses a ".PackageInfo" file and fills a BPackageInfo object with the
18 * package info elements found.
20 class BPackageInfo::Parser {
21 public:
22 Parser(ParseErrorListener* listener = NULL);
24 status_t Parse(const BString& packageInfoString,
25 BPackageInfo* packageInfo);
27 status_t ParseVersion(const BString& versionString,
28 bool revisionIsOptional,
29 BPackageVersion& _version);
30 status_t ParseResolvableExpression(
31 const BString& expressionString,
32 BPackageResolvableExpression& _expression);
34 private:
35 struct UrlStringValidator;
36 struct StringValidator;
37 struct ParseError;
38 struct Token;
39 struct ListElementParser;
40 friend struct ListElementParser;
42 enum TokenType {
43 TOKEN_STRING,
44 TOKEN_OPERATOR_ASSIGN,
45 TOKEN_OPERATOR_LESS,
46 TOKEN_OPERATOR_LESS_EQUAL,
47 TOKEN_OPERATOR_EQUAL,
48 TOKEN_OPERATOR_NOT_EQUAL,
49 TOKEN_OPERATOR_GREATER_EQUAL,
50 TOKEN_OPERATOR_GREATER,
51 TOKEN_OPEN_BRACE,
52 TOKEN_CLOSE_BRACE,
53 TOKEN_ITEM_SEPARATOR,
55 TOKEN_EOF,
58 private:
59 Token _NextToken();
60 void _RewindTo(const Token& token);
62 void _ParseStringValue(BString* value,
63 const char** _tokenPos = NULL);
64 uint32 _ParseFlags();
65 void _ParseArchitectureValue(
66 BPackageArchitecture* value);
67 void _ParseVersionValue(BPackageVersion* value,
68 bool revisionIsOptional);
69 static void _ParseVersionValue(Token& word,
70 BPackageVersion* value,
71 bool revisionIsOptional);
72 void _ParseResolvableExpression(
73 const Token& token,
74 BPackageResolvableExpression& _value,
75 BString* _basePackage);
76 void _ParseList(ListElementParser& elementParser,
77 bool allowSingleNonListElement);
78 void _ParseStringList(BStringList* value,
79 bool requireResolvableName = false,
80 bool convertToLowerCase = false,
81 StringValidator* stringValidator = NULL);
82 void _ParseResolvableList(
83 BObjectList<BPackageResolvable>* value);
84 void _ParseResolvableExprList(
85 BObjectList<BPackageResolvableExpression>*
86 value,
87 BString* _basePackage = NULL);
88 void _ParseGlobalWritableFileInfos(
89 GlobalWritableFileInfoList* infos);
90 void _ParseUserSettingsFileInfos(
91 UserSettingsFileInfoList* infos);
92 void _ParseUsers(UserList* users);
94 void _Parse(BPackageInfo* packageInfo);
96 static bool _IsAlphaNumUnderscore(const BString& string,
97 const char* additionalChars,
98 int32* _errorPos);
99 static bool _IsAlphaNumUnderscore(const char* string,
100 const char* additionalChars,
101 int32* _errorPos);
102 static bool _IsAlphaNumUnderscore(const char* start,
103 const char* end,
104 const char* additionalChars,
105 int32* _errorPos);
106 static bool _IsValidResolvableName(const char* string,
107 int32* _errorPos);
109 private:
110 ParseErrorListener* fListener;
111 const char* fPos;
115 struct BPackageInfo::Parser::ParseError {
116 BString message;
117 const char* pos;
119 ParseError(const BString& _message, const char* _pos)
120 : message(_message), pos(_pos)
126 struct BPackageInfo::Parser::Token {
127 TokenType type;
128 BString text;
129 const char* pos;
131 Token(TokenType _type, const char* _pos, int length = 0,
132 const char* text = NULL)
134 type(_type),
135 pos(_pos)
137 if (text != NULL)
138 this->text = text;
139 else if (length != 0)
140 this->text.SetTo(pos, length);
143 operator bool() const
145 return type != TOKEN_EOF;
150 struct BPackageInfo::Parser::ListElementParser {
151 virtual ~ListElementParser()
155 virtual void operator()(const Token& token) = 0;
159 struct BPackageInfo::Parser::StringValidator {
160 public:
161 virtual void Validate(const BString &string, const char *pos) = 0;
165 struct BPackageInfo::Parser::UrlStringValidator
166 : public BPackageInfo::Parser::StringValidator {
167 public:
168 virtual void Validate(const BString &string, const char* pos);
172 } // namespace BPackageKit
175 #endif // PACKAGE_INFO_PARSER_H