1/*
2 * Copyright (c) 2006-2007 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24//
25// reqdumper - Requirement un-parsing (disassembly)
26//
27#ifndef _H_REQDUMPER
28#define _H_REQDUMPER
29
30#include "reqreader.h"
31#include <ctype.h>
32
33
34namespace Security {
35namespace CodeSigning {
36
37
38//
39// A decompiler for (compiled) requirements programs.
40// This is intended to produce compiler-ready source, and the
41// (decompile . compile) cycle is meant to be loss-less.
42//
43// Note that a Dumper is a type of Interpreter, so it can use the program stream
44// accessors of the Interpreter. However, the evaluaton Context is absent, so
45// actual validation functions must not be called.
46//
47class Dumper : public Requirement::Reader {
48public:
49	explicit Dumper(const Requirement *req, bool debug = false)
50		: Reader(req), mDebug(debug) { }
51
52	enum SyntaxLevel {
53		slPrimary,		// syntax primary
54		slAnd,			// conjunctive
55		slOr,			// disjunctive
56		slTop			// where we start
57	};
58
59	void dump();		// decompile this (entire) requirement
60	void expr(SyntaxLevel level = slTop); // decompile one requirement expression
61
62	std::string value() const { return mOutput; }
63	operator std::string () const { return value(); }
64
65	typedef unsigned char Byte;
66
67public:
68	// all-in-one dumping
69	static string dump(const Requirements *reqs, bool debug = false);
70	static string dump(const Requirement *req, bool debug = false);
71	static string dump(const BlobCore *req, bool debug = false);	// dumps either
72
73protected:
74	enum PrintMode {
75		isSimple,		// printable and does not require quotes
76		isPrintable,	// can be quoted safely
77		isBinary		// contains binary bytes (use 0xnnn form)
78	};
79	void data(PrintMode bestMode = isSimple, bool dotOkay = false);
80	void dotString() { data(isSimple, true); }
81	void quotedString() { data(isPrintable); }
82	void hashData();	// H"bytes"
83	void certSlot();	// symbolic certificate slot indicator (explicit)
84	void match();		// a match suffix (op + value)
85
86	void print(const char *format, ...);
87
88private:
89	void printBytes(const Byte *data, size_t length); // just write hex bytes
90
91private:
92	std::string mOutput;		// output accumulator
93	bool mDebug;				// include debug output in mOutput
94};
95
96
97}	// CodeSigning
98}	// Security
99
100#endif //_H_REQDUMPER
101