aboutsummaryrefslogtreecommitdiffstats
path: root/src/fns.c
diff options
context:
space:
mode:
authorGerd Moellmann2000-11-30 13:46:42 +0000
committerGerd Moellmann2000-11-30 13:46:42 +0000
commit57916a7a871972cb7e39981925db3fe124fb44eb (patch)
tree5ed52a0c3480116245ba9874003fe9ce8f66f152 /src/fns.c
parentd06b86880df06442b9681d647e92ef029d20e8e5 (diff)
downloademacs-57916a7a871972cb7e39981925db3fe124fb44eb.tar.gz
emacs-57916a7a871972cb7e39981925db3fe124fb44eb.zip
(Fmd5): New function.
(syms_of_fns): Defsubr md5.
Diffstat (limited to 'src/fns.c')
-rw-r--r--src/fns.c164
1 files changed, 163 insertions, 1 deletions
diff --git a/src/fns.c b/src/fns.c
index b09008f7586..f9dea02a491 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -1,5 +1,6 @@
1/* Random utility Lisp functions. 1/* Random utility Lisp functions.
2 Copyright (C) 1985, 86, 87, 93, 94, 95, 97, 98, 99, 2000 Free Software Foundation, Inc. 2 Copyright (C) 1985, 86, 87, 93, 94, 95, 97, 98, 99, 2000
3 Free Software Foundation, Inc.
3 4
4This file is part of GNU Emacs. 5This file is part of GNU Emacs.
5 6
@@ -4964,6 +4965,166 @@ integers, including negative integers.")
4964 4965
4965 4966
4966 4967
4968
4969
4970
4971#include "md5.h"
4972
4973DEFUN ("md5", Fmd5, Smd5, 1, 5, 0,
4974 "Return MD5 hash of OBJECT, a buffer or string.\n\
4975\n\
4976The two optional arguments START and END are character positions;\n\
4977they can be in either order.\n\
4978\n\
4979The third optional argument CODING-SYSTEM specify coding system text\n\
4980should be converted to before computing digest. If nil, uses the\n\
4981current format or guesses.\n\
4982\n\
4983Fourth optional argument NOERROR doesn't do anything (for XEmacs\n\
4984compatibility).")
4985 (object, start, end, coding_system, noerror)
4986 Lisp_Object object, start, end, coding_system, noerror;
4987{
4988 unsigned char digest[16];
4989 unsigned char value[33];
4990 int i;
4991 int size;
4992 int size_byte = 0;
4993 int start_char = 0, end_char = 0;
4994 int start_byte = 0, end_byte = 0;
4995 register int b, e;
4996 register struct buffer *bp;
4997 int temp;
4998
4999 if (STRINGP(object))
5000 {
5001 if (NILP (coding_system))
5002 {
5003 /* we should guess coding system */
5004 if (STRING_MULTIBYTE (object))
5005 {
5006 /* we make a unibyte string and guess it's coding system
5007 (is this correct?) */
5008 object = string_make_unibyte (object);
5009 coding_system = detect_coding_system
5010 (XSTRING(object)->data, STRING_BYTES(XSTRING (object)), 1);
5011 }
5012 else
5013 {
5014 /* guess coding system */
5015 coding_system = detect_coding_system
5016 (XSTRING(object)->data, STRING_BYTES(XSTRING (object)), 1);
5017 }
5018
5019 /* encode unibyte string into desired coding system
5020 (yes encoding functions handle unibyte source) */
5021 object = code_convert_string1 (object, coding_system, Qnil, 1);
5022 }
5023 else
5024 {
5025 /* convert string into given coding system */
5026 if (STRING_MULTIBYTE (object))
5027 {
5028 /* just encode it */
5029 object = code_convert_string1 (object, coding_system, Qnil, 1);
5030 } else {
5031 /* assume string is encoded */
5032 }
5033 }
5034
5035 size = XSTRING (object)->size;
5036 size_byte = STRING_BYTES (XSTRING (object));
5037
5038 if (!NILP (start))
5039 {
5040 CHECK_NUMBER (start, 1);
5041
5042 start_char = XINT (start);
5043
5044 if (start_char < 0)
5045 start_char += size;
5046
5047 start_byte = string_char_to_byte (object, start_char);
5048 }
5049
5050 if (NILP (end))
5051 {
5052 end_char = size;
5053 end_byte = size_byte;
5054 }
5055 else
5056 {
5057 CHECK_NUMBER (end, 2);
5058
5059 end_char = XINT (end);
5060
5061 if (end_char < 0)
5062 end_char += size;
5063
5064 end_byte = string_char_to_byte (object, end_char);
5065 }
5066
5067 if (!(0 <= start_char && start_char <= end_char && end_char <= size))
5068 args_out_of_range_3 (object, make_number (start_char),
5069 make_number (end_char));
5070 }
5071 else
5072 {
5073 CHECK_BUFFER(object, 0);
5074
5075 bp = XBUFFER (object);
5076
5077 if (NILP (start))
5078 b = BUF_BEGV (bp);
5079 else
5080 {
5081 CHECK_NUMBER_COERCE_MARKER (start, 0);
5082 b = XINT (start);
5083 }
5084
5085 if (NILP (end))
5086 e = BUF_ZV (bp);
5087 else
5088 {
5089 CHECK_NUMBER_COERCE_MARKER (end, 1);
5090 e = XINT (end);
5091 }
5092
5093 if (b > e)
5094 temp = b, b = e, e = temp;
5095
5096 if (!(BUF_BEGV (bp) <= b && e <= BUF_ZV (bp)))
5097 args_out_of_range (start, end);
5098
5099 if (NILP (coding_system))
5100 {
5101 /* we should guess coding system of buffer */
5102 coding_system = XBUFFER (object)->buffer_file_coding_system;
5103 if (NILP (coding_system))
5104 {
5105 /* xxx this can (and should) be handled. I do not know how. */
5106 Fsignal (Qerror,
5107 Fcons (build_string ("No coding system found"), Qnil));
5108 }
5109 }
5110
5111 object = make_buffer_string (b, e, 0);
5112
5113 if (STRING_MULTIBYTE (object))
5114 object = code_convert_string1 (object, coding_system, Qnil, 1);
5115 }
5116
5117 md5_buffer (XSTRING(object)->data + start_byte,
5118 STRING_BYTES(XSTRING (object)) - (size_byte - end_byte),
5119 digest);
5120
5121 for (i = 0; i < 16; i++)
5122 sprintf (&value[2*i], "%02x", digest[i]);
5123 value[32] = '\0';
5124
5125 return make_string (value, 32);
5126}
5127
4967 5128
4968void 5129void
4969syms_of_fns () 5130syms_of_fns ()
@@ -5111,6 +5272,7 @@ invoked by mouse clicks and mouse menu items.");
5111 defsubr (&Sbase64_decode_region); 5272 defsubr (&Sbase64_decode_region);
5112 defsubr (&Sbase64_encode_string); 5273 defsubr (&Sbase64_encode_string);
5113 defsubr (&Sbase64_decode_string); 5274 defsubr (&Sbase64_decode_string);
5275 defsubr (&Smd5);
5114} 5276}
5115 5277
5116 5278