unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Eli Zaretskii <eliz@gnu.org>
To: Juri Linkov <juri@linkov.net>
Cc: larsi@gnus.org, schwab@linux-m68k.org, 38587@debbugs.gnu.org
Subject: bug#38587: base64-decode-region breaks encoding
Date: Mon, 16 Dec 2019 17:58:29 +0200	[thread overview]
Message-ID: <835zig5kka.fsf@gnu.org> (raw)
In-Reply-To: <87zhft9rl4.fsf@mail.linkov.net> (message from Juri Linkov on Mon, 16 Dec 2019 00:40:55 +0200)

> From: Juri Linkov <juri@linkov.net>
> Date: Mon, 16 Dec 2019 00:40:55 +0200
> Cc: Lars Ingebrigtsen <larsi@gnus.org>, 38587@debbugs.gnu.org
> 
> > BASE64 is defined on a sequence of bytes.  It doesn't make sense to
> > apply it to characters.
> 
> But isn't UTF-8 a multibyte encoding represented by a sequence of bytes
> (e.g. when saved to a file)?

When saved to a file, yes.

> Then why base64-encode-region couldn't use the buffer's coding
> to convert the region to a sequence of bytes?

Because it isn't guaranteed that the buffer's encoding is indeed the
right one for this job.

> Also why base64-encode-region accepts region's characters
> only from the charsets ‘eight-bit-control’ and ‘eight-bit-graphic’,
> but not other UTF-8 characters?

Because it wants raw bytes, and only eight-bit charsets fit that
condition.  Eight-bit charset is the charset of raw bytes in a
multibyte buffer or string.

(base64-encode-region can also work on unibyte buffers and strings, in
which case "charset" of such "text" has no meaning.)

> > The input of base64-encode-region needs to be encoded into bytes and the
> > output of base64-decode-region needs to be decoded into characters.  If
> > you do that, you get a full reversible operation.
> 
> I guess base64-encode-region already encodes the region into bytes,
> but only partially - it signals an error on some characters,
> I don't understand why it can't encode all of them.

Once again, because it wants to process only raw bytes.

> But is it still possible to tell base64-decode-region
> about the expected output coding system?  Maybe using
> a prefix arg: C-u M-x base64-decode-region could ask
> for a coding, defaulting to the buffer's coding.

If we want to make such a change, then "C-x RET c" is a better prefix
command, as it is consistent with other commands that accept
coding-system overrides.

> Is there an equivalent of force_encoding('UTF-8') in Emacs?

"C-x RET c utf-8 RET M-x SOME-COMMAND RET"

> Also this doesn't work on the string output:
> 
>   (decode-coding-string (base64-decode-string (base64-encode-string "ä"))
>                         'utf-8)

It will work if you encode "ä" first:

  (decode-coding-string (base64-decode-string
                         (base64-encode-string
			  (encode-coding-string "ä" 'utf-8)))
			'utf-8)





  reply	other threads:[~2019-12-16 15:58 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-12 23:55 bug#38587: base64-decode-region breaks encoding Juri Linkov
2019-12-13  2:52 ` Lars Ingebrigtsen
2019-12-13  7:12   ` Eli Zaretskii
2019-12-14 23:31   ` Juri Linkov
2019-12-15  8:56     ` Andreas Schwab
2019-12-15 22:40       ` Juri Linkov
2019-12-16 15:58         ` Eli Zaretskii [this message]
2019-12-16 21:51           ` Juri Linkov
2019-12-17 16:04             ` Eli Zaretskii
2019-12-17 23:10               ` Juri Linkov
2019-12-24 15:37                 ` Lars Ingebrigtsen
2019-12-24 16:13                   ` Lars Ingebrigtsen
2019-12-16 16:18         ` Andreas Schwab
2019-12-17 16:27         ` Lars Ingebrigtsen
2019-12-15 15:26     ` Eli Zaretskii
2019-12-15 22:41       ` Juri Linkov
2019-12-16  3:28         ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=835zig5kka.fsf@gnu.org \
    --to=eliz@gnu.org \
    --cc=38587@debbugs.gnu.org \
    --cc=juri@linkov.net \
    --cc=larsi@gnus.org \
    --cc=schwab@linux-m68k.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).