예제 #1
0
/* Encodes and transposes a message using a given ADFGX machine */
char* encode_hard(adfgx* machine, const char* text) {
	unsigned int i, j, k, l = 0;
	char *transposition_sorted, *encoded_simple, *encoded_hard;
	encoded_simple = encode_simple(machine, text);
	encoded_hard = allocate_strlen(strlen(encoded_simple));
	transposition_sorted = sort(machine->transposition);
	for (i = 0; i<strlen(transposition_sorted); i++) {
		k = get_index(transposition_sorted[i], machine->transposition);
		for (j = k; j<strlen(encoded_simple); j += strlen(machine->transposition)) {
			encoded_hard[l++] = encoded_simple[j];
		}
	}
	free(transposition_sorted);
	free(encoded_simple);
	return encoded_hard;
}
예제 #2
0
char *idset_encode (const struct idset *idset, int flags)
{
    char *str = NULL;
    size_t strsz = 0;
    size_t strlength = 0;
    int count;

    if (validate_idset_flags (flags, IDSET_FLAG_BRACKETS
                                   | IDSET_FLAG_RANGE) < 0)
        return NULL;
    if (!idset) {
        errno = EINVAL;
        return NULL;
    }
    if ((flags & IDSET_FLAG_BRACKETS)) {    // add open brace, if requested
        if (catprintf (&str, &strsz, &strlength, "[") < 0)
            goto error;
    }
    if ((flags & IDSET_FLAG_RANGE))
        count = encode_ranged (idset, &str, &strsz, &strlength);
    else
        count = encode_simple (idset, &str, &strsz, &strlength);
    if (count < 0)
        goto error;
    if ((flags & IDSET_FLAG_BRACKETS) && count > 1) { // add close brace
        if (catprintf (&str, &strsz, &strlength, "]") < 0)
            goto error;
    }
    if (!str) {
        if (!(str = strdup ("")))
            goto error;
    }
    if (count <= 1 && str[0] == '[')        // no braces for singletons
        memmove (str, str + 1, strlength);  // moves '\0' too
    return str;
error:
    free (str);
    errno = ENOMEM;
    return NULL;
}
/****************
 * Encode FILENAME as a literal data packet only. Take input from
 * stdin if FILENAME is NULL.
 */
int
encode_store( const char *filename )
{
    return encode_simple( filename, 0, 0 );
}
/****************
 * Encode FILENAME with only the symmetric cipher.  Take input from
 * stdin if FILENAME is NULL.
 */
int
encode_symmetric( const char *filename )
{
    return encode_simple( filename, 1, 0 );
}