0

i am currently having a lot of struggle with a, for me personally, very complex structure

struct crypto_tfm
{
    uint32_t crt_flags;

    union
    {
        struct ablkcipher_tfm ablkcipher;
        struct aead_tfm aead;
        struct blkcipher_tfm blkcipher;
        struct cipher_tfm cipher;
        struct hash_tfm hash;
        struct compress_tfm compress;
        struct rng_tfm rng;
    } crt_u;

    void (*exit)(struct crypto_tfm *tfm);

    struct crypto_alg *crt_alg;

    void *crt_ctx[] CRYPTO_MINALIGN_ATTR;
};

I completely have no idea how to use this struct. so basicly i am completely lost with this

the function using this expects a struct crypto_tfm *tfm

first idea is the following:

struct crypto_tfm *new_tfm()
{
    struct crypto_tfm *tfm = malloc(sizeof(struct crypto_tfm));
    tfm -> crt_flags = 0;
    tfm -> crt_u.
}

but i dont know how to get further,

the given structs within the union are also using another structs. kinda too complicated for me right now

timrau
  • 22,578
  • 4
  • 51
  • 64
user3694354
  • 105
  • 8

1 Answers1

1

This is untested, but should be a good example:

struct st_a
{
  int a;
};

struct st_b
{
  int b;
};

union un_c
{
  struct st_a aa;
  struct st_b bb;
};

struct st_d
{
  int d;
  union un_c cc;
};

int main ()
{
  struct st_d *dd = malloc (sizeof (struct st_d));
  dd->d = 0;
  /* The following two lines might (probably are) accessing
     the same area of memory.  */
  dd->cc.aa.a = 0;
  dd->cc.bb.b = 1;
}
Andrew
  • 3,770
  • 15
  • 22
  • so i definetely have to also use every variable struct or whatever is inside a struct or just simply the thigs i need? – user3694354 Jun 10 '14 at 12:26
  • In the example I gave you'd usually only access `cc.aa.a` *or* `cc.aa.b` depending on which is valid. It's normal to have some flag, like `d` in this case, that is not part of the union which would guide you to know which part of the union is valid. All the members of the union will usually share the same memory, so accessing multiple members of the union at the same time does not (usually) make much sense. – Andrew Jun 10 '14 at 12:34