From 507658bd54e638a02df562be38dd9dbdf17082f2 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 16 Sep 2008 12:33:40 +0000 Subject: * string.c (rb_str_crypt): orthodox crypt() sees only first two bytes of salt. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19383 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- string.c | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) (limited to 'string.c') diff --git a/string.c b/string.c index 6308ff541a..dafabe43e3 100644 --- a/string.c +++ b/string.c @@ -5539,7 +5539,7 @@ rb_str_each_char(VALUE str) static VALUE rb_str_each_codepoint(VALUE str) { - int i, len, n; + int len, n; unsigned int c; const char *ptr, *end; rb_encoding *enc; @@ -6099,30 +6099,27 @@ rb_str_crypt(VALUE str, VALUE salt) { extern char *crypt(const char *, const char *); VALUE result; - const char *s; + const char *s, *saltp; #ifdef BROKEN_CRYPT - VALUE salt_8bit_clean; - rb_encoding *enc; + char salt_8bit_clean[3]; #endif StringValue(salt); if (RSTRING_LEN(salt) < 2) rb_raise(rb_eArgError, "salt too short (need >=2 bytes)"); - if (RSTRING_PTR(str)) s = RSTRING_PTR(str); - else s = ""; + s = RSTRING_PTR(str); + if (!s) s = ""; + saltp = RSTRING_PTR(salt); #ifdef BROKEN_CRYPT - salt_8bit_clean = rb_str_dup(salt); - enc = rb_ascii8bit_encoding(); - str_modifiable(salt_8bit_clean); - rb_enc_associate(salt_8bit_clean, enc); - salt_8bit_clean = rb_str_tr(salt_8bit_clean, - rb_enc_str_new("\x80-\xFF", 3, enc), - rb_usascii_str_new("\x00-\x7F", 3)); - result = rb_str_new2(crypt(s, RSTRING_PTR(salt_8bit_clean))); -#else - result = rb_str_new2(crypt(s, RSTRING_PTR(salt))); + if (!ISASCII((unsigned char)saltp[0]) || !ISASCII((unsigned char)saltp[1])) { + salt_8bit_clean[0] = saltp[0] & 0x7f; + salt_8bit_clean[1] = saltp[1] & 0x7f; + salt_8bit_clean[2] = '\0'; + saltp = salt_8bit_clean; + } #endif + result = rb_str_new2(crypt(s, saltp)); OBJ_INFECT(result, str); OBJ_INFECT(result, salt); return result; -- cgit v1.2.3