summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeffrey Stedfast <fejj@ximian.com>2001-09-18 19:07:25 +0000
committerJeffrey Stedfast <fejj@src.gnome.org>2001-09-18 19:07:25 +0000
commit7a211b41fe20e5b2dcc63e2a6b256cf7ec2b9e20 (patch)
tree296f4cb33ca6b266c1013a08470eb5be945dd1de
parent1cfab9c2b7ba4949890acf7cdc7e901b75c08a22 (diff)
downloadgmime-7a211b41fe20e5b2dcc63e2a6b256cf7ec2b9e20.tar.gz
Fix offset calculations.
2001-09-18 Jeffrey Stedfast <fejj@ximian.com> * gmime-parser.c (g_mime_parser_construct_part_internal): Fix offset calculations.
-rw-r--r--ChangeLog5
-rw-r--r--gmime-parser.c29
-rw-r--r--gmime-stream-file.c2
-rw-r--r--gmime-stream-fs.c4
-rw-r--r--gmime-stream.c20
-rw-r--r--gmime/gmime-parser.c29
-rw-r--r--gmime/gmime-stream-file.c2
-rw-r--r--gmime/gmime-stream-fs.c4
-rw-r--r--gmime/gmime-stream.c20
-rw-r--r--test-parser.c25
-rw-r--r--tests/test-parser.c25
11 files changed, 75 insertions, 90 deletions
diff --git a/ChangeLog b/ChangeLog
index b100647e..58234c59 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
2001-09-18 Jeffrey Stedfast <fejj@ximian.com>
+ * gmime-parser.c (g_mime_parser_construct_part_internal): Fix
+ offset calculations.
+
+2001-09-18 Jeffrey Stedfast <fejj@ximian.com>
+
* test-parser.c (test_parser): Updated.
* test-mime.c (test_parser): Updated.
diff --git a/gmime-parser.c b/gmime-parser.c
index ccc5e821..9798ebe3 100644
--- a/gmime-parser.c
+++ b/gmime-parser.c
@@ -110,7 +110,7 @@ g_strstrbound (const char *haystack, const char *needle, const char *end)
}
static const char *
-find_header_part_end (const char* in, guint inlen)
+find_header_part_end (const char *in, guint inlen)
{
const char *pch;
const char *hdr_end = NULL;
@@ -296,12 +296,9 @@ g_mime_parser_construct_part_internal (GMimeStream *stream, GMimeStreamMem *mem)
const char *inend;
const char *hdr_end;
const char *in;
- off_t offset;
int inlen;
- offset = g_mime_stream_tell (stream);
-
- in = mem->buffer->data + GMIME_STREAM (mem)->bound_start;
+ in = mem->buffer->data + GMIME_STREAM (mem)->position;
inend = mem->buffer->data + GMIME_STREAM (mem)->bound_end;
inlen = inend - in;
@@ -324,10 +321,11 @@ g_mime_parser_construct_part_internal (GMimeStream *stream, GMimeStreamMem *mem)
GMimePart *subpart;
const char *part_begin;
const char *part_end;
- off_t start, end;
+ off_t start, end, pos;
- start = stream->bound_start;
- end = stream->bound_end;
+ pos = GMIME_STREAM (mem)->position;
+ start = GMIME_STREAM (mem)->bound_start;
+ end = GMIME_STREAM (mem)->bound_end;
part_begin = g_strstrbound (inptr, boundary, inend);
while (part_begin && part_begin < inend) {
@@ -347,16 +345,17 @@ g_mime_parser_construct_part_internal (GMimeStream *stream, GMimeStreamMem *mem)
/* get the subpart */
part_begin += strlen (boundary);
- g_mime_stream_set_bounds (stream, part_begin - in, part_end - in);
- subpart = g_mime_parser_construct_part (stream);
+ g_mime_stream_set_bounds (GMIME_STREAM (mem), pos + (part_begin - in),
+ pos + (part_end - in));
+ subpart = g_mime_parser_construct_part_internal (stream, mem);
g_mime_part_add_subpart (mime_part, subpart);
- g_mime_stream_set_bounds (stream, start, end);
/* the next part begins where the last one left off */
part_begin = part_end;
}
g_mime_stream_set_bounds (stream, start, end);
+ g_mime_stream_seek (GMIME_STREAM (mem), pos, GMIME_STREAM_SEEK_SET);
/* free our temp boundary strings */
g_free (boundary);
@@ -385,12 +384,16 @@ g_mime_parser_construct_part_internal (GMimeStream *stream, GMimeStreamMem *mem)
if (len > 0) {
if (GMIME_IS_STREAM_MEM (stream)) {
/* if we've already got it in memory, we use less memory if we
- * use individual streams per part */
+ * use individual mem streams per part after parsing... */
g_mime_part_set_pre_encoded_content (mime_part, content, len, encoding);
} else {
GMimeDataWrapper *wrapper;
GMimeStream *substream;
- off_t start, end;
+ off_t offset, start, end;
+
+ offset = g_mime_stream_tell (stream);
+ if (stream != GMIME_STREAM (mem))
+ offset += GMIME_STREAM (mem)->position;
start = offset + (content - in);
end = start + len;
diff --git a/gmime-stream-file.c b/gmime-stream-file.c
index 48141eaa..aeed4afd 100644
--- a/gmime-stream-file.c
+++ b/gmime-stream-file.c
@@ -64,7 +64,7 @@ stream_read (GMimeStream *stream, char *buf, size_t len)
GMimeStreamFile *fstream = (GMimeStreamFile *) stream;
ssize_t nread;
- if (stream->bound_end == -1 && stream->position > stream->bound_end)
+ if (stream->bound_end != -1 && stream->position > stream->bound_end)
return -1;
if (stream->bound_end != -1)
diff --git a/gmime-stream-fs.c b/gmime-stream-fs.c
index d344ea48..92f0748d 100644
--- a/gmime-stream-fs.c
+++ b/gmime-stream-fs.c
@@ -67,9 +67,9 @@ static ssize_t
stream_read (GMimeStream *stream, char *buf, size_t len)
{
GMimeStreamFs *fstream = (GMimeStreamFs *) stream;
- size_t nread;
+ ssize_t nread;
- if (stream->bound_end == -1 && stream->position > stream->bound_end)
+ if (stream->bound_end != -1 && stream->position > stream->bound_end)
return -1;
if (stream->bound_end != -1)
diff --git a/gmime-stream.c b/gmime-stream.c
index c653360f..0d197c0e 100644
--- a/gmime-stream.c
+++ b/gmime-stream.c
@@ -360,19 +360,21 @@ g_mime_stream_write_to_stream (GMimeStream *src, GMimeStream *dest)
g_return_val_if_fail (dest != NULL, -1);
while (!g_mime_stream_eos (src)) {
- nwritten = 0;
nread = g_mime_stream_read (src, buf, sizeof (buf));
if (nread < 0)
return -1;
- while (nwritten < nread) {
- ssize_t len;
-
- len = g_mime_stream_write (dest, buf + nwritten, nread - nwritten);
- if (len < 0)
- return -1;
-
- nwritten += len;
+ if (nread > 0) {
+ nwritten = 0;
+ while (nwritten < nread) {
+ ssize_t len;
+
+ len = g_mime_stream_write (dest, buf + nwritten, nread - nwritten);
+ if (len < 0)
+ return -1;
+
+ nwritten += len;
+ }
}
total += nwritten;
diff --git a/gmime/gmime-parser.c b/gmime/gmime-parser.c
index ccc5e821..9798ebe3 100644
--- a/gmime/gmime-parser.c
+++ b/gmime/gmime-parser.c
@@ -110,7 +110,7 @@ g_strstrbound (const char *haystack, const char *needle, const char *end)
}
static const char *
-find_header_part_end (const char* in, guint inlen)
+find_header_part_end (const char *in, guint inlen)
{
const char *pch;
const char *hdr_end = NULL;
@@ -296,12 +296,9 @@ g_mime_parser_construct_part_internal (GMimeStream *stream, GMimeStreamMem *mem)
const char *inend;
const char *hdr_end;
const char *in;
- off_t offset;
int inlen;
- offset = g_mime_stream_tell (stream);
-
- in = mem->buffer->data + GMIME_STREAM (mem)->bound_start;
+ in = mem->buffer->data + GMIME_STREAM (mem)->position;
inend = mem->buffer->data + GMIME_STREAM (mem)->bound_end;
inlen = inend - in;
@@ -324,10 +321,11 @@ g_mime_parser_construct_part_internal (GMimeStream *stream, GMimeStreamMem *mem)
GMimePart *subpart;
const char *part_begin;
const char *part_end;
- off_t start, end;
+ off_t start, end, pos;
- start = stream->bound_start;
- end = stream->bound_end;
+ pos = GMIME_STREAM (mem)->position;
+ start = GMIME_STREAM (mem)->bound_start;
+ end = GMIME_STREAM (mem)->bound_end;
part_begin = g_strstrbound (inptr, boundary, inend);
while (part_begin && part_begin < inend) {
@@ -347,16 +345,17 @@ g_mime_parser_construct_part_internal (GMimeStream *stream, GMimeStreamMem *mem)
/* get the subpart */
part_begin += strlen (boundary);
- g_mime_stream_set_bounds (stream, part_begin - in, part_end - in);
- subpart = g_mime_parser_construct_part (stream);
+ g_mime_stream_set_bounds (GMIME_STREAM (mem), pos + (part_begin - in),
+ pos + (part_end - in));
+ subpart = g_mime_parser_construct_part_internal (stream, mem);
g_mime_part_add_subpart (mime_part, subpart);
- g_mime_stream_set_bounds (stream, start, end);
/* the next part begins where the last one left off */
part_begin = part_end;
}
g_mime_stream_set_bounds (stream, start, end);
+ g_mime_stream_seek (GMIME_STREAM (mem), pos, GMIME_STREAM_SEEK_SET);
/* free our temp boundary strings */
g_free (boundary);
@@ -385,12 +384,16 @@ g_mime_parser_construct_part_internal (GMimeStream *stream, GMimeStreamMem *mem)
if (len > 0) {
if (GMIME_IS_STREAM_MEM (stream)) {
/* if we've already got it in memory, we use less memory if we
- * use individual streams per part */
+ * use individual mem streams per part after parsing... */
g_mime_part_set_pre_encoded_content (mime_part, content, len, encoding);
} else {
GMimeDataWrapper *wrapper;
GMimeStream *substream;
- off_t start, end;
+ off_t offset, start, end;
+
+ offset = g_mime_stream_tell (stream);
+ if (stream != GMIME_STREAM (mem))
+ offset += GMIME_STREAM (mem)->position;
start = offset + (content - in);
end = start + len;
diff --git a/gmime/gmime-stream-file.c b/gmime/gmime-stream-file.c
index 48141eaa..aeed4afd 100644
--- a/gmime/gmime-stream-file.c
+++ b/gmime/gmime-stream-file.c
@@ -64,7 +64,7 @@ stream_read (GMimeStream *stream, char *buf, size_t len)
GMimeStreamFile *fstream = (GMimeStreamFile *) stream;
ssize_t nread;
- if (stream->bound_end == -1 && stream->position > stream->bound_end)
+ if (stream->bound_end != -1 && stream->position > stream->bound_end)
return -1;
if (stream->bound_end != -1)
diff --git a/gmime/gmime-stream-fs.c b/gmime/gmime-stream-fs.c
index d344ea48..92f0748d 100644
--- a/gmime/gmime-stream-fs.c
+++ b/gmime/gmime-stream-fs.c
@@ -67,9 +67,9 @@ static ssize_t
stream_read (GMimeStream *stream, char *buf, size_t len)
{
GMimeStreamFs *fstream = (GMimeStreamFs *) stream;
- size_t nread;
+ ssize_t nread;
- if (stream->bound_end == -1 && stream->position > stream->bound_end)
+ if (stream->bound_end != -1 && stream->position > stream->bound_end)
return -1;
if (stream->bound_end != -1)
diff --git a/gmime/gmime-stream.c b/gmime/gmime-stream.c
index c653360f..0d197c0e 100644
--- a/gmime/gmime-stream.c
+++ b/gmime/gmime-stream.c
@@ -360,19 +360,21 @@ g_mime_stream_write_to_stream (GMimeStream *src, GMimeStream *dest)
g_return_val_if_fail (dest != NULL, -1);
while (!g_mime_stream_eos (src)) {
- nwritten = 0;
nread = g_mime_stream_read (src, buf, sizeof (buf));
if (nread < 0)
return -1;
- while (nwritten < nread) {
- ssize_t len;
-
- len = g_mime_stream_write (dest, buf + nwritten, nread - nwritten);
- if (len < 0)
- return -1;
-
- nwritten += len;
+ if (nread > 0) {
+ nwritten = 0;
+ while (nwritten < nread) {
+ ssize_t len;
+
+ len = g_mime_stream_write (dest, buf + nwritten, nread - nwritten);
+ if (len < 0)
+ return -1;
+
+ nwritten += len;
+ }
}
total += nwritten;
diff --git a/test-parser.c b/test-parser.c
index f5e4839b..a2d68bc4 100644
--- a/test-parser.c
+++ b/test-parser.c
@@ -42,26 +42,21 @@ print_mime_struct (GMimePart *part, int depth)
}
void
-test_parser (char *data)
+test_parser (GMimeStream *stream)
{
GMimeMessage *message;
- GMimeStream *stream;
gboolean is_html;
char *text;
char *body;
fprintf (stdout, "\nTesting MIME parser...\n\n");
- stream = g_mime_stream_mem_new_with_buffer (data, strlen (data));
-
ZenTimerStart();
message = g_mime_parser_construct_message (stream, TRUE);
ZenTimerStop();
ZenTimerReport ("gmime::parser_construct_message");
ZenTimerStart();
- g_mime_stream_unref (stream);
-
text = g_mime_message_to_string (message);
ZenTimerStop();
ZenTimerReport ("gmime::message_to_string");
@@ -85,8 +80,7 @@ test_parser (char *data)
int main (int argc, char *argv[])
{
char *filename = NULL;
- char *data;
- struct stat st;
+ GMimeStream *stream;
int fd;
if (argc > 1)
@@ -98,20 +92,11 @@ int main (int argc, char *argv[])
if (fd == -1)
return 0;
- if (fstat (fd, &st) == -1)
- return 0;
+ stream = g_mime_stream_fs_new (fd);
- data = g_malloc0 (st.st_size + 1);
+ test_parser (stream);
- read (fd, data, st.st_size);
-
- close (fd);
-
- /*fprintf (stdout, "%s\n", data);*/
-
- test_parser (data);
-
- g_free (data);
+ g_mime_stream_unref (stream);
return 0;
}
diff --git a/tests/test-parser.c b/tests/test-parser.c
index f5e4839b..a2d68bc4 100644
--- a/tests/test-parser.c
+++ b/tests/test-parser.c
@@ -42,26 +42,21 @@ print_mime_struct (GMimePart *part, int depth)
}
void
-test_parser (char *data)
+test_parser (GMimeStream *stream)
{
GMimeMessage *message;
- GMimeStream *stream;
gboolean is_html;
char *text;
char *body;
fprintf (stdout, "\nTesting MIME parser...\n\n");
- stream = g_mime_stream_mem_new_with_buffer (data, strlen (data));
-
ZenTimerStart();
message = g_mime_parser_construct_message (stream, TRUE);
ZenTimerStop();
ZenTimerReport ("gmime::parser_construct_message");
ZenTimerStart();
- g_mime_stream_unref (stream);
-
text = g_mime_message_to_string (message);
ZenTimerStop();
ZenTimerReport ("gmime::message_to_string");
@@ -85,8 +80,7 @@ test_parser (char *data)
int main (int argc, char *argv[])
{
char *filename = NULL;
- char *data;
- struct stat st;
+ GMimeStream *stream;
int fd;
if (argc > 1)
@@ -98,20 +92,11 @@ int main (int argc, char *argv[])
if (fd == -1)
return 0;
- if (fstat (fd, &st) == -1)
- return 0;
+ stream = g_mime_stream_fs_new (fd);
- data = g_malloc0 (st.st_size + 1);
+ test_parser (stream);
- read (fd, data, st.st_size);
-
- close (fd);
-
- /*fprintf (stdout, "%s\n", data);*/
-
- test_parser (data);
-
- g_free (data);
+ g_mime_stream_unref (stream);
return 0;
}