split away conversion functions sync
authorVolker Birk <vb@pep-project.org>
Thu, 12 Dec 2019 11:21:53 +0100
branchsync
changeset 42693d466b014d2e
parent 4268 fba3e28be8ea
child 4270 948445131f47
split away conversion functions
src/key_reset.c
src/map_asn1.c
     1.1 --- a/src/key_reset.c	Thu Dec 12 10:56:05 2019 +0100
     1.2 +++ b/src/key_reset.c	Thu Dec 12 11:21:53 2019 +0100
     1.3 @@ -709,20 +709,20 @@
     1.4      return PEP_STATUS_OK;
     1.5  }
     1.6  
     1.7 -PEP_STATUS key_reset_commands_to_PER(const keyreset_command_list *command_list, char **cmds, size_t *size)
     1.8 +Distribution_t *Distribution_from_keyreset_command_list(
     1.9 +        const keyreset_command_list *command_list,
    1.10 +        Distribution_t *dist
    1.11 +    )
    1.12  {
    1.13 -    PEP_STATUS status = PEP_STATUS_OK;
    1.14 +    bool allocated = !dist;
    1.15  
    1.16 -    assert(command_list && cmds);
    1.17 -    if (!(command_list && cmds))
    1.18 -        return PEP_ILLEGAL_VALUE;
    1.19 +    assert(command_list);
    1.20 +    if (!command_list)
    1.21 +        return NULL;
    1.22  
    1.23 -    *cmds = NULL;
    1.24 -    *size = 0;
    1.25 +    if (allocated)
    1.26 +        dist = (Distribution_t *) calloc(1, sizeof(Distribution_t));
    1.27  
    1.28 -    // convert to ASN.1 struct
    1.29 -
    1.30 -    Distribution_t *dist = (Distribution_t *) calloc(1, sizeof(Distribution_t));
    1.31      assert(dist);
    1.32      if (!dist)
    1.33          goto enomem;
    1.34 @@ -752,6 +752,31 @@
    1.35          }
    1.36      }
    1.37  
    1.38 +    return dist;
    1.39 +
    1.40 +enomem:
    1.41 +    ASN_STRUCT_FREE(asn_DEF_Distribution, dist);
    1.42 +    return NULL;
    1.43 +}
    1.44 +
    1.45 +PEP_STATUS key_reset_commands_to_PER(const keyreset_command_list *command_list, char **cmds, size_t *size)
    1.46 +{
    1.47 +    PEP_STATUS status = PEP_STATUS_OK;
    1.48 +
    1.49 +    assert(command_list && cmds);
    1.50 +    if (!(command_list && cmds))
    1.51 +        return PEP_ILLEGAL_VALUE;
    1.52 +
    1.53 +    *cmds = NULL;
    1.54 +    *size = 0;
    1.55 +
    1.56 +    // convert to ASN.1 struct
    1.57 +
    1.58 +    Distribution_t *dist = Distribution_from_keyreset_command_list(command_list, NULL);
    1.59 +    assert(dist);
    1.60 +    if (!dist)
    1.61 +        goto enomem;
    1.62 +
    1.63      // encode
    1.64  
    1.65      char *_cmds;
    1.66 @@ -774,6 +799,53 @@
    1.67      return status;
    1.68  }
    1.69  
    1.70 +keyreset_command_list * Distribution_to_keyreset_command_list(
    1.71 +        Distribution_t *dist,
    1.72 +        keyreset_command_list *command_list
    1.73 +    )
    1.74 +{
    1.75 +    bool allocated = !command_list;
    1.76 +
    1.77 +    assert(dist);
    1.78 +    if (!dist)
    1.79 +        return NULL;
    1.80 +
    1.81 +    // convert from ASN.1 struct
    1.82 +
    1.83 +    if (allocated)
    1.84 +        command_list = new_keyreset_command_list(NULL);
    1.85 +    if (!command_list)
    1.86 +        goto enomem;
    1.87 +
    1.88 +    struct Commands__commandlist *cl = &dist->choice.keyreset.choice.commands.commandlist;
    1.89 +    keyreset_command_list *_result = command_list;
    1.90 +    for (int i=0; i<cl->list.count; i++) {
    1.91 +        pEp_identity *ident = Identity_to_Struct(&cl->list.array[i]->ident, NULL);
    1.92 +        if (!ident)
    1.93 +            goto enomem;
    1.94 +
    1.95 +        const char *new_key = (const char *) cl->list.array[i]->newkey.buf;
    1.96 +
    1.97 +        keyreset_command *command = new_keyreset_command(ident, new_key);
    1.98 +        if (!command) {
    1.99 +            free_identity(ident);
   1.100 +            goto enomem;
   1.101 +        }
   1.102 +
   1.103 +        _result = keyreset_command_list_add(_result, command);
   1.104 +        free_identity(ident);
   1.105 +        if (!_result)
   1.106 +            goto enomem;
   1.107 +    }
   1.108 +
   1.109 +    return command_list;
   1.110 +
   1.111 +enomem:
   1.112 +    if (allocated)
   1.113 +        free_keyreset_command_list(command_list);
   1.114 +    return NULL;
   1.115 +}
   1.116 +
   1.117  PEP_STATUS PER_to_key_reset_commands(const char *cmds, size_t size, keyreset_command_list **command_list)
   1.118  {
   1.119      assert(command_list && cmds);
   1.120 @@ -781,7 +853,6 @@
   1.121          return PEP_ILLEGAL_VALUE;
   1.122  
   1.123      *command_list = NULL;
   1.124 -    keyreset_command_list *result = NULL;
   1.125  
   1.126      // decode
   1.127  
   1.128 @@ -803,31 +874,10 @@
   1.129  
   1.130      // convert from ASN.1 struct
   1.131  
   1.132 -    result = new_keyreset_command_list(NULL);
   1.133 +    keyreset_command_list *result = Distribution_to_keyreset_command_list(dist, NULL);
   1.134      if (!result)
   1.135          goto enomem;
   1.136  
   1.137 -    struct Commands__commandlist *cl = &dist->choice.keyreset.choice.commands.commandlist;
   1.138 -    keyreset_command_list *_result = result;
   1.139 -    for (int i=0; i<cl->list.count; i++) {
   1.140 -        pEp_identity *ident = Identity_to_Struct(&cl->list.array[i]->ident, NULL);
   1.141 -        if (!ident)
   1.142 -            goto enomem;
   1.143 -
   1.144 -        const char *new_key = (const char *) cl->list.array[i]->newkey.buf;
   1.145 -
   1.146 -        keyreset_command *command = new_keyreset_command(ident, new_key);
   1.147 -        if (!command) {
   1.148 -            free_identity(ident);
   1.149 -            goto enomem;
   1.150 -        }
   1.151 -
   1.152 -        _result = keyreset_command_list_add(_result, command);
   1.153 -        free_identity(ident);
   1.154 -        if (!_result)
   1.155 -            goto enomem;
   1.156 -    }
   1.157 -
   1.158      // return result
   1.159  
   1.160      *command_list = result;
     2.1 --- a/src/map_asn1.c	Thu Dec 12 10:56:05 2019 +0100
     2.2 +++ b/src/map_asn1.c	Thu Dec 12 11:21:53 2019 +0100
     2.3 @@ -15,9 +15,8 @@
     2.4      if (!ident)
     2.5          return NULL;
     2.6  
     2.7 -    if (allocated){
     2.8 +    if (allocated)
     2.9          result = (Identity_t *) calloc(1, sizeof(Identity_t));
    2.10 -    }
    2.11      assert(result);
    2.12      if (!result)
    2.13          return NULL;
    2.14 @@ -65,9 +64,8 @@
    2.15      return result;
    2.16  
    2.17  enomem:
    2.18 -    if (allocated){
    2.19 +    if (allocated)
    2.20          ASN_STRUCT_FREE(asn_DEF_Identity, result);
    2.21 -    }
    2.22      return NULL;
    2.23  }
    2.24