uuscan.c

来自「UUDeview是一个编码解码器」· C语言 代码 · 共 2,492 行 · 第 1/5 页

C
2,492
字号
      }    }    variable = NULL;  }  else {    /*     * nothing interesting     */    return theheaders;  }  /*   * okay, so extract the actual data   */  if (variable) {    length = 0;    ptr = uuscan_phtext;    while (isspace (*value))      value++;    while (*value && (delimit==0 || *value!=delimit) &&	   *value != '\012' && *value != '\015' && length < 255) {      *ptr++ = *value++;      length++;    }    while (length && isspace(*(ptr-1))) {      ptr--; length--;    }    *ptr = '\0';    if ((*variable = _FP_strdup (uuscan_phtext)) == NULL)      return NULL;  }  return theheaders;}/* * is this a header line we know about? */static intIsKnownHeader (char *line){  char **iter = knownmsgheaders;  while (iter && *iter) {    if (_FP_strnicmp (line, *iter, strlen (*iter)) == 0)      return 1;    iter++;  }  iter = knownmimeheaders;  while (iter && *iter) {    if (_FP_strnicmp (line, *iter, strlen (*iter)) == 0)      return 2;    iter++;  }  return 0;}/* * Scan a header */intUUScanHeader (FILE *datei, headers *envelope){  char *ptr;  while (!feof (datei)) {    if ((ptr = ScanHeaderLine (datei, NULL)) == NULL)      break;    if (*ptr == '\0' || *ptr == '\012' || *ptr == '\015')      break;    ParseHeader (envelope, ptr);  }  return 0;}/* * Scan something for encoded data and fill the fileread* structure. * If boundary is non-NULL, we stop after having read it. If Check- * Headers != 0, we also stop after we've found uu_headercount recog- * nized header lines. * If we have a boundary, then we also don't accept Base64; MIME mails * really should handle this properly. * We return -1 if something went wrong, 0 if everything is normal, * 1 if we found a new header and 2 if we found a boundary. * In MIME message bodies (not multiparts), we also disable our reduced * MIME handling. */static intScanData (FILE *datei, char *fname, int *errcode,	  char *boundary, int ismime, int checkheaders,	  fileread *result){  char *line=uuscan_sdline, *bhds1=uuscan_sdbhds1, *bhds2=uuscan_sdbhds2;  static char *ptr, *p2, *p3=NULL, *bhdsp, bhl;  int islen[10], isb64[10], isuue[10], isxxe[10], isbhx[10], iscnt;  int cbb64, cbuue, cbxxe, cbbhx;  int bhflag=0, vflag, haddh=0, hadct=0;  int bhrpc=0, bhnf=0, c, hcount, lcount, blen=0;  int encoding=0, dflag=0, ctline=42;  int dontcare=0, hadnl=0;  long preheaders=0, oldposition;  long yefilesize=0, yepartends=0;  size_t dcc, bhopc;  *errcode = UURET_OK;  (void) UUDecodeLine (NULL, NULL, 0);          /* init */  bhdsp = bhds2;  if (datei == NULL || feof (datei))    return -1;  result->startpos = ftell (datei);  hcount = lcount  = 0;  for (iscnt=0; iscnt<10; iscnt++) {    isb64[iscnt] = isuue[iscnt] = isxxe[iscnt] = isbhx[iscnt] = 0;    islen[iscnt] = -1;  }  iscnt = 0;  if (boundary)    blen = strlen (boundary);  while (!feof (datei)) {    oldposition = ftell (datei);    if (_FP_fgets (line, 255, datei) == NULL)      break;    if (ferror (datei))      break;    line[255] = '\0'; /* For Safety of string functions */    /*     * Make Busy Polls     */    if (UUBUSYPOLL(ftell(datei),progress.fsize)) {      UUMessage (uuscan_id, __LINE__, UUMSG_NOTE,		 uustring (S_SCAN_CANCEL));      *errcode = UURET_CANCEL;      break;    }    if (IsLineEmpty (line)) { /* line empty? */      hcount = 0;      hadnl  = 1;      continue;               /* then ignore */    }    if (checkheaders) {      if (IsKnownHeader (line)) {	(void) ScanHeaderLine (datei, line);	if (hcount == 0) {	  preheaders = oldposition;	  lcount     = 0;	}	hcount++;	lcount++;	/*	 * check for the various restart counters	 */	if ((hcount >= hlcount.restart) ||	    (hcount >= hlcount.afterdata && ismime == 0) ||	    (hcount >= hlcount.afterdata && result->uudet) ||	    (hcount >= hlcount.afternl   && result->uudet && hadnl)) {	  /*	   * Hey, a new header starts here	   */	  fseek (datei, preheaders, SEEK_SET);	  break;	}	/* continue; */      }      else if (lcount > WAITHEADER) {	hcount = 0;	lcount = 0;	dontcare=0;      }      else if (hcount) {	lcount++;	dontcare=1;      }      else {	dontcare=0;      }    }    else {      dontcare=0;    }    if (boundary != NULL && 	line[0] == '-' && line[1] == '-' &&	strncmp (line+2, boundary, blen) == 0) {      fseek (datei, oldposition, SEEK_SET);      break;    }    if (boundary != NULL && line[0] == 'C' && line[1] == 'o' &&	_FP_strnicmp (line, "Content-Type:", 13) == 0) {      ptr = ScanHeaderLine (datei, line);      p2  = (ptr)?_FP_stristr(ptr,"boundary"):NULL;      p3  = (p2)?ParseValue(p2):NULL;      if (p3 && strcmp (p3, boundary) == 0) {	fseek (datei, oldposition, SEEK_SET);	break;      }      else {	p3 = NULL;      }    }    if (strncmp      (line, "begin ",       6) == 0 ||	_FP_strnicmp (line, "<pre>begin ", 11) == 0) {      if ((result->begin || result->end ||	   result->uudet == B64ENCODED ||	   result->uudet == BH_ENCODED) && !uu_more_mime) {	fseek (datei, oldposition, SEEK_SET);	break;      }            if (*line == '<')	ptr = line + 10;      else	ptr = line + 5;      while (*ptr == ' ') ptr++;      while (isdigit (*ptr)) 	result->mode = result->mode * 8 + *ptr++ - '0';      while (*ptr == ' ') ptr++;      /*       * We may have picked up a filename from a uuenview-style header       */      _FP_free (result->filename);      result->filename = _FP_strdup (ptr);      result->begin    = 1;      while (isspace (result->filename[strlen(result->filename)-1]))	result->filename[strlen(result->filename)-1] = '\0';      continue;    }    if ((strncmp (line, "end", 3) == 0) &&	result->uudet != BH_ENCODED &&	result->uudet != YENC_ENCODED) {      if (result->uudet == B64ENCODED && result->begin)	result->uudet = XX_ENCODED;      if (result->uudet != B64ENCODED) {	result->end = 1;	if (dflag && encoding)	  result->uudet = encoding;	continue;      }    }    hadnl = 0;    /*     * Detect a UUDeview-Style header     */    if (_FP_strnicmp (line, "_=_ Part ", 9) == 0 &&	result->uudet != YENC_ENCODED) {      if (result->uudet) {	fseek (datei, oldposition, SEEK_SET);	break;      }      result->partno = atoi (line + 8);      if ((ptr = _FP_stristr (line, "of file ")) != NULL) {	ptr += 8;	while (isspace (*ptr)) ptr++;	p2 = ptr;	while (isalnum(*p2) || 	       *p2 == '.' || *p2=='_' || *p2 == '-' ||	       *p2 == '!' || *p2=='@' || *p2 == '$')	  p2++;	c = *p2; *p2 = '\0';	if (p2 != ptr && result->filename == NULL)	  result->filename = _FP_strdup (ptr);	else if (p2 - ptr > 5 && strchr (ptr, '.') != NULL) {	  /*	   * This file name looks good, too. Let's use it	   */	  _FP_free (result->filename);	  result->filename = _FP_strdup (ptr);	}	*p2 = c;      }    }    /*     * Some reduced MIME handling. Only use if boundary == NULL. Also     * accept the "X-Orcl-Content-Type" used by some braindead program.     */    if (boundary == NULL && !ismime && !uu_more_mime &&	result->uudet != YENC_ENCODED) {      if (_FP_strnicmp (line, "Content-Type", 12) == 0 ||	  _FP_strnicmp (line, "X-Orcl-Content-Type", 19) == 0) {	/*	 * We use Content-Type to mark a new attachment and split the file.	 * However, we do not split if we haven't found anything encoded yet.	 */	if (result->uudet) {	  fseek (datei, oldposition, SEEK_SET);	  break;	}	if ((ptr = strchr (line, ':')) != NULL) {	  ptr++;	  while (isspace (*ptr)) ptr++; p2 = ptr;	  while (!isspace (*p2) && *p2 != ';') p2++;	  c = *p2; *p2 = '\0';	  if (p2 != ptr) {	    _FP_free (result->mimetype);	    result->mimetype = _FP_strdup (ptr);	  }	  *p2 = c;	}	ctline=0;	hadct=1;      }      if ((ptr = _FP_stristr (line, "number=")) && ctline<4) {	ptr += 7; if (*ptr == '"') ptr++;	result->partno = atoi (ptr);      }      if ((ptr = _FP_stristr (line, "total=")) && ctline<4) {	ptr += 6; if (*ptr == '"') ptr++;	result->maxpno = atoi (ptr);      }      if ((ptr = _FP_stristr (line, "name=")) && ctline<4) {	ptr += 5;	while (isspace (*ptr)) ptr++;	if (*ptr == '"' && *(ptr+1) && (p2 = strchr (ptr+2, '"')) != NULL) {	  c = *p2; *p2 = '\0';	  _FP_free (result->filename);	  result->filename = _FP_strdup (ptr+1);	  *p2 = c;	}	else if (*ptr=='\''&&*(ptr+1)&&(p2 = strchr(ptr+2, '\'')) != NULL) {	  c = *p2; *p2 = '\0';	  _FP_free (result->filename);	  result->filename = _FP_strdup (ptr+1);	  *p2 = c;	}	else {	  p2 = ptr;	  while (isalnum(*p2) || 		 *p2 == '.' || *p2=='_' || *p2 == '-' ||		 *p2 == '!' || *p2=='@' || *p2 == '$')	    p2++;	  c = *p2; *p2 = '\0';	  if (p2 != ptr && result->filename == NULL)	    result->filename = _FP_strdup (ptr);	  else if (p2 - ptr > 5 && strchr (ptr, '.') != NULL) {	    /*	     * This file name looks good, too. Let's use it	     */	    _FP_free (result->filename);	    result->filename = _FP_strdup (ptr);	  }	  *p2 = c;	}      }      if ((ptr = _FP_stristr (line, "id=")) && ctline<4) {	p2 = ptr += 3;	if (*p2 == '"') {	  p2 = strchr (++ptr, '"');	}	else {	  while (*p2 && isprint(*p2) && !isspace(*p2) && *p2 != ';')	    p2++;	}	if (p2 && *p2 && p2!=ptr) {	  c = *p2; *p2 = '\0';	  if (result->mimeid)	    _FP_free (result->mimeid);	  result->mimeid = _FP_strdup (ptr);	  *p2 = c;	}      }            /*        * Handling for very short Base64 files.       */      if (uu_tinyb64 && !ismime && !uu_more_mime) {	if (line[0] == '-' && line[1] == '-') {	  if (dflag && (encoding==B64ENCODED || result->uudet==B64ENCODED)) {	    if (encoding==B64ENCODED && result->uudet==0 && (haddh||hadct)) {	      result->uudet = encoding;	      encoding = dflag = 0;	    }	    haddh = 1;	    continue;	  }	  hadct = 0;	}      }    } /* end of reduced MIME handling */    /*     * If we're in "freestyle" mode, have not encountered anything     * interesting yet, and stumble upon something that looks like     * a boundary, followed by a Content-* line, try to use it.     */    if (boundary == NULL && !ismime && !uu_more_mime && dflag <= 1 &&	line[0] == '-' && line[1] == '-' && strlen(line+2)>10 &&	(((ptr = _FP_strrstr (line+2, "--")) == NULL) ||	 (*(ptr+2) != '\012' && *(ptr+2) != '\015')) &&	_FP_strstr (line+2, "_=_") != NULL) {      if (_FP_fgets (line, 255, datei) == NULL) {	break;      }      if (_FP_strnicmp (line, "Content-", 8) == 0) {	/*	 * Okay, let's do it. This breaks out of ScanData. ScanPart will	 * recognize the boundary on the next call and use it.	 */	fseek (datei, oldposition, SEEK_SET);	break;      }    }    /*     * Detection for yEnc encoding     */    if (strncmp (line, "=ybegin ", 8) == 0 &&	_FP_strstr (line, " name=") != NULL) {      if ((result->begin || result->end || result->uudet) && !uu_more_mime) {	fseek (datei, oldposition, SEEK_SET);	break;      }      /*       * name continues to the end of the line       */            _FP_free (result->filename);      ptr = _FP_strstr (line, " name=") + 6;      result->filename = _FP_strdup (ptr);      while (isspace (result->filename[strlen(result->filename)-1]))	result->filename[strlen(result->filename)-1] = '\0';      /*       * Determine size       */      if ((ptr = _FP_strstr (line, " size=")) != NULL) {	ptr += 6;	yefilesize = atoi (ptr);      }      else {	yefilesize = -1;      }      /*       * check for multipart file and read =ypart line       */      if ((ptr = _FP_strstr (line, " part=")) != NULL) {	result->partno = atoi (ptr + 6);	if (result->partno == 1) {	  result->begin = 1;	}	if (_FP_fgets (line, 255, datei) == NULL) {	  break;	}	line[255] = '\0';	if (strncmp (line, "=ypart ", 7) != 0) {	  break;	}	if ((ptr = _FP_strstr (line, " end=")) == NULL) {	  break;	}       	yepartends = atoi (ptr + 5);      }      else {	result->partno = 1;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?