]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c++-lib/inc/sm_vdasnacc.h
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
19 /* @(#) sm_vdasnacc.h 1.21 5/1/98 09:59:08 */
22 #ifndef _SM_VDASNACC_H_
23 #define _SM_VDASNACC_H_
29 #define SM_SIZE_T size_t
34 #define SM_FOPEN_WRITE "wb"
35 #define SM_FOPEN_READ "rb"
36 #define SM_FOPEN_APPEND "ab"
38 #define SM_FOPEN_WRITE "w"
39 #define SM_FOPEN_READ "r"
40 #define SM_FOPEN_APPEND "a"
44 //////////////////////////////////////////////////////////////////////////
45 // CSM_Buffer is the general purpose buffer used throughout the SFL
46 class CSM_Buffer
: public AsnType
51 #if !defined(macintosh) && !defined(__APPLE__)
57 SM_SIZE_T m_lCacheSize
;
59 #if !defined(macintosh) && !defined(__APPLE__)
60 // returns bool value indicating if the buffer is in a file
61 bool InFile() { if (m_pszFN
== NULL
) return false; else return true; }
64 // AllocMoreMem allocates specified more bytes for mem buffer
65 void AllocMoreMem(SM_SIZE_T lSize
);
69 // use this constructor to create a complete empty buffer
71 // use this constructor to create a memory buffer of size lSize
72 CSM_Buffer(size_t lSize
);
73 // use this constructor to create a buffer in file pszFileName
74 //CSM_Buffer(char *pszFileName);
75 // use this constructor to init the memory buffer with a ptr and size
76 CSM_Buffer(const char *pBuf
, SM_SIZE_T lSize
);
77 // use this constructor to make a copy of the provided buffer
78 // and put it into this buffer
79 CSM_Buffer(const CSM_Buffer
&b
);
81 virtual ~CSM_Buffer(); // DESTRUCTOR
83 // Inheirited from AsnType.
84 virtual AsnType
*Clone() const;
85 virtual AsnType
*Copy() const;
87 virtual AsnLen
BEnc (BUF_TYPE b
);
88 void Print (ostream
&os
) const;
90 // CONTENT MODIFYING MEMBERS
94 // return size of the buffer
95 SM_SIZE_T
Length() const;
96 // copy the provided null terminated memory in memory buffer
97 void Set(const char *psz
);
98 // copy the provided memory of size lSize in memory buffer
99 void Set(const char *p
, SM_SIZE_T lSize
);
100 // set the length of the buffer
101 void SetLength(SM_SIZE_T lSize
) { m_lSize
= lSize
; }
102 #if !defined(macintosh) && !defined(__APPLE__)
103 // copy the provided file name into m_pszFN
104 void SetFileName(char *pszFN
)
109 m_pszFN
= (char *)malloc (strlen (pszFN
) + 1);
110 strcpy (m_pszFN
, pszFN
);
114 // allocate memory in the buffer and return ptr to it
115 char* Alloc(SM_SIZE_T lSize
);
116 // compare this with b, return 0 if match
117 long Compare(const CSM_Buffer
&b
);
118 // ReSet copies b into this
119 long ReSet(const CSM_Buffer
&b
);
121 // BUFFER DATA ACCESS MEMBERS
122 // return a pointer to the actual data, if in file, call CopyAll
123 const char* Access() const;
124 // return a copy of the actual data and return the size
125 char* Get(SM_SIZE_T
&l
) const;
126 // return a copy of the actual data
127 char* Get() const { SM_SIZE_T l
; return Get(l
); }
129 // COMPARISON OPERATORS
130 bool operator == (/*const*/ CSM_Buffer
&b
) {
131 if (Compare(b
) == 0) return true; else return false; }
132 bool operator != (/*const*/ CSM_Buffer
&b
) {
133 if (Compare(b
) == 0) return false; else return true; }
135 // ASSIGNMENT OPERATOR
136 CSM_Buffer
&operator = (/*const*/ CSM_Buffer
&b
) {
137 ReSet(b
); return *this; }
139 #if !defined(macintosh) && !defined(__APPLE__)
140 // BUFFER CONVERSION MEMBERS
141 long ConvertFileToMemory();
142 long ConvertMemoryToFile(char *pszFN
);
146 long Open(char *pszMode
);
147 long Seek(SM_SIZE_T lOffset
, SM_SIZE_T lOrigin
);
151 long cRead(char *pBuffer
, SM_SIZE_T lSize
);
152 long Write(const char *pBuffer
, SM_SIZE_T lSize
);
153 char* nRead(SM_SIZE_T lSize
, SM_SIZE_T
&lBytesRead
);
157 long vdasnacc_sortSet(CSM_Buffer
*pEncBuf
[], int icount
);
158 long vdasnacc_sortSetOf(CSM_Buffer
**&pEncBuf
, int icount
);
159 long SM_WriteToAsnBuf(CSM_Buffer
*&pCBuf
, AsnBuf
&SNACCinputBuf
);
160 long SM_WriteToAsnBuf(CSM_Buffer
&CBuf
, AsnBuf
&SNACCoutputBuf
);
161 long SM_ReadFromAsnBuf(CSM_Buffer
*&pCBuf
, // OUT,copied data.
162 AsnBuf
&SNACCinputBuf
, // IN, input SNACC buffer
163 long length
, // IN, length of data to read.
164 CSM_Buffer
*preLoad
); // IN, optional data to be pre-loaded;
165 // (for SNACC support)
166 // no alloc version of SM_ReadFromAsnBuf
167 long SM_ReadFromAsnBuf(
168 AsnBuf
&SNACCinputBuf
, // IN, input SNACC buffer
169 CSM_Buffer
*pCBuf
, // OUT,copied data.
170 long length
, // IN, length of data to read.
171 CSM_Buffer
*preLoad
); // IN, optional data to be pre-loaded;
172 // (for SNACC support)
173 // function to convert an AsnBits to a CSM_Buffer
174 long SM_AsnBits2Buffer(AsnBits
*pBits
, CSM_Buffer
*pBuffer
);
175 long SM_Buffer2AsnBits(CSM_Buffer
*pBuffer
, AsnBits
*pBits
, size_t lBits
);
176 long SM_BufferReverseBits(CSM_Buffer
*pBuffer
);
179 #define SM_BUF_2_BIG_INT_STR 0
180 #if SM_BUF_2_BIG_INT_STR
181 // FIXME - why doesn't this link properly?
182 // prototypes for converting to and from BigIntegerStr and CSM_Buffer.
183 long SM_Buffer2BigIntegerStr( CSM_Buffer
*asn1Data
,
184 BigIntegerStr
&pSnaccBigIntStr
,
187 long SM_Buffer2BigIntegerStr( CSM_Buffer
*asn1Data
,
188 BigIntegerStr
*&pSnaccBigIntStr
,
190 #endif /* SM_BUF_2_BIG_INT_STR */
192 // VDASNACC_ENCDEC_BUFSIZE is the number of bytes in the global
193 // buffer used for encoding and decoding
194 #define VDASNACC_ENCDEC_BUFSIZE 100000
198 // long lgth; /* Number of characters in string */
199 // unsigned char *str; /* Pointer to character string */
202 #define NULL_STR (Str_struct *) NULL
206 //#include <stdio.h> /**** Standard I/O includes ****/
207 //long vdasnacc_sortSetOf(Str_struct **strEnc, int icount);
208 //long vdasnacc_sortSet(Str_struct **strEnc, int icount);
209 //void free_Str(Str_struct *str);
210 //void free_Str_content(Str_struct *str);
213 #define ENCODE_ANY(encodedData,asnAny)\
215 CSM_Buffer *blob=new CSM_Buffer;\
217 if ((encodedData) && (asnAny))\
219 ENCODE_BUF((encodedData), blob)\
220 (asnAny)->value = (AsnType *)blob;\
224 #define DECODE_ANY(decodeData,asnAny)\
228 blob=(CSM_Buffer *)(asnAny)->value;\
231 DECODE_BUF((decodeData), blob)\
234 // This macro is usually only necessary if a SNACC AsnBuf is used
235 // immediately after being loaded by an application (e.g. consecutive
236 // encode decode operations).
237 #define SNACC_BUFRESET_READ(pSnaccBuf) (pSnaccBuf)->ResetInReadMode();
238 #define SNACC_BUFRESET_WRITE(pSnaccBuf) (pSnaccBuf)->ResetInWriteRvsMode();
240 #define ENCODE_BUF_NO_ALLOC(encodeData, blob)\
242 char *pchBuffer = (char *)calloc(1, \
243 VDASNACC_ENCDEC_BUFSIZE);\
248 outputBuf.Init(pchBuffer, VDASNACC_ENCDEC_BUFSIZE);\
249 outputBuf.ResetInWriteRvsMode();\
250 status = (encodeData)->BEncPdu (outputBuf, encodedLen);\
251 outputBuf.ResetInReadMode();\
252 SM_ReadFromAsnBuf(outputBuf, (blob), outputBuf.DataLen(),NULL);\
256 #define ENCODE_BUF(encodeData, blob)\
258 char *pchBuffer = (char *)calloc(1, \
259 VDASNACC_ENCDEC_BUFSIZE);\
264 outputBuf.Init(pchBuffer, VDASNACC_ENCDEC_BUFSIZE);\
265 outputBuf.ResetInWriteRvsMode();\
266 if((status = (encodeData)->BEncPdu (outputBuf, encodedLen))==false)\
267 SME_THROW(33, "BAD SNACC Encode", NULL);\
268 outputBuf.ResetInReadMode();\
269 SM_ReadFromAsnBuf((blob), outputBuf, outputBuf.DataLen(),NULL);\
273 #define DECODE_BUF(decodeData, blob)\
275 char *pchBuffer = (char *)calloc(1, \
276 VDASNACC_ENCDEC_BUFSIZE);\
281 outputBuf.Init(pchBuffer, VDASNACC_ENCDEC_BUFSIZE);\
282 outputBuf.ResetInWriteRvsMode();\
283 SM_WriteToAsnBuf((blob), outputBuf);\
284 outputBuf.ResetInReadMode();\
285 if ((nDecStatus = (decodeData)->BDecPdu(outputBuf, encodedLen)) == false)\
286 SME_THROW(34, "BAD SNACC Decode", NULL);\
290 #define SM_ASSIGN_ANYBUF(lpBuf, asnAny)\
292 (asnAny)->value = (AsnType *)new CSM_Buffer(*(lpBuf));\
295 /* don't know if this actually works... dave */
296 #define SM_EXTRACT_ANYBUF(pSS, asnAny)\
298 (pSS) = new CSM_Buffer(*(CSM_Buffer *)(asnAny)->value);\
301 #define ENCODE_BUF1(encodeContent, encodeLen)\
306 lpszBuf = (char *)calloc(1, VDASNACC_ENCDEC_BUFSIZE/2);\
307 outputBuf.Init(lpszBuf, VDASNACC_ENCDEC_BUFSIZE/2);\
308 outputBuf.ResetInWriteRvsMode();\
309 (encodeLen) = encodeContent(outputBuf);
311 #define ENCODE_BUF2(blob)\
312 outputBuf.ResetInReadMode();\
313 SM_ReadFromAsnBuf((blob), outputBuf, outputBuf.DataLen(),NULL);\
317 // RWC; The following macro defines the ASN ANY load for "BEnc...()"
318 // RWC; operations into the final output buffers. NO ERROR checking
319 // RWC; is performed to be sure the buffer is ASN decodable.
320 // RWC; this convention for loading ANY results is only valid for
321 // RWC; the SMIME/MSP library loads, where previous logic has
322 // RWC; loaded the "AsnType *value" element with a "CSM_Buffer *"
323 // RWC; containing the encoded ANY result.
324 // RWC; The "Str_struct *" needs to be freed when class destroyed.
325 // RWC; Place encoded ASN directly into buffer.
326 #if defined(macintosh) || defined(__APPLE__)
327 #define ENC_LOAD_ANYBUF(asnType, Bbuf, l) \
328 if ((asnType)->value != NULL)\
330 l = (asnType)->value->BEnc(Bbuf);\
333 #define ENC_LOAD_ANYBUF(asnType, Bbuf, l) \
334 if ((CSM_Buffer *)(asnType)->value != NULL)\
336 SM_WriteToAsnBuf(((CSM_Buffer *&)(asnType)->value), Bbuf);\
337 l = ((CSM_Buffer *)(asnType)->value)->Length();\
341 // RWC; The following macro decodes the ANY buffer tag and length to
342 // RWC; allocate a "CSM_Buffer", then copies the unencoded results.
343 // RWC; The assumption is that the "readloc" buffer will still be intact
344 // RWC; even after the decode of the tag and length. (HOPEFULLY!)
345 // RWC; Once the data for this ANY is copied, unencoded into the CSM_Buffer
346 // RWC; then we set the buffer "readloc" pointer to after this element.
347 // RWC; "bBuf.GetSeg(elmtLen)"
348 #define DEC_LOAD_ANYBUF(asnType, Bbuf, l, env) \
350 size_t len = (size_t) 0; \
351 AsnLen bytesDecoded = 0L; \
352 size_t elmtLen = (size_t) 0; \
354 char *readloc = NULL; \
356 CSM_Buffer *preLoad;\
358 readloc = Bbuf.GetSeg (&len);\
359 tag = BDecTag (Bbuf, bytesDecoded, env);\
360 elmtLen = BDecLen (Bbuf, bytesDecoded, env);\
362 preLoad = new CSM_Buffer(readloc, len);\
363 SM_ReadFromAsnBuf(blob, (Bbuf), elmtLen,preLoad);\
364 (asnType)->value = blob;\
370 // RWC; Correctly process our OID values, the "char *" "asnOid->Set()" function
371 // directly loads the "->oid" private variable, no processing!!!
372 //int SM_STR_TO_OID(char *lpStrOid, AsnOid *asnOid);
373 //int SM_OID_TO_STR(char *lpStrOid, AsnOid *asnOid);
376 #define SNACC_OID_FIX(asnOid, long_arr4) \
378 unsigned long int a[11];\
380 for (i=0; i < (long_arr4)->lgth; i++) a[i] = (long_arr4)->int_arr[i];\
381 for (i=(long_arr4)->lgth; i < 11; i++) a[i] = -1;\
382 (asnOid)->Set(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9],\
387 #endif // _SM_VDASNACC_H_