Commit d4f70062 d4f70062143f7a313c288ad0ccec29fa82147e31 by Sergey Poznyakoff

Use lbuf stuff

1 parent 7ada98e7
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
32 #include <mailutils/stream.h> 32 #include <mailutils/stream.h>
33 33
34 #include <gsasl.h> 34 #include <gsasl.h>
35 #include <lbuf.h>
35 36
36 char *gsasl_cram_md5_pwd = SITE_CRAM_MD5_PWD; 37 char *gsasl_cram_md5_pwd = SITE_CRAM_MD5_PWD;
37 38
...@@ -87,47 +88,17 @@ struct _gsasl_stream { ...@@ -87,47 +88,17 @@ struct _gsasl_stream {
87 int last_err; /* Last Gsasl error code */ 88 int last_err; /* Last Gsasl error code */
88 89
89 int fd; /* File descriptor */ 90 int fd; /* File descriptor */
90 91 struct _line_buffer *lb;
91 char *buffer; /* Line buffer */
92 size_t size; /* Allocated size */
93 size_t level; /* Current filling level */
94 }; 92 };
95 93
96 static void 94 static void
97 _gsasl_destroy (stream_t stream) 95 _gsasl_destroy (stream_t stream)
98 { 96 {
99 struct _gsasl_stream *s = stream_get_owner (stream); 97 struct _gsasl_stream *s = stream_get_owner (stream);
100 free (s->buffer); 98 _auth_lb_destroy (&s->lb);
101 s->buffer = NULL; 99 s->buffer = NULL;
102 } 100 }
103 101
104 #define buffer_drop(s) s->level = 0
105
106 int
107 buffer_grow (struct _gsasl_stream *s, const char *ptr, size_t size)
108 {
109 if (!s->buffer)
110 {
111 s->buffer = malloc (size);
112 s->size = size;
113 s->level = 0;
114 }
115 else if (s->size - s->level < size)
116 {
117 size_t newsize = s->size + size;
118 s->buffer = realloc (s->buffer, newsize);
119 if (s->buffer)
120 s->size = newsize;
121 }
122
123 if (!s->buffer)
124 return ENOMEM;
125
126 memcpy (s->buffer + s->level, ptr, size);
127 s->level += size;
128 return 0;
129 }
130
131 static int 102 static int
132 _gsasl_readline (stream_t stream, char *optr, size_t osize, 103 _gsasl_readline (stream_t stream, char *optr, size_t osize,
133 off_t offset, size_t *nbytes) 104 off_t offset, size_t *nbytes)
...@@ -137,15 +108,10 @@ _gsasl_readline (stream_t stream, char *optr, size_t osize, ...@@ -137,15 +108,10 @@ _gsasl_readline (stream_t stream, char *optr, size_t osize,
137 size_t len, sz; 108 size_t len, sz;
138 char *bufp; 109 char *bufp;
139 110
140 if (s->level) 111 if (_auth_lb_level (&s->lb))
141 {
142 len = s->level > osize ? osize : s->level;
143 memcpy (optr, s->buffer, len);
144 if (s->level > len)
145 { 112 {
146 memmove (s->buffer, s->buffer + len, s->level - len); 113 len = _auth_lb_readline (s->lb, optr, osize-1);
147 s->level -= len; 114 optr[len] = 0;
148 }
149 if (nbytes) 115 if (nbytes)
150 *nbytes = len; 116 *nbytes = len;
151 return 0; 117 return 0;
...@@ -164,7 +130,7 @@ _gsasl_readline (stream_t stream, char *optr, size_t osize, ...@@ -164,7 +130,7 @@ _gsasl_readline (stream_t stream, char *optr, size_t osize,
164 return errno; 130 return errno;
165 } 131 }
166 132
167 rc = buffer_grow (s, buf, sz); 133 rc = _auth_lb_grow (s, buf, sz);
168 if (rc) 134 if (rc)
169 return rc; 135 return rc;
170 136
...@@ -182,7 +148,8 @@ _gsasl_readline (stream_t stream, char *optr, size_t osize, ...@@ -182,7 +148,8 @@ _gsasl_readline (stream_t stream, char *optr, size_t osize,
182 bufp = malloc (len + 1); 148 bufp = malloc (len + 1);
183 if (!bufp) 149 if (!bufp)
184 return ENOMEM; 150 return ENOMEM;
185 rc = gsasl_decode (s->sess_ctx, s->buffer, s->level, bufp, &len); 151 rc = gsasl_decode (s->sess_ctx,
152 _auth_lb_data (s->lb), _auth_lb_level (s->lb), bufp, &len);
186 if (rc != GSASL_OK) 153 if (rc != GSASL_OK)
187 { 154 {
188 s->last_err = rc; 155 s->last_err = rc;
...@@ -195,13 +162,13 @@ _gsasl_readline (stream_t stream, char *optr, size_t osize, ...@@ -195,13 +162,13 @@ _gsasl_readline (stream_t stream, char *optr, size_t osize,
195 if (len > osize) 162 if (len > osize)
196 { 163 {
197 memcpy (optr, bufp, osize); 164 memcpy (optr, bufp, osize);
198 buffer_drop (s); 165 _auth_lb_drop (s);
199 buffer_grow (s, bufp + osize, len - osize); 166 _auth_lb_grow (s, bufp + osize, len - osize);
200 len = osize; 167 len = osize;
201 } 168 }
202 else 169 else
203 { 170 {
204 buffer_drop (s); 171 _auth_lb_drop (s);
205 memcpy (optr, bufp, len); 172 memcpy (optr, bufp, len);
206 } 173 }
207 174
...@@ -257,7 +224,7 @@ _gsasl_write (stream_t stream, const char *iptr, size_t isize, ...@@ -257,7 +224,7 @@ _gsasl_write (stream_t stream, const char *iptr, size_t isize,
257 int rc; 224 int rc;
258 struct _gsasl_stream *s = stream_get_owner (stream); 225 struct _gsasl_stream *s = stream_get_owner (stream);
259 226
260 rc = buffer_grow (s, iptr, isize); 227 rc = _auth_lb_grow (s, iptr, isize);
261 if (rc) 228 if (rc)
262 return rc; 229 return rc;
263 230
...@@ -377,6 +344,8 @@ gsasl_stream_create (stream_t *stream, int fd, ...@@ -377,6 +344,8 @@ gsasl_stream_create (stream_t *stream, int fd,
377 else 344 else
378 stream_set_write (*stream, _gsasl_write, s); 345 stream_set_write (*stream, _gsasl_write, s);
379 346
347 _auth_lb_create (&s->lb);
348
380 return 0; 349 return 0;
381 } 350 }
382 351
......