taler-xotp_fw

xOTP generator firmware
Log | Files | Refs | Submodules | README

talerAmount.c (2262B)


      1 /**
      2  * @file talerAmount.c
      3  * @author Adrian STEINER (steia19@bfh.ch)
      4  * @brief Help functions to handle the talerAmount data structures
      5  * @version 0.1
      6  * @date 19-02-2025
      7  *
      8  * @copyright (C) 2025 Adrian STEINER
      9  * This program is free software: you can redistribute it and/or modify
     10  * it under the terms of the GNU General Public License as published by
     11  * the Free Software Foundation, either version 3 of the License, or
     12  * (at your option) any later version.
     13  *
     14  * This program is distributed in the hope that it will be useful,
     15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17  * GNU General Public License for more details.
     18  *
     19  * You should have received a copy of the GNU General Public License
     20  * along with this program.  If not, see <https: //www.gnu.org/licenses/>.
     21  *
     22  */
     23 
     24 #include "talerAmount.h"
     25 
     26 #include <stddef.h>
     27 #include <string.h>
     28 
     29 #include "byteOrder.h"
     30 
     31 void TALER_amount2AmountNBO(TALER_AmountNBO *target,
     32                             const TALER_xData *amountInfo,
     33                             const uint64_t enteredAmount)
     34 {
     35   if (NULL == target || NULL == amountInfo) {
     36     return;
     37   }
     38   if (amountInfo->fraction > 0) {
     39     uint64_t amountValue = enteredAmount / amountInfo->fraction;
     40     uint32_t amountFraction = (enteredAmount % amountInfo->fraction) *
     41                               (TALER_AMOUNT_FRAC_BASE / amountInfo->fraction);
     42     target->value = h2n64(amountValue);
     43     target->fraction = h2n32(amountFraction);
     44   } else {
     45     target->value = h2n64(enteredAmount);
     46     target->fraction = 0;
     47   }
     48   memcpy(target->currency, amountInfo->currency, TALER_CURRENCY_LEN);
     49 }
     50 
     51 void TALER_xDataInit(TALER_xData *dataHandler,
     52                      const uint8_t *currency,
     53                      uint32_t fraction,
     54                      const uint8_t *templateName)
     55 {
     56   if (NULL != dataHandler) {
     57     strncpy((char *)dataHandler->currency, (const char *)currency,
     58             TALER_CURRENCY_LEN);
     59     dataHandler->fraction = fraction;
     60     if (templateName) {
     61       strncpy((char *)dataHandler->merchantTemplateName, (char *)templateName,
     62               XTOTP_MERCHANT_TEMPLATE_SIZE);
     63     } else {
     64       dataHandler->merchantTemplateName[0] = '\0';
     65     }
     66   }
     67 }