Commit | Line | Data |
---|---|---|
9e8e5516 C |
1 | /* prim2str.c - presentation element to octet string */ |
2 | ||
3 | #ifndef lint | |
4 | static char *rcsid = "$Header: /f/osi/psap/RCS/prim2str.c,v 7.1 91/02/22 09:36:27 mrose Interim $"; | |
5 | #endif | |
6 | ||
7 | /* | |
8 | * $Header: /f/osi/psap/RCS/prim2str.c,v 7.1 91/02/22 09:36:27 mrose Interim $ | |
9 | * | |
10 | * | |
11 | * $Log: prim2str.c,v $ | |
12 | * Revision 7.1 91/02/22 09:36:27 mrose | |
13 | * Interim 6.8 | |
14 | * | |
15 | * Revision 7.0 89/11/23 22:13:16 mrose | |
16 | * Release 6.0 | |
17 | * | |
18 | */ | |
19 | ||
20 | /* | |
21 | * NOTICE | |
22 | * | |
23 | * Acquisition, use, and distribution of this module and related | |
24 | * materials are subject to the restrictions of a license agreement. | |
25 | * Consult the Preface in the User's Manual for the full terms of | |
26 | * this agreement. | |
27 | * | |
28 | */ | |
29 | ||
30 | ||
31 | /* LINTLIBRARY */ | |
32 | ||
33 | #include <stdio.h> | |
34 | #include "psap.h" | |
35 | ||
36 | /* \f */ | |
37 | ||
38 | /* Similar to pe_pullup. Returns a newly allocated string, composed of | |
39 | of any sub-elements in pe, whereas pe_pullup always reverts "pe" to | |
40 | a primitive. The string is null-terminated, though pe_len specifically | |
41 | does NOT reflect this. */ | |
42 | ||
43 | char *prim2str (pe, len) | |
44 | register PE pe; | |
45 | register int *len; | |
46 | { | |
47 | register int i, | |
48 | k; | |
49 | int j; | |
50 | register char *dp, | |
51 | *ep, | |
52 | *fp; | |
53 | register PElementClass class; | |
54 | register PElementID id; | |
55 | register PE p; | |
56 | ||
57 | *len = 0; | |
58 | switch (pe -> pe_form) { | |
59 | case PE_FORM_PRIM: | |
60 | if ((dp = malloc ((unsigned) ((i = pe -> pe_len) + 1))) == NULLCP) | |
61 | return pe_seterr (pe, PE_ERR_NMEM, NULLCP); | |
62 | bcopy ((char *) pe -> pe_prim, dp, i); | |
63 | break; | |
64 | ||
65 | case PE_FORM_CONS: | |
66 | if ((p = pe -> pe_cons) == NULLPE) { | |
67 | if ((dp = malloc ((unsigned) ((i = 0) + 1))) == NULLCP) | |
68 | return pe_seterr (pe, PE_ERR_NMEM, NULLCP); | |
69 | break; | |
70 | } | |
71 | dp = NULLCP, i = 0; | |
72 | class = p -> pe_class, id = p -> pe_id; | |
73 | for (p = pe -> pe_cons; p; p = p -> pe_next) { | |
74 | if ((p -> pe_class != class || p -> pe_id != id) | |
75 | && (p -> pe_class != PE_CLASS_UNIV | |
76 | || p -> pe_id != PE_PRIM_OCTS)) { | |
77 | if (dp) | |
78 | free (dp); | |
79 | return pe_seterr (pe, PE_ERR_TYPE, NULLCP); | |
80 | } | |
81 | ||
82 | if ((ep = prim2str (p, &j)) == NULLCP) { | |
83 | if (dp) | |
84 | free (dp); | |
85 | return pe_seterr (pe, PE_ERR_NMEM, NULLCP); | |
86 | } | |
87 | if (dp) { | |
88 | if ((fp = realloc (dp, (unsigned) ((k = i + j) + 1))) | |
89 | == NULLCP) { | |
90 | free (dp); | |
91 | return pe_seterr (pe, PE_ERR_NMEM, NULLCP); | |
92 | } | |
93 | bcopy (ep, fp + i, j); | |
94 | dp = fp, i = k; | |
95 | } | |
96 | else | |
97 | dp = ep, i += j; | |
98 | } | |
99 | break; | |
100 | } | |
101 | ||
102 | if (dp) | |
103 | dp[*len = i] = NULL; | |
104 | ||
105 | return dp; | |
106 | } |