LCOV - code coverage report
Current view: top level - json - json_helper.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 47 72 65.3 %
Date: 2017-11-25 11:31:41 Functions: 7 8 87.5 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014, 2015, 2016 GNUnet e.V.
       4             : 
       5             :   TALER 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             :   TALER 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             :   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
      15             : */
      16             : /**
      17             :  * @file json/json_helper.c
      18             :  * @brief helper functions to generate specifications to parse
      19             :  *        Taler-specific JSON objects with libgnunetjson
      20             :  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
      21             :  * @author Christian Grothoff
      22             :  */
      23             : #include "platform.h"
      24             : #include <gnunet/gnunet_util_lib.h>
      25             : #include "taler_util.h"
      26             : #include "taler_json_lib.h"
      27             : 
      28             : 
      29             : /**
      30             :  * Convert a TALER amount to a JSON object.
      31             :  *
      32             :  * @param amount the amount
      33             :  * @return a json object describing the amount
      34             :  */
      35             : json_t *
      36         345 : TALER_JSON_from_amount (const struct TALER_Amount *amount)
      37             : {
      38             :   json_t *j;
      39             : 
      40         345 :   if ( (amount->value != (uint64_t) ((json_int_t) amount->value)) ||
      41         345 :        (0 > ((json_int_t) amount->value)) )
      42             :   {
      43             :     /* Theoretically, json_int_t can be a 32-bit "long", or we might
      44             :        have a 64-bit value which converted to a 63-bit signed long
      45             :        long causes problems here.  So we check.  Note that depending
      46             :        on the platform, the compiler may be able to statically tell
      47             :        that at least the first check is always false. */
      48           0 :     GNUNET_break (0);
      49           0 :     return NULL;
      50             :   }
      51        1035 :   j = json_pack ("{s:s, s:I, s:I}",
      52         345 :                  "currency", amount->currency,
      53         345 :                  "value", (json_int_t) amount->value,
      54         345 :                  "fraction", (json_int_t) amount->fraction);
      55         345 :   GNUNET_assert (NULL != j);
      56         345 :   return j;
      57             : }
      58             : 
      59             : 
      60             : 
      61             : /**
      62             :  * Convert a TALER amount to a JSON object.
      63             :  *
      64             :  * @param amount the amount
      65             :  * @return a json object describing the amount
      66             :  */
      67             : json_t *
      68           0 : TALER_JSON_from_amount_nbo (const struct TALER_AmountNBO *amount)
      69             : {
      70             :   struct TALER_Amount a;
      71             : 
      72           0 :   TALER_amount_ntoh (&a,
      73             :                      amount);
      74           0 :   return TALER_JSON_from_amount (&a);
      75             : }
      76             : 
      77             : 
      78             : /**
      79             :  * Parse given JSON object to Amount
      80             :  *
      81             :  * @param cls closure, NULL
      82             :  * @param root the json object representing data
      83             :  * @param[out] spec where to write the data
      84             :  * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
      85             :  */
      86             : static int
      87         196 : parse_amount (void *cls,
      88             :               json_t *root,
      89             :               struct GNUNET_JSON_Specification *spec)
      90             : {
      91         196 :   struct TALER_Amount *r_amount = spec->ptr;
      92             :   json_int_t value;
      93             :   json_int_t fraction;
      94             :   const char *currency;
      95             : 
      96         196 :   memset (r_amount,
      97             :           0,
      98             :           sizeof (struct TALER_Amount));
      99         196 :   if (0 != json_unpack (root,
     100             :                         "{s:I, s:I, s:s}",
     101             :                         "value", &value,
     102             :                         "fraction", &fraction,
     103             :                         "currency", &currency))
     104             :   {
     105             :     char *json_enc;
     106             : 
     107           0 :     if (NULL == (json_enc = json_dumps (root,
     108             :                                         JSON_COMPACT | JSON_ENCODE_ANY)))
     109             :     {
     110           0 :       GNUNET_break (0);
     111           0 :       return GNUNET_SYSERR;
     112             :     }
     113           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     114             :                 "Malformed JSON amount: %s\n",
     115             :                 json_enc);
     116           0 :     free (json_enc);
     117           0 :     return GNUNET_SYSERR;
     118             :   }
     119         196 :   if ( (value < 0) ||
     120         196 :        (fraction < 0) ||
     121         196 :        (value > UINT64_MAX) ||
     122         196 :        (fraction > UINT32_MAX) )
     123             :   {
     124           0 :     GNUNET_break_op (0);
     125           0 :     return GNUNET_SYSERR;
     126             :   }
     127         196 :   if (strlen (currency) >= TALER_CURRENCY_LEN)
     128             :   {
     129           0 :     GNUNET_break_op (0);
     130           0 :     return GNUNET_SYSERR;
     131             :   }
     132         196 :   r_amount->value = (uint64_t) value;
     133         196 :   r_amount->fraction = (uint32_t) fraction;
     134         196 :   strcpy (r_amount->currency, currency);
     135         196 :   (void) TALER_amount_normalize (r_amount);
     136         196 :   return GNUNET_OK;
     137             : }
     138             : 
     139             : 
     140             : /**
     141             :  * Provide specification to parse given JSON object to an amount.
     142             :  *
     143             :  * @param name name of the amount field in the JSON
     144             :  * @param[out] r_amount where the amount has to be written
     145             :  */
     146             : struct GNUNET_JSON_Specification
     147         240 : TALER_JSON_spec_amount (const char *name,
     148             :                         struct TALER_Amount *r_amount)
     149             : {
     150         240 :   struct GNUNET_JSON_Specification ret = {
     151             :     .parser = &parse_amount,
     152             :     .cleaner = NULL,
     153             :     .cls = NULL,
     154             :     .field = name,
     155             :     .ptr = r_amount,
     156             :     .ptr_size = 0,
     157             :     .size_ptr = NULL
     158             :   };
     159         240 :   return ret;
     160             : }
     161             : 
     162             : 
     163             : /**
     164             :  * Parse given JSON object to Amount in NBO.
     165             :  *
     166             :  * @param cls closure, NULL
     167             :  * @param root the json object representing data
     168             :  * @param[out] spec where to write the data
     169             :  * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
     170             :  */
     171             : static int
     172           9 : parse_amount_nbo (void *cls,
     173             :                   json_t *root,
     174             :                   struct GNUNET_JSON_Specification *spec)
     175             : {
     176           9 :   struct TALER_AmountNBO *r_amount = spec->ptr;
     177             :   struct TALER_Amount amount;
     178             :   json_int_t value;
     179             :   json_int_t fraction;
     180             :   const char *currency;
     181             : 
     182           9 :   memset (&amount,
     183             :           0,
     184             :           sizeof (struct TALER_Amount));
     185           9 :   if (0 != json_unpack (root,
     186             :                         "{s:I, s:I, s:s}",
     187             :                         "value", &value,
     188             :                         "fraction", &fraction,
     189             :                         "currency", &currency))
     190             :   {
     191             :     char *json_enc;
     192             : 
     193           0 :     if (NULL == (json_enc = json_dumps (root,
     194             :                                         JSON_COMPACT | JSON_ENCODE_ANY)))
     195             :     {
     196           0 :       GNUNET_break (0);
     197           0 :       return GNUNET_SYSERR;
     198             :     }
     199           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     200             :                 "Malformed JSON amount: %s\n",
     201             :                 json_enc);
     202           0 :     free (json_enc);
     203           0 :     return GNUNET_SYSERR;
     204             :   }
     205          18 :   if ( (value < 0) ||
     206          18 :        (fraction < 0) ||
     207           9 :        (fraction > (json_int_t) UINT32_MAX) )
     208             :   {
     209           0 :     GNUNET_break_op (0);
     210           0 :     return GNUNET_SYSERR;
     211             :   }
     212           9 :   if (strlen (currency) >= TALER_CURRENCY_LEN)
     213             :   {
     214           0 :     GNUNET_break_op (0);
     215           0 :     return GNUNET_SYSERR;
     216             :   }
     217           9 :   amount.value = (uint64_t) value;
     218           9 :   amount.fraction = (uint32_t) fraction;
     219           9 :   strcpy (amount.currency, currency);
     220           9 :   (void) TALER_amount_normalize (&amount);
     221           9 :   TALER_amount_hton (r_amount,
     222             :                      &amount);
     223           9 :   return GNUNET_OK;
     224             : }
     225             : 
     226             : 
     227             : /**
     228             :  * Provide specification to parse given JSON object to an amount.
     229             :  *
     230             :  * @param name name of the amount field in the JSON
     231             :  * @param[out] r_amount where the amount has to be written
     232             :  */
     233             : struct GNUNET_JSON_Specification
     234           9 : TALER_JSON_spec_amount_nbo (const char *name,
     235             :                             struct TALER_AmountNBO *r_amount)
     236             : {
     237           9 :   struct GNUNET_JSON_Specification ret = {
     238             :     .parser = &parse_amount_nbo,
     239             :     .cleaner = NULL,
     240             :     .cls = NULL,
     241             :     .field = name,
     242             :     .ptr = r_amount,
     243             :     .ptr_size = 0,
     244             :     .size_ptr = NULL
     245             :   };
     246           9 :   return ret;
     247             : }
     248             : 
     249             : 
     250             : /**
     251             :  * Generate line in parser specification for denomination public key.
     252             :  *
     253             :  * @param field name of the field
     254             :  * @param[out] pk key to initialize
     255             :  * @return corresponding field spec
     256             :  */
     257             : struct GNUNET_JSON_Specification
     258         105 : TALER_JSON_spec_denomination_public_key (const char *field,
     259             :                                          struct TALER_DenominationPublicKey *pk)
     260             : {
     261         105 :   return GNUNET_JSON_spec_rsa_public_key (field,
     262             :                                           &pk->rsa_public_key);
     263             : }
     264             : 
     265             : 
     266             : /**
     267             :  * Generate line in parser specification for denomination signature.
     268             :  *
     269             :  * @param field name of the field
     270             :  * @param sig the signature to initialize
     271             :  * @return corresponding field spec
     272             :  */
     273             : struct GNUNET_JSON_Specification
     274          47 : TALER_JSON_spec_denomination_signature (const char *field,
     275             :                                         struct TALER_DenominationSignature *sig)
     276             : {
     277          47 :   return GNUNET_JSON_spec_rsa_signature (field,
     278             :                                          &sig->rsa_signature);
     279             : }
     280             : 
     281             : 
     282             : /* end of json/json_helper.c */

Generated by: LCOV version 1.13