Edit

kc3-lang/libtommath/bn_mp_fwrite.c

Branch :

  • Show log

    Commit

  • Author : Daniel Mendler
    Date : 2019-05-11 10:22:20
    Hash : 61d9e528
    Message : hardening: add MP_ZERO_BUFFER, MP_ZERO_DIGITS * (!) Always zero buffer before freeing if MP_NO_ZERO_ON_FREE is not defined * Add MP_NO_ZERO_ON_FREE to disable hardening * Add MP_ZERO_BUFFER, MP_ZERO_DIGITS, MP_FREE_BUFFFER, MP_FREE_DIGITS * Never use MP_FREE directly, use MP_FREE_DIGITS or MP_FREE_BUFFER * Add MP_USE_MEMSET to use memset instead of loop * Disable astyle backups which are annonying in the times of git

  • bn_mp_fwrite.c
  • #include "tommath_private.h"
    #ifdef BN_MP_FWRITE_C
    /* LibTomMath, multiple-precision integer library -- Tom St Denis */
    /* SPDX-License-Identifier: Unlicense */
    
    #ifndef MP_NO_FILE
    int mp_fwrite(const mp_int *a, int radix, FILE *stream)
    {
       char *buf;
       int err, len, x;
    
       if ((err = mp_radix_size(a, radix, &len)) != MP_OKAY) {
          return err;
       }
    
       buf = (char *) MP_MALLOC((size_t)len);
       if (buf == NULL) {
          return MP_MEM;
       }
    
       if ((err = mp_toradix(a, buf, radix)) != MP_OKAY) {
          MP_FREE_BUFFER(buf, (size_t)len);
          return err;
       }
    
       for (x = 0; x < len; x++) {
          if (fputc((int)buf[x], stream) == EOF) {
             MP_FREE_BUFFER(buf, (size_t)len);
             return MP_VAL;
          }
       }
    
       MP_FREE_BUFFER(buf, (size_t)len);
       return MP_OKAY;
    }
    #endif
    
    #endif