pgpconf.c

来自「著名的加密软件的应用于电子邮件中」· C语言 代码 · 共 666 行 · 第 1/2 页

C
666
字号
			key = line;

			opt = configLookup (ui, arg, linenum, key, keylen,
				 	linenum ? OPT_CMD : 0);
			if (!opt)
				 return PGPERR_CONFIG_BADOPT;
			if (!opt->name)	/* In the config file, not found isn't fatal */
				 return linenum ? 0 : PGPERR_CONFIG_BADOPT;
			if ((opt->flags & OPT_TYPEMASK) == OPT_IGNORE)
				 return 0;

			msgarg1.type = PGP_UI_ARG_UNSIGNED;
			msgarg1.val.u = linenum;
			msgarg2.type = PGP_UI_ARG_BUFFER;
			msgarg2.val.buf.buf = (byte *)key;
			msgarg2.val.buf.len = keylen;

			line += keylen;
line += skipWhite(line);
if (*line != '=') {
 			/* On the command line, "armor" means "armor=on" */
 			if (!linenum && *line == '\0' &&
 			    (opt->flags & OPT_TYPEMASK) == OPT_BINARY)
 			{
 			if (opt->flags & OPT_FUNC)
 					(void)configCallFunci(env, opt->var, opt->max,
 							pri);
 			else
 					(void)pgpenvSetInt(env,
 							(enum PgpEnvInts)opt->var,
 							opt->max, pri);
 			return 0;
	}

ui->message (arg, PGPERR_CONFIG, PGPMSG_CONFIG_MISSING_EQUAL,
	2, &msgarg1, &msgarg2);

					return PGPERR_CONFIG;
			}
			line++; /* skip '=' */

			/* Skip assignment and following whitespace */
			line += skipWhite(line);

			switch (opt->flags & OPT_TYPEMASK) {
			case OPT_BINARY:
					i = getWord(line, 0);
					if ((i == 3 && xmemicmp("off", line, 3) == 0) ||
					(i == 1 && line[0] == '0'))
					{
							if (opt->flags & OPT_FUNC)
									(void)configCallFunci(env, opt->var, opt->min,
											 pri);
							else
									(void)pgpenvSetInt(env,
											 (enum PgpEnvInts)opt->var,
											 opt->min, pri);
							break;
					} else if ((i == 2 && xmemicmp("on", line, 2) == 0) ||
					(i == 1 && line[0] == '1'))
					{
							if (opt->flags & OPT_FUNC)
									(void)configCallFunci(env, opt->var, opt->max,
											 pri);
							else
							(void)pgpenvSetInt(env,
								 	(enum PgpEnvInts)opt->var,
								 	opt->max, pri);
					break;
} else if (i == 0) {
					ui->message (arg, PGPERR_CONFIG,
						PGPMSG_CONFIG_MISSING_BOOLEAN, 2,
						&msgarg1, &msgarg2);
					break;
			}
/* Fall through: default case */
msgarg3.type = PGP_UI_ARG_BUFFER;
msgarg3.val.buf.buf = (byte *)line;
msgarg3.val.buf.len = i;

ui->message (arg, PGPERR_CONFIG,
	PGPMSG_CONFIG_UNKNOWN_BOOLEAN, 3, &msgarg1,
	&msgarg2, &msgarg3);
return PGPERR_CONFIG;

				case OPT_INT:
					val = strtol (line, &p, 0);
					i = getWord (line, 0);

msgarg3.type = PGP_UI_ARG_BUFFER;
msgarg3.val.buf.buf = (byte *)line;
msgarg3.val.buf.len = i;

if (!isspace(*p) && *p != '\0') {
			if (i) {
				 ui->message
				 	(arg, PGPERR_CONFIG,
				 	PGPMSG_CONFIG_UNKNOWN_INTEGER, 3,
				 	&msgarg1, &msgarg2, &msgarg3);
			} else {
					ui->message
						(arg, PGPERR_CONFIG,
						PGPMSG_CONFIG_MISSING_INTEGER, 2,
						&msgarg1, &msgarg2);
			}
			return PGPERR_CONFIG;
	}
msgarg4.type = PGP_UI_ARG_INT;
msgarg4.val.i = val;
if (val > (long)opt->max) {
			msgarg5.type = PGP_UI_ARG_INT;
			msgarg5.val.i = opt->max;
			ui->message (arg, PGPERR_CONFIG,
				 PGPMSG_CONFIG_INT_TOO_HIGH, 4,
				 &msgarg1, &msgarg2, &msgarg4, &msgarg5);
			val = (long)opt->max;
} else if (val < (long)opt->min) {
			msgarg5.type = PGP_UI_ARG_INT;
			msgarg5.val.u = opt->min;
			ui->message (arg, PGPERR_CONFIG,
				 PGPMSG_CONFIG_INT_TOO_LOW, 4,
				 &msgarg1, &msgarg2, &msgarg4, &msgarg5);
			val = (long)opt->min;
	}
if ((opt->flags & OPT_FUNC) == 0) {
	(void)pgpenvSetInt(env, (enum PgpEnvInts)opt->var,
			(int)val, pri);
} else if (configCallFunci(env, opt->var, (int)val, pri) < 0) {
			ui->message (arg, PGPERR_CONFIG,
				 PGPMSG_CONFIG_INVALID_INTEGER, 3,
				 &msgarg1, &msgarg2, &msgarg3);
			return PGPERR_CONFIG;
	}
					break;

				case OPT_STRING:
					if (*line == '"') {
						i = getQString(ui, arg, linenum, ++line);
						if (i == (unsigned)-1)
							 return PGPERR_CONFIG;
					} else {
						i = getString(line);
					}

					msgarg3.type = PGP_UI_ARG_BUFFER;
					msgarg3.val.buf.buf = (byte *)line;
					msgarg3.val.buf.len = i;

					if (i > (unsigned)opt->max-1) {
							msgarg4.type = PGP_UI_ARG_INT;
							msgarg4.val.i = opt->max-1;
							ui->message (arg, PGPERR_CONFIG,
								 PGPMSG_CONFIG_STRING_TOO_LONG, 3,
								 &msgarg1, &msgarg2, &msgarg4);
							return PGPERR_CONFIG;
							/* Call function to set: never any need to copy */
					}
					{
							char c;
							int status;

							c = line[i];
							line[i] = '\0';	/* Null-terminate the string */

							if (opt->flags & OPT_FUNC)
									status = configCallFuncs (env, opt->var,
											 	line, i, pri);
							else
									status = pgpenvSetString
										(env, (enum PgpEnvStrings) opt->var,
										line, pri);

							line[i] = c;	 /* Restore the string */
							if (status >= 0)
								 break;	 /* Success */
							ui->message (arg, PGPERR_CONFIG,
								 PGPMSG_CONFIG_INVALID_STRING, 3,
								 &msgarg1, &msgarg2, &msgarg3);
							return PGPERR_CONFIG;
					}
					break;
			}

			return 0;
}

