386BSD 0.1 development
[unix-history] / usr / othersrc / contrib / isode / dsap / common / file_enc.c
CommitLineData
04c6839a
WJ
1#include "quipu/util.h"
2#include "quipu/attrvalue.h"
3
4extern LLog * log_dsap;
5extern char dsa_mode;
6
7int file_attr_length;
8
9static char *
10readfile (file)
11FILE * file;
12{
13char *parse_buffer;
14int parse_len;
15char *buf;
16int buflen;
17int curlen;
18int size;
19
20 curlen = 0;
21 buflen = parse_len = LINESIZE;
22 buf = parse_buffer = smalloc(LINESIZE);
23
24 for (;;) {
25 if ( buflen <= 100) {
26 parse_len += LINESIZE;
27 buflen += LINESIZE;
28 if ((parse_buffer = realloc(parse_buffer, (unsigned)parse_len)) == NULLCP)
29 exit (2); /* ??? */
30 buf = parse_buffer + curlen;
31 }
32
33 if (fgets (buf, buflen,file) == NULLCP) {
34 file_attr_length = curlen;
35 return (parse_buffer);
36 }
37
38 size = strlen(buf);
39 if (buf[size - 1] == '\n') {
40 buf[--size] = '\0';
41 }
42 buf += size;
43 buflen -= size;
44 curlen += size;
45
46 }
47
48 /* NOTREACHED */
49}
50
51
52PE grab_filepe (av)
53AttributeValue av;
54{
55FILE * fptr;
56struct file_syntax * fs;
57sntx_table *tbl, * get_syntax_table();
58PE ret_pe = NULLPE, grab_pe();
59
60 fs = (struct file_syntax *) av->av_struct;
61
62 if (fs->fs_attr != NULLAttrV)
63 return (grab_pe (fs->fs_attr));
64
65 if (fs->fs_name == NULLCP)
66 goto out; /* should never happen */
67
68 if ((fptr = fopen (fs->fs_name,"r")) != NULL) {
69 tbl = get_syntax_table (fs->fs_real_syntax);
70 if (tbl->s_parse == NULLIFP) { /* treat as pure asn */
71 PS fps;
72 fps = ps_alloc (std_open);
73 if ((std_setup (fps,fptr)) == NOTOK) {
74 (void) fclose (fptr);
75 ps_free (fps);
76 goto out;
77 }
78 ret_pe = ps2pe (fps);
79 if (fps->ps_errno != PS_ERR_NONE) {
80 LLOG (log_dsap,LLOG_EXCEPTIONS,("%s in attribute file '%s'",ps_error(fps->ps_errno),fs->fs_name));
81 if (ret_pe) {
82 pe_free (ret_pe);
83 ret_pe = NULLPE;
84 }
85 } else if (ret_pe == NULLPE)
86 LLOG (log_dsap,LLOG_EXCEPTIONS,("invalid ASN in file '%s'",fs->fs_name));
87 ps_free (fps);
88 } else {
89 char * buffer;
90 AttributeValue newav;
91
92 buffer = readfile (fptr);
93
94 if ((newav = str2AttrV (buffer,fs->fs_real_syntax)) == NULLAttrV){
95 LLOG (log_dsap,LLOG_EXCEPTIONS,("invalid format in file %s",fs->fs_name));
96 } else {
97 ret_pe = grab_pe (newav);
98 AttrV_free (newav);
99 }
100 file_attr_length = 0;
101 free (buffer);
102 }
103 (void) fclose (fptr);
104 } else
105 LLOG (log_dsap, LLOG_EXCEPTIONS, ("Attribute file '%s' not found",fs->fs_name));
106
107out:;
108 if ((ret_pe == NULLPE) && dsa_mode) {
109 LLOG(log_dsap,LLOG_NOTICE,("Error with file attribute '%s'",fs->fs_name));
110 /* As we are a DSA, return the PE containing NULL
111 /* a NULLPE will cause the encode to fail, thus make the
112 /* operation fail.
113 /* Real solution is to remove the attribute at the encode
114 /* stage - but that is tricky...
115 /* DUAs should fail.
116 */
117 return (pe_alloc (PE_CLASS_UNIV, PE_FORM_PRIM, PE_PRIM_NULL));
118 }
119 return (ret_pe);
120
121}
122
123
124file_decode (x)
125AttributeValue x;
126{
127struct file_syntax * fs;
128
129 fs = (struct file_syntax *) smalloc (sizeof(struct file_syntax));
130 fs->fs_ref = 1;
131 fs->fs_real_syntax = x->av_syntax;
132 if (x->av_syntax >= AV_WRITE_FILE)
133 fs->fs_real_syntax -= AV_WRITE_FILE;
134 fs->fs_name = NULLCP;
135 fs->fs_mode = FS_DEFAULT;
136
137 fs->fs_attr = AttrV_alloc ();
138 fs->fs_attr->av_syntax = fs->fs_real_syntax;
139 fs->fs_attr->av_struct = x->av_struct;
140
141 x->av_syntax = AV_FILE;
142 x->av_struct = (caddr_t)fs;
143}