LCOV - code coverage report
Current view: top level - json - json.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 13 19 68.4 %
Date: 2017-11-25 11:31:41 Functions: 2 2 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.c
      18             :  * @brief helper functions for JSON processing using libjansson
      19             :  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
      20             :  */
      21             : #include "platform.h"
      22             : #include <gnunet/gnunet_util_lib.h>
      23             : #include "taler_util.h"
      24             : #include "taler_json_lib.h"
      25             : 
      26             : 
      27             : /**
      28             :  * Hash a JSON for binary signing.
      29             :  *
      30             :  * @param[in] json some JSON value
      31             :  * @param[out] hc resulting hash code
      32             :  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
      33             :  */
      34             : int
      35         106 : TALER_JSON_hash (const json_t *json,
      36             :                  struct GNUNET_HashCode *hc)
      37             : {
      38             :   char *wire_enc;
      39             :   size_t len;
      40             : 
      41         106 :   if (NULL == (wire_enc = json_dumps (json,
      42             :                                       JSON_COMPACT | JSON_SORT_KEYS)))
      43             :   {
      44           0 :     GNUNET_break (0);
      45           0 :     return GNUNET_SYSERR;
      46             :   }
      47         106 :   len = strlen (wire_enc) + 1;
      48         106 :   GNUNET_CRYPTO_hash (wire_enc,
      49             :                       len,
      50             :                       hc);
      51         106 :   free (wire_enc);
      52         106 :   return GNUNET_OK;
      53             : }
      54             : 
      55             : 
      56             : 
      57             : /**
      58             :  * Extract the Taler error code from the given @a json object.
      59             :  * Note that #TALER_EC_NONE is returned if no "code" is present.
      60             :  *
      61             :  * @param json response to extract the error code from
      62             :  * @return the "code" value from @a json
      63             :  */
      64             : enum TALER_ErrorCode
      65          31 : TALER_JSON_get_error_code (const json_t *json)
      66             : {
      67             :   const json_t *jc;
      68             : 
      69          31 :   if (NULL == json)
      70             :   {
      71           0 :     GNUNET_break_op (0);
      72           0 :     return TALER_EC_INVALID_RESPONSE;
      73             :   }
      74          31 :   jc = json_object_get (json, "code");
      75          31 :   if (NULL == jc)
      76          19 :     return TALER_EC_NONE;
      77          12 :   if (json_is_integer (jc))
      78          12 :     return (enum TALER_ErrorCode) json_integer_value (jc);
      79           0 :   GNUNET_break_op (0);
      80           0 :   return TALER_EC_INVALID;
      81             : }
      82             : 
      83             : 
      84             : 
      85             : /* End of json/json.c */

Generated by: LCOV version 1.13