]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/compiler/back-ends/str-util.c
Security-54.1.9.tar.gz
[apple/security.git] / SecuritySNACCRuntime / compiler / back-ends / str-util.c
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19 /*
20 * compiler/back_ends/c_gen/str_util.c - bunch of ASN.1/C string utilities
21 *
22 *
23 * Mike Sample
24 * 91/08/12
25 * Copyright (C) 1991, 1992 Michael Sample
26 * and the University of British Columbia
27 *
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation; either version 2 of the License, or
31 * (at your option) any later version.
32 *
33 * $Header: /cvs/root/Security/SecuritySNACCRuntime/compiler/back-ends/Attic/str-util.c,v 1.1.1.1 2001/05/18 23:14:08 mb Exp $
34 * $Log: str-util.c,v $
35 * Revision 1.1.1.1 2001/05/18 23:14:08 mb
36 * Move from private repository to open source repository
37 *
38 * Revision 1.3 2001/05/05 00:59:27 rmurphy
39 * Adding darwin license headers
40 *
41 * Revision 1.2 2000/05/10 21:36:43 rmurphy
42 * changing the suffix for c++ output files to .cpp - requires -DMACOS on the compilation line
43 *
44 * Revision 1.1.1.1 1999/03/16 18:06:39 aram
45 * Originals from SMIME Free Library.
46 *
47 * Revision 1.4 1995/07/25 18:13:31 rj
48 * include string(s).h
49 *
50 * by default, snacc now derives output file names from the .asn1 input file name instead of the module name.
51 * the global keepbaseG variable switches between the two behaviours.
52 *
53 * additional filename generator for idl backend.
54 *
55 * changed `_' to `-' in file names.
56 *
57 * Revision 1.3 1994/10/08 03:48:17 rj
58 * since i was still irritated by cpp standing for c++ and not the C preprocessor, i renamed them to cxx (which is one known suffix for C++ source files). since the standard #define is __cplusplus, cplusplus would have been the more obvious choice, but it is a little too long.
59 *
60 * Revision 1.2 1994/09/01 00:25:31 rj
61 * snacc_config.h removed; more portable .h file inclusion.
62 *
63 * Revision 1.1 1994/08/28 09:48:37 rj
64 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
65 *
66 */
67
68 #include "asn-incl.h"
69
70 #include <ctype.h>
71 #if HAVE_UNISTD_H
72 #include <unistd.h> /* for pathconf (..) */
73 #endif
74 #if STDC_HEADERS || HAVE_STRING_H
75 #include <string.h>
76 #else
77 #include <strings.h>
78 #endif
79 #include <stdio.h>
80
81 #include "asn1module.h"
82 #include "mem.h"
83 #include "define.h"
84 #include "c-gen/rules.h"
85 #include "c-gen/type-info.h"
86 #include "c-gen/kwd.h"
87 #include "c++-gen/kwd.h"
88 #include "str-util.h"
89
90
91 #define DIGIT_TO_ASCII( d) (((d) % 10) + '0')
92
93 int keepbaseG = TRUE;
94
95 /*
96 * allocates new and returns a copy of the given
97 * string with '-'s (dashes) replaced by '_'s (underscores)
98 */
99 char *
100 Asn1TypeName2CTypeName PARAMS ((aName),
101 char *aName)
102 {
103 char *retVal;
104 if (aName == NULL)
105 return NULL;
106
107 retVal = Malloc (strlen (aName) + 1);
108 strcpy (retVal, aName);
109 Dash2Underscore (retVal, strlen (retVal));
110
111 return retVal;
112 } /* Asn1TypeName2CTypeName */
113
114
115 /*
116 * allocates new str and returns a copy of the given
117 * string with '-'s (dashes) replaced by '_'s (underscores)
118 */
119 char *
120 Asn1FieldName2CFieldName PARAMS ((aName),
121 char *aName)
122 {
123 char *retVal;
124 if (aName == NULL)
125 return NULL;
126
127 retVal = Malloc (strlen (aName) + 1);
128 strcpy (retVal, aName);
129 Dash2Underscore (retVal, strlen (retVal));
130
131 return retVal;
132 } /* Asn1FieldName2CFieldName */
133
134
135 /*
136 * allocates new str and returns a copy of the given
137 * string with '-'s (dashes) replaced by '_'s (underscores)
138 */
139 char *
140 Asn1ValueName2CValueName PARAMS ((aName),
141 char *aName)
142 {
143 char *retVal;
144 if (aName == NULL)
145 return NULL;
146
147 retVal = Malloc (strlen (aName) + 1);
148 strcpy (retVal, aName);
149 Dash2Underscore (retVal, strlen (retVal));
150
151 return retVal;
152 } /* Asn1FieldName2CFieldName */
153
154
155 /*
156 * allocates and returns a string with all of
157 * the caps from the given string
158 */
159 char *
160 GetCaps PARAMS ((str),
161 char *str)
162 {
163 int i, j;
164 char *retVal;
165
166 if (str == NULL)
167 return NULL;
168
169 retVal = Malloc (strlen (str) + 1);
170
171 for (j = 0, i = 0; i < strlen (str); i++)
172 {
173 if (isupper (str[i]))
174 retVal[j++] = str[i];
175 }
176
177 retVal[j] = '\0'; /* null terminate */
178
179 return retVal;
180
181 } /* GetCaps */
182
183
184 /*
185 * allocates and returns a string with all of
186 * the caps and digits from the given string
187 */
188 char *
189 GetCapsAndDigits PARAMS ((str),
190 char *str)
191 {
192 int i, j;
193 char *retVal;
194
195 if (str == NULL)
196 return NULL;
197
198 retVal = Malloc (strlen (str) + 1);
199
200 for (j = 0, i = 0; i < strlen (str); i++)
201 {
202 if ((isupper (str[i])) || (isdigit (str[i])))
203 retVal[j++] = str[i];
204 }
205
206 retVal[j] = '\0'; /* null terminate */
207
208 return retVal;
209
210 } /* GetCapsAndDigits */
211
212
213 /*
214 * replaces lowercase chars in given str
215 * with upper case version
216 * NOTE: modifies given str
217 */
218 void
219 Str2UCase PARAMS ((str, len),
220 char *str _AND_
221 int len)
222 {
223 int i;
224 for (i=0; i < len; i++)
225 {
226 if (islower (str[i]))
227 str[i] = toupper (str[i]);
228 }
229 } /* Str2UCase */
230
231
232 /*
233 * replaces uppercase chars in given str
234 * with lower case version
235 * NOTE: modifies given str
236 */
237 void
238 Str2LCase PARAMS ((str, len),
239 char *str _AND_
240 int len)
241 {
242 int i;
243 for (i=0; i < len; i++)
244 {
245 if (isupper (str[i]))
246 str[i] = tolower (str[i]);
247 }
248 } /* Str2LCase */
249
250
251 /*
252 * replace dash chars in given str
253 * with underscores
254 * NOTE: modifies given str
255 */
256 void
257 Dash2Underscore PARAMS ((str, len),
258 char *str _AND_
259 int len)
260 {
261 int i;
262 for (i=0; i < len; i++)
263 {
264 if (str[i] == '-')
265 str[i] = '_';
266 }
267 } /* Dash2Underscore */
268
269
270 /*
271 * tacks on the ascii version of the given digit
272 * at the end of the given str.
273 * NOTE: make sure the str you give has enough space
274 * for the digits
275 */
276 void
277 AppendDigit PARAMS ((str, digit),
278 char *str _AND_
279 int digit)
280 {
281 int high = 1000000000;
282 int currDigit;
283 int value;
284 char digitStr[20]; /* arbitrary length > max */
285
286 if (digit < 0)
287 digit *= -1;
288
289 currDigit = 0;
290 while (high > 0)
291 {
292 value = digit / high;
293 if (value != 0)
294 digitStr[currDigit++]= DIGIT_TO_ASCII (value);
295
296 digit = digit % high;
297 high = high/10;
298 }
299
300 if (currDigit == 0)
301 strcat (str, "0");
302 else
303 {
304 digitStr[currDigit] = '\0'; /* null terminate */
305 strcat (str, digitStr);
306 }
307 } /* AppendDigit */
308
309
310
311
312 /*
313 * given a defined object list containing null termintated strs,
314 * a str to be made unique wrt to the list by adding digits to the
315 * end, the max number of digits to add and the digit to start
316 * at, str is modified to be unique. It is not added to the
317 * defined object list. The given str must have enough spare,
318 * allocated chars after it's null terminator to hold maxDigits
319 * more characters.
320 * Only appends digits if the string is not unique or is a C keyword.
321 *
322 * Eg MakeCStrUnique ({ "Foo", "Bar" }, "Foo\0 ", 3, 1)
323 * modifies the the Str "Foo" to "Foo1"
324 */
325 void
326 MakeCStrUnique PARAMS ((nameList, str, maxDigits, startingDigit),
327 DefinedObj *nameList _AND_
328 char *str _AND_
329 int maxDigits _AND_
330 int startingDigit)
331 {
332 int digit, len, maxDigitVal;
333
334 if (ObjIsDefined (nameList, str, StrObjCmp) || IsCKeyWord (str))
335 {
336 for (maxDigitVal = 1; maxDigits > 0; maxDigits--)
337 maxDigitVal *= 10;
338
339 len = strlen (str);
340 digit = startingDigit;
341 do
342 {
343 str[len] = '\0';
344 AppendDigit (str, digit++);
345 } while (ObjIsDefined (nameList, str, StrObjCmp) && (digit < maxDigitVal));
346 }
347 } /* MakeCStrUnique */
348
349
350 /*
351 * same as MakeCStrUnique except checks against C++ keywords
352 */
353 void
354 MakeCxxStrUnique PARAMS ((nameList, str, maxDigits, startingDigit),
355 DefinedObj *nameList _AND_
356 char *str _AND_
357 int maxDigits _AND_
358 int startingDigit)
359 {
360 int digit, len, maxDigitVal;
361
362 if (ObjIsDefined (nameList, str, StrObjCmp) || IsCxxKeyWord (str))
363 {
364 for (maxDigitVal = 1; maxDigits > 0; maxDigits--)
365 maxDigitVal *= 10;
366
367 len = strlen (str);
368 digit = startingDigit;
369 do
370 {
371 str[len] = '\0';
372 AppendDigit (str, digit++);
373 } while (ObjIsDefined (nameList, str, StrObjCmp) && (digit < maxDigitVal));
374 }
375 } /* MakeCxxStrUnique */
376
377
378 /*
379 * if (keepbaseG)
380 * {
381 * strip leading path and trailing suffix
382 * }
383 * else
384 * {
385 * allocates and returns a base file name generated from
386 * the module's name. May shorten the name if the
387 * expected length exceed the systems max path component length
388 * (eg to support SYS V 14 char filename len limit)
389 * }
390 * Base file name is used as the base name for the generated C source files.
391 */
392 char *
393 MakeBaseFileName PARAMS ((refName),
394 const char *refName)
395 {
396 if (keepbaseG)
397 {
398 char *base, *dot;
399 int stublen;
400 char *stub;
401
402 if (base = strrchr (refName, '/'))
403 base++;
404 else
405 base = refName;
406
407 if (dot = strrchr (base, '.'))
408 stublen = dot - base;
409 else
410 stublen = strlen (base);
411
412 stub = Malloc (stublen+1);
413 memcpy (stub, base, stublen);
414 stub[stublen] = '\0';
415
416 return stub;
417 }
418 else
419 {
420 int fNameLen;
421 int cpyLen;
422 char *retVal;
423 int maxPathComponentLen;
424 char pathName[1024];
425 # define MAX_SUFFIX_LEN 2 /* .c, .h, .C */
426 extern int maxFileNameLenG; /* declared in snacc.c */
427
428 /*
429 * if the user has not given the max file name len
430 * via the -mf option,
431 * find the max filename len (ala POSIX method)
432 * if possible. Otherwise hardwire it to 14
433 * to support underpowered OSes
434 */
435 if (maxFileNameLenG > 2)
436 maxPathComponentLen = maxFileNameLenG;
437 else
438 #ifdef _PC_NAME_MAX
439 maxPathComponentLen = pathconf (getcwd (pathName, 1024), _PC_NAME_MAX);
440 #else
441 maxPathComponentLen = 14;
442 #endif
443
444 retVal = (char *)Malloc (strlen (refName) +1);
445 fNameLen = strlen (refName) + MAX_SUFFIX_LEN;
446 if ((fNameLen > maxPathComponentLen) && (maxPathComponentLen != -1))
447 {
448 cpyLen = maxPathComponentLen - MAX_SUFFIX_LEN;
449
450 /* don't allow trailing dash */
451 if (refName[cpyLen-1] == '-')
452 cpyLen--;
453
454 strncpy (retVal, refName, cpyLen);
455 retVal[cpyLen] = '\0';
456 }
457 else
458 strcpy (retVal, refName);
459
460 return retVal;
461 }
462 } /* MakeBaseFileName */
463
464
465
466
467 /*
468 * given a module name and a suffix, the
469 * suffix is appended to the module name
470 * and the whole string is put into lower case
471 * and underscores are inserted in likely places
472 * (ie MTSAbstractSvc.h -> mts_abstract_svc.h)
473 */
474 char *
475 MakeFileName PARAMS ((refName, suffix),
476 const char *refName _AND_
477 const char *suffix)
478 {
479 if (keepbaseG)
480 {
481 size_t baselen = strlen (refName),
482 sufflen = strlen (suffix);
483 char *filename = Malloc (baselen + sufflen + 1);
484
485 memcpy (filename, refName, baselen);
486 memcpy (filename+baselen, suffix, sufflen);
487 filename[baselen+sufflen] = '\0';
488
489 return filename;
490 }
491 else
492 {
493 int i, cpyIndex, len;
494 char *hdrCpy;
495 int fNameLen;
496 char *fName;
497 #define MAX_UNDERSCORE 10
498
499 fName = Malloc (strlen (refName) + strlen (suffix) + 1);
500 strcpy (fName, refName);
501 strcat (fName, suffix);
502
503
504 fNameLen = strlen (fName);
505
506 /*
507 * convert dashes to underscores, add spaces
508 */
509 Dash2Underscore (fName, fNameLen);
510
511
512 /*
513 * remove the next two lines if you uncomment the
514 * following underscore inserter
515 */
516 Str2LCase (fName, fNameLen - strlen (suffix));
517 return fName;
518
519 /*
520 * NO LONGER DONE - LET THE USER MODIFY THE ASN.1 IF DESIRED
521 * add underscore between Lcase/Ucase of UCase/UcaseLcasce
522 * eg MTSAbstractSvc -> MTS_Abstract_Svc
523 * (if enough space)
524 len = strlen (fName) + MAX_UNDERSCORE + 1;
525 hdrCpy = (char *) Malloc (len);
526
527 hdrCpy[0] = fName[0];
528 for (i = 1, cpyIndex = 1; (cpyIndex < len) && (i < fNameLen); i++)
529 {
530 if (((islower (fName[i-1])) && (isupper (fName[i]))) ||
531 ((isupper (fName[i-1])) && (isupper (fName[i])) &&
532 ((i < (fNameLen-1)) && (islower (fName[i+1])))))
533 {
534 hdrCpy[cpyIndex++] = '_';
535 hdrCpy[cpyIndex++] = fName[i];
536 }
537 else
538 hdrCpy[cpyIndex++] = fName[i];
539 }
540 hdrCpy[cpyIndex++] = '\0';
541
542 Str2LCase (hdrCpy, cpyIndex - strlen (suffix));
543
544 Free (fName);
545 return hdrCpy;
546 */
547 }
548 } /* MakeFileName */
549
550
551 char *
552 MakeCHdrFileName PARAMS ((refName),
553 const char *refName)
554 {
555 return MakeFileName (refName, ".h");
556 }
557
558 char *
559 MakeCSrcFileName PARAMS ((refName),
560 const char *refName)
561 {
562 return MakeFileName (refName, ".c");
563 }
564
565 char *
566 MakeCxxHdrFileName PARAMS ((refName),
567 const char *refName)
568 {
569 return MakeFileName (refName, ".h");
570 }
571
572 char *
573 MakeCxxSrcFileName PARAMS ((refName),
574 const char *refName)
575 {
576 #ifndef MACOS
577 return MakeFileName (refName, ".C");
578 #else
579 return MakeFileName (refName, ".cpp"); /* ignore cpp rant */
580 #endif
581 }
582
583 #ifdef _IBM_ENC_
584 char * /* 19.8.93 IBM-ENC */
585 MakedbHdrFileName PARAMS ((refName),
586 const char *refName)
587 {
588 return MakeFileName (refName, "db.h");
589 }
590
591 char * /* 19.8.93 IBM-ENC */
592 MakedbSrcFileName PARAMS ((refName),
593 const char *refName)
594 {
595 return MakeFileName (refName, "db.C");
596 }
597 #endif /* _IBM_ENC_ */
598
599 #if IDL
600 char *
601 MakeIDLFileName PARAMS ((refName),
602 const char *refName)
603 {
604 return MakeFileName (refName, ".idl");
605 }
606 #endif