Switch to unified view

a b/src/bincimapmime/mime-parseonlyheader.cc
1
/* -*- Mode: c++; -*- */
2
/*  --------------------------------------------------------------------
3
 *  Filename:
4
 *    mime-parseonlyheader.cc
5
 *  
6
 *  Description:
7
 *    Implementation of main mime parser components
8
 *  --------------------------------------------------------------------
9
 *  Copyright 2002-2004 Andreas Aardal Hanssen
10
 *
11
 *  This program is free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  This program is distributed in the hope that it will be useful,
17
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 *  GNU General Public License for more details.
20
 *
21
 *  You should have received a copy of the GNU General Public License
22
 *  along with this program; if not, write to the Free Software
23
 *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
24
 *  --------------------------------------------------------------------
25
 */
26
#ifdef HAVE_CONFIG_H
27
#include <config.h>
28
#endif
29
30
#include "mime.h"
31
#include "mime-utils.h"
32
#include "mime-inputsource.h"
33
#include "convert.h"
34
#include <string>
35
#include <vector>
36
#include <map>
37
#include <exception>
38
#include <iostream>
39
40
#include <string.h>
41
#include <ctype.h>
42
#include <stdio.h>
43
#include <errno.h>
44
45
using namespace ::std;
46
47
//------------------------------------------------------------------------
48
void Binc::MimeDocument::parseOnlyHeader(int fd) const
49
{
50
  if (allIsParsed || headerIsParsed)
51
    return;
52
  
53
  headerIsParsed = true;
54
55
  if (!mimeSource || mimeSource->getFileDescriptor() != fd) {
56
    delete mimeSource;
57
    mimeSource = new MimeInputSource(fd);
58
  } else {
59
    mimeSource->reset();
60
  }
61
62
63
  headerstartoffsetcrlf = 0;
64
  headerlength = 0;
65
  bodystartoffsetcrlf = 0;
66
  bodylength = 0;
67
  messagerfc822 = false;
68
  multipart = false;
69
70
  nlines = 0;
71
  nbodylines = 0;
72
73
  MimePart::parseOnlyHeader("");
74
}
75
76
//------------------------------------------------------------------------
77
int Binc::MimePart::parseOnlyHeader(const string &toboundary) const
78
{
79
  string name;
80
  string content;
81
  char cqueue[4];
82
  memset(cqueue, 0, sizeof(cqueue));
83
84
  headerstartoffsetcrlf = mimeSource->getOffset();
85
86
  bool quit = false;
87
  char c = '\0';
88
89
  while (!quit) {
90
    // read name
91
    while (1) {
92
      if (!mimeSource->getChar(&c)) {
93
  quit = true;
94
  break;
95
      }
96
97
      if (c == '\n') ++nlines;
98
      if (c == ':') break;
99
      if (c == '\n') {
100
  for (int i = name.length() - 1; i >= 0; --i)
101
    mimeSource->ungetChar();
102
103
  quit = true;
104
  name = "";
105
  break;
106
      }
107
108
      name += c;
109
110
      if (name.length() == 2 && name.substr(0, 2) == "\r\n") {
111
  name = "";
112
  quit = true;
113
  break;
114
      }
115
    }
116
117
    if (name.length() == 1 && name[0] == '\r') {
118
      name = "";
119
      break;
120
    }
121
122
    if (quit) break;
123
124
    while (!quit) {
125
      if (!mimeSource->getChar(&c)) {
126
  quit = true;
127
  break;
128
      }
129
130
      if (c == '\n') ++nlines;
131
132
      for (int i = 0; i < 3; ++i)
133
  cqueue[i] = cqueue[i + 1];
134
      cqueue[3] = c;
135
136
      if (strncmp(cqueue, "\r\n\r\n", 4) == 0) {
137
  quit = true;
138
  break;
139
      }
140
141
      if (cqueue[2] == '\n') {
142
143
  // guess the mime rfc says what can not appear on the beginning
144
  // of a line.
145
  if (!isspace(cqueue[3])) {
146
    if (content.length() > 2)
147
      content.resize(content.length() - 2);
148
149
    trim(content);
150
    h.add(name, content);
151
152
    name = c;
153
    content = "";
154
    break;
155
  }
156
      }
157
158
      content += c;
159
    }
160
  }
161
162
  if (name != "") {
163
    if (content.length() > 2)
164
      content.resize(content.length() - 2);
165
    h.add(name, content);
166
  }
167
168
  headerlength = mimeSource->getOffset() - headerstartoffsetcrlf;
169
170
  return 1;
171
}