1/*
2 * Copyright (c) 2004 Apple Computer, 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/*
26 * DotMacTpMutils.m - ObjC utils, callable from any language
27 */
28
29#include "dotMacTpMutils.h"
30#include <Security/cssmapple.h>
31
32#if		XML_DEBUG
33#include <Cocoa/Cocoa.h>
34void logCFstr(
35	const char *cstr,
36	CFStringRef cfstr)
37{
38	NSLog(@"%s %@\n", cstr, cfstr);
39}
40#endif
41
42#if		DICTIONARY_DEBUG
43
44#include <Foundation/NSObjCRuntime.h>
45
46void dumpDictionary(
47	const char *title,
48	CFDictionaryRef dict)
49{
50	printf("%s:\n", title);
51	#if 0
52	NSLog(@"%@\n", CFCopyDescription(dict));
53	#else
54	CFIndex items = CFDictionaryGetCount(dict);
55	if(items <= 0) {
56		printf("Error on CFDictionaryGetCount\n");
57		return;
58	}
59	const void **keys = (const void **)malloc(items * sizeof(void *));
60	const void **values = (const void **)malloc(items * sizeof(void *));
61	CFDictionaryGetKeysAndValues(dict, keys, values);
62	CFIndex dex;
63	for(dex=0; dex<items; dex++) {
64		CFStringRef key = (CFStringRef)keys[dex];
65		CFTypeID keyType = CFGetTypeID(key);
66		if(CFStringGetTypeID() == keyType) {
67			NSLog(@"key %d : %@ \n", (int)dex, key);
68		}
69		else {
70			fprintf(stderr, "<key %d is not a string>\n", (int)dex);
71		}
72		CFTypeID valType = CFGetTypeID(values[dex]);
73		if(valType == CFStringGetTypeID()) {
74			NSLog(@"  val type = CFString : %@\n", values[dex]);
75		}
76		else if(valType == CFArrayGetTypeID()) {
77			NSLog(@"  val type = CFArray\n");
78		}
79		else if(valType == CFDictionaryGetTypeID()) {
80			NSLog(@"  val type = CFDictionary\n");
81			fprintf(stderr, "======== recursively dumping dictionary value ========\n");
82			dumpDictionary("Dictionary contents", (CFDictionaryRef)values[dex]);
83			fprintf(stderr, "======== end of dictionary value ========\n");
84		}
85		else if(valType == CFNumberGetTypeID()) {
86			NSLog(@"  val type = CFNumber: %@\n", CFCopyDescription(values[dex]));
87		}
88		else {
89			NSLog(@"  val type = unknown: %@\n", CFCopyDescription(values[dex]));
90		}
91	}
92	#endif
93}
94#endif
95
96/*
97 * Map an HTTP status to a CSSM status. Good luck!
98 */
99CSSM_RETURN dotMacHttpStatToOs(
100	unsigned httpStat)
101{
102	CSSM_RETURN crtn = CSSM_OK;
103	switch(httpStat) {
104		case 200:
105			crtn = CSSM_OK;
106			break;
107		case 401:
108			crtn = CSSMERR_TP_AUTHENTICATION_FAILED;
109			break;
110		case 403:
111			crtn = CSSMERR_TP_REQUEST_REJECTED;
112			break;
113		case 500:
114			crtn = CSSMERR_APPLE_DOTMAC_REQ_SERVER_NOT_AVAIL;
115			break;
116		default:
117			/* FIXME - anything else? */
118			crtn = CSSMERR_APPLETP_NETWORK_FAILURE;
119			break;
120	}
121	return crtn;
122}
123