header.c 13.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
/* GNU mailutils - a suite of utilities for electronic mail
   Copyright (C) 1999, 2000 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Library Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */


#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

#include <header0.h>
#include <stream0.h>

static int header_parse (header_t h, const char *blurb, int len);
static int header_read (stream_t is, char *buf, size_t buflen,
			off_t off, size_t *pnread);
static int header_write (stream_t os, const char *buf, size_t buflen,
			 off_t off, size_t *pnwrite);
static int fill_blurb (header_t header);

int
header_create (header_t *ph, const char *blurb, size_t len, void *owner)
{
  header_t h;
  int status = 0;
  h = calloc (1, sizeof (*h));
  if (h == NULL)
    return ENOMEM;
  h->owner = owner;

  /* Ignore the return value.  */
  status = header_parse (h, blurb, len);

  *ph = h;
  return status;
}

void
header_destroy (header_t *ph, void *owner)
{
  if (ph && *ph)
    {
      header_t h = *ph;

      /* Can we destroy ?.  */
      if (h->owner == owner)
	{
	  stream_destroy (&(h->stream), h);
	  if (h->hdr)
	    free (h->hdr);
	  if (h->blurb)
	    free (h->blurb);
	  free (h);
	}
      *ph = NULL;
    }
}

/* Parsing is done in a rather simple fashion.
   meaning we just consider an entry to be
   a field-name an a field-value.  So they
   maybe duplicate of field-name like "Received"
   they are just put in the array, see _get_value()
   on how to handle the case.
   in the case of error .i.e a bad header construct
   we do a full stop and return what we have so far.  */
static int
header_parse (header_t header, const char *blurb, int len)
{
  char *header_end;
  char *header_start;
  char *header_start2;
  struct _hdr *hdr;

  /* Nothing to parse.  */
  if (blurb == NULL || len == 0)
    return 0;

  header->blurb_len = len;
  header->blurb = calloc (header->blurb_len + 1, 1);
  if (header->blurb == NULL)
    return ENOMEM;
  memcpy (header->blurb, blurb, header->blurb_len);

  if (header->hdr)
    free (header->hdr);
  header->hdr = NULL;
  header->hdr_count = 0;

  /* Get a header, a header is :
     field-name ':'  ' ' field-value '\r' '\n'
     [ (' ' | '\t') field-value '\r' '\n' ]
  */
  /* First loop goes throught the blurb */
  for (header_start = header->blurb;; header_start = ++header_end)
    {
      char *fn, *fn_end, *fv, *fv_end;

      if (header_start[0] == ' ' ||
	  header_start[0] == '\t' ||
	  header_start[0]== '\n')
	break;

      /* Second loop extract one header field.  */
      for (header_start2 = header_start;;header_start2 = ++header_end)
	{
	  header_end = memchr (header_start2, '\n', len);
	  if (header_end == NULL)
	    break;
	  else
	    {
	      len -= (header_end - header_start2 + 1);
	      if (len < 0)
		{
		  header_end = NULL;
		  break;
		}
	      if (header_end[1] != ' '
		  && header_end[1] != '\t')
		break; /* New header break the inner for. */
	    }
	  /* *header_end = ' ';  smash LF ? NO */
	}

      if (header_end == NULL)
	break; /* Bail out.  */

      /* Now save the header in the data structure.  */

      /* Treats unix "From " specially.  */
      if ((header_end - header_start >= 5)
	  && strncmp (header_start, "From ", 5) == 0)
	{
	  fn = header_start;
	  fn_end = header_start + 5;
	  fv = header_start + 5;
	  fv_end = header_end;
	}
      else /* Break the header in key: value */
	{
	  char *colon = memchr (header_start, ':', header_end - header_start);

	  /* Houston we have a problem.  */
	  if (colon == NULL)
	    break; /* Disregard the rest and bailout.  */

	  fn = header_start;
	  fn_end = colon;
	  /* Skip leading spaces. */
	  while (*(++colon) == ' ');
	  fv = colon;
	  fv_end = header_end;
	}

      /* Allocate a new slot for the field:value.  */
      hdr = realloc (header->hdr, (header->hdr_count + 1) * sizeof (*hdr));
      if (hdr == NULL)
	{
	  free (header->blurb);
	  free (header->hdr);
	  return ENOMEM;
	}
      header->hdr = hdr;
      header->hdr[header->hdr_count].fn = fn;
      header->hdr[header->hdr_count].fn_end = fn_end;
      header->hdr[header->hdr_count].fv = fv;
      header->hdr[header->hdr_count].fv_end = fv_end;
      header->hdr_count++;
    } /* for (header_start ...) */

 return 0;
}

/* FIXME: grossly inneficient, to many copies and reallocating.
   This all header business need a good rewrite.  */