/*
 * "option" may be modified temporarily, but it is restored prior to
 * returning.
 */
int
pgpConfigLineProcess(struct PgpUICb const *ui, void *arg,
			struct PgpEnv *env, char *option, int pri)
	{
			if (!ui || !ui->message)
				 return PGPERR_UI_INVALID;

			return configLineParse (ui, arg, env, 0, option, pri);
	}

#define CPM_EOF	26	/* ^Z - accept this as a file terminator. */
/*
 * Read a line from file f, into buf, up to the given length. Anything after
 * that is ignored. Strips trailing spaces and line terminator, can read
 * LF, CRLF and CR terminated lines. Also accepts CPM_EOF (^Z) as a file
 * terminator.
 */
static char *
configLineGet(FILE *f, char *buf, unsigned len)
	{
			int c;
			char *p = buf;

			/* Skip leading whitespace */
			do {
				 c = getc(f);
			} while (c == ' ' || c == '\t');

			while (len > 0 && c != '\n' && c != '\r' && c != EOF && c != CPM_EOF)
			{
					*p++ = c;
					--len;
					c = getc(f);
			}

			/* Return NULL on EOF */
			if (p == buf && (c == EOF || c == CPM_EOF)) {
					*buf = '\0';
					return (char *)0;
			}
		
			/*
			* Skip to end of line, setting len to non-zero if anything trailing is
			* not a space (meaning that any trailing whitespace in the buffer is
			* not trailing whitespace on the line and should not be stripped).
			*/
			len = 0;
			while (c != '\n' && c != '\r' && c != EOF && c != CPM_EOF) {
				 len |= c ^ ' ';	/* Avoid branches for fast processors */
				 c = getc(f);
			}

			/* Put back CPM_EOF, and character after \r if it's not \n */
			if (c == CPM_EOF || (c == '\r' && (c = getc(f)) != '\n'))
				 ungetc(c, f);
			/* If line wasn't too long, skip trailing WS and null-terminate */
			if (!len) { /* Skip trailing whitespace, as described above */
				 while (p >= buf && p[-1] == ' ')
				 	--p;
				 *p = '\0';
		}
		return buf;
}

