Austin Schuh | dace2a6 | 2020-08-18 10:56:48 -0700 | [diff] [blame] | 1 | /* __gmp_sprintf_funs -- support for gmp_sprintf and gmp_vsprintf. |
| 2 | |
| 3 | THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY. THEY'RE ALMOST |
| 4 | CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN |
| 5 | FUTURE GNU MP RELEASES. |
| 6 | |
| 7 | Copyright 2001 Free Software Foundation, Inc. |
| 8 | |
| 9 | This file is part of the GNU MP Library. |
| 10 | |
| 11 | The GNU MP Library is free software; you can redistribute it and/or modify |
| 12 | it under the terms of either: |
| 13 | |
| 14 | * the GNU Lesser General Public License as published by the Free |
| 15 | Software Foundation; either version 3 of the License, or (at your |
| 16 | option) any later version. |
| 17 | |
| 18 | or |
| 19 | |
| 20 | * the GNU General Public License as published by the Free Software |
| 21 | Foundation; either version 2 of the License, or (at your option) any |
| 22 | later version. |
| 23 | |
| 24 | or both in parallel, as here. |
| 25 | |
| 26 | The GNU MP Library is distributed in the hope that it will be useful, but |
| 27 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 28 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 29 | for more details. |
| 30 | |
| 31 | You should have received copies of the GNU General Public License and the |
| 32 | GNU Lesser General Public License along with the GNU MP Library. If not, |
| 33 | see https://www.gnu.org/licenses/. */ |
| 34 | |
| 35 | #include <stdarg.h> |
| 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | |
| 40 | #include "gmp-impl.h" |
| 41 | |
| 42 | |
| 43 | /* The data parameter "bufp" points to a "char *buf" which is the next |
| 44 | character to be written, having started as the destination from the |
| 45 | application. This is then increased each time output is produced. */ |
| 46 | |
| 47 | |
| 48 | /* If vsprintf returns -1 then pass it upwards. It doesn't matter that |
| 49 | "*bufp" is ruined in this case, since gmp_doprint will bail out |
| 50 | immediately anyway. */ |
| 51 | static int |
| 52 | gmp_sprintf_format (char **bufp, const char *fmt, va_list ap) |
| 53 | { |
| 54 | char *buf = *bufp; |
| 55 | int ret; |
| 56 | vsprintf (buf, fmt, ap); |
| 57 | ret = strlen (buf); |
| 58 | *bufp = buf + ret; |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | static int |
| 63 | gmp_sprintf_memory (char **bufp, const char *str, size_t len) |
| 64 | { |
| 65 | char *buf = *bufp; |
| 66 | *bufp = buf + len; |
| 67 | memcpy (buf, str, len); |
| 68 | return len; |
| 69 | } |
| 70 | |
| 71 | static int |
| 72 | gmp_sprintf_reps (char **bufp, int c, int reps) |
| 73 | { |
| 74 | char *buf = *bufp; |
| 75 | ASSERT (reps >= 0); |
| 76 | *bufp = buf + reps; |
| 77 | memset (buf, c, reps); |
| 78 | return reps; |
| 79 | } |
| 80 | |
| 81 | static int |
| 82 | gmp_sprintf_final (char **bufp) |
| 83 | { |
| 84 | char *buf = *bufp; |
| 85 | *buf = '\0'; |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | const struct doprnt_funs_t __gmp_sprintf_funs = { |
| 90 | (doprnt_format_t) gmp_sprintf_format, |
| 91 | (doprnt_memory_t) gmp_sprintf_memory, |
| 92 | (doprnt_reps_t) gmp_sprintf_reps, |
| 93 | (doprnt_final_t) gmp_sprintf_final |
| 94 | }; |