attribute.c body.c header.c io.c mbx_unix.c message.c mime.c
net.c tcp.c transcode.c url_pop.c include/private/io0.h include/private/url0.h include/public/attribute.h include/public/io.h include/public/mailbox.h all function *_init() rename to *_create().
Showing
16 changed files
with
221 additions
and
198 deletions
... | @@ -254,8 +254,7 @@ attribute_copy (attribute_t dest, attribute_t src) | ... | @@ -254,8 +254,7 @@ attribute_copy (attribute_t dest, attribute_t src) |
254 | } | 254 | } |
255 | 255 | ||
256 | int | 256 | int |
257 | string_to_attribute (const char *buffer, size_t len, | 257 | string_to_attribute (const char *buffer, attribute_t *pattr, void *owner) |
258 | attribute_t *pattr, void *owner) | ||
259 | { | 258 | { |
260 | char *sep; | 259 | char *sep; |
261 | int status; | 260 | int status; |
... | @@ -265,9 +264,11 @@ string_to_attribute (const char *buffer, size_t len, | ... | @@ -265,9 +264,11 @@ string_to_attribute (const char *buffer, size_t len, |
265 | return status; | 264 | return status; |
266 | 265 | ||
267 | /* Set the attribute */ | 266 | /* Set the attribute */ |
268 | if (len > 7 && strncasecmp (buffer, "Status:", 7) == 0) | 267 | if (strncasecmp (buffer, "Status:", 7) == 0) |
269 | { | 268 | { |
270 | sep = strchr(buffer, ':'); /* pass the ':' */ | 269 | sep = strchr(buffer, ':'); /* pass the ':' */ |
270 | sep++; | ||
271 | while (*sep == ' ') sep++; /* glob spaces */ | ||
271 | if (strchr (sep, 'R') != NULL || strchr (sep, 'r') != NULL) | 272 | if (strchr (sep, 'R') != NULL || strchr (sep, 'r') != NULL) |
272 | attribute_set_read (*pattr); | 273 | attribute_set_read (*pattr); |
273 | if (strchr (sep, 'O') != NULL || strchr (sep, 'o') != NULL) | 274 | if (strchr (sep, 'O') != NULL || strchr (sep, 'o') != NULL) |
... | @@ -280,3 +281,40 @@ string_to_attribute (const char *buffer, size_t len, | ... | @@ -280,3 +281,40 @@ string_to_attribute (const char *buffer, size_t len, |
280 | return 0; | 281 | return 0; |
281 | } | 282 | } |
282 | 283 | ||
284 | int | ||
285 | attribute_to_string (attribute_t attr, char *buffer, size_t len, size_t *pn) | ||
286 | { | ||
287 | char status[32]; | ||
288 | char a[8]; | ||
289 | size_t i; | ||
290 | |||
291 | *status = *a = '\0'; | ||
292 | |||
293 | if (attribute_is_seen (attr)) | ||
294 | strcat (a, "R"); | ||
295 | if (attribute_is_answered (attr)) | ||
296 | strcat (a, "A"); | ||
297 | if (attribute_is_flagged (attr)) | ||
298 | strcat (a, "F"); | ||
299 | if (attribute_is_read (attr)) | ||
300 | strcat (a, "O"); | ||
301 | |||
302 | if (*a != '\0') | ||
303 | { | ||
304 | strcpy (status, "Status: "); | ||
305 | strcat (status, a); | ||
306 | strcat (status, "\n"); | ||
307 | } | ||
308 | |||
309 | i = strlen (status); | ||
310 | |||
311 | if (buffer && len != 0) | ||
312 | { | ||
313 | strncpy (buffer, status, len - 1); | ||
314 | buffer[len - 1] = '\0'; | ||
315 | i = strlen (buffer); | ||
316 | } | ||
317 | if (pn) | ||
318 | *pn = i; | ||
319 | return 0; | ||
320 | } | ... | ... |
... | @@ -82,7 +82,7 @@ body_get_stream (body_t body, stream_t *pstream) | ... | @@ -82,7 +82,7 @@ body_get_stream (body_t body, stream_t *pstream) |
82 | /* lazy floating body it is created when | 82 | /* lazy floating body it is created when |
83 | * doing the first body_write call | 83 | * doing the first body_write call |
84 | */ | 84 | */ |
85 | status = stream_create (&stream, body); | 85 | status = stream_create (&stream, 0, body); |
86 | if (status != 0) | 86 | if (status != 0) |
87 | return status; | 87 | return status; |
88 | stream_set_read (stream, body_read, body); | 88 | stream_set_read (stream, body_read, body); | ... | ... |
... | @@ -68,7 +68,7 @@ header_create (header_t *ph, const char *blurb, size_t len, void *owner) | ... | @@ -68,7 +68,7 @@ header_create (header_t *ph, const char *blurb, size_t len, void *owner) |
68 | 68 | ||
69 | header_parse (h, (char *)blurb, len); | 69 | header_parse (h, (char *)blurb, len); |
70 | 70 | ||
71 | status = stream_create (&(h->stream), h); | 71 | status = stream_create (&(h->stream), 0, h); |
72 | if (status != 0) | 72 | if (status != 0) |
73 | return status; | 73 | return status; |
74 | 74 | ... | ... |
... | @@ -35,6 +35,8 @@ extern "C" { | ... | @@ -35,6 +35,8 @@ extern "C" { |
35 | struct _stream | 35 | struct _stream |
36 | { | 36 | { |
37 | void *owner; | 37 | void *owner; |
38 | int flags; | ||
39 | void (*_destroy) __P ((void *)); | ||
38 | int (*_get_fd) __P ((stream_t, int *)); | 40 | int (*_get_fd) __P ((stream_t, int *)); |
39 | int (*_read) __P ((stream_t, char *, size_t, off_t, size_t *)); | 41 | int (*_read) __P ((stream_t, char *, size_t, off_t, size_t *)); |
40 | int (*_write) __P ((stream_t, const char *, size_t, off_t, size_t *)); | 42 | int (*_write) __P ((stream_t, const char *, size_t, off_t, size_t *)); | ... | ... |
... | @@ -92,22 +92,9 @@ struct _url | ... | @@ -92,22 +92,9 @@ struct _url |
92 | /* MMDF */ | 92 | /* MMDF */ |
93 | 93 | ||
94 | /* POP3 */ | 94 | /* POP3 */ |
95 | struct _url_pop; | ||
96 | typedef struct _url_pop * url_pop_t; | ||
97 | |||
98 | struct _url_pop | ||
99 | { | ||
100 | /* we use the fields from url_t */ | ||
101 | /* user, passwd, host, port */ | ||
102 | char * auth; | ||
103 | int (*_get_auth) __P ((const url_pop_t, char *, size_t, size_t *)); | ||
104 | }; | ||
105 | 95 | ||
106 | #define MU_POP_PORT 110 | 96 | #define MU_POP_PORT 110 |
107 | 97 | ||
108 | /* pop*/ | ||
109 | extern int url_pop_get_auth (const url_t, char *, size_t, size_t *); | ||
110 | |||
111 | /* UNIX MBOX */ | 98 | /* UNIX MBOX */ |
112 | 99 | ||
113 | #ifdef __cplusplus | 100 | #ifdef __cplusplus | ... | ... |
... | @@ -67,8 +67,10 @@ extern int attribute_is_equal __P ((attribute_t att1, attribute_t att2)); | ... | @@ -67,8 +67,10 @@ extern int attribute_is_equal __P ((attribute_t att1, attribute_t att2)); |
67 | extern int attribute_copy __P ((attribute_t dst, | 67 | extern int attribute_copy __P ((attribute_t dst, |
68 | attribute_t src)); | 68 | attribute_t src)); |
69 | 69 | ||
70 | extern int string_to_attribute __P ((const char *buf, size_t len, | 70 | extern int string_to_attribute __P ((const char *buf, |
71 | attribute_t *pattr, void *owner)); | 71 | attribute_t *pattr, void *owner)); |
72 | extern int attribute_to_string __P ((attribute_t attr, char *buf, | ||
73 | size_t len, size_t *)); | ||
72 | extern int attribute_get_owner __P ((attribute_t attr, void **owner)); | 74 | extern int attribute_get_owner __P ((attribute_t attr, void **owner)); |
73 | #ifdef __cplusplus | 75 | #ifdef __cplusplus |
74 | } | 76 | } | ... | ... |
... | @@ -35,9 +35,16 @@ extern "C" { /*}*/ | ... | @@ -35,9 +35,16 @@ extern "C" { /*}*/ |
35 | struct _stream; | 35 | struct _stream; |
36 | typedef struct _stream *stream_t; | 36 | typedef struct _stream *stream_t; |
37 | 37 | ||
38 | extern int stream_create __P ((stream_t *, void *owner)); | 38 | /* stream will be destroy on stream_destroy */ |
39 | #define MU_STREAM_NO_CHECK 1 | ||
40 | |||
41 | extern int stream_create __P ((stream_t *, int flags, void *owner)); | ||
39 | extern void stream_destroy __P ((stream_t *, void *owner)); | 42 | extern void stream_destroy __P ((stream_t *, void *owner)); |
40 | 43 | ||
44 | extern int stream_set_destroy __P ((stream_t, | ||
45 | void (*_destroy) __P ((void *)), | ||
46 | void *owner)); | ||
47 | |||
41 | extern int stream_set_fd __P ((stream_t, | 48 | extern int stream_set_fd __P ((stream_t, |
42 | int (*_get_fd)(stream_t, int *), | 49 | int (*_get_fd)(stream_t, int *), |
43 | void *owner)); | 50 | void *owner)); | ... | ... |
... | @@ -81,7 +81,7 @@ extern int mailbox_is_updated __P ((mailbox_t)); | ... | @@ -81,7 +81,7 @@ extern int mailbox_is_updated __P ((mailbox_t)); |
81 | extern int mailbox_scan __P ((mailbox_t, size_t msgno, size_t *count)); | 81 | extern int mailbox_scan __P ((mailbox_t, size_t msgno, size_t *count)); |
82 | 82 | ||
83 | /* mailbox size ? */ | 83 | /* mailbox size ? */ |
84 | extern int mailbox_size __P ((mailbox_t, off_t size)); | 84 | extern int mailbox_size __P ((mailbox_t, off_t *size)); |
85 | 85 | ||
86 | extern int mailbox_get_url __P ((mailbox_t, url_t *)); | 86 | extern int mailbox_get_url __P ((mailbox_t, url_t *)); |
87 | 87 | ... | ... |
... | @@ -22,7 +22,7 @@ | ... | @@ -22,7 +22,7 @@ |
22 | #include <stdio.h> | 22 | #include <stdio.h> |
23 | 23 | ||
24 | int | 24 | int |
25 | stream_create (stream_t *pstream, void *owner) | 25 | stream_create (stream_t *pstream, int flags, void *owner) |
26 | { | 26 | { |
27 | stream_t stream; | 27 | stream_t stream; |
28 | if (pstream == NULL || owner == NULL) | 28 | if (pstream == NULL || owner == NULL) |
... | @@ -31,6 +31,7 @@ stream_create (stream_t *pstream, void *owner) | ... | @@ -31,6 +31,7 @@ stream_create (stream_t *pstream, void *owner) |
31 | if (stream == NULL) | 31 | if (stream == NULL) |
32 | return ENOMEM; | 32 | return ENOMEM; |
33 | stream->owner = owner; | 33 | stream->owner = owner; |
34 | stream->flags = flags; | ||
34 | *pstream = stream; | 35 | *pstream = stream; |
35 | return 0; | 36 | return 0; |
36 | } | 37 | } |
... | @@ -41,13 +42,30 @@ stream_destroy (stream_t *pstream, void *owner) | ... | @@ -41,13 +42,30 @@ stream_destroy (stream_t *pstream, void *owner) |
41 | if (pstream && *pstream) | 42 | if (pstream && *pstream) |
42 | { | 43 | { |
43 | stream_t stream = *pstream; | 44 | stream_t stream = *pstream; |
44 | if (stream->owner == owner) | 45 | |
46 | if (stream->_destroy) | ||
47 | stream->_destroy (owner); | ||
48 | if ((stream->flags & MU_STREAM_NO_CHECK) || stream->owner == owner) | ||
45 | free (stream); | 49 | free (stream); |
46 | *pstream = NULL; | 50 | *pstream = NULL; |
47 | } | 51 | } |
48 | } | 52 | } |
49 | 53 | ||
50 | int | 54 | int |
55 | stream_set_destroy (stream_t stream, void (*_destroy) (void *), | ||
56 | void *owner) | ||
57 | { | ||
58 | if (stream == NULL) | ||
59 | return EINVAL; | ||
60 | |||
61 | if (stream->owner != owner) | ||
62 | return EACCES; | ||
63 | |||
64 | stream->_destroy = _destroy; | ||
65 | return 0; | ||
66 | } | ||
67 | |||
68 | int | ||
51 | stream_set_fd (stream_t stream, int (*_get_fd) (stream_t, int *), void *owner) | 69 | stream_set_fd (stream_t stream, int (*_get_fd) (stream_t, int *), void *owner) |
52 | { | 70 | { |
53 | if (stream == NULL) | 71 | if (stream == NULL) | ... | ... |
... | @@ -908,7 +908,7 @@ mailbox_unix_readstream (stream_t is, char *buffer, size_t buflen, | ... | @@ -908,7 +908,7 @@ mailbox_unix_readstream (stream_t is, char *buffer, size_t buflen, |
908 | mailbox_unix_message_t mum; | 908 | mailbox_unix_message_t mum; |
909 | size_t nread = 0; | 909 | size_t nread = 0; |
910 | 910 | ||
911 | if (is == NULL || (mum = (mailbox_unix_message_t)is->owner) == NULL) | 911 | if (is == NULL || (mum = is->owner) == NULL) |
912 | return EINVAL; | 912 | return EINVAL; |
913 | 913 | ||
914 | if (buffer == NULL || buflen == 0) | 914 | if (buffer == NULL || buflen == 0) |
... | @@ -1167,7 +1167,9 @@ mailbox_unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg) | ... | @@ -1167,7 +1167,9 @@ mailbox_unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg) |
1167 | message_destroy (&msg, mum); | 1167 | message_destroy (&msg, mum); |
1168 | return status; | 1168 | return status; |
1169 | } | 1169 | } |
1170 | status = stream_create (&stream, mum); | 1170 | message_set_body (msg, body, mum); |
1171 | |||
1172 | status = stream_create (&stream, 0, mum); | ||
1171 | if (status != 0) | 1173 | if (status != 0) |
1172 | { | 1174 | { |
1173 | message_destroy (&msg, mum); | 1175 | message_destroy (&msg, mum); |
... | @@ -1177,9 +1179,7 @@ mailbox_unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg) | ... | @@ -1177,9 +1179,7 @@ mailbox_unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg) |
1177 | stream_set_fd (stream, mailbox_unix_getfd, mum); | 1179 | stream_set_fd (stream, mailbox_unix_getfd, mum); |
1178 | body_set_stream (body, stream, mum); | 1180 | body_set_stream (body, stream, mum); |
1179 | body_set_size (body, mailbox_unix_body_size, mum); | 1181 | body_set_size (body, mailbox_unix_body_size, mum); |
1180 | /* set the line */ | ||
1181 | body_set_lines (body, mailbox_unix_body_lines, mum); | 1182 | body_set_lines (body, mailbox_unix_body_lines, mum); |
1182 | message_set_body (msg, body, mum); | ||
1183 | 1183 | ||
1184 | /* set the attribute */ | 1184 | /* set the attribute */ |
1185 | status = message_set_attribute (msg, mum->new_attr, mum); | 1185 | status = message_set_attribute (msg, mum->new_attr, mum); |
... | @@ -1293,7 +1293,7 @@ mailbox_unix_append_message (mailbox_t mbox, message_t msg) | ... | @@ -1293,7 +1293,7 @@ mailbox_unix_append_message (mailbox_t mbox, message_t msg) |
1293 | } | 1293 | } |
1294 | 1294 | ||
1295 | static int | 1295 | static int |
1296 | mailbox_unix_size (mailbox_t mbox, off_t *size) | 1296 | mailbox_unix_size (mailbox_t mbox, off_t *psize) |
1297 | { | 1297 | { |
1298 | mailbox_unix_data_t mud; | 1298 | mailbox_unix_data_t mud; |
1299 | struct stat st; | 1299 | struct stat st; |
... | @@ -1310,7 +1310,8 @@ mailbox_unix_size (mailbox_t mbox, off_t *size) | ... | @@ -1310,7 +1310,8 @@ mailbox_unix_size (mailbox_t mbox, off_t *size) |
1310 | /* oops !! */ | 1310 | /* oops !! */ |
1311 | if (fstat (fd, &st) != 0) | 1311 | if (fstat (fd, &st) != 0) |
1312 | return errno; | 1312 | return errno; |
1313 | *size = st.st_size; | 1313 | if (psize) |
1314 | *psize = st.st_size; | ||
1314 | return 0; | 1315 | return 0; |
1315 | } | 1316 | } |
1316 | 1317 | ... | ... |
... | @@ -47,7 +47,7 @@ message_create (message_t *pmsg, void *owner) | ... | @@ -47,7 +47,7 @@ message_create (message_t *pmsg, void *owner) |
47 | msg = calloc (1, sizeof (*msg)); | 47 | msg = calloc (1, sizeof (*msg)); |
48 | if (msg == NULL) | 48 | if (msg == NULL) |
49 | return ENOMEM; | 49 | return ENOMEM; |
50 | status = stream_create (&stream, msg); | 50 | status = stream_create (&stream, 0, msg); |
51 | if (status != 0) | 51 | if (status != 0) |
52 | { | 52 | { |
53 | free (msg); | 53 | free (msg); |
... | @@ -188,7 +188,7 @@ message_get_stream (message_t msg, stream_t *pstream) | ... | @@ -188,7 +188,7 @@ message_get_stream (message_t msg, stream_t *pstream) |
188 | { | 188 | { |
189 | stream_t stream; | 189 | stream_t stream; |
190 | int status; | 190 | int status; |
191 | status = stream_create (&stream, msg); | 191 | status = stream_create (&stream, 0, msg); |
192 | if (status != 0) | 192 | if (status != 0) |
193 | return status; | 193 | return status; |
194 | stream_set_read (stream, message_read, msg); | 194 | stream_set_read (stream, message_read, msg); | ... | ... |
... | @@ -42,7 +42,7 @@ static int _mime_append_part(mime_t mime, message_t msg, int body_offset, int bo | ... | @@ -42,7 +42,7 @@ static int _mime_append_part(mime_t mime, message_t msg, int body_offset, int bo |
42 | { | 42 | { |
43 | struct _mime_part *mime_part, **part_arr; | 43 | struct _mime_part *mime_part, **part_arr; |
44 | int ret; | 44 | int ret; |
45 | 45 | ||
46 | if ( ( mime_part = calloc(1, sizeof(*mime_part)) ) == NULL ) | 46 | if ( ( mime_part = calloc(1, sizeof(*mime_part)) ) == NULL ) |
47 | return ENOMEM; | 47 | return ENOMEM; |
48 | 48 | ||
... | @@ -86,14 +86,14 @@ static int _mime_append_part(mime_t mime, message_t msg, int body_offset, int bo | ... | @@ -86,14 +86,14 @@ static int _mime_append_part(mime_t mime, message_t msg, int body_offset, int bo |
86 | static struct _mime_part *_mime_get_owner(mime_t mime, message_t msg) | 86 | static struct _mime_part *_mime_get_owner(mime_t mime, message_t msg) |
87 | { | 87 | { |
88 | int i; | 88 | int i; |
89 | 89 | ||
90 | for ( i = 0; i < mime->nmtp_parts; i++ ) { | 90 | for ( i = 0; i < mime->nmtp_parts; i++ ) { |
91 | if ( mime->mtp_parts[i] == msg->owner ) | 91 | if ( mime->mtp_parts[i] == msg->owner ) |
92 | return mime->mtp_parts[i]; | 92 | return mime->mtp_parts[i]; |
93 | } | 93 | } |
94 | return NULL; | 94 | return NULL; |
95 | } | 95 | } |
96 | 96 | ||
97 | static char *_strltrim(char *str) | 97 | static char *_strltrim(char *str) |
98 | { | 98 | { |
99 | char *p; | 99 | char *p; |
... | @@ -106,7 +106,7 @@ static char *_strltrim(char *str) | ... | @@ -106,7 +106,7 @@ static char *_strltrim(char *str) |
106 | static char *_strttrim(char *str) | 106 | static char *_strttrim(char *str) |
107 | { | 107 | { |
108 | char *p; | 108 | char *p; |
109 | 109 | ||
110 | for (p = str + strlen(str) - 1; isspace(*p) && p >= str; --p) | 110 | for (p = str + strlen(str) - 1; isspace(*p) && p >= str; --p) |
111 | ; | 111 | ; |
112 | *++p = '\0'; | 112 | *++p = '\0'; |
... | @@ -121,28 +121,28 @@ char *_strtrim(char *str); | ... | @@ -121,28 +121,28 @@ char *_strtrim(char *str); |
121 | || ((c) == '@') || ((c) == ',') || ((c) == ';') || ((c) == ':') \ | 121 | || ((c) == '@') || ((c) == ',') || ((c) == ';') || ((c) == ':') \ |
122 | || ((c) == '\\') || ((c) == '"') || ((c) == '.') || ((c) == '[') \ | 122 | || ((c) == '\\') || ((c) == '"') || ((c) == '.') || ((c) == '[') \ |
123 | || ((c) == ']') ) | 123 | || ((c) == ']') ) |
124 | 124 | ||
125 | static void _mime_munge_content_header(char *field_body ) | 125 | static void _mime_munge_content_header(char *field_body ) |
126 | { | 126 | { |
127 | char *p, *e, *str = field_body; | 127 | char *p, *e, *str = field_body; |
128 | int quoted = 0; | 128 | int quoted = 0; |
129 | 129 | ||
130 | _strtrim(field_body); | 130 | _strtrim(field_body); |
131 | 131 | ||
132 | if ( ( e = p = strchr(str, ';') ) == NULL ) | 132 | if ( ( e = p = strchr(str, ';') ) == NULL ) |
133 | return; | 133 | return; |
134 | e++; | 134 | e++; |
135 | while ( *e && isspace(*e) ) /* remove space upto param */ | 135 | while ( *e && isspace(*e) ) /* remove space upto param */ |
136 | e++; | 136 | e++; |
137 | memmove(p+1, e, strlen(e)+1); | 137 | memmove(p+1, e, strlen(e)+1); |
138 | e = p+1; | 138 | e = p+1; |
139 | 139 | ||
140 | while ( *e && *e != '=' ) /* find end of value */ | 140 | while ( *e && *e != '=' ) /* find end of value */ |
141 | e++; | 141 | e++; |
142 | e = p = e+1; | 142 | e = p = e+1; |
143 | while ( *e && (quoted || !_ISSPECIAL(*e) || !isspace(*e) ) ) { | 143 | while ( *e && (quoted || !_ISSPECIAL(*e) || !isspace(*e) ) ) { |
144 | if ( *e == '\\' ) { /* escaped */ | 144 | if ( *e == '\\' ) { /* escaped */ |
145 | memmove(e, e+1, strlen(e)+2); | 145 | memmove(e, e+1, strlen(e)+2); |
146 | } else if ( *e == '\"' ) | 146 | } else if ( *e == '\"' ) |
147 | quoted = ~quoted; | 147 | quoted = ~quoted; |
148 | e++; | 148 | e++; |
... | @@ -153,7 +153,7 @@ static char *_mime_get_param(char *field_body, const char *param, int *len) | ... | @@ -153,7 +153,7 @@ static char *_mime_get_param(char *field_body, const char *param, int *len) |
153 | { | 153 | { |
154 | char *str, *p, *v, *e; | 154 | char *str, *p, *v, *e; |
155 | int quoted = 0, was_quoted = 0; | 155 | int quoted = 0, was_quoted = 0; |
156 | 156 | ||
157 | if ( len == NULL || ( str = field_body ) == NULL ) | 157 | if ( len == NULL || ( str = field_body ) == NULL ) |
158 | return NULL; | 158 | return NULL; |
159 | 159 | ||
... | @@ -168,7 +168,7 @@ static char *_mime_get_param(char *field_body, const char *param, int *len) | ... | @@ -168,7 +168,7 @@ static char *_mime_get_param(char *field_body, const char *param, int *len) |
168 | if ( *e == '\"' ) | 168 | if ( *e == '\"' ) |
169 | quoted = ~quoted, was_quoted = 1; | 169 | quoted = ~quoted, was_quoted = 1; |
170 | else | 170 | else |
171 | (*len)++; | 171 | (*len)++; |
172 | e++; | 172 | e++; |
173 | } | 173 | } |
174 | if ( strncasecmp(p, param, strlen(param)) ) { /* no match jump to next */ | 174 | if ( strncasecmp(p, param, strlen(param)) ) { /* no match jump to next */ |
... | @@ -177,7 +177,7 @@ static char *_mime_get_param(char *field_body, const char *param, int *len) | ... | @@ -177,7 +177,7 @@ static char *_mime_get_param(char *field_body, const char *param, int *len) |
177 | } | 177 | } |
178 | else | 178 | else |
179 | return was_quoted ? v + 1 : v; /* return unquted value */ | 179 | return was_quoted ? v + 1 : v; /* return unquted value */ |
180 | } | 180 | } |
181 | return NULL; | 181 | return NULL; |
182 | } | 182 | } |
183 | 183 | ||
... | @@ -211,11 +211,11 @@ static int _mime_parse_mpart_message(mime_t mime) | ... | @@ -211,11 +211,11 @@ static int _mime_parse_mpart_message(mime_t mime) |
211 | char *cp, *cp2; | 211 | char *cp, *cp2; |
212 | int blength, body_length, body_offset, ret; | 212 | int blength, body_length, body_offset, ret; |
213 | size_t nbytes; | 213 | size_t nbytes; |
214 | 214 | ||
215 | if ( !(mime->flags & MIME_PARSER_ACTIVE) ) { | 215 | if ( !(mime->flags & MIME_PARSER_ACTIVE) ) { |
216 | char *boundary; | 216 | char *boundary; |
217 | int len; | 217 | int len; |
218 | 218 | ||
219 | if ( ( ret = _mime_setup_buffers(mime) ) != 0 ) | 219 | if ( ( ret = _mime_setup_buffers(mime) ) != 0 ) |
220 | return ret; | 220 | return ret; |
221 | if ( ( boundary = _mime_get_param(mime->content_type, "boundary", &len) ) == NULL ) | 221 | if ( ( boundary = _mime_get_param(mime->content_type, "boundary", &len) ) == NULL ) |
... | @@ -223,7 +223,7 @@ static int _mime_parse_mpart_message(mime_t mime) | ... | @@ -223,7 +223,7 @@ static int _mime_parse_mpart_message(mime_t mime) |
223 | if ( ( mime->boundary = calloc(1, len + 1) ) == NULL ) | 223 | if ( ( mime->boundary = calloc(1, len + 1) ) == NULL ) |
224 | return ENOMEM; | 224 | return ENOMEM; |
225 | strncpy(mime->boundary, boundary, len ); | 225 | strncpy(mime->boundary, boundary, len ); |
226 | 226 | ||
227 | mime->cur_offset = 0; | 227 | mime->cur_offset = 0; |
228 | mime->line_ndx = 0; | 228 | mime->line_ndx = 0; |
229 | mime->parser_state = MIME_STATE_SCAN_BOUNDARY; | 229 | mime->parser_state = MIME_STATE_SCAN_BOUNDARY; |
... | @@ -231,7 +231,7 @@ static int _mime_parse_mpart_message(mime_t mime) | ... | @@ -231,7 +231,7 @@ static int _mime_parse_mpart_message(mime_t mime) |
231 | } | 231 | } |
232 | body_length = mime->body_length; | 232 | body_length = mime->body_length; |
233 | body_offset = mime->body_offset; | 233 | body_offset = mime->body_offset; |
234 | 234 | ||
235 | while ( ( ret = stream_read(mime->stream, mime->cur_buf, mime->buf_size, mime->cur_offset, &nbytes) ) == 0 && nbytes ) { | 235 | while ( ( ret = stream_read(mime->stream, mime->cur_buf, mime->buf_size, mime->cur_offset, &nbytes) ) == 0 && nbytes ) { |
236 | cp = mime->cur_buf; | 236 | cp = mime->cur_buf; |
237 | while ( nbytes ) { | 237 | while ( nbytes ) { |
... | @@ -247,14 +247,14 @@ static int _mime_parse_mpart_message(mime_t mime) | ... | @@ -247,14 +247,14 @@ static int _mime_parse_mpart_message(mime_t mime) |
247 | cp2 = mime->cur_line[0] == '\n' ? mime->cur_line + 1 : mime->cur_line; | 247 | cp2 = mime->cur_line[0] == '\n' ? mime->cur_line + 1 : mime->cur_line; |
248 | blength = strlen(mime->boundary); | 248 | blength = strlen(mime->boundary); |
249 | if ( mime->line_ndx >= blength ) { | 249 | if ( mime->line_ndx >= blength ) { |
250 | if ( ( !strncasecmp(cp2,"--", 2) && !strncasecmp(cp2+2, mime->boundary, blength) ) | 250 | if ( ( !strncasecmp(cp2,"--", 2) && !strncasecmp(cp2+2, mime->boundary, blength) ) |
251 | || !strncasecmp(cp2, mime->boundary, blength) ) { | 251 | || !strncasecmp(cp2, mime->boundary, blength) ) { |
252 | mime->parser_state = MIME_STATE_HEADERS; | 252 | mime->parser_state = MIME_STATE_HEADERS; |
253 | mime->flags &= ~MIME_PARSER_HAVE_CR; | 253 | mime->flags &= ~MIME_PARSER_HAVE_CR; |
254 | body_length = mime->cur_offset - body_offset - mime->line_ndx + 1; | 254 | body_length = mime->cur_offset - body_offset - mime->line_ndx + 1; |
255 | if ( mime->header_length ) /* this skips the preamble */ | 255 | if ( mime->header_length ) /* this skips the preamble */ |
256 | _mime_append_part(mime, NULL, body_offset, body_length, FALSE ); | 256 | _mime_append_part(mime, NULL, body_offset, body_length, FALSE ); |
257 | if ( ( cp2 + blength + 2 < cp && !strncasecmp(cp2+2+blength, "--",2) ) || | 257 | if ( ( cp2 + blength + 2 < cp && !strncasecmp(cp2+2+blength, "--",2) ) || |
258 | !strncasecmp(cp2+blength, "--",2) ) { /* very last boundary */ | 258 | !strncasecmp(cp2+blength, "--",2) ) { /* very last boundary */ |
259 | break; | 259 | break; |
260 | } | 260 | } |
... | @@ -262,7 +262,7 @@ static int _mime_parse_mpart_message(mime_t mime) | ... | @@ -262,7 +262,7 @@ static int _mime_parse_mpart_message(mime_t mime) |
262 | break; | 262 | break; |
263 | } | 263 | } |
264 | } | 264 | } |
265 | mime->line_ndx = 0; | 265 | mime->line_ndx = 0; |
266 | mime->cur_line[0] = *cp; /* stay in this state but leave '\n' at begining */ | 266 | mime->cur_line[0] = *cp; /* stay in this state but leave '\n' at begining */ |
267 | break; | 267 | break; |
268 | case MIME_STATE_HEADERS: | 268 | case MIME_STATE_HEADERS: |
... | @@ -271,7 +271,7 @@ static int _mime_parse_mpart_message(mime_t mime) | ... | @@ -271,7 +271,7 @@ static int _mime_parse_mpart_message(mime_t mime) |
271 | if ( mime->line_ndx == 1 || mime->cur_line[0] == '\r' ) { | 271 | if ( mime->line_ndx == 1 || mime->cur_line[0] == '\r' ) { |
272 | mime->parser_state = MIME_STATE_BEGIN_LINE; | 272 | mime->parser_state = MIME_STATE_BEGIN_LINE; |
273 | body_offset = mime->cur_offset + 1; | 273 | body_offset = mime->cur_offset + 1; |
274 | } | 274 | } |
275 | mime->line_ndx = -1; | 275 | mime->line_ndx = -1; |
276 | break; | 276 | break; |
277 | } | 277 | } |
... | @@ -284,12 +284,12 @@ static int _mime_parse_mpart_message(mime_t mime) | ... | @@ -284,12 +284,12 @@ static int _mime_parse_mpart_message(mime_t mime) |
284 | mime->cur_offset++; | 284 | mime->cur_offset++; |
285 | nbytes--; | 285 | nbytes--; |
286 | cp++; | 286 | cp++; |
287 | } | 287 | } |
288 | if ( mime->flags & MIME_INCREAMENTAL_PARSER ) { | 288 | if ( mime->flags & MIME_INCREAMENTAL_PARSER ) { |
289 | ret = EAGAIN; | 289 | ret = EAGAIN; |
290 | break; | 290 | break; |
291 | } | 291 | } |
292 | } | 292 | } |
293 | mime->body_length = body_length; | 293 | mime->body_length = body_length; |
294 | mime->body_offset = body_offset; | 294 | mime->body_offset = body_offset; |
295 | if ( ret != EAGAIN ) { /* finished cleanup */ | 295 | if ( ret != EAGAIN ) { /* finished cleanup */ |
... | @@ -303,7 +303,7 @@ static int _mime_message_read(stream_t stream, char *buf, size_t buflen, off_t o | ... | @@ -303,7 +303,7 @@ static int _mime_message_read(stream_t stream, char *buf, size_t buflen, off_t o |
303 | { | 303 | { |
304 | struct _mime_part *mime_part = stream->owner; | 304 | struct _mime_part *mime_part = stream->owner; |
305 | size_t read_len; | 305 | size_t read_len; |
306 | 306 | ||
307 | if ( nbytes == NULL ) | 307 | if ( nbytes == NULL ) |
308 | return(EINVAL); | 308 | return(EINVAL); |
309 | 309 | ||
... | @@ -321,7 +321,7 @@ static int _mime_new_message_read(stream_t stream, char *buf, size_t buflen, off | ... | @@ -321,7 +321,7 @@ static int _mime_new_message_read(stream_t stream, char *buf, size_t buflen, off |
321 | (void)stream; (void)buf; (void)buflen; (void)off; | 321 | (void)stream; (void)buf; (void)buflen; (void)off; |
322 | if ( nbytes == NULL ) | 322 | if ( nbytes == NULL ) |
323 | return(EINVAL); | 323 | return(EINVAL); |
324 | 324 | ||
325 | return 0; | 325 | return 0; |
326 | } | 326 | } |
327 | 327 | ||
... | @@ -361,10 +361,10 @@ int mime_create(mime_t *pmime, message_t msg, int flags) | ... | @@ -361,10 +361,10 @@ int mime_create(mime_t *pmime, message_t msg, int flags) |
361 | mime->msg = msg; | 361 | mime->msg = msg; |
362 | mime->buf_size = MIME_DFLT_BUF_SIZE; | 362 | mime->buf_size = MIME_DFLT_BUF_SIZE; |
363 | message_get_stream(msg, &(mime->stream)); | 363 | message_get_stream(msg, &(mime->stream)); |
364 | } | 364 | } |
365 | } | 365 | } |
366 | } | 366 | } |
367 | else { | 367 | else { |
368 | if ( ( ret = message_create( &msg, mime ) ) == 0 ) { | 368 | if ( ( ret = message_create( &msg, mime ) ) == 0 ) { |
369 | mime->flags |= MIME_NEW_MESSAGE; | 369 | mime->flags |= MIME_NEW_MESSAGE; |
370 | } | 370 | } |
... | @@ -386,22 +386,22 @@ void mime_destroy(mime_t *pmime) | ... | @@ -386,22 +386,22 @@ void mime_destroy(mime_t *pmime) |
386 | mime_t mime; | 386 | mime_t mime; |
387 | struct _mime_part *mime_part; | 387 | struct _mime_part *mime_part; |
388 | int i; | 388 | int i; |
389 | 389 | ||
390 | if (pmime && *pmime) { | 390 | if (pmime && *pmime) { |
391 | mime = *pmime; | 391 | mime = *pmime; |
392 | if ( mime->mtp_parts != NULL ) { | 392 | if ( mime->mtp_parts != NULL ) { |
393 | for ( i = 0; i < mime->nmtp_parts; i++ ) { | 393 | for ( i = 0; i < mime->nmtp_parts; i++ ) { |
394 | mime_part = mime->mtp_parts[i]; | 394 | mime_part = mime->mtp_parts[i]; |
395 | if ( mime_part->msg ) | 395 | if ( mime_part->msg ) |
396 | message_destroy(&mime_part->msg, mime_part); | 396 | message_destroy(&mime_part->msg, mime_part); |
397 | else | 397 | else |
398 | header_destroy(&mime_part->hdr, mime_part); | 398 | header_destroy(&mime_part->hdr, mime_part); |
399 | } | 399 | } |
400 | } | 400 | } |
401 | if ( mime->cap_msgs != NULL ) { | 401 | if ( mime->cap_msgs != NULL ) { |
402 | for ( i = 0; i < mime->ncap_msgs; i++ ) { | 402 | for ( i = 0; i < mime->ncap_msgs; i++ ) { |
403 | mime_part = mime->cap_msgs[i]; | 403 | mime_part = mime->cap_msgs[i]; |
404 | if ( mime_part->msg ) | 404 | if ( mime_part->msg ) |
405 | message_destroy(&mime_part->msg, mime_part); | 405 | message_destroy(&mime_part->msg, mime_part); |
406 | else | 406 | else |
407 | header_destroy(&mime_part->hdr, mime_part); | 407 | header_destroy(&mime_part->hdr, mime_part); |
... | @@ -413,7 +413,7 @@ void mime_destroy(mime_t *pmime) | ... | @@ -413,7 +413,7 @@ void mime_destroy(mime_t *pmime) |
413 | free(mime->cur_buf); | 413 | free(mime->cur_buf); |
414 | if ( mime->cur_line ) | 414 | if ( mime->cur_line ) |
415 | free(mime->cur_line); | 415 | free(mime->cur_line); |
416 | if ( mime->boundary ) | 416 | if ( mime->boundary ) |
417 | free(mime->boundary); | 417 | free(mime->boundary); |
418 | if ( mime->header_buf ) | 418 | if ( mime->header_buf ) |
419 | free(mime->header_buf); | 419 | free(mime->header_buf); |
... | @@ -437,7 +437,7 @@ int mime_get_part(mime_t mime, int part, message_t *msg) | ... | @@ -437,7 +437,7 @@ int mime_get_part(mime_t mime, int part, message_t *msg) |
437 | stream_t stream; | 437 | stream_t stream; |
438 | body_t body; | 438 | body_t body; |
439 | struct _mime_part *mime_part; | 439 | struct _mime_part *mime_part; |
440 | 440 | ||
441 | if ( ( ret = mime_get_num_parts(mime, &nmtp_parts ) ) == 0 ) { | 441 | if ( ( ret = mime_get_num_parts(mime, &nmtp_parts ) ) == 0 ) { |
442 | if ( part < 1 || part > nmtp_parts ) | 442 | if ( part < 1 || part > nmtp_parts ) |
443 | return EINVAL; | 443 | return EINVAL; |
... | @@ -449,7 +449,7 @@ int mime_get_part(mime_t mime, int part, message_t *msg) | ... | @@ -449,7 +449,7 @@ int mime_get_part(mime_t mime, int part, message_t *msg) |
449 | message_set_header(mime_part->msg, mime_part->hdr, mime_part); | 449 | message_set_header(mime_part->msg, mime_part->hdr, mime_part); |
450 | header_size(mime_part->hdr, &hsize); | 450 | header_size(mime_part->hdr, &hsize); |
451 | if ( ( ret = body_create(&body, mime_part) ) == 0 ) { | 451 | if ( ( ret = body_create(&body, mime_part) ) == 0 ) { |
452 | if ( ( ret = stream_create(&stream, mime_part) ) == 0 ) { | 452 | if ( ( ret = stream_create(&stream, 0, mime_part) ) == 0 ) { |
453 | body_set_size (body, _mime_body_size, mime_part); | 453 | body_set_size (body, _mime_body_size, mime_part); |
454 | stream_set_read(stream, _mime_message_read, mime_part); | 454 | stream_set_read(stream, _mime_message_read, mime_part); |
455 | body_set_stream(body, stream, mime_part); | 455 | body_set_stream(body, stream, mime_part); |
... | @@ -473,10 +473,10 @@ int mime_unencapsulate(mime_t mime, message_t msg, message_t *newmsg) | ... | @@ -473,10 +473,10 @@ int mime_unencapsulate(mime_t mime, message_t msg, message_t *newmsg) |
473 | stream_t stream; | 473 | stream_t stream; |
474 | body_t body; | 474 | body_t body; |
475 | struct _mime_part *mime_part; | 475 | struct _mime_part *mime_part; |
476 | 476 | ||
477 | if ( mime == NULL || msg == NULL || newmsg == NULL || mime->flags & MIME_NEW_MESSAGE ) | 477 | if ( mime == NULL || msg == NULL || newmsg == NULL || mime->flags & MIME_NEW_MESSAGE ) |
478 | return EINVAL; | 478 | return EINVAL; |
479 | 479 | ||
480 | if ( mime->msg != msg && ( mime_part = _mime_get_owner( mime, msg ) ) == NULL ) /* I don't know about or own this message */ | 480 | if ( mime->msg != msg && ( mime_part = _mime_get_owner( mime, msg ) ) == NULL ) /* I don't know about or own this message */ |
481 | return EPERM; | 481 | return EPERM; |
482 | 482 | ||
... | @@ -521,7 +521,7 @@ int mime_unencapsulate(mime_t mime, message_t msg, message_t *newmsg) | ... | @@ -521,7 +521,7 @@ int mime_unencapsulate(mime_t mime, message_t msg, message_t *newmsg) |
521 | if ( ( ret = message_create(&(mime_part->msg), mime_part) ) == 0) { | 521 | if ( ( ret = message_create(&(mime_part->msg), mime_part) ) == 0) { |
522 | message_set_header(mime_part->msg, mime_part->hdr, mime_part); | 522 | message_set_header(mime_part->msg, mime_part->hdr, mime_part); |
523 | if ( ( ret = body_create(&body, mime_part) ) == 0 ) { | 523 | if ( ( ret = body_create(&body, mime_part) ) == 0 ) { |
524 | if ( ( ret = stream_create(&stream, mime_part) ) == 0 ) { | 524 | if ( ( ret = stream_create(&stream, 0, mime_part) ) == 0 ) { |
525 | stream_set_read(stream, _mime_message_read, mime_part); | 525 | stream_set_read(stream, _mime_message_read, mime_part); |
526 | body_set_size (body, _mime_body_size, mime_part); | 526 | body_set_size (body, _mime_body_size, mime_part); |
527 | body_set_stream( body, stream, mime_part); | 527 | body_set_stream( body, stream, mime_part); |
... | @@ -537,13 +537,13 @@ int mime_unencapsulate(mime_t mime, message_t msg, message_t *newmsg) | ... | @@ -537,13 +537,13 @@ int mime_unencapsulate(mime_t mime, message_t msg, message_t *newmsg) |
537 | } | 537 | } |
538 | } | 538 | } |
539 | } | 539 | } |
540 | return ret; | 540 | return ret; |
541 | } | 541 | } |
542 | 542 | ||
543 | int mime_get_num_parts(mime_t mime, int *nmtp_parts) | 543 | int mime_get_num_parts(mime_t mime, int *nmtp_parts) |
544 | { | 544 | { |
545 | int ret = 0; | 545 | int ret = 0; |
546 | 546 | ||
547 | if ( mime->nmtp_parts == 0 || mime->flags & MIME_PARSER_ACTIVE ) { | 547 | if ( mime->nmtp_parts == 0 || mime->flags & MIME_PARSER_ACTIVE ) { |
548 | if ( mime_is_multi_part(mime) ) { | 548 | if ( mime_is_multi_part(mime) ) { |
549 | if ( ( ret = _mime_parse_mpart_message(mime) ) != 0 ) | 549 | if ( ( ret = _mime_parse_mpart_message(mime) ) != 0 ) |
... | @@ -553,14 +553,14 @@ int mime_get_num_parts(mime_t mime, int *nmtp_parts) | ... | @@ -553,14 +553,14 @@ int mime_get_num_parts(mime_t mime, int *nmtp_parts) |
553 | } | 553 | } |
554 | *nmtp_parts = mime->nmtp_parts; | 554 | *nmtp_parts = mime->nmtp_parts; |
555 | return(ret); | 555 | return(ret); |
556 | 556 | ||
557 | } | 557 | } |
558 | 558 | ||
559 | int mime_add_part(mime_t mime, message_t msg) | 559 | int mime_add_part(mime_t mime, message_t msg) |
560 | { | 560 | { |
561 | if ( mime == NULL || msg == NULL || ( mime->flags & MIME_NEW_MESSAGE ) == 0 ) | 561 | if ( mime == NULL || msg == NULL || ( mime->flags & MIME_NEW_MESSAGE ) == 0 ) |
562 | return EINVAL; | 562 | return EINVAL; |
563 | return _mime_append_part(mime, msg, 0, 0, FALSE); | 563 | return _mime_append_part(mime, msg, 0, 0, FALSE); |
564 | } | 564 | } |
565 | 565 | ||
566 | int mime_get_message(mime_t mime, message_t *msg) | 566 | int mime_get_message(mime_t mime, message_t *msg) | ... | ... |
... | @@ -17,6 +17,7 @@ | ... | @@ -17,6 +17,7 @@ |
17 | 17 | ||
18 | 18 | ||
19 | #include <stdlib.h> | 19 | #include <stdlib.h> |
20 | #include <string.h> | ||
20 | #include <errno.h> | 21 | #include <errno.h> |
21 | 22 | ||
22 | #include <net0.h> | 23 | #include <net0.h> |
... | @@ -27,22 +28,22 @@ int net_api_create(net_t *net, net_t parent, const char *type) | ... | @@ -27,22 +28,22 @@ int net_api_create(net_t *net, net_t parent, const char *type) |
27 | { | 28 | { |
28 | net_t n; | 29 | net_t n; |
29 | int i, napis, ret = 0; | 30 | int i, napis, ret = 0; |
30 | 31 | ||
31 | if ( net == NULL || type == NULL ) | 32 | if ( net == NULL || type == NULL ) |
32 | return EINVAL; | 33 | return EINVAL; |
33 | 34 | ||
34 | *net = NULL; | 35 | *net = NULL; |
35 | 36 | ||
36 | if ( ( n = calloc(1, sizeof(*n)) ) == NULL ) | 37 | if ( ( n = calloc(1, sizeof(*n)) ) == NULL ) |
37 | return ENOMEM; | 38 | return ENOMEM; |
38 | napis = sizeof(_netreg) / sizeof(_netreg[0]); | 39 | napis = sizeof(_netreg) / sizeof(_netreg[0]); |
39 | for( i = 0; i < napis; i++ ) { | 40 | for( i = 0; i < napis; i++ ) { |
40 | if ( strcasecmp(_netreg[i].type, type) == 0 ) | 41 | if ( strcasecmp(_netreg[i].type, type) == 0 ) |
41 | break; | 42 | break; |
42 | } | 43 | } |
43 | if ( i == napis ) | 44 | if ( i == napis ) |
44 | return ENOTSUP; | 45 | return ENOTSUP; |
45 | if ( ret = ( _netreg[i].create(&(n->data), &(n->api)) ) != 0 ) | 46 | if ( (ret = ( _netreg[i].create(&(n->data), &(n->api)) )) != 0 ) |
46 | free(n); | 47 | free(n); |
47 | n->parent = parent; | 48 | n->parent = parent; |
48 | n->net_reg = &_netreg[i]; | 49 | n->net_reg = &_netreg[i]; |
... | @@ -52,35 +53,35 @@ int net_api_create(net_t *net, net_t parent, const char *type) | ... | @@ -52,35 +53,35 @@ int net_api_create(net_t *net, net_t parent, const char *type) |
52 | 53 | ||
53 | int net_api_set_option(net_t net, const char *name, const char *value) | 54 | int net_api_set_option(net_t net, const char *name, const char *value) |
54 | { | 55 | { |
55 | if ( net && name && value ) | 56 | if ( net && name && value ) |
56 | return net->net_reg->set_option(net->data, name, value); | 57 | return net->net_reg->set_option(net->data, name, value); |
57 | return EINVAL; | 58 | return EINVAL; |
58 | } | 59 | } |
59 | 60 | ||
60 | int net_api_destroy(net_t *net) | 61 | int net_api_destroy(net_t *net) |
61 | { | 62 | { |
62 | net_t n; | 63 | net_t n; |
63 | if ( net == NULL || *net == NULL ) | 64 | if ( net == NULL || *net == NULL ) |
64 | return EINVAL; | 65 | return EINVAL; |
65 | 66 | ||
66 | n = *net; | 67 | n = *net; |
67 | n->net_reg->destroy(&n->data); | 68 | n->net_reg->destroy(&n->data); |
68 | free(n); | 69 | free(n); |
69 | *net = NULL; | 70 | *net = NULL; |
70 | return 0; | 71 | return 0; |
71 | } | 72 | } |
72 | 73 | ||
73 | int net_new(net_t net, netinstance_t *inst) | 74 | int net_new(net_t net, netinstance_t *inst) |
74 | { | 75 | { |
75 | netinstance_t netinst; | 76 | netinstance_t netinst; |
76 | int ret = 0; | 77 | int ret = 0; |
77 | 78 | ||
78 | if ( net == NULL || inst == NULL ) | 79 | if ( net == NULL || inst == NULL ) |
79 | return EINVAL; | 80 | return EINVAL; |
80 | 81 | ||
81 | *inst = NULL; | 82 | *inst = NULL; |
82 | 83 | ||
83 | if ( ( netinst = calloc(1, sizeof(*netinst)) ) == NULL ) | 84 | if ( ( netinst = calloc(1, sizeof(*netinst)) ) == NULL ) |
84 | return ENOMEM; | 85 | return ENOMEM; |
85 | netinst->api = net->api; | 86 | netinst->api = net->api; |
86 | if ( ( ret = net->api->new(net->data, net->parent, &(netinst->data)) ) != 0 ) { | 87 | if ( ( ret = net->api->new(net->data, net->parent, &(netinst->data)) ) != 0 ) { |
... | @@ -95,7 +96,7 @@ int net_connect(netinstance_t inst, const char *host, int port) | ... | @@ -95,7 +96,7 @@ int net_connect(netinstance_t inst, const char *host, int port) |
95 | { | 96 | { |
96 | if ( inst == NULL || host == NULL ) | 97 | if ( inst == NULL || host == NULL ) |
97 | return EINVAL; | 98 | return EINVAL; |
98 | 99 | ||
99 | return inst->api->connect(inst->data, host, port); | 100 | return inst->api->connect(inst->data, host, port); |
100 | } | 101 | } |
101 | 102 | ||
... | @@ -103,7 +104,7 @@ int net_get_stream(netinstance_t inst, stream_t *iostr) | ... | @@ -103,7 +104,7 @@ int net_get_stream(netinstance_t inst, stream_t *iostr) |
103 | { | 104 | { |
104 | if ( inst == NULL || iostr == NULL ) | 105 | if ( inst == NULL || iostr == NULL ) |
105 | return EINVAL; | 106 | return EINVAL; |
106 | 107 | ||
107 | return inst->api->get_stream(inst->data, iostr); | 108 | return inst->api->get_stream(inst->data, iostr); |
108 | } | 109 | } |
109 | 110 | ||
... | @@ -111,7 +112,7 @@ int net_close(netinstance_t inst) | ... | @@ -111,7 +112,7 @@ int net_close(netinstance_t inst) |
111 | { | 112 | { |
112 | if ( inst == NULL ) | 113 | if ( inst == NULL ) |
113 | return EINVAL; | 114 | return EINVAL; |
114 | 115 | ||
115 | return inst->api->close(inst->data); | 116 | return inst->api->close(inst->data); |
116 | } | 117 | } |
117 | 118 | ||
... | @@ -119,10 +120,10 @@ int net_free(netinstance_t *pinst) | ... | @@ -119,10 +120,10 @@ int net_free(netinstance_t *pinst) |
119 | { | 120 | { |
120 | int ret; | 121 | int ret; |
121 | netinstance_t inst; | 122 | netinstance_t inst; |
122 | 123 | ||
123 | if ( pinst == NULL || *pinst == NULL ) | 124 | if ( pinst == NULL || *pinst == NULL ) |
124 | return EINVAL; | 125 | return EINVAL; |
125 | 126 | ||
126 | inst = *pinst; | 127 | inst = *pinst; |
127 | ret = inst->api->free(&(inst->data)); | 128 | ret = inst->api->free(&(inst->data)); |
128 | free(inst); | 129 | free(inst); | ... | ... |
... | @@ -39,7 +39,7 @@ static int _tcp_doconnect(struct _tcp_instance *tcp) | ... | @@ -39,7 +39,7 @@ static int _tcp_doconnect(struct _tcp_instance *tcp) |
39 | struct sockaddr_in peer_addr; | 39 | struct sockaddr_in peer_addr; |
40 | struct hostent *phe; | 40 | struct hostent *phe; |
41 | struct sockaddr_in soc_addr; | 41 | struct sockaddr_in soc_addr; |
42 | 42 | ||
43 | switch( tcp->state ) { | 43 | switch( tcp->state ) { |
44 | case TCP_STATE_INIT: | 44 | case TCP_STATE_INIT: |
45 | if ( tcp->fd == -1 ) { | 45 | if ( tcp->fd == -1 ) { |
... | @@ -53,7 +53,7 @@ static int _tcp_doconnect(struct _tcp_instance *tcp) | ... | @@ -53,7 +53,7 @@ static int _tcp_doconnect(struct _tcp_instance *tcp) |
53 | } | 53 | } |
54 | tcp->state = TCP_STATE_RESOLVING; | 54 | tcp->state = TCP_STATE_RESOLVING; |
55 | case TCP_STATE_RESOLVING: | 55 | case TCP_STATE_RESOLVING: |
56 | if ( tcp->host == NULL || tcp->port == -1 ) | 56 | if ( tcp->host == NULL || tcp->port == -1 ) |
57 | return EINVAL; | 57 | return EINVAL; |
58 | tcp->address = inet_addr(tcp->host); | 58 | tcp->address = inet_addr(tcp->host); |
59 | if (tcp->address == INADDR_NONE) { | 59 | if (tcp->address == INADDR_NONE) { |
... | @@ -68,9 +68,9 @@ static int _tcp_doconnect(struct _tcp_instance *tcp) | ... | @@ -68,9 +68,9 @@ static int _tcp_doconnect(struct _tcp_instance *tcp) |
68 | case TCP_STATE_RESOLVE: | 68 | case TCP_STATE_RESOLVE: |
69 | memset (&soc_addr, 0, sizeof (soc_addr)); | 69 | memset (&soc_addr, 0, sizeof (soc_addr)); |
70 | soc_addr.sin_family = AF_INET; | 70 | soc_addr.sin_family = AF_INET; |
71 | soc_addr.sin_port = htons(tcp->port); | 71 | soc_addr.sin_port = htons(tcp->port); |
72 | soc_addr.sin_addr.s_addr = tcp->address; | 72 | soc_addr.sin_addr.s_addr = tcp->address; |
73 | 73 | ||
74 | if ( ( connect(tcp->fd, (struct sockaddr *) &soc_addr, sizeof(soc_addr)) ) == -1 ) { | 74 | if ( ( connect(tcp->fd, (struct sockaddr *) &soc_addr, sizeof(soc_addr)) ) == -1 ) { |
75 | ret = errno; | 75 | ret = errno; |
76 | if ( ret == EINPROGRESS || ret == EAGAIN ) { | 76 | if ( ret == EINPROGRESS || ret == EAGAIN ) { |
... | @@ -101,21 +101,21 @@ static int _tcp_get_fd(stream_t stream, int *fd) | ... | @@ -101,21 +101,21 @@ static int _tcp_get_fd(stream_t stream, int *fd) |
101 | 101 | ||
102 | if ( fd == NULL ) | 102 | if ( fd == NULL ) |
103 | return EINVAL; | 103 | return EINVAL; |
104 | 104 | ||
105 | if ( tcp->fd == -1 ) { | 105 | if ( tcp->fd == -1 ) { |
106 | if ( ( tcp->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ) | 106 | if ( ( tcp->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ) |
107 | return errno; | 107 | return errno; |
108 | } | 108 | } |
109 | *fd = tcp->fd; | 109 | *fd = tcp->fd; |
110 | return 0; | 110 | return 0; |
111 | } | 111 | } |
112 | 112 | ||
113 | static int _tcp_read(stream_t stream, char *buf, size_t buf_size, off_t offset, size_t *br) | 113 | static int _tcp_read(stream_t stream, char *buf, size_t buf_size, off_t offset, size_t *br) |
114 | { | 114 | { |
115 | struct _tcp_instance *tcp = stream->owner; | 115 | struct _tcp_instance *tcp = stream->owner; |
116 | 116 | ||
117 | offset; | 117 | offset; |
118 | if ( br == NULL ) | 118 | if ( br == NULL ) |
119 | return EINVAL; | 119 | return EINVAL; |
120 | *br = 0; | 120 | *br = 0; |
121 | if ( ( *br = recv(tcp->fd, buf, buf_size, 0) ) == -1 ) { | 121 | if ( ( *br = recv(tcp->fd, buf, buf_size, 0) ) == -1 ) { |
... | @@ -128,9 +128,9 @@ static int _tcp_read(stream_t stream, char *buf, size_t buf_size, off_t offset, | ... | @@ -128,9 +128,9 @@ static int _tcp_read(stream_t stream, char *buf, size_t buf_size, off_t offset, |
128 | static int _tcp_write(stream_t stream, const char *buf, size_t buf_size, off_t offset, size_t *bw) | 128 | static int _tcp_write(stream_t stream, const char *buf, size_t buf_size, off_t offset, size_t *bw) |
129 | { | 129 | { |
130 | struct _tcp_instance *tcp = stream->owner; | 130 | struct _tcp_instance *tcp = stream->owner; |
131 | 131 | ||
132 | offset; | 132 | offset; |
133 | if ( bw == NULL ) | 133 | if ( bw == NULL ) |
134 | return EINVAL; | 134 | return EINVAL; |
135 | *bw = 0; | 135 | *bw = 0; |
136 | if ( ( *bw = send(tcp->fd, buf, buf_size, 0) ) == -1 ) { | 136 | if ( ( *bw = send(tcp->fd, buf, buf_size, 0) ) == -1 ) { |
... | @@ -143,9 +143,9 @@ static int _tcp_write(stream_t stream, const char *buf, size_t buf_size, off_t o | ... | @@ -143,9 +143,9 @@ static int _tcp_write(stream_t stream, const char *buf, size_t buf_size, off_t o |
143 | static int _tcp_new(void *netdata, net_t parent, void **data) | 143 | static int _tcp_new(void *netdata, net_t parent, void **data) |
144 | { | 144 | { |
145 | struct _tcp_instance *tcp; | 145 | struct _tcp_instance *tcp; |
146 | 146 | ||
147 | if ( parent ) /* tcp must be top level api */ | 147 | if ( parent ) /* tcp must be top level api */ |
148 | return EINVAL; | 148 | return EINVAL; |
149 | 149 | ||
150 | if ( ( tcp = malloc(sizeof(*tcp)) ) == NULL ) | 150 | if ( ( tcp = malloc(sizeof(*tcp)) ) == NULL ) |
151 | return ENOMEM; | 151 | return ENOMEM; |
... | @@ -154,7 +154,7 @@ static int _tcp_new(void *netdata, net_t parent, void **data) | ... | @@ -154,7 +154,7 @@ static int _tcp_new(void *netdata, net_t parent, void **data) |
154 | tcp->host = NULL; | 154 | tcp->host = NULL; |
155 | tcp->port = -1; | 155 | tcp->port = -1; |
156 | tcp->state = TCP_STATE_INIT; | 156 | tcp->state = TCP_STATE_INIT; |
157 | stream_create(&tcp->stream, tcp); | 157 | stream_create(&tcp->stream, 0, tcp); |
158 | stream_set_read(tcp->stream, _tcp_read, tcp); | 158 | stream_set_read(tcp->stream, _tcp_read, tcp); |
159 | stream_set_write(tcp->stream, _tcp_write, tcp); | 159 | stream_set_write(tcp->stream, _tcp_write, tcp); |
160 | stream_set_fd(tcp->stream, _tcp_get_fd, tcp); | 160 | stream_set_fd(tcp->stream, _tcp_get_fd, tcp); |
... | @@ -165,9 +165,9 @@ static int _tcp_new(void *netdata, net_t parent, void **data) | ... | @@ -165,9 +165,9 @@ static int _tcp_new(void *netdata, net_t parent, void **data) |
165 | static int _tcp_connect(void *data, const char *host, int port) | 165 | static int _tcp_connect(void *data, const char *host, int port) |
166 | { | 166 | { |
167 | struct _tcp_instance *tcp = data; | 167 | struct _tcp_instance *tcp = data; |
168 | 168 | ||
169 | if ( tcp->state == TCP_STATE_INIT ) { | 169 | if ( tcp->state == TCP_STATE_INIT ) { |
170 | tcp->port = port; | 170 | tcp->port = port; |
171 | if ( ( tcp->host = strdup(host) ) == NULL ) | 171 | if ( ( tcp->host = strdup(host) ) == NULL ) |
172 | return ENOMEM; | 172 | return ENOMEM; |
173 | } | 173 | } |
... | @@ -199,15 +199,15 @@ static int _tcp_free(void **data) | ... | @@ -199,15 +199,15 @@ static int _tcp_free(void **data) |
199 | { | 199 | { |
200 | struct _tcp_instance *tcp; | 200 | struct _tcp_instance *tcp; |
201 | 201 | ||
202 | if ( data == NULL || *data == NULL ) | 202 | if ( data == NULL || *data == NULL ) |
203 | return EINVAL; | 203 | return EINVAL; |
204 | tcp = *data; | 204 | tcp = *data; |
205 | 205 | ||
206 | if ( tcp->host ) | 206 | if ( tcp->host ) |
207 | free(tcp->host); | 207 | free(tcp->host); |
208 | if ( tcp->fd != -1 ) | 208 | if ( tcp->fd != -1 ) |
209 | close(tcp->fd); | 209 | close(tcp->fd); |
210 | 210 | ||
211 | free(*data); | 211 | free(*data); |
212 | *data = NULL; | 212 | *data = NULL; |
213 | return 0; | 213 | return 0; |
... | @@ -221,10 +221,10 @@ static struct _net_api _tcp_net_api = { | ... | @@ -221,10 +221,10 @@ static struct _net_api _tcp_net_api = { |
221 | _tcp_free | 221 | _tcp_free |
222 | }; | 222 | }; |
223 | 223 | ||
224 | int _tcp_create(void **netdata, struct _net_api **netapi) | 224 | int _tcp_create(void **netdata, struct _net_api **netapi) |
225 | { | 225 | { |
226 | struct _tcp_options *options; | 226 | struct _tcp_options *options; |
227 | 227 | ||
228 | if ( ( options = malloc(sizeof(*options)) ) == NULL ) | 228 | if ( ( options = malloc(sizeof(*options)) ) == NULL ) |
229 | return ENOMEM; | 229 | return ENOMEM; |
230 | 230 | ||
... | @@ -239,13 +239,13 @@ int _tcp_create(void **netdata, struct _net_api **netapi) | ... | @@ -239,13 +239,13 @@ int _tcp_create(void **netdata, struct _net_api **netapi) |
239 | int _tcp_set_option(void *netdata, const char *name, const char *value) | 239 | int _tcp_set_option(void *netdata, const char *name, const char *value) |
240 | { | 240 | { |
241 | struct _tcp_options *options = netdata; | 241 | struct _tcp_options *options = netdata; |
242 | 242 | ||
243 | if ( strcasecmp(name, "tcp_non_block") == 0 ) { | 243 | if ( strcasecmp(name, "tcp_non_block") == 0 ) { |
244 | if ( value[0] == 't' || value[0] == 'T' || value[0] == '1' || value[0] == 'y' || value[0] == 'Y') | 244 | if ( value[0] == 't' || value[0] == 'T' || value[0] == '1' || value[0] == 'y' || value[0] == 'Y') |
245 | options->non_block = 1; | 245 | options->non_block = 1; |
246 | else | 246 | else |
247 | options->non_block = 0; | 247 | options->non_block = 0; |
248 | } | 248 | } |
249 | else if ( strcasecmp(name, "tcp_timeout") == 0 ) | 249 | else if ( strcasecmp(name, "tcp_timeout") == 0 ) |
250 | options->net_timeout = atoi(value); | 250 | options->net_timeout = atoi(value); |
251 | else | 251 | else |
... | @@ -256,7 +256,7 @@ int _tcp_set_option(void *netdata, const char *name, const char *value) | ... | @@ -256,7 +256,7 @@ int _tcp_set_option(void *netdata, const char *name, const char *value) |
256 | int _tcp_destroy(void **netdata) | 256 | int _tcp_destroy(void **netdata) |
257 | { | 257 | { |
258 | struct _tcp_options *options = *netdata; | 258 | struct _tcp_options *options = *netdata; |
259 | 259 | ||
260 | free(options); | 260 | free(options); |
261 | *netdata = NULL; | 261 | *netdata = NULL; |
262 | return 0; | 262 | return 0; | ... | ... |
... | @@ -40,7 +40,7 @@ struct _tc_desc { | ... | @@ -40,7 +40,7 @@ struct _tc_desc { |
40 | }; | 40 | }; |
41 | 41 | ||
42 | #define NUM_TRANSCODERS 5 | 42 | #define NUM_TRANSCODERS 5 |
43 | struct _tc_desc tclist[NUM_TRANSCODERS] = { { "base64", _base64_decode, _base64_encode, _base64_destroy }, | 43 | struct _tc_desc tclist[NUM_TRANSCODERS] = { { "base64", _base64_decode, _base64_encode, _base64_destroy }, |
44 | { "quoted-printable", _qp_decode, _qp_encode, NULL }, | 44 | { "quoted-printable", _qp_decode, _qp_encode, NULL }, |
45 | { "7bit", _identity_decode, _identity_encode, NULL }, | 45 | { "7bit", _identity_decode, _identity_encode, NULL }, |
46 | { "8bit", _identity_decode, _identity_encode, NULL }, | 46 | { "8bit", _identity_decode, _identity_encode, NULL }, |
... | @@ -51,7 +51,7 @@ int transcode_create(transcoder_t *ptc, char *encoding) | ... | @@ -51,7 +51,7 @@ int transcode_create(transcoder_t *ptc, char *encoding) |
51 | { | 51 | { |
52 | transcoder_t tc; | 52 | transcoder_t tc; |
53 | int i; | 53 | int i; |
54 | 54 | ||
55 | if ( ( tc = calloc(sizeof(struct _transcoder), 1) ) == NULL ) | 55 | if ( ( tc = calloc(sizeof(struct _transcoder), 1) ) == NULL ) |
56 | return ENOMEM; | 56 | return ENOMEM; |
57 | for( i = 0; i < NUM_TRANSCODERS; i++ ) { | 57 | for( i = 0; i < NUM_TRANSCODERS; i++ ) { |
... | @@ -61,7 +61,7 @@ int transcode_create(transcoder_t *ptc, char *encoding) | ... | @@ -61,7 +61,7 @@ int transcode_create(transcoder_t *ptc, char *encoding) |
61 | if ( i == NUM_TRANSCODERS ) | 61 | if ( i == NUM_TRANSCODERS ) |
62 | return ENOTSUP; | 62 | return ENOTSUP; |
63 | 63 | ||
64 | stream_create(&tc->ustream, tc ); | 64 | stream_create(&tc->ustream, 0, tc ); |
65 | stream_set_read(tc->ustream, tclist[i].decode_read, tc ); | 65 | stream_set_read(tc->ustream, tclist[i].decode_read, tc ); |
66 | stream_set_write(tc->ustream, tclist[i].encode_write, tc ); | 66 | stream_set_write(tc->ustream, tclist[i].encode_write, tc ); |
67 | tc->destroy = tclist[i].destroy; | 67 | tc->destroy = tclist[i].destroy; |
... | @@ -73,8 +73,8 @@ void transcode_destroy(transcoder_t *ptc) | ... | @@ -73,8 +73,8 @@ void transcode_destroy(transcoder_t *ptc) |
73 | { | 73 | { |
74 | transcoder_t tc = *ptc; | 74 | transcoder_t tc = *ptc; |
75 | 75 | ||
76 | if ( tc->destroy) | 76 | if ( tc->destroy) |
77 | tc->destroy(tc); | 77 | tc->destroy(tc); |
78 | stream_destroy(&tc->ustream, tc); | 78 | stream_destroy(&tc->ustream, tc); |
79 | free(tc); | 79 | free(tc); |
80 | *ptc = NULL; | 80 | *ptc = NULL; |
... | @@ -85,7 +85,7 @@ int transcode_get_stream(transcoder_t tc, stream_t *pstream) | ... | @@ -85,7 +85,7 @@ int transcode_get_stream(transcoder_t tc, stream_t *pstream) |
85 | if (tc == NULL || pstream == NULL) | 85 | if (tc == NULL || pstream == NULL) |
86 | return EINVAL; | 86 | return EINVAL; |
87 | *pstream = tc->ustream; | 87 | *pstream = tc->ustream; |
88 | return 0; | 88 | return 0; |
89 | } | 89 | } |
90 | 90 | ||
91 | int transcode_set_stream(transcoder_t tc, stream_t stream) | 91 | int transcode_set_stream(transcoder_t tc, stream_t stream) |
... | @@ -93,7 +93,7 @@ int transcode_set_stream(transcoder_t tc, stream_t stream) | ... | @@ -93,7 +93,7 @@ int transcode_set_stream(transcoder_t tc, stream_t stream) |
93 | if (tc == NULL || stream == NULL) | 93 | if (tc == NULL || stream == NULL) |
94 | return EINVAL; | 94 | return EINVAL; |
95 | tc->stream = stream; | 95 | tc->stream = stream; |
96 | return 0; | 96 | return 0; |
97 | } | 97 | } |
98 | 98 | ||
99 | /*------------------------------------------------------ | 99 | /*------------------------------------------------------ |
... | @@ -142,9 +142,9 @@ int _base64_decode(stream_t ustream, char *optr, size_t osize, off_t offset, siz | ... | @@ -142,9 +142,9 @@ int _base64_decode(stream_t ustream, char *optr, size_t osize, off_t offset, siz |
142 | int i, tmp = 0, ret; | 142 | int i, tmp = 0, ret; |
143 | size_t isize, consumed = 0; | 143 | size_t isize, consumed = 0; |
144 | char *iptr; | 144 | char *iptr; |
145 | transcoder_t tc = (transcoder_t)ustream->owner; | 145 | transcoder_t tc = (transcoder_t)ustream->owner; |
146 | struct _b64_decode *decode; | 146 | struct _b64_decode *decode; |
147 | 147 | ||
148 | if ( nbytes == NULL || optr == NULL || osize == 0 ) | 148 | if ( nbytes == NULL || optr == NULL || osize == 0 ) |
149 | return EINVAL; | 149 | return EINVAL; |
150 | 150 | ||
... | @@ -152,19 +152,19 @@ int _base64_decode(stream_t ustream, char *optr, size_t osize, off_t offset, siz | ... | @@ -152,19 +152,19 @@ int _base64_decode(stream_t ustream, char *optr, size_t osize, off_t offset, siz |
152 | if ( ( decode = tc->tcdata ) == NULL ) { | 152 | if ( ( decode = tc->tcdata ) == NULL ) { |
153 | if ( ( decode = calloc(1, sizeof(struct _b64_decode)) ) == NULL ) | 153 | if ( ( decode = calloc(1, sizeof(struct _b64_decode)) ) == NULL ) |
154 | return ENOMEM; | 154 | return ENOMEM; |
155 | tc->tcdata = decode; | 155 | tc->tcdata = decode; |
156 | } | 156 | } |
157 | if ( decode->offset != offset ) | 157 | if ( decode->offset != offset ) |
158 | return ESPIPE; | 158 | return ESPIPE; |
159 | 159 | ||
160 | if ( ( iptr = alloca(osize) ) == NULL ) | 160 | if ( ( iptr = alloca(osize) ) == NULL ) |
161 | return ENOMEM; | 161 | return ENOMEM; |
162 | isize = osize; | 162 | isize = osize; |
163 | 163 | ||
164 | if ( ( ret = stream_read(tc->stream, iptr, isize, decode->cur_offset, &isize) ) != 0 ) | 164 | if ( ( ret = stream_read(tc->stream, iptr, isize, decode->cur_offset, &isize) ) != 0 ) |
165 | return ret; | 165 | return ret; |
166 | 166 | ||
167 | decode->cur_offset += isize; | 167 | decode->cur_offset += isize; |
168 | i = decode->data_len; | 168 | i = decode->data_len; |
169 | while ( consumed < isize ) { | 169 | while ( consumed < isize ) { |
170 | while ( ( i < 4 ) && ( consumed < isize ) ) { | 170 | while ( ( i < 4 ) && ( consumed < isize ) ) { |
... | @@ -234,7 +234,7 @@ int _qp_decode(stream_t ustream, char *optr, size_t osize, off_t offset, size_t | ... | @@ -234,7 +234,7 @@ int _qp_decode(stream_t ustream, char *optr, size_t osize, off_t offset, size_t |
234 | size_t isize, consumed = 0; | 234 | size_t isize, consumed = 0; |
235 | struct _qp_decode *decode; | 235 | struct _qp_decode *decode; |
236 | char *iptr; | 236 | char *iptr; |
237 | 237 | ||
238 | if ( nbytes == NULL || optr == NULL ) | 238 | if ( nbytes == NULL || optr == NULL ) |
239 | return EINVAL; | 239 | return EINVAL; |
240 | 240 | ||
... | @@ -242,11 +242,11 @@ int _qp_decode(stream_t ustream, char *optr, size_t osize, off_t offset, size_t | ... | @@ -242,11 +242,11 @@ int _qp_decode(stream_t ustream, char *optr, size_t osize, off_t offset, size_t |
242 | if ( ( decode = tc->tcdata ) == NULL ) { | 242 | if ( ( decode = tc->tcdata ) == NULL ) { |
243 | if ( ( decode = calloc(1, sizeof(struct _qp_decode)) ) == NULL ) | 243 | if ( ( decode = calloc(1, sizeof(struct _qp_decode)) ) == NULL ) |
244 | return ENOMEM; | 244 | return ENOMEM; |
245 | tc->tcdata = decode; | 245 | tc->tcdata = decode; |
246 | } | 246 | } |
247 | if ( decode->offset != offset ) | 247 | if ( decode->offset != offset ) |
248 | return ESPIPE; | 248 | return ESPIPE; |
249 | 249 | ||
250 | if ( ( iptr = alloca(osize) ) == NULL ) | 250 | if ( ( iptr = alloca(osize) ) == NULL ) |
251 | return ENOMEM; | 251 | return ENOMEM; |
252 | isize = osize; | 252 | isize = osize; |
... | @@ -254,11 +254,11 @@ int _qp_decode(stream_t ustream, char *optr, size_t osize, off_t offset, size_t | ... | @@ -254,11 +254,11 @@ int _qp_decode(stream_t ustream, char *optr, size_t osize, off_t offset, size_t |
254 | if ( decode->data_len ) | 254 | if ( decode->data_len ) |
255 | memcpy( iptr, decode->data, decode->data_len); | 255 | memcpy( iptr, decode->data, decode->data_len); |
256 | 256 | ||
257 | if ( ( ret = stream_read(tc->stream, iptr + decode->data_len, isize - decode->data_len, decode->cur_offset, &isize) ) != 0 ) | 257 | if ( ( ret = stream_read(tc->stream, iptr + decode->data_len, isize - decode->data_len, decode->cur_offset, &isize) ) != 0 ) |
258 | return ret; | 258 | return ret; |
259 | 259 | ||
260 | decode->data_len = 0; | 260 | decode->data_len = 0; |
261 | decode->cur_offset += isize; | 261 | decode->cur_offset += isize; |
262 | while (consumed < isize) { | 262 | while (consumed < isize) { |
263 | c = *iptr++; | 263 | c = *iptr++; |
264 | if ( ((c >= 33) && (c <= 60)) || ((c >= 62) && (c <= 126)) || ((c == '=') && !_ishex(*iptr)) ) { | 264 | if ( ((c >= 33) && (c <= 60)) || ((c >= 62) && (c <= 126)) || ((c == '=') && !_ishex(*iptr)) ) { |
... | @@ -271,7 +271,7 @@ int _qp_decode(stream_t ustream, char *optr, size_t osize, off_t offset, size_t | ... | @@ -271,7 +271,7 @@ int _qp_decode(stream_t ustream, char *optr, size_t osize, off_t offset, size_t |
271 | if ((isize - consumed) < 3) { | 271 | if ((isize - consumed) < 3) { |
272 | memcpy( decode->data, iptr-1, decode->data_len = isize - consumed + 1); | 272 | memcpy( decode->data, iptr-1, decode->data_len = isize - consumed + 1); |
273 | return 0; | 273 | return 0; |
274 | } | 274 | } |
275 | else { | 275 | else { |
276 | // you get =XX where XX are hex characters | 276 | // you get =XX where XX are hex characters |
277 | char chr[2]; | 277 | char chr[2]; |
... | @@ -334,7 +334,7 @@ int _qp_encode(stream_t ustream, const char *iptr, size_t isize, off_t offset, s | ... | @@ -334,7 +334,7 @@ int _qp_encode(stream_t ustream, const char *iptr, size_t isize, off_t offset, s |
334 | int consumed = 0, c, osize; | 334 | int consumed = 0, c, osize; |
335 | char *obuf; | 335 | char *obuf; |
336 | struct _qp_encode *encode; | 336 | struct _qp_encode *encode; |
337 | 337 | ||
338 | 338 | ||
339 | if ( nbytes == NULL || iptr == NULL ) | 339 | if ( nbytes == NULL || iptr == NULL ) |
340 | return EINVAL; | 340 | return EINVAL; |
... | @@ -342,9 +342,9 @@ int _qp_encode(stream_t ustream, const char *iptr, size_t isize, off_t offset, s | ... | @@ -342,9 +342,9 @@ int _qp_encode(stream_t ustream, const char *iptr, size_t isize, off_t offset, s |
342 | if ( isize == 0 ) | 342 | if ( isize == 0 ) |
343 | return 0; | 343 | return 0; |
344 | 344 | ||
345 | *nbytes = 0; | 345 | *nbytes = 0; |
346 | if ( ( encode = tc->tcdata ) == NULL ) { | 346 | if ( ( encode = tc->tcdata ) == NULL ) { |
347 | 347 | ||
348 | } | 348 | } |
349 | if ( ( encode->obuf = malloc(isize) ) == NULL ) | 349 | if ( ( encode->obuf = malloc(isize) ) == NULL ) |
350 | return ENOMEM; | 350 | return ENOMEM; | ... | ... |
... | @@ -19,7 +19,6 @@ | ... | @@ -19,7 +19,6 @@ |
19 | #include <registrar.h> | 19 | #include <registrar.h> |
20 | 20 | ||
21 | #include <errno.h> | 21 | #include <errno.h> |
22 | #include <cpystr.h> | ||
23 | #include <stdlib.h> | 22 | #include <stdlib.h> |
24 | #include <string.h> | 23 | #include <string.h> |
25 | #include <errno.h> | 24 | #include <errno.h> |
... | @@ -33,27 +32,6 @@ struct url_registrar _url_pop_registrar = | ... | @@ -33,27 +32,6 @@ struct url_registrar _url_pop_registrar = |
33 | url_pop_create, url_pop_destroy | 32 | url_pop_create, url_pop_destroy |
34 | }; | 33 | }; |
35 | 34 | ||
36 | static int get_auth (const url_pop_t up, char *s, size_t len, size_t *); | ||
37 | |||
38 | static int | ||
39 | get_auth (const url_pop_t up, char *s, size_t len, size_t *n) | ||
40 | { | ||
41 | size_t i; | ||
42 | if (up) | ||
43 | return EINVAL; | ||
44 | i = _cpystr (s, up->auth, len); | ||
45 | if (n) | ||
46 | *n = i; | ||
47 | return 0; | ||
48 | } | ||
49 | |||
50 | int | ||
51 | (url_pop_get_auth) (const url_t url, char *auth, size_t len, size_t *n) | ||
52 | { | ||
53 | return (url) ? | ||
54 | ((url_pop_t)(url->data))->_get_auth(url->data, auth, len, n) : EINVAL; | ||
55 | } | ||
56 | |||
57 | static void | 35 | static void |
58 | url_pop_destroy (url_t *purl) | 36 | url_pop_destroy (url_t *purl) |
59 | { | 37 | { |
... | @@ -64,12 +42,6 @@ url_pop_destroy (url_t *purl) | ... | @@ -64,12 +42,6 @@ url_pop_destroy (url_t *purl) |
64 | free (url->user); | 42 | free (url->user); |
65 | free (url->passwd); | 43 | free (url->passwd); |
66 | free (url->host); | 44 | free (url->host); |
67 | if (url->data) | ||
68 | { | ||
69 | url_pop_t up = url->data; | ||
70 | free (up->auth); | ||
71 | } | ||
72 | free (url->data); | ||
73 | free (url); | 45 | free (url); |
74 | *purl = NULL; | 46 | *purl = NULL; |
75 | } | 47 | } |
... | @@ -77,28 +49,25 @@ url_pop_destroy (url_t *purl) | ... | @@ -77,28 +49,25 @@ url_pop_destroy (url_t *purl) |
77 | 49 | ||
78 | /* | 50 | /* |
79 | POP URL | 51 | POP URL |
80 | pop://<user>;AUTH=<auth>@<host>:<port> | 52 | pop://[<user>;AUTH=<auth>@]<host>[:<port>] |
81 | */ | 53 | */ |
82 | static int | 54 | static int |
83 | url_pop_create (url_t *purl, const char *name) | 55 | url_pop_create (url_t *purl, const char *name) |
84 | { | 56 | { |
85 | const char *host_port, *indexe; | 57 | const char *host_port, *indexe; |
86 | struct url_registrar *ureg = &_url_pop_registrar; | 58 | struct url_registrar *ureg = &_url_pop_registrar; |
87 | size_t len, scheme_len = strlen (ureg->scheme); | 59 | size_t scheme_len = strlen (ureg->scheme); |
88 | url_t url; | 60 | url_t url; |
89 | url_pop_t up; | ||
90 | 61 | ||
91 | /* reject the obvious */ | 62 | /* reject the obvious */ |
92 | if (name == NULL || strncmp (ureg->scheme, name, scheme_len) != 0 | 63 | if (name == NULL || strncmp (ureg->scheme, name, scheme_len) != 0) |
93 | || (host_port = strchr (name, '@')) == NULL | 64 | return EINVAL; |
94 | || (len = strlen (name)) < 9 /* 6(scheme)+1(user)+1(@)+1(host)*/) | ||
95 | return ENOMEM; | ||
96 | 65 | ||
97 | /* do I need to decode url encoding '% hex hex' ? */ | 66 | /* do I need to decode url encoding '% hex hex' ? */ |
98 | 67 | ||
99 | url = calloc(1, sizeof (*url)); | 68 | url = calloc(1, sizeof (*url)); |
100 | url->data = up = calloc(1, sizeof(*up)); | 69 | if (url == NULL) |
101 | up->_get_auth = get_auth; | 70 | return ENOMEM; |
102 | 71 | ||
103 | /* TYPE */ | 72 | /* TYPE */ |
104 | url->_create = _url_pop_registrar._create; | 73 | url->_create = _url_pop_registrar._create; |
... | @@ -114,24 +83,22 @@ url_pop_create (url_t *purl, const char *name) | ... | @@ -114,24 +83,22 @@ url_pop_create (url_t *purl, const char *name) |
114 | 83 | ||
115 | name += scheme_len; /* pass the scheme */ | 84 | name += scheme_len; /* pass the scheme */ |
116 | 85 | ||
86 | host_port = strchr (name, '@'); | ||
87 | if (host_port == NULL) | ||
88 | host_port= name; | ||
89 | |||
117 | /* looking for "user;auth=auth-enc" */ | 90 | /* looking for "user;auth=auth-enc" */ |
118 | for (indexe = name; indexe != host_port; indexe++) | 91 | for (indexe = name; indexe != host_port; indexe++) |
119 | { | 92 | { |
120 | /* Auth ? */ | 93 | /* Auth ? */ |
121 | if (*indexe == ';') | 94 | if (*indexe == ';') |
122 | { | 95 | { |
123 | /* make sure it the token */ | 96 | /* make sure it's the token */ |
124 | if (strncasecmp(indexe + 1, "auth=", 5) == 0) | 97 | if (strncasecmp(indexe + 1, "auth=", 5) == 0) |
125 | break; | 98 | break; |
126 | } | 99 | } |
127 | } | 100 | } |
128 | 101 | ||
129 | if (indexe == name) | ||
130 | { | ||
131 | url_pop_destroy (&url); | ||
132 | return -1; | ||
133 | } | ||
134 | |||
135 | /* USER */ | 102 | /* USER */ |
136 | url->user = malloc(indexe - name + 1); | 103 | url->user = malloc(indexe - name + 1); |
137 | if (url->user == NULL) | 104 | if (url->user == NULL) |
... | @@ -142,29 +109,29 @@ url_pop_create (url_t *purl, const char *name) | ... | @@ -142,29 +109,29 @@ url_pop_create (url_t *purl, const char *name) |
142 | ((char *)memcpy(url->user, name, indexe - name))[indexe - name] = '\0'; | 109 | ((char *)memcpy(url->user, name, indexe - name))[indexe - name] = '\0'; |
143 | 110 | ||
144 | /* AUTH */ | 111 | /* AUTH */ |
145 | if ((host_port - indexe) <= 6 /*strlen(";AUTH=")*/) | 112 | if (indexe == host_port) |
146 | { | 113 | { |
147 | /* Use default AUTH '*' */ | 114 | /* Use default AUTH '*' */ |
148 | up->auth = malloc (1 + 1); | 115 | url->passwd = malloc (1 + 1); |
149 | if (up->auth) | 116 | if (url->passwd) |
150 | { | 117 | { |
151 | up->auth[0] = '*'; | 118 | url->passwd[0] = '*'; |
152 | up->auth[1] = '\0'; | 119 | url->passwd[1] = '\0'; |
153 | } | 120 | } |
154 | } | 121 | } |
155 | else | 122 | else |
156 | { | 123 | { |
157 | /* move pass AUTH= */ | 124 | /* move pass AUTH= */ |
158 | indexe += 6; | 125 | indexe += 6; |
159 | up->auth = malloc (host_port - indexe + 1); | 126 | url->passwd = malloc (host_port - indexe + 1); |
160 | if (up->auth) | 127 | if (url->passwd) |
161 | { | 128 | { |
162 | ((char *)memcpy (up->auth, indexe, host_port - indexe)) | 129 | ((char *)memcpy (url->passwd, indexe, host_port - indexe)) |
163 | [host_port - indexe] = '\0'; | 130 | [host_port - indexe] = '\0'; |
164 | } | 131 | } |
165 | } | 132 | } |
166 | 133 | ||
167 | if (up->auth == NULL) | 134 | if (url->passwd == NULL) |
168 | { | 135 | { |
169 | url_pop_destroy (&url); | 136 | url_pop_destroy (&url); |
170 | return -1; | 137 | return -1; | ... | ... |
-
Please register or sign in to post a comment