/ OSX / libsecurity_codesigning / lib / reqdumper.h
reqdumper.h
  1  /*
  2   * Copyright (c) 2006-2007,2011 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  
 34  namespace Security {
 35  namespace 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  //
 47  class Dumper : public Requirement::Reader {
 48  public:
 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  	
 67  public:
 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  
 73  protected:
 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 timestamp();
 81  	void dotString() { data(isSimple, true); }
 82  	void quotedString() { data(isPrintable); }
 83  	void hashData();	// H"bytes"
 84  	void certSlot();	// symbolic certificate slot indicator (explicit)
 85  	void match();		// a match suffix (op + value)
 86  	
 87  	void print(const char *format, ...) __attribute((format(printf,2,3)));
 88  
 89  private:
 90  	void printBytes(const Byte *data, size_t length); // just write hex bytes
 91  	
 92  private:
 93  	std::string mOutput;		// output accumulator
 94  	bool mDebug;				// include debug output in mOutput
 95  };
 96  
 97  
 98  }	// CodeSigning
 99  }	// Security
100  
101  #endif //_H_REQDUMPER