1 /* -*- Mode: c++; -*- */
2 /*  --------------------------------------------------------------------
3  *  Filename:
4  *    mime.cc
5  *
6  *  Description:
7  *    Implementation of main mime parser components
8  *
9  *  Authors:
10  *    Andreas Aardal Hanssen <andreas-binc curly bincimap spot org>
11  *
12  *  Bugs:
13  *
14  *  ChangeLog:
15  *
16  *  --------------------------------------------------------------------
17  *  Copyright 2002-2005 Andreas Aardal Hanssen
18  *
19  *  This program is free software; you can redistribute it and/or modify
20  *  it under the terms of the GNU General Public License as published by
21  *  the Free Software Foundation; either version 2 of the License, or
22  *  (at your option) any later version.
23  *
24  *  This program is distributed in the hope that it will be useful,
25  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
26  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  *  GNU General Public License for more details.
28  *
29  *  You should have received a copy of the GNU General Public License
30  *  along with this program; if not, write to the Free Software
31  *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
32  *  --------------------------------------------------------------------
33  */
34 #ifndef mime_utils_h_included
35 #define mime_utils_h_included
36 
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40 
41 #include <string.h>
42 #include <ctype.h>
43 #include <stdio.h>
44 #include <errno.h>
45 
46 #include "io.h"
47 
48 using namespace ::std;
49 
compareStringToQueue(const std::string & s_in,char * bqueue,int pos,int size)50 inline bool compareStringToQueue(const std::string &s_in,
51 				 char *bqueue, int pos, int size)
52 {
53   if (s_in[0] != bqueue[pos % size]) return false;
54 
55   for (int i = 0; i < size; ++i)
56     if (s_in.at(i) != bqueue[(pos + i) % size])
57       return false;
58 
59   return true;
60 }
61 
62 extern int crlffile;
63 extern char crlfdata[];
64 extern unsigned int crlfoffset;
65 extern unsigned int crlftail;
66 extern unsigned int crlfhead;
67 extern char lastchar;
68 
69 bool fillInputBuffer(void);
70 
crlfGetChar(char & c)71 inline bool crlfGetChar(char &c)
72 {
73   if (crlfhead == crlftail && !fillInputBuffer())
74     return false;
75 
76   c = crlfdata[crlfhead++ & 0xfff];
77   ++crlfoffset;
78   return true;
79 }
80 
crlfUnGetChar(void)81 inline void crlfUnGetChar(void)
82 {
83   --crlfhead;
84   --crlfoffset;
85 }
86 
crlfReset(void)87 inline void crlfReset(void)
88 {
89   if (crlfoffset != 0) {
90     crlfoffset = 0;
91     crlfhead = crlftail = 0;
92     lastchar = '\0';
93     lseek(crlffile, 0, SEEK_SET);
94   }
95 }
96 
crlfSeek(unsigned int offset)97 inline void crlfSeek(unsigned int offset)
98 {
99   if (crlfoffset > offset)
100     crlfReset();
101 
102   char c;
103   int n = 0;
104   while (offset > crlfoffset) {
105     if (!crlfGetChar(c))
106       break;
107     ++n;
108   }
109 }
110 #endif
111