]> git.saurik.com Git - apple/security.git/blame - lib/generateErrStrings.pl
Security-27569.tar.gz
[apple/security.git] / lib / generateErrStrings.pl
CommitLineData
563f4f96
A
1#!/usr/bin/perl
2#
3# Copyright (c) 2003-2004 Apple Computer, Inc. All Rights Reserved.
4#
5# @APPLE_LICENSE_HEADER_START@
6#
7# This file contains Original Code and/or Modifications of Original Code
8# as defined in and that are subject to the Apple Public Source License
9# Version 2.0 (the 'License'). You may not use this file except in
10# compliance with the License. Please obtain a copy of the License at
11# http://www.opensource.apple.com/apsl/ and read it before using this
12# file.
13#
14# The Original Code and all software distributed under the License are
15# distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16# EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17# INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18# FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19# Please see the License for the specific language governing rights and
20# limitations under the License.
21#
22# @APPLE_LICENSE_HEADER_END@
23#
24# generatorX.pl - create error strings files from the Security header files
25#
26# John Hurley, Summer 2003. Based on generator.pl, Perry The Cynic, Fall 1999.
27#
28# Usage:
29# perl generatorX.pl input-directory output-directory <files>
30#
31# Currently supported files are SecBase.h, SecureTransport.h and Authorization.h
32#
33# perl generatorX.pl `pwd` `pwd` SecBase2.h SecureTransport2.h Authorization.h
34#
35# Input will be like:
36#
37# errSSLProtocol = -9800, /* SSL protocol error */
38# errSSLNegotiation = -9801, /* Cipher Suite negotiation failure */
39#
40# Output should be like (in Unicode):
41#
42# /* errSSLProtocol */
43# "-9800" = "SSL protocol error";
44#
45# /* errSSLNegotiation */
46# "-9801" = "Cipher Suite negotiation failure";
47#
48# Note that the list of errors must be numerically unique across all input files, or the strings file
49# will be invalid.Comments that span multiple lines will be ignored, as will lines with no comment. C++
50# style comments are not supported.
51#
52use strict;
53use Encode;
54
55my $INPUTFILE=$ARGV[0]; # list of input files
56my $FRAMEWORK=$ARGV[1]; # directory containing Security.framework
57my $TARGETFILE=$ARGV[2]; # where to put the output file
58
59my $tabs = "\t\t\t"; # argument indentation (noncritical)
60my $warning = "This file was automatically generated. Do not edit on penalty of futility!";
61
62#
63# Read error headers into memory (all just concatenated blindly)
64#
65open(ERR, "$INPUTFILE") or die "Cannot open $INPUTFILE";
66$/=undef; # big gulp mode
67$_ = <ERR>;
68close(ERR);
69
70#
71# Prepare output file
72#
73open(OUT, ">$TARGETFILE") or die "Cannot write $TARGETFILE: $^E";
74my $msg = "//\n// Security error code tables.\n// $warning\n//\n";
75
76
77#
78# Extract errors from accumulated header text. Format:
79# errBlahWhatever = number, /* text */
80#
81my @errorlines =
82 m{(?:^\s*)(err[Sec|Authorization|SSL]\w+)(?:\s*=\s*)(-?\d+)(?:\s*,?\s*)(?:/\*\s*)(.*)(?:\*/)(?:$\s*)}gm;
83while (my $errx = shift @errorlines)
84{
85 my $value = shift @errorlines; # or die;
86 my $str = shift @errorlines; # or die;
87 $str =~ s/\s*$//; # drop trailing white space
88 if ( $value != 0) # can't output duplicate error codes
89 {
90 $msg = $msg . "\n/* $errx */\n\"$value\" = \"$str\";\n";
91 }
92};
93$msg = $msg . "\n";
94
95
96#
97# Extract errors from CSSM headers. Format:
98# CSSMERR_whatever = some compile-time C expression
99# [We just build a C program and running it. So sue us.]
100#
101my $PROG = "/tmp/cssmerrors.$$.c";
102my $PROGB = "/tmp/cssmerrors.$$";
103
104open(PROG, ">$PROG") or die "Cannot open $PROG";
105print PROG <<END;
106#include <Security/cssmerr.h>
107#include <Security/cssmapple.h>
108#include <stdio.h>
109int main() {
110END
111@errorlines =
112 m{(?:^\s*)CSSMERR_([A-Z_]+)\s+=}gm;
113for my $error (@errorlines) {
114 print PROG "printf(\"\\n/* CSSMERR_$error */\\n\\\"%ld\\\" = \\\"$error\\\";\\n\", CSSMERR_$error);\n";
115}
116print PROG "}\n";
117close(PROG);
118
119system("cc", "-o", $PROGB, $PROG, "-I$FRAMEWORK/SecurityPieces/Headers") == 0 or die "cannot build CSSM collector";
120open(PROGR, "$PROGB|") or die "Cannot run CSSM collector";
121$msg .= <PROGR>;
122close(PROGR);
123
124#
125# Write output file and clean up
126#
127print OUT encode("UTF-16", $msg, Encode::FB_PERLQQ);
128close(OUT);