static int ens_read(struct ens_handle *handle, char *buf, int len) { if (handle->s == NULL || handle->pos == handle->s->length) { char *p; handle->s = StrmyISgets(handle->is); if (handle->s->length == 0) return 0; cleanup_line(handle->s, PAGER_MODE); if (handle->encoding == ENC_BASE64) Strchop(handle->s); else if (handle->encoding == ENC_UUENCODE) { if (!strncmp(handle->s->ptr, "begin", 5)) handle->s = StrmyISgets(handle->is); Strchop(handle->s); } p = handle->s->ptr; if (handle->encoding == ENC_QUOTE) handle->s = decodeQP(&p); else if (handle->encoding == ENC_BASE64) handle->s = decodeB(&p); else if (handle->encoding == ENC_UUENCODE) handle->s = decodeU(&p); handle->pos = 0; } if (len > handle->s->length - handle->pos) len = handle->s->length - handle->pos; bcopy(&handle->s->ptr[handle->pos], buf, len); handle->pos += len; return len; }
/* * carsonl, jan 8,98 * Decodes a string encoded in RFC2047 format. If the string is not encoded * returns the original string. Can be used on the header value returned by * getHeader() and getAllHeaders() methods in MIMEMessage class. * * parameter : * * szInputString : string to be trimmed * * return : The decoded header string. */ char *decodeHeader( char *szInputString ) { int len, retLen = 0; int i, j, outBytes, outBits; int nStart = 0; int nEnd = 0; char *szEncodedText = NULL; char *szDecodedText = NULL; char *plainPart = NULL, *retBuf=NULL; char *leftOverBuf=NULL, *leftOverDecodedBuf=NULL; int plainTextLen = 0, leftOverLen=0; char achCharset[16]; BOOLEAN bEncodeBase64 = FALSE; BOOLEAN bEncodeQP = FALSE; BOOLEAN bEncodeString = FALSE; if ( szInputString == NULL ) return NULL; len = strlen( szInputString ); /* find start of encoding text */ for ( i = 0; i < len && szInputString[i] != 0 ; i++ ) { if ( szInputString[i] == '=' && szInputString[i+1] == '?' ) { bEncodeString = TRUE; break; } } if (bEncodeString == FALSE) return (strdup (szInputString)); if (i > 0) { plainTextLen = (i); plainPart = malloc (i+1); strncpy (plainPart, szInputString, plainTextLen); plainPart [i] = 0; } /* get charset */ for ( i += 2, j = 0; i < len && szInputString[i] != '?' && j < 15; i++, j++ ) { achCharset[j] = szInputString[i]; } achCharset[j] = 0; i++; /* skip ? */ /* get encoding type */ if ( szInputString[i] == 'Q' || szInputString[i] == 'q' ) bEncodeQP = TRUE; else if ( szInputString[i] == 'B' || szInputString[i] == 'b' ) bEncodeBase64 = TRUE; if (szInputString[i] == '?' ) i++; /* skip ? */ nStart = ++i; /* look for end of encoded text */ for ( j = 0; i < len && szInputString[i] != 0; i++, j++ ) { if ( szInputString[i] == '?' && (i+1) < len && szInputString[i+1] == '=' ) { nEnd = i; break; } } if ( nEnd > 0 ) { /* extract encoded text */ szEncodedText = (char *) malloc( j + 1 ); szDecodedText = (char *) malloc( j + 1 ); if ( szEncodedText != NULL && szDecodedText != NULL ) { strncpy( szEncodedText, &szInputString[nStart], j ); if ( bEncodeQP ) decodeQP( szEncodedText, szDecodedText, j ); else if ( bEncodeBase64 ) decodeBase64( szEncodedText, szDecodedText, j, j, &outBytes, &outBits ); free( szEncodedText ); } } if (nEnd+2 < len-1) { leftOverLen = len - (nEnd+2); leftOverBuf = malloc (leftOverLen + 1); strncpy (leftOverBuf, &szInputString[nEnd+2], leftOverLen); leftOverBuf [leftOverLen] = 0; leftOverDecodedBuf = decodeHeader (leftOverBuf); } retLen = plainTextLen + strlen (szDecodedText) + strlen (leftOverDecodedBuf); retBuf = malloc (retLen +1); if (plainPart != NULL && szDecodedText != NULL && leftOverDecodedBuf != NULL) { sprintf (retBuf, "%s%s%s", plainPart, szDecodedText, leftOverDecodedBuf); free (plainPart); free (szDecodedText); free (leftOverBuf); free (leftOverDecodedBuf); } else if (plainPart != NULL && szDecodedText != NULL) { sprintf (retBuf, "%s%s", plainPart, szDecodedText); free (plainPart); free (szDecodedText); } else if (szDecodedText != NULL && leftOverDecodedBuf != NULL) { sprintf (retBuf, "%s%s", szDecodedText, leftOverDecodedBuf); free (szDecodedText); free (leftOverBuf); free (leftOverDecodedBuf); } else if (szDecodedText != NULL) { free (retBuf); return szDecodedText; } else { free (retBuf); return null; } return retBuf; }