int
pgpConfigFileProcess(struct PgpUICb const *ui, void *arg,
				 struct PgpEnv *env, char const *filename, int pri)
	{
			FILE *f;
			unsigned linenum = 0;
			unsigned errors = 0;
			int i;
			char buf[130];	/* Maximum allowable line size */
			struct PgpUICbArg msgarg1, msgarg2, msgarg3;

if (!ui || !ui->message)
	return PGPERR_UI_INVALID;

f = fopen (filename, "r");
if (f == NULL) {
			msgarg1.type = PGP_UI_ARG_STRING;
			msgarg1.val.s = filename;
			ui->message (arg, PGPERR_CONFIG,
				 PGPMSG_CONFIG_NO_FILE, 1, &msgarg1);
			return 0;	/* treat like empty config file */
	}

buf[sizeof(buf)-1] = '\0';
while (errors < MAX_ERRORS && configLineGet(f, buf, sizeof(buf))) {
			++linenum;
			msgarg1.type = PGP_UI_ARG_UNSIGNED;
			msgarg1.val.u = linenum;
			msgarg2.type = PGP_UI_ARG_STRING;
			msgarg2.val.s = buf;

			/* Line too long? */
			if (buf[sizeof(buf)-1] != '\0') {
					buf[sizeof(buf)-1] = '\0';
					ui->message (arg, PGPERR_CONFIG,
							PGPMSG_CONFIG_LINE_TOO_LONG, 2,
							&msgarg1, &msgarg2);
					errors++;
					continue;
			}
					/* Illegal character? */
					for (i = 0; buf[i] != '\0'; i++) {
						if (!isprint(buf[i]) && buf[i] != '\t')
							break;
					}
					if (buf[i] != '\0') {
							msgarg3.type = PGP_UI_ARG_INT;
							msgarg3.val.i = i;
							ui->message (arg, PGPERR_CONFIG,
									PGPMSG_CONFIG_BAD_CHAR, 3,
									&msgarg1, &msgarg2, &msgarg3);
							errors++;
							continue;
				}
if (configLineParse (ui, arg, env, linenum, buf, pri)
				< 0)
		errors++;
}

fclose (f);

if (errors > 0) {

msgarg1.type = PGP_UI_ARG_INT;
msgarg1.val.i = errors;

if (errors >= MAX_ERRORS)
					ui->message (arg, PGPERR_CONFIG,
						PGPMSG_CONFIG_MAX_ERRORS, 1, &msgarg1);
		else
					ui->message (arg, PGPERR_CONFIG,
						PGPMSG_CONFIG_NUM_ERRORS, 1, &msgarg1);

return PGPERR_CONFIG;
}

			return 0;
}

⌨️ 快捷键说明

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