anastasis_api_redux_free.c (10220B)
1 /* 2 This file is part of Anastasis 3 Copyright (C) 2020, 2021, 2022 Anastasis SARL 4 5 Anastasis is free software; you can redistribute it and/or modify it under the 6 terms of the GNU General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License along with 14 Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file reducer/anastasis_api_redux_free.c 18 * @brief release a parsed reducer state 19 * @author Christian Grothoff 20 * 21 * Every structure here is allocated zeroed (GNUNET_new(_array)) and every 22 * array's length field is set before the array is filled, so this code also 23 * has to cope with partially initialized states: the parser simply hands a 24 * half-built state to ANASTASIS_REDUX_state_free_() when it gives up. 25 */ 26 #include "platform.h" 27 #include "anastasis_redux.h" 28 #include "anastasis_api_redux.h" 29 #include "anastasis_api_redux_state.h" 30 31 32 void 33 ANASTASIS_REDUX_provider_url_clear_ (struct ANASTASIS_ProviderUrl *pu) 34 { 35 GNUNET_free (pu->url); 36 } 37 38 39 void 40 ANASTASIS_REDUX_provider_config_clear_ (struct ANASTASIS_ReduxProviderConfig * 41 cfg 42 ) 43 { 44 for (unsigned int j = 0; j < cfg->methods_len; j++) 45 GNUNET_free (cfg->methods[j].type); 46 GNUNET_free (cfg->methods); 47 GNUNET_free (cfg->business_name); 48 GNUNET_free (cfg->currency); 49 memset (cfg, 50 0, 51 sizeof (*cfg)); 52 } 53 54 55 void 56 ANASTASIS_REDUX_providers_clear_ (struct ANASTASIS_ReduxCommon *common) 57 { 58 for (unsigned int i = 0; i < common->providers_len; i++) 59 { 60 struct ANASTASIS_ReduxProvider *p = &common->providers[i]; 61 62 ANASTASIS_REDUX_provider_url_clear_ (&p->url); 63 if (p->have_config) 64 ANASTASIS_REDUX_provider_config_clear_ (&p->config); 65 } 66 GNUNET_free (common->providers); 67 common->providers = NULL; 68 common->providers_len = 0; 69 } 70 71 72 void 73 ANASTASIS_REDUX_continents_clear_ (struct ANASTASIS_ReduxCommon *common) 74 { 75 for (unsigned int i = 0; i < common->continents_len; i++) 76 { 77 struct ANASTASIS_ReduxContinent *c = &common->continents[i]; 78 79 GNUNET_free (c->name); 80 json_decref (c->name_i18n); 81 } 82 GNUNET_free (common->continents); 83 common->continents = NULL; 84 common->continents_len = 0; 85 } 86 87 88 void 89 ANASTASIS_REDUX_countries_clear_ (struct ANASTASIS_ReduxCommon *common) 90 { 91 for (unsigned int i = 0; i < common->countries_len; i++) 92 { 93 struct ANASTASIS_ReduxCountry *c = &common->countries[i]; 94 95 GNUNET_free (c->code); 96 GNUNET_free (c->name); 97 GNUNET_free (c->continent); 98 GNUNET_free (c->call_code); 99 GNUNET_free (c->currency); 100 json_decref (c->name_i18n); 101 json_decref (c->continent_i18n); 102 } 103 GNUNET_free (common->countries); 104 common->countries = NULL; 105 common->countries_len = 0; 106 } 107 108 109 void 110 ANASTASIS_REDUX_required_attributes_clear_ ( 111 struct ANASTASIS_ReduxCommon *common) 112 { 113 for (unsigned int i = 0; i < common->required_attributes_len; i++) 114 { 115 struct ANASTASIS_ReduxAttributeSpec *a = &common->required_attributes[i]; 116 117 GNUNET_free (a->type); 118 GNUNET_free (a->name); 119 GNUNET_free (a->label); 120 GNUNET_free (a->widget); 121 GNUNET_free (a->uuid); 122 GNUNET_free (a->tooltip); 123 GNUNET_free (a->validation_regex); 124 GNUNET_free (a->validation_logic); 125 GNUNET_free (a->autocomplete); 126 json_decref (a->label_i18n); 127 } 128 GNUNET_free (common->required_attributes); 129 common->required_attributes = NULL; 130 common->required_attributes_len = 0; 131 } 132 133 134 /** 135 * Free the contents of @a common. 136 * 137 * @param[in,out] common state to free 138 */ 139 static void 140 free_common (struct ANASTASIS_ReduxCommon *common) 141 { 142 ANASTASIS_REDUX_continents_clear_ (common); 143 ANASTASIS_REDUX_countries_clear_ (common); 144 ANASTASIS_REDUX_required_attributes_clear_ (common); 145 ANASTASIS_REDUX_providers_clear_ (common); 146 for (unsigned int i = 0; i < common->currencies_len; i++) 147 GNUNET_free (common->currencies[i]); 148 GNUNET_free (common->currencies); 149 json_decref (common->identity_attributes); 150 GNUNET_free (common->selected_continent); 151 GNUNET_free (common->selected_country); 152 } 153 154 155 /** 156 * Free the contents of @a backup. 157 * 158 * @param[in,out] backup state to free 159 */ 160 void 161 ANASTASIS_REDUX_policy_clear_ (struct ANASTASIS_ReduxPolicy *p) 162 { 163 for (unsigned int j = 0; j < p->methods_len; j++) 164 { 165 struct ANASTASIS_ReduxPolicyMethod *pm = &p->methods[j]; 166 167 ANASTASIS_REDUX_provider_url_clear_ (&pm->provider); 168 if (NULL != pm->truth) 169 ANASTASIS_truth_free (pm->truth); 170 } 171 GNUNET_free (p->methods); 172 p->methods = NULL; 173 p->methods_len = 0; 174 } 175 176 177 void 178 ANASTASIS_REDUX_policies_clear_ (struct ANASTASIS_ReduxBackup *backup) 179 { 180 for (unsigned int i = 0; i < backup->policies_len; i++) 181 ANASTASIS_REDUX_policy_clear_ (&backup->policies[i]); 182 GNUNET_free (backup->policies); 183 backup->policies = NULL; 184 backup->policies_len = 0; 185 } 186 187 188 void 189 ANASTASIS_REDUX_policy_providers_clear_ (struct ANASTASIS_ReduxBackup *backup) 190 { 191 for (unsigned int i = 0; i < backup->policy_providers_len; i++) 192 ANASTASIS_REDUX_provider_url_clear_ ( 193 &backup->policy_providers[i].provider_url); 194 GNUNET_free (backup->policy_providers); 195 backup->policy_providers = NULL; 196 backup->policy_providers_len = 0; 197 } 198 199 200 void 201 ANASTASIS_REDUX_payments_clear_ (struct ANASTASIS_ReduxBackup *backup) 202 { 203 for (unsigned int i = 0; i < backup->payments_len; i++) 204 GNUNET_free (backup->payments[i]); 205 GNUNET_free (backup->payments); 206 backup->payments = NULL; 207 backup->payments_len = 0; 208 backup->have_payments = false; 209 } 210 211 212 void 213 ANASTASIS_REDUX_policy_payment_requests_clear_ ( 214 struct ANASTASIS_ReduxBackup *backup) 215 { 216 for (unsigned int i = 0; i < backup->policy_payment_requests_len; i++) 217 { 218 struct ANASTASIS_ReduxPolicyPaymentRequest *ppr 219 = &backup->policy_payment_requests[i]; 220 221 GNUNET_free (ppr->payto); 222 ANASTASIS_REDUX_provider_url_clear_ (&ppr->provider); 223 } 224 GNUNET_free (backup->policy_payment_requests); 225 backup->policy_payment_requests = NULL; 226 backup->policy_payment_requests_len = 0; 227 backup->have_policy_payment_requests = false; 228 } 229 230 231 static void 232 free_backup (struct ANASTASIS_ReduxBackup *backup) 233 { 234 for (unsigned int i = 0; i < backup->authentication_methods_len; i++) 235 { 236 struct ANASTASIS_ReduxAuthMethod *am = &backup->authentication_methods[i]; 237 238 GNUNET_free (am->type); 239 GNUNET_free (am->instructions); 240 GNUNET_free (am->challenge); 241 GNUNET_free (am->mime_type); 242 } 243 GNUNET_free (backup->authentication_methods); 244 ANASTASIS_REDUX_policies_clear_ (backup); 245 ANASTASIS_REDUX_policy_providers_clear_ (backup); 246 GNUNET_free (backup->upload_fees); 247 ANASTASIS_REDUX_payments_clear_ (backup); 248 ANASTASIS_REDUX_policy_payment_requests_clear_ (backup); 249 for (unsigned int i = 0; i < backup->success_details_len; i++) 250 ANASTASIS_REDUX_provider_url_clear_ ( 251 &backup->success_details[i].provider_url); 252 GNUNET_free (backup->success_details); 253 json_decref (backup->core_secret); 254 GNUNET_free (backup->secret_name); 255 } 256 257 258 /** 259 * Free the variant-specific contents of @a fb, but not @a fb itself. 260 * 261 * @param[in,out] fb feedback to reset 262 */ 263 void 264 ANASTASIS_REDUX_feedback_clear_ (struct ANASTASIS_ReduxFeedback *fb) 265 { 266 GNUNET_free (fb->display_hint); 267 switch (fb->status) 268 { 269 case ANASTASIS_RFS_CODE_IN_FILE: 270 GNUNET_free (fb->details.code_in_file.filename); 271 break; 272 case ANASTASIS_RFS_SEND_TO_ADDRESS: 273 GNUNET_free (fb->details.send_to_address.address_hint); 274 break; 275 case ANASTASIS_RFS_TALER_PAYMENT: 276 GNUNET_free (fb->details.taler_payment.taler_pay_uri); 277 ANASTASIS_REDUX_provider_url_clear_ (&fb->details.taler_payment.provider); 278 break; 279 case ANASTASIS_RFS_IBAN_INSTRUCTIONS: 280 GNUNET_free (fb->details.iban_instructions.target_iban); 281 GNUNET_free (fb->details.iban_instructions.target_business_name); 282 GNUNET_free (fb->details.iban_instructions.wire_transfer_subject); 283 break; 284 case ANASTASIS_RFS_SERVER_FAILURE: 285 case ANASTASIS_RFS_TRUTH_UNKNOWN: 286 case ANASTASIS_RFS_SOLVED: 287 case ANASTASIS_RFS_INCORRECT_ANSWER: 288 case ANASTASIS_RFS_RATE_LIMIT_EXCEEDED: 289 /* nothing heap-allocated beyond the display hint */ 290 break; 291 } 292 memset (&fb->details, 293 0, 294 sizeof (fb->details)); 295 fb->display_hint = NULL; 296 } 297 298 299 void 300 ANASTASIS_REDUX_recovery_info_free_ (struct ANASTASIS_ReduxRecoveryInfo *ri) 301 { 302 if (NULL == ri) 303 return; 304 for (unsigned int i = 0; i < ri->challenges_len; i++) 305 { 306 GNUNET_free (ri->challenges[i].type); 307 GNUNET_free (ri->challenges[i].instructions); 308 GNUNET_free (ri->challenges[i].answer); 309 } 310 GNUNET_free (ri->challenges); 311 for (unsigned int i = 0; i < ri->policies_len; i++) 312 GNUNET_free (ri->policies[i].uuids); 313 GNUNET_free (ri->policies); 314 GNUNET_free (ri->secret_name); 315 ANASTASIS_REDUX_provider_url_clear_ (&ri->provider_url); 316 GNUNET_free (ri); 317 } 318 319 320 /** 321 * Free the contents of @a recovery. 322 * 323 * @param[in,out] recovery state to free 324 */ 325 static void 326 free_recovery (struct ANASTASIS_ReduxRecovery *recovery) 327 { 328 if (NULL != recovery->r) 329 ANASTASIS_recovery_abort (recovery->r); 330 ANASTASIS_REDUX_recovery_info_free_ (recovery->ri); 331 for (unsigned int i = 0; i < recovery->challenge_feedback_len; i++) 332 ANASTASIS_REDUX_feedback_clear_ (&recovery->challenge_feedback[i]); 333 GNUNET_free (recovery->challenge_feedback); 334 json_decref (recovery->core_secret); 335 } 336 337 338 void 339 ANASTASIS_REDUX_state_free_ (struct ANASTASIS_ReduxState *rs) 340 { 341 if (NULL == rs) 342 return; 343 switch (rs->type) 344 { 345 case ANASTASIS_RT_BACKUP: 346 free_backup (&rs->details.backup); 347 free_common (&rs->common); 348 break; 349 case ANASTASIS_RT_RECOVERY: 350 free_recovery (&rs->details.recovery); 351 free_common (&rs->common); 352 break; 353 case ANASTASIS_RT_ERROR: 354 GNUNET_free (rs->details.error.hint); 355 GNUNET_free (rs->details.error.detail); 356 break; 357 } 358 GNUNET_free (rs); 359 } 360 361 362 /* end of anastasis_api_redux_free.c */