LCOV - code coverage report
Current view: top level - json - json_helper.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 47 69 68.1 %
Date: 2017-09-17 17:24:28 Functions: 7 7 100.0 %

          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             :  * Parse given JSON object to Amount
      62             :  *
      63             :  * @param cls closure, NULL
      64             :  * @param root the json object representing data
      65             :  * @param[out] spec where to write the data
      66             :  * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
      67             :  */
      68             : static int
      69         196 : parse_amount (void *cls,
      70             :               json_t *root,
      71             :               struct GNUNET_JSON_Specification *spec)
      72             : {
      73         196 :   struct TALER_Amount *r_amount = spec->ptr;
      74             :   json_int_t value;
      75             :   json_int_t fraction;
      76             :   const char *currency;
      77             : 
      78         196 :   memset (r_amount,
      79             :           0,
      80             :           sizeof (struct TALER_Amount));
      81         196 :   if (0 != json_unpack (root,
      82             :                         "{s:I, s:I, s:s}",
      83             :                         "value", &value,
      84             :                         "fraction", &fraction,
      85             :                         "currency", &currency))
      86             :   {
      87             :     char *json_enc;
      88             : 
      89           0 :     if (NULL == (json_enc = json_dumps (root,
      90             :                                         JSON_COMPACT | JSON_ENCODE_ANY)))
      91             :     {
      92           0 :       GNUNET_break (0);
      93           0 :       return GNUNET_SYSERR;
      94             :     }
      95           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
      96             :                 "Malformed JSON amount: %s\n",
      97             :                 json_enc);
      98           0 :     free (json_enc);
      99           0 :     return GNUNET_SYSERR;
     100             :   }
     101         196 :   if ( (value < 0) ||
     102         196 :        (fraction < 0) ||
     103         196 :        (value > UINT64_MAX) ||
     104         196 :        (fraction > UINT32_MAX) )
     105             :   {
     106           0 :     GNUNET_break_op (0);
     107           0 :     return GNUNET_SYSERR;
     108             :   }
     109         196 :   if (strlen (currency) >= TALER_CURRENCY_LEN)
     110             :   {
     111           0 :     GNUNET_break_op (0);
     112           0 :     return GNUNET_SYSERR;
     113             :   }
     114         196 :   r_amount->value = (uint64_t) value;
     115         196 :   r_amount->fraction = (uint32_t) fraction;
     116         196 :   strcpy (r_amount->currency, currency);
     117         196 :   (void) TALER_amount_normalize (r_amount);
     118         196 :   return GNUNET_OK;
     119             : }
     120             : 
     121             : 
     122             : /**
     123             :  * Provide specification to parse given JSON object to an amount.
     124             :  *
     125             :  * @param name name of the amount field in the JSON
     126             :  * @param[out] r_amount where the amount has to be written
     127             :  */
     128             : struct GNUNET_JSON_Specification
     129         240 : TALER_JSON_spec_amount (const char *name,
     130             :                         struct TALER_Amount *r_amount)
     131             : {
     132         240 :   struct GNUNET_JSON_Specification ret = {
     133             :     .parser = &parse_amount,
     134             :     .cleaner = NULL,
     135             :     .cls = NULL,
     136             :     .field = name,
     137             :     .ptr = r_amount,
     138             :     .ptr_size = 0,
     139             :     .size_ptr = NULL
     140             :   };
     141         240 :   return ret;
     142             : }
     143             : 
     144             : 
     145             : /**
     146             :  * Parse given JSON object to Amount in NBO.
     147             :  *
     148             :  * @param cls closure, NULL
     149             :  * @param root the json object representing data
     150             :  * @param[out] spec where to write the data
     151             :  * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
     152             :  */
     153             : static int
     154           9 : parse_amount_nbo (void *cls,
     155             :               json_t *root,
     156             :               struct GNUNET_JSON_Specification *spec)
     157             : {
     158           9 :   struct TALER_AmountNBO *r_amount = spec->ptr;
     159             :   struct TALER_Amount amount;
     160             :   json_int_t value;
     161             :   json_int_t fraction;
     162             :   const char *currency;
     163             : 
     164           9 :   memset (&amount,
     165             :           0,
     166             :           sizeof (struct TALER_Amount));
     167           9 :   if (0 != json_unpack (root,
     168             :                         "{s:I, s:I, s:s}",
     169             :                         "value", &value,
     170             :                         "fraction", &fraction,
     171             :                         "currency", &currency))
     172             :   {
     173             :     char *json_enc;
     174             : 
     175           0 :     if (NULL == (json_enc = json_dumps (root,
     176             :                                         JSON_COMPACT | JSON_ENCODE_ANY)))
     177             :     {
     178           0 :       GNUNET_break (0);
     179           0 :       return GNUNET_SYSERR;
     180             :     }
     181           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     182             :                 "Malformed JSON amount: %s\n",
     183             :                 json_enc);
     184           0 :     free (json_enc);
     185           0 :     return GNUNET_SYSERR;
     186             :   }
     187          18 :   if ( (value < 0) ||
     188          18 :        (fraction < 0) ||
     189           9 :        (fraction > (json_int_t) UINT32_MAX) )
     190             :   {
     191           0 :     GNUNET_break_op (0);
     192           0 :     return GNUNET_SYSERR;
     193             :   }
     194           9 :   if (strlen (currency) >= TALER_CURRENCY_LEN)
     195             :   {
     196           0 :     GNUNET_break_op (0);
     197           0 :     return GNUNET_SYSERR;
     198             :   }
     199           9 :   amount.value = (uint64_t) value;
     200           9 :   amount.fraction = (uint32_t) fraction;
     201           9 :   strcpy (amount.currency, currency);
     202           9 :   (void) TALER_amount_normalize (&amount);
     203           9 :   TALER_amount_hton (r_amount,
     204             :                      &amount);
     205           9 :   return GNUNET_OK;
     206             : }
     207             : 
     208             : 
     209             : /**
     210             :  * Provide specification to parse given JSON object to an amount.
     211             :  *
     212             :  * @param name name of the amount field in the JSON
     213             :  * @param[out] r_amount where the amount has to be written
     214             :  */
     215             : struct GNUNET_JSON_Specification
     216           9 : TALER_JSON_spec_amount_nbo (const char *name,
     217             :                             struct TALER_AmountNBO *r_amount)
     218             : {
     219           9 :   struct GNUNET_JSON_Specification ret = {
     220             :     .parser = &parse_amount_nbo,
     221             :     .cleaner = NULL,
     222             :     .cls = NULL,
     223             :     .field = name,
     224             :     .ptr = r_amount,
     225             :     .ptr_size = 0,
     226             :     .size_ptr = NULL
     227             :   };
     228           9 :   return ret;
     229             : }
     230             : 
     231             : 
     232             : /**
     233             :  * Generate line in parser specification for denomination public key.
     234             :  *
     235             :  * @param field name of the field
     236             :  * @param[out] pk key to initialize
     237             :  * @return corresponding field spec
     238             :  */
     239             : struct GNUNET_JSON_Specification
     240         105 : TALER_JSON_spec_denomination_public_key (const char *field,
     241             :                                          struct TALER_DenominationPublicKey *pk)
     242             : {
     243         105 :   return GNUNET_JSON_spec_rsa_public_key (field,
     244             :                                           &pk->rsa_public_key);
     245             : }
     246             : 
     247             : 
     248             : /**
     249             :  * Generate line in parser specification for denomination signature.
     250             :  *
     251             :  * @param field name of the field
     252             :  * @param sig the signature to initialize
     253             :  * @return corresponding field spec
     254             :  */
     255             : struct GNUNET_JSON_Specification
     256          47 : TALER_JSON_spec_denomination_signature (const char *field,
     257             :                                         struct TALER_DenominationSignature *sig)
     258             : {
     259          47 :   return GNUNET_JSON_spec_rsa_signature (field,
     260             :                                          &sig->rsa_signature);
     261             : }
     262             : 
     263             : 
     264             : /* end of json/json_helper.c */

Generated by: LCOV version 1.13