Ejemplo n.º 1
0
void print_data(calc* divisions, int* quantity, int* divs)
{
    int     length = 1 + (*quantity) * (*divs);

    make_pad(&length);
    n_print_calc(divisions, &qua, &div);
}
feeReturn feePubKeyCreatePad(feePubKey myKey,
	feePubKey theirKey,
	unsigned char **padData,	/* RETURNED */
	unsigned *padDataLen)		/* RETURNED padData length in bytes */
{
	pubKeyInst *myPkinst = (pubKeyInst *) myKey;
	pubKeyInst *theirPkinst = (pubKeyInst *) theirKey;
	giant pad;
	unsigned char *result;
    	unsigned padLen;
	key pkey;

	/*
	 * Do some compatibility checking (myKey, theirKey) here...?
	 */
	if(DEFAULT_CURVE == CURVE_PLUS) {
		pkey = theirPkinst->plus;
	}
	else {
		pkey = theirPkinst->minus;
	}
	pad = make_pad(myPkinst->privGiant, pkey);
	result = mem_from_giant(pad, &padLen);
	freeGiant(pad);

	/*
	 * Ensure we have a the minimum necessary for DES. A bit of a hack,
	 * to be sure.
	 */
	if(padLen >= FEE_DES_MIN_STATE_SIZE) {
		*padData = result;
		*padDataLen = padLen;
	}
	else {
		*padData = (unsigned char*) fmalloc(FEE_DES_MIN_STATE_SIZE);
		*padDataLen = FEE_DES_MIN_STATE_SIZE;
		bzero(*padData, FEE_DES_MIN_STATE_SIZE);
		bcopy(result, *padData, padLen);
		ffree(result);
	}
	return FR_Success;
}
/* 
 * Diffie-Hellman. Public key is specified either as a feePubKey or 
 * a ANSI X9.62 format public key string (0x04 | x | y). In either case
 * the caller must ensure that the two keys are on the same curve. 
 * Output data is fmalloc'd here; caller must free. Output data is 
 * exactly the size of the curve's modulus in bytes. 
 */
feeReturn feePubKeyECDH(
	feePubKey privKey,
	/* one of the following two is non-NULL */
	feePubKey pubKey,
	const unsigned char *pubKeyStr,
	unsigned pubKeyStrLen,
	/* output fmallocd and RETURNED here */
	unsigned char **output,
	unsigned *outputLen)
{
	feePubKey theirPub = pubKey;
	feeReturn frtn = FR_Success;
	pubKeyInst *privInst = (pubKeyInst *) privKey;
	
	if(privInst->privGiant == NULL) {
		dbgLog(("feePubKeyECDH: privKey not a private key\n"));
		return FR_IncompatibleKey;
	}
	
	if(theirPub == NULL) {
		if(pubKeyStr == NULL) {
			return FR_IllegalArg;
		}
		
		/* Cook up a public key with the same curveParams as the private key */
		feeDepth depth;
		frtn = curveParamsDepth(privInst->cp, &depth);
		if(frtn) {
			return frtn;
		}
		theirPub = feePubKeyAlloc();
		if(theirPub == NULL) {
			return FR_Memory;
		}
		frtn = feePubKeyInitFromECDSAPubBlob(theirPub, pubKeyStr, pubKeyStrLen, depth);
		if(frtn) {
			goto errOut;
		}
	}
	
	pubKeyInst *pubInst = (pubKeyInst *) theirPub;
	
	giant outputGiant = make_pad(privInst->privGiant, pubInst->plus);
	if(outputGiant == NULL) {
		dbgLog(("feePubKeyECDH: make_pad error\n"));
		frtn = FR_Internal;
	}
	else {
		*outputLen = (privInst->cp->q + 7) / 8;
		*output = (unsigned char *)fmalloc(*outputLen);
		if(*output == NULL) {
			frtn = FR_Memory;
			goto errOut;
		}
		serializeGiant(outputGiant, *output, *outputLen);
		freeGiant(outputGiant);
	}
errOut:
	if((pubKey == NULL) && (theirPub != NULL)) {
		feePubKeyFree(theirPub);
	}
	return frtn;
}
Ejemplo n.º 4
0
/*
 * THE (s)printf() function.
 * It returns a pointer to it's internal buffer (or a string in the text
 * segment) thus, the string must be copied if it has to survive after
 * this function is called again, or if it's going to be modified (esp.
 * if it risks being free()ed).
 */