int
header_set_value (header_t header, const char *fn, const char *fv, int replace)
{
  char *blurb;
  size_t len;

  if (header == NULL || fn == NULL || fv == NULL)
    return EINVAL;

  /* Overload.  */
  if (header->_set_value)
    return header->_set_value (header, fn, fv, replace);

  /* Try to fill out the buffer, if we know how.  */
  if (header->blurb == NULL)
    {
      int err = fill_blurb (header);
      if (err != 0)
	return err;
    }

  /* Easy approach: if replace, overwrite the field-{name,value} and readjust
     the pointers by calling header_parse () this is wastefull, we're just
     fragmenting the memory it can be done better.  But that may imply a
     rewite of the headers So for another day.  */
  if (replace)
    {
      size_t name_len;
      size_t i;
      size_t fn_len;
      size_t fv_len;
      len = header->blurb_len;
      for (name_len = strlen (fn), i = 0; i < header->hdr_count; i++)
	{
	  fn_len = header->hdr[i].fn_end - header->hdr[i].fn;
	  fv_len = header->hdr[i].fv_end - header->hdr[i].fv;
	  if (fn_len == name_len &&
	      strncasecmp (header->hdr[i].fn, fn, fn_len) == 0)
	    {
	      blurb = header->blurb;
	      memmove (header->hdr[i].fn, header->hdr[i + 1].fn,
		       header->hdr[header->hdr_count - 1].fv_end
		       - header->hdr[i + 1].fn + 1 + 1);
	      /* readjust the pointers if move */
	      len -= fn_len + fv_len + 2;
	      i--;
	      blurb = header->blurb;
	      header_parse (header, blurb, len);
	      free (blurb);
	    }
	}
    }

  /* Replacing was taking care of above now just add to the end the new
     header.  Really not cute.  */
  len = strlen (fn) + strlen (fv) + 1 + 1 + 1 + 1;
  blurb = calloc (header->blurb_len + len, 1);
  if (blurb == NULL)
    return ENOMEM;
  sprintf (blurb, "%s: %s\n", fn, fv);
  memcpy (blurb + len - 1, header->blurb, header->blurb_len);
  free (header->blurb);
  header_parse (header, blurb, len + header->blurb_len);
  free (blurb);
  return 0;
}

int
header_get_value (header_t header, const char *name, char *buffer,
		  size_t buflen, size_t *pn)
{
  size_t i = 0;
  size_t name_len;
  size_t total = 0, fn_len = 0, fv_len = 0;
  int threshold;
  int err = 0;

  if (header == NULL || name == NULL)
    return EINVAL;

  /* Overload.  */
  if (header->_get_value)
    return header->_get_value (header, name, buffer, buflen, pn);

  /* Try to fill out the buffer, if we know how.  */
  if (header->blurb == NULL)
    {
      err = fill_blurb (header);
      if (err != 0)
	return err;
    }

  /* We set the threshold to be 1 less for the null.  */
  threshold = --buflen;

  /* Caution: We may have more then one value for a field name, for example
     a "Received" field-name is added by each passing MTA.  The way that the
     parsing (_parse()) is done it's not take to account.  So we just stuff
     in the buffer all the field-values to a corresponding field-name.
     FIXME: Should we kosher the output ? meaning replace occurences of
     " \t\r\n" for spaces ? for now we don't.  */
  for (name_len = strlen (name), i = 0; i < header->hdr_count; i++)
    {
      fn_len = header->hdr[i].fn_end - header->hdr[i].fn;
      if (fn_len == name_len &&
	  strncasecmp (header->hdr[i].fn, name, fn_len) == 0)
	{
	  fv_len = (header->hdr[i].fv_end - header->hdr[i].fv);
	  total += fv_len;
	  /* Can everything fit in the buffer.  */
	  if (buffer && threshold > 0)
	    {
	      threshold -= fv_len;
	      if (threshold >= 0)
		{
		  memcpy (buffer, header->hdr[i].fv, fv_len);
		  buffer += fv_len;
		}
	      else if (threshold < 0)
		{
		  threshold += fv_len;
		  memcpy (buffer, header->hdr[i].fv, threshold);
		  buffer += threshold;
		  threshold = 0;
		}
	    }
	}
    }
  if (buffer)
    *buffer = '\0'; /* Null terminated.  */
  if (pn)
    *pn = total;

  if (total == 0)
    err = ENOENT;

  return err;
}

int
header_set_lines (header_t header, int (*_lines)
		 (header_t, size_t *), void *owner)
{
  if (header ==  NULL)
    return EINVAL;
  if (header->owner != owner)
    return EACCES;
  header->_lines = _lines;
  return 0;
}

