X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/72a12576750f52947eb043106ba5c12c0d07decf..b1ab9ed8d0e0f1c3b66d7daa8fd5564444c56195:/libsecurity_codesigning/lib/antlrplugin.cpp diff --git a/libsecurity_codesigning/lib/antlrplugin.cpp b/libsecurity_codesigning/lib/antlrplugin.cpp new file mode 100644 index 00000000..fc38e53c --- /dev/null +++ b/libsecurity_codesigning/lib/antlrplugin.cpp @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2007 Apple Inc. All Rights Reserved. + * + * @APPLE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this + * file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_LICENSE_HEADER_END@ + */ + +#include "antlrplugin.h" +#include "cserror.h" +#include "RequirementLexer.hpp" +#include "RequirementParser.hpp" +#include + + +namespace Security { +namespace CodeSigning { + +namespace Parser = Security_CodeSigning; + + +// +// Lexer input adapters +// +class StdioInputStream : public antlr::InputBuffer { +public: + StdioInputStream(FILE *fp) : mFile(fp) { } + int getChar() { return fgetc(mFile); } + +private: + FILE *mFile; +}; + +class StringInputStream : public antlr::InputBuffer { +public: + StringInputStream(const string &s) : mInput(s), mPos(mInput.begin()) { } + int getChar() { return (mPos == mInput.end()) ? EOF : *mPos++; } + +private: + string mInput; + string::const_iterator mPos; +}; + + +// +// Generic parser driver +// +template +const Result *parse(Source source, Result *(Parser::RequirementParser::*rule)(), std::string &errors) +{ + Input input(source); + Parser::RequirementLexer lexer(input); + Parser::RequirementParser parser(lexer); + try { + const Result *result = (parser.*rule)(); + errors = parser.errors; + if (errors.empty()) + return result; + else + ::free((void *)result); + } catch (const antlr::TokenStreamException &ex) { + errors = ex.toString() + "\n"; + } + return NULL; // signal failure +} + + +// +// Hook up each supported parsing action to the plugin interface +// +const Requirement *fileRequirement(FILE *source, string &errors) +{ return parse(source, &Parser::RequirementParser::requirement, errors); } + +const Requirement *stringRequirement(string source, string &errors) +{ return parse(source, &Parser::RequirementParser::requirement, errors); } + +const Requirements *fileRequirements(FILE *source, string &errors) +{ return parse(source, &Parser::RequirementParser::requirementSet, errors); } + +const Requirements *stringRequirements(string source, string &errors) +{ return parse(source, &Parser::RequirementParser::requirementSet, errors); } + +const BlobCore *fileGeneric(FILE *source, string &errors) +{ return parse(source, &Parser::RequirementParser::autosense, errors); } + +const BlobCore *stringGeneric(string source, string &errors) +{ return parse(source, &Parser::RequirementParser::autosense, errors); } + + +// +// Basic plugin hookup +// +static AntlrPlugin plugin = { + fileRequirement, + fileRequirements, + fileGeneric, + stringRequirement, + stringRequirements, + stringGeneric +}; + +AntlrPlugin *findAntlrPlugin() +{ + return &plugin; +} + + +} // end namespace CodeSigning +} // end namespace Security