/security/nss/lib/freebl/mpi/utils/gcd.c

http://github.com/zpao/v8monkey · C · 119 lines · 59 code · 18 blank · 42 comment · 11 complexity · 37a24a192869ec799ee4c8978d9373ed MD5 · raw file

  1. /*
  2. * gcd.c
  3. *
  4. * Greatest common divisor
  5. *
  6. * ***** BEGIN LICENSE BLOCK *****
  7. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  8. *
  9. * The contents of this file are subject to the Mozilla Public License Version
  10. * 1.1 (the "License"); you may not use this file except in compliance with
  11. * the License. You may obtain a copy of the License at
  12. * http://www.mozilla.org/MPL/
  13. *
  14. * Software distributed under the License is distributed on an "AS IS" basis,
  15. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  16. * for the specific language governing rights and limitations under the
  17. * License.
  18. *
  19. * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
  20. *
  21. * The Initial Developer of the Original Code is
  22. * Michael J. Fromberger.
  23. * Portions created by the Initial Developer are Copyright (C) 1998
  24. * the Initial Developer. All Rights Reserved.
  25. *
  26. * Contributor(s):
  27. *
  28. * Alternatively, the contents of this file may be used under the terms of
  29. * either the GNU General Public License Version 2 or later (the "GPL"), or
  30. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  31. * in which case the provisions of the GPL or the LGPL are applicable instead
  32. * of those above. If you wish to allow use of your version of this file only
  33. * under the terms of either the GPL or the LGPL, and not to allow others to
  34. * use your version of this file under the terms of the MPL, indicate your
  35. * decision by deleting the provisions above and replace them with the notice
  36. * and other provisions required by the GPL or the LGPL. If you do not delete
  37. * the provisions above, a recipient may use your version of this file under
  38. * the terms of any one of the MPL, the GPL or the LGPL.
  39. *
  40. * ***** END LICENSE BLOCK ***** */
  41. /* $Id: gcd.c,v 1.3 2004/04/27 23:04:37 gerv%gerv.net Exp $ */
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include "mpi.h"
  46. char *g_prog = NULL;
  47. void print_mp_int(mp_int *mp, FILE *ofp);
  48. int main(int argc, char *argv[])
  49. {
  50. mp_int a, b, x, y;
  51. mp_err res;
  52. int ext = 0;
  53. g_prog = argv[0];
  54. if(argc < 3) {
  55. fprintf(stderr, "Usage: %s <a> <b>\n", g_prog);
  56. return 1;
  57. }
  58. mp_init(&a); mp_read_radix(&a, argv[1], 10);
  59. mp_init(&b); mp_read_radix(&b, argv[2], 10);
  60. /* If we were called 'xgcd', compute x, y so that g = ax + by */
  61. if(strcmp(g_prog, "xgcd") == 0) {
  62. ext = 1;
  63. mp_init(&x); mp_init(&y);
  64. }
  65. if(ext) {
  66. if((res = mp_xgcd(&a, &b, &a, &x, &y)) != MP_OKAY) {
  67. fprintf(stderr, "%s: error: %s\n", g_prog, mp_strerror(res));
  68. mp_clear(&a); mp_clear(&b);
  69. mp_clear(&x); mp_clear(&y);
  70. return 1;
  71. }
  72. } else {
  73. if((res = mp_gcd(&a, &b, &a)) != MP_OKAY) {
  74. fprintf(stderr, "%s: error: %s\n", g_prog,
  75. mp_strerror(res));
  76. mp_clear(&a); mp_clear(&b);
  77. return 1;
  78. }
  79. }
  80. print_mp_int(&a, stdout);
  81. if(ext) {
  82. fputs("x = ", stdout); print_mp_int(&x, stdout);
  83. fputs("y = ", stdout); print_mp_int(&y, stdout);
  84. }
  85. mp_clear(&a); mp_clear(&b);
  86. if(ext) {
  87. mp_clear(&x);
  88. mp_clear(&y);
  89. }
  90. return 0;
  91. }
  92. void print_mp_int(mp_int *mp, FILE *ofp)
  93. {
  94. char *buf;
  95. int len;
  96. len = mp_radix_size(mp, 10);
  97. buf = calloc(len, sizeof(char));
  98. mp_todecimal(mp, buf);
  99. fprintf(ofp, "%s\n", buf);
  100. free(buf);
  101. }