int
header_lines (header_t header, size_t *plines)
{
  int n;
  size_t lines = 0;
  if (header == NULL || plines == NULL)
    return EINVAL;

  /* Overload.  */
  if (header->_lines)
    return header->_lines (header, plines);

  /* Try to fill out the buffer, if we know how.  */
  if (header->blurb == NULL)
    {
      int err = fill_blurb (header);
      if (err != 0)
	return err;
    }

  for (n = header->blurb_len - 1; n >= 0; n--)
    {
      if (header->blurb[n] == '\n')
	lines++;
    }
  if (plines)
    *plines = lines;
  return 0;
}

int
header_set_size (header_t header, int (*_size)
		 (header_t, size_t *), void *owner)
{
  if (header ==  NULL)
    return EINVAL;
  if (header->owner != owner)
    return EACCES;
  header->_size = _size;
  return 0;
}

int
header_size (header_t header, size_t *psize)
{
  if (header == NULL)
      return EINVAL;

  /* Overload.  */
  if (header->_size)
    return header->_size (header, psize);

  /* Try to fill out the buffer, if we know how.  */
  if (header->blurb == NULL)
    {
      int err = fill_blurb (header);
      if (err != 0)
	return err;
    }

  if (psize)
    *psize = header->blurb_len;
  return 0;
}

int
header_set_get_value (header_t header, int (*_get_value)
		     (header_t, const char *, char *, size_t, size_t *),
		     void *owner)
{
  if (header == NULL)
    return EINVAL;
  if (header->owner != owner)
    return EACCES;
  header->_get_value = _get_value;
  return 0;
}

int
header_set_set_value (header_t header, int (*_set_value)
		      (header_t , const char *, const char *, int),
		      void *owner)
{
  if (header == NULL)
    return EINVAL;
  if (header->owner != owner)
    return EACCES;
  header->_set_value = _set_value;
  return 0;
}

int
header_set_stream (header_t header, stream_t stream, void *owner)
{
  if (header == NULL)
    return EINVAL;
  if (header->owner != owner)
    return EACCES;
  header->stream = stream;
  return 0;
}

static int
fill_blurb (header_t header)
{
  stream_t is;
  int status;
  char buf[1024];
  char *tbuf;
  size_t nread = 0;

  status = header_get_stream (header, &is);
  if (status != 0)
    return status;

  do
    {
      status = stream_read (is, buf, sizeof (buf), header->temp_blurb_len,
			    &nread);
      if (status != 0)
	{
	  if (status != EAGAIN && status != EINTR)
	    {
	      free (header->temp_blurb);
	      header->temp_blurb = NULL;
	      header->temp_blurb_len = 0;
	    }
	  return status;
	}
      tbuf = realloc (header->temp_blurb, header->temp_blurb_len + nread);
      if (tbuf == NULL)
	{
	  free (header->temp_blurb);
	  header->temp_blurb = NULL;
	  header->temp_blurb_len = 0;
	  return ENOMEM;
	}
      header->temp_blurb = tbuf;
      memcpy (header->temp_blurb + header->temp_blurb_len, buf, nread);
      header->temp_blurb_len += nread;
    }
  while (nread > 0);

  /* parse it. */
  status = header_parse (header, header->temp_blurb, header->temp_blurb_len);
  free (header->temp_blurb);
  header->temp_blurb = NULL;
  header->temp_blurb_len = 0;
  return status;
}

static int
header_write (stream_t os, const char *buf, size_t buflen,
	      off_t off, size_t *pnwrite)
{
  header_t header;
  if (os == NULL || (header = (header_t)os->owner) == NULL)
    return EINVAL;

  (void)buf; (void)off;
  if (buflen == 0)
    return 0;

  if (pnwrite)
    *pnwrite = 0;

  return ENOSYS;
}

static int
header_read (stream_t is, char *buf, size_t buflen,
	     off_t off, size_t *pnread)
{
  header_t header;
  int len;

  if (is == NULL || (header = (header_t)is->owner) == NULL)
    return EINVAL;

  len = header->blurb_len - off;
  if (len > 0)
    {
      if (buf)
	{
	  len = (buflen < (size_t)len) ? buflen : len;
	  memcpy (buf, header->blurb + off, len);
	}
    }
  else
    len = 0;

  if (pnread)
    *pnread = len;
  return 0;
}

int
header_get_stream (header_t header, stream_t *pstream)
{
  if (header == NULL || pstream == NULL)
    return EINVAL;
  if (header->stream == NULL)
    {
      int status = stream_create (&(header->stream), MU_STREAM_RDWR, header);
      if (status != 0)
	return status;
      stream_set_read  (header->stream, header_read, header);
      stream_set_write (header->stream, header_write, header);
    }
  *pstream = header->stream;
  return 0;
}