char *string_print_formatted (const char * format_str, int argc, svalue_t * argv)
{
    format_info finfo;
    svalue_t *carg;     /* current arg */
    unsigned int nelemno = 0;   /* next offset into array */
    unsigned int fpos;          /* position in format_str */
    int fs;                     /* field size */
    int pres;                   /* precision */
    pad_info_t pad;             /* fs pad string */
    unsigned int i;
    char *retvalue;
    int last;

    push_sprintf_state();
    STACK_INC;
    sp->type = T_ERROR_HANDLER;
    sp->u.error_handler = pop_sprintf_state;

    last = 0;
    for (fpos = 0; 1; fpos++) {
        char c = format_str[fpos];

        if (c == '\n' || !c) {
            int column_stat = 0;

            if (last != fpos) {
                add_nstr(format_str + last, fpos - last);
                last = fpos + 1;
            } else last++;

            if (!sprintf_state->csts) {
                if (!c)
                    break;
                ADD_CHAR('\n');
                continue;
            }
            ADD_CHAR('\n');
            while (sprintf_state->csts) {
                cst **temp;

                temp = &(sprintf_state->csts);
                while (*temp) {
                    if ((*temp)->info & INFO_COLS) {
                        if (*((*temp)->d.col - 1) != '\n')
                            while (*((*temp)->d.col) == ' ')
                                (*temp)->d.col++;
                        add_pad(0, (*temp)->start - get_curpos());
                        column_stat = add_column(temp, 0);
                        if (!column_stat)
                            temp = &((*temp)->next);
                    } else {
                        add_pad(0, (*temp)->start - get_curpos());
                        if (!add_table(temp))
                            temp = &((*temp)->next);
                    }
                }               /* of while (*temp) */
                if (sprintf_state->csts || c == '\n')
                    ADD_CHAR('\n');
            }                   /* of while (sprintf_state->csts) */
            if (column_stat == 2)
                ADD_CHAR('\n');
            if (!c)
                break;
        } else
            if (c == '%') {
                if (last != fpos) {
                    add_nstr(format_str + last, fpos - last);
                    last = fpos + 1;
                } else last++;
                if (format_str[fpos + 1] == '%') {
                    ADD_CHAR('%');
                    fpos++;
                    last++;
                    continue;
                }
                GET_NEXT_ARG;
                fs = 0;
                pres = 0;
                pad.len = 0;
                finfo = 0;
                for (fpos++; !(finfo & INFO_T); fpos++) {
                    if (!format_str[fpos]) {
                        finfo |= INFO_T_ERROR;
                        break;
                    }
                    if (((format_str[fpos] >= '0') && (format_str[fpos] <= '9'))
                            || (format_str[fpos] == '*')) {
                        if (pres == -1) {   /* then looking for pres */
                            if (format_str[fpos] == '*') {
                                if (carg->type != T_NUMBER)
                                    ERROR(ERR_INVALID_STAR);
                                pres = carg->u.number;
                                GET_NEXT_ARG;
                                continue;
                            }
                            pres = format_str[fpos] - '0';
                            for (fpos++;
                                    (format_str[fpos] >= '0') && (format_str[fpos] <= '9'); fpos++) {
                                pres = pres * 10 + format_str[fpos] - '0';
                            }
                            if (pres < 0) pres = 0;
                        } else {    /* then is fs (and maybe pres) */
                            if ((format_str[fpos] == '0') && (((format_str[fpos + 1] >= '1')
                                            && (format_str[fpos + 1] <= '9')) || (format_str[fpos + 1] == '*'))) {
                                pad.what = "0";
                                pad.len = 1;
                            } else {
                                if (format_str[fpos] == '*') {
                                    if (carg->type != T_NUMBER)
                                        ERROR(ERR_INVALID_STAR);
                                    fs = carg->u.number;
                                    if (fs < 0) fs = 0;
                                    if (pres == -2)
                                        pres = fs;  /* colon */
                                    GET_NEXT_ARG;
                                    continue;
                                }
                                fs = format_str[fpos] - '0';
                            }
                            for (fpos++;
                                    (format_str[fpos] >= '0') && (format_str[fpos] <= '9'); fpos++) {
                                fs = fs * 10 + format_str[fpos] - '0';
                            }
                            if (fs < 0) fs = 0;
                            if (pres == -2) {       /* colon */
                                pres = fs;
                            }
                        }
                        fpos--; /* about to get incremented */
                        continue;
                    }
                    switch (format_str[fpos]) {
                        case ' ':
                            finfo |= INFO_PP_SPACE;
                            break;
                        case '+':
                            finfo |= INFO_PP_PLUS;
                            break;
                        case '-':
                            finfo |= INFO_J_LEFT;
                            break;
                        case '|':
                            finfo |= INFO_J_CENTRE;
                            break;
                        case '@':
                            finfo |= INFO_ARRAY;
                            break;
                        case '=':
                            finfo |= INFO_COLS;
                            break;
                        case '#':
                            finfo |= INFO_TABLE;
                            break;
                        case '.':
                            pres = -1;
                            break;
                        case ':':
                            pres = -2;
                            break;
#ifdef DEBUG
                        case '%':
                            finfo |= INFO_T_NULL;
                            break;      /* never reached */
#endif
                        case 'O':
                            finfo |= INFO_T_LPC;
                            break;
                        case 's':
                            finfo |= INFO_T_STRING;
                            break;
                        case 'd':
                        case 'i':
                            finfo |= INFO_T_INT;
                            break;
                        case 'f':
                            finfo |= INFO_T_FLOAT;
                            break;
                        case 'c':
                            finfo |= INFO_T_CHAR;
                            break;
                        case 'o':
                            finfo |= INFO_T_OCT;
                            break;
                        case 'x':
                            finfo |= INFO_T_HEX;
                            break;
                        case 'X':
                            finfo |= INFO_T_C_HEX;
                            break;
                        case '\'':
                            fpos++;
                            pad.what = format_str + fpos;
                            while (1) {
                                if (!format_str[fpos])
                                    ERROR(ERR_UNEXPECTED_EOS);
                                if (format_str[fpos] == '\\') {
                                    if (!format_str[++fpos])
                                        ERROR(ERR_UNEXPECTED_EOS);
                                } else
                                    if (format_str[fpos] == '\'') {
                                        pad.len = format_str + fpos - pad.what;
                                        if (!pad.len)
                                            ERROR(ERR_NULL_PS);
                                        break;
                                    }
                                fpos++;
                            }
                            break;
                        default:
                            finfo |= INFO_T_ERROR;
                    }
                }                   /* end of for () */
                if (pres < 0)
                    ERROR(ERR_PRES_EXPECTED);
                /*
                 * now handle the different arg types...
                 */
                if (finfo & INFO_ARRAY) {
                    if (carg->type != T_ARRAY)
                        ERROR(ERR_ARRAY_EXPECTED);
                    if (carg->u.arr->size == 0) {
                        last = fpos;
                        fpos--;     /* 'bout to get incremented */
                        continue;
                    }
                    carg = (argv + sprintf_state->cur_arg)->u.arr->item;
                    nelemno = 1;    /* next element number */
                }
                while (1) {
                    if ((finfo & INFO_T) == INFO_T_LPC) {
                        outbuffer_t outbuf;

                        outbuf_zero(&outbuf);
                        svalue_to_string(carg, &outbuf, 0, 0, 0);
                        outbuf_fix(&outbuf);

                        sprintf_state->clean.type = T_STRING;
                        sprintf_state->clean.subtype = STRING_MALLOC;
                        sprintf_state->clean.u.string = outbuf.buffer;
                        carg = &(sprintf_state->clean);
                        finfo ^= INFO_T_LPC;
                        finfo |= INFO_T_STRING;
                    }
                    if ((finfo & INFO_T) == INFO_T_ERROR) {
                        ERROR(ERR_INVALID_FORMAT_STR);
#ifdef DEBUG
                    } else if ((finfo & INFO_T) == INFO_T_NULL) {
                        /* never reached... */
                        fprintf(stderr, "/%s: (s)printf: INFO_T_NULL.... found.\n",
                                current_object->obname);
                        ADD_CHAR('%');
#endif
                    } else if ((finfo & INFO_T) == INFO_T_STRING) {
                        int slen;
                        /*
                         * %s null handling added 930709 by Luke Mewburn
                         * <*****@*****.**>
                         */
                        if (carg->type == T_NUMBER && carg->u.number == 0) {
                            sprintf_state->clean.type = T_STRING;
                            sprintf_state->clean.subtype = STRING_MALLOC;
                            sprintf_state->clean.u.string = string_copy(NULL_MSG, "sprintf NULL");
                            carg = &(sprintf_state->clean);
                        } else
                            if (carg->type != T_STRING) {
                                ERROR(ERR_INCORRECT_ARG_S);
                            }
                        slen = SVALUE_STRLEN(carg);
                        if ((finfo & INFO_COLS) || (finfo & INFO_TABLE)) {
                            cst **temp;

                            if (!fs) {
                                ERROR(ERR_CST_REQUIRES_FS);
                            }

                            temp = &(sprintf_state->csts);
                            while (*temp)
                                temp = &((*temp)->next);
                            if (finfo & INFO_COLS) {
                                int tmp;
                                if (pres > fs) pres = fs;
                                *temp = ALLOCATE(cst, TAG_TEMPORARY, "string_print: 3");
                                (*temp)->next = 0;
                                (*temp)->d.col = carg->u.string;
                                (*temp)->pad = make_pad(&pad);
                                (*temp)->size = fs;
                                (*temp)->pres = (pres) ? pres : fs;
                                (*temp)->info = finfo;
                                (*temp)->start = get_curpos();
#ifdef TCC
                                puts("tcc has some bugs");
#endif
                                tmp = ((format_str[fpos] != '\n')
                                        && (format_str[fpos] != '\0'))
                                    || ((finfo & INFO_ARRAY)
                                            && (nelemno < (argv + sprintf_state->cur_arg)->u.arr->size));
                                tmp = add_column(temp, tmp);
                                if (tmp == 2 && !format_str[fpos]) {
                                    ADD_CHAR('\n');
                                }
                            } else {/* (finfo & INFO_TABLE) */
                                unsigned int n, len, max_len;
                                const char *p1, *p2;

#define TABLE carg->u.string
                                (*temp) = ALLOCATE(cst, TAG_TEMPORARY, "string_print: 4");
                                (*temp)->d.tab = 0;
                                (*temp)->pad = make_pad(&pad);
                                (*temp)->info = finfo;
                                (*temp)->start = get_curpos();
                                (*temp)->next = 0;
                                max_len = 0;
                                n = 1;

                                p2 = p1 = TABLE;
                                while (*p1) {
                                    if (*p1 == '\n') {
                                        if (p1 - p2 > max_len)
                                            max_len = p1 - p2;
                                        p1++;
                                        if (*(p2 = p1))
                                            n++;
                                    } else
                                        p1++;
                                }
                                if (!pres) {
                                    /* the null terminated word */
                                    if (p1 - p2 > max_len)
                                        max_len = p1 - p2;
                                    pres = fs / (max_len + 2); /* at least two
                                                                * separating spaces */
                                    if (!pres)
                                        pres = 1;

                                    /* This moves some entries from the right side
                                     * of the table to fill out the last line,
                                     * which makes the table look a bit nicer.
                                     * E.g.
                                     * (n=13,p=6)      (l=3,p=5)
                                     * X X X X X X     X X X X X
                                     * X X X X X X  -> X X X X X
                                     * X               X X X X
                                     *
                                     */
                                    len = (n-1)/pres + 1;
                                    if (n > pres && n % pres)
                                        pres -= (pres - n % pres) / len;
                                } else {
                                    len = (n-1)/pres + 1;
                                }
                                (*temp)->size = fs / pres;
                                (*temp)->remainder = fs % pres;
                                if (n < pres) {
                                    /* If we have fewer elements than columns,
                                     * pretend we are dealing with a smaller
                                     * table.
                                     */
                                    (*temp)->remainder += (pres - n)*((*temp)->size);
                                    pres = n;
                                }

                                (*temp)->d.tab = CALLOCATE(pres + 1, tab_data_t,
                                        TAG_TEMPORARY, "string_print: 5");
                                (*temp)->nocols = pres;     /* heavy sigh */
                                (*temp)->d.tab[0].start = TABLE;
                                if (pres == 1) {
                                    (*temp)->d.tab[1].start = TABLE + SVALUE_STRLEN(carg) + 1;
                                } else {
                                    i = 1;  /* the next column number */
                                    n = 0;  /* the current "word" number in this
                                             * column */

                                    p1 = TABLE;
                                    while (*p1) {
                                        if (*p1++ == '\n' && ++n >= len) {
                                            (*temp)->d.tab[i++].start = p1;
                                            n = 0;
                                        }
                                    }
                                    for ( ; i <= pres; i++)
                                        (*temp)->d.tab[i].start = ++p1;
                                }
                                for (i = 0; i < pres; i++)
                                    (*temp)->d.tab[i].cur = (*temp)->d.tab[i].start;

                                add_table(temp);
                            }
                        } else {    /* not column or table */
                            const char *tmp = carg->u.string; //work around tcc bug;
                            if (pres && pres < slen)
                                slen = pres;
                            add_justified(tmp, slen, &pad, fs, finfo,
                                    (((format_str[fpos] != '\n') && (format_str[fpos] != '\0'))
                                     || ((finfo & INFO_ARRAY) && (nelemno < (argv + sprintf_state->cur_arg)->u.arr->size)))
                                    || carg->u.string[slen - 1] != '\n');
                        }
                    } else if (finfo & INFO_T_INT) {        /* one of the integer
                                                             * types */
                        char cheat[20];
                        char temp[100];

                        *cheat = '%';
                        i = 1;
                        switch (finfo & INFO_PP) {
                            case INFO_PP_SPACE:
                                cheat[i++] = ' ';
                                break;
                            case INFO_PP_PLUS:
                                cheat[i++] = '+';
                                break;
                        }
                        if (pres) {
                            cheat[i++] = '.';
                            if(pres >= sizeof(temp))
                                sprintf(cheat + i, "%ld", sizeof(temp) - 1);
                            else
                                sprintf(cheat + i, "%d", pres);

                            i += strlen(cheat + i);
                        }
                        switch (finfo & INFO_T) {
                            case INFO_T_INT:
                                cheat[i++] = 'l';
                                cheat[i++] = 'd';
                                break;
                            case INFO_T_FLOAT:
                                cheat[i++] = 'f';
                                break;
                            case INFO_T_CHAR:
                                cheat[i++] = 'c';
                                break;
                            case INFO_T_OCT:
                                cheat[i++] = 'l';
                                cheat[i++] = 'o';
                                break;
                            case INFO_T_HEX:
                                cheat[i++] = 'l';
                                cheat[i++] = 'x';
                                break;
                            case INFO_T_C_HEX:
                                cheat[i++] = 'l';
                                cheat[i++] = 'X';
                                break;
                            default:
                                ERROR(ERR_BAD_INT_TYPE);
                        }
                        if ((cheat[i - 1] == 'f' && carg->type != T_REAL) || (cheat[i - 1] != 'f' && carg->type != T_NUMBER)) {
#ifdef RETURN_ERROR_MESSAGES
                            sprintf(buff,
                                    "ERROR: (s)printf(): Incorrect argument type to %%%c. (arg: %u)\n",
                                    cheat[i - 1], sprintf_state->cur_arg);
                            fprintf(stderr, "Program /%s File: %s: %s", current_prog->name,
                                    get_line_number_if_any(), buff);
                            debug_message("%s", buff);
                            if (current_object) {
                                debug_message("program: /%s, object: %s, file: %s\n",
                                        current_prog ? current_prog->name : "",
                                        current_object->name,
                                        get_line_number_if_any());
                            }
                            ERROR(ERR_RECOVERY_ONLY);
#else
                            error("ERROR: (s)printf(): Incorrect argument type to %%%c.\n",
                                    cheat[i - 1]);
#endif                          /* RETURN_ERROR_MESSAGES */
                        }
                        cheat[i] = '\0';

                        if (carg->type == T_REAL) {
                            sprintf(temp, cheat, carg->u.real);
                        } else
                            sprintf(temp, cheat, carg->u.number);
                        {
                            int tmpl = strlen(temp);

                            add_justified(temp, tmpl, &pad, fs, finfo,
                                    (((format_str[fpos] != '\n') && (format_str[fpos] != '\0'))
                                     || ((finfo & INFO_ARRAY) && (nelemno < (argv + sprintf_state->cur_arg)->u.arr->size))));
                        }
                    } else          /* type not found */
                        ERROR(ERR_UNDEFINED_TYPE);
                    if (sprintf_state->clean.type != T_NUMBER) {
                        free_svalue(&(sprintf_state->clean), "string_print_formatted");
                        sprintf_state->clean.type = T_NUMBER;
                    }

                    if (!(finfo & INFO_ARRAY))
                        break;
                    if (nelemno >= (argv + sprintf_state->cur_arg)->u.arr->size)
                        break;
                    carg = (argv + sprintf_state->cur_arg)->u.arr->item + nelemno++;
                }                   /* end of while (1) */
                last = fpos;
                fpos--;             /* bout to get incremented */
            }
    }                           /* end of for (fpos=0; 1; fpos++) */

    outbuf_fix(&sprintf_state->obuff);
    retvalue = sprintf_state->obuff.buffer;
    sprintf_state->obuff.buffer = 0;
    pop_stack();                /* pop off our error handler, will call pop_sprintf_state */
    return retvalue;
}                               /* end of string_print_formatted() */
Ejemplo n.º 5
0
void print_harmonics(harmonic* harm_series, int* quantity)
{
    make_pad(quantity);
    n_print_harmonics(harm_series, quantity);
}