guix-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

01/03: file-systems: Fix UTF-16 handling in initrd.


From: guix-commits
Subject: 01/03: file-systems: Fix UTF-16 handling in initrd.
Date: Sun, 3 May 2020 17:05:49 -0400 (EDT)

dannym pushed a commit to branch master
in repository guix.

commit bb357c509e1c017e1fef5aa5f4d05beea0c25157
Author: Danny Milosavljevic <address@hidden>
AuthorDate: Sun May 3 21:40:04 2020 +0200

    file-systems: Fix UTF-16 handling in initrd.
    
    Follow-up to f73f4b3a2d7a313a6cb1667bd69205ea4b09f57c.
    
    * gnu/build/file-systems.scm (bytevector->u16-list): New procedure.
    (utf16->string): New procedure.
---
 gnu/build/file-systems.scm | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/gnu/build/file-systems.scm b/gnu/build/file-systems.scm
index 4ac672d..b920e8f 100644
--- a/gnu/build/file-systems.scm
+++ b/gnu/build/file-systems.scm
@@ -110,6 +110,31 @@ NUL terminator, return the size of the bytevector."
               (loop (+ index 2)))
           length))))
 
+(define* (bytevector->u16-list bv endianness #:optional (index 0))
+  (if (< index (bytevector-length bv))
+      (cons (bytevector-u16-ref bv index endianness)
+            (bytevector->u16-list bv endianness (+ index 2)))
+      '()))
+
+;; The initrd doesn't have iconv data, so do the conversion ourselves.
+(define (utf16->string bv endianness)
+  (list->string
+   (map integer->char
+        (reverse
+         (let loop ((remainder (bytevector->u16-list bv endianness))
+                    (result '()))
+             (match remainder
+              (() result)
+              ((a) (cons a result))
+              ((a b x ...)
+               (if (and (>= a #xD800) (< a #xDC00) ; high surrogate
+                        (>= b #xDC00) (< b #xE000)) ; low surrogate
+                   (loop x (cons (+ #x10000
+                                    (* #x400 (- a #xD800))
+                                    (- b #xDC00))
+                                 result))
+                   (loop (cons b x) (cons a result))))))))))
+
 (define (null-terminated-utf16->string bv endianness)
   (utf16->string (sub-bytevector bv 0 (bytevector-utf16-length bv))
                  endianness))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]