Changeset 35034 in project


Ignore:
Timestamp:
01/21/18 12:15:17 (7 months ago)
Author:
felix
Message:

z3 1.45: fixed z3:encode-buffer when dest buffer is too small (with assistance from Oskar + Peter)

Location:
release/4/z3
Files:
2 added
6 edited
1 copied

Legend:

Unmodified
Added
Removed
  • release/4/z3/tags/1.45/tests/run.scm

    r15109 r35034  
    5050 (test/equal "comparing results" (string=? BSD-LICENCE (get-output-string dest)) #t)
    5151
     52 (test-define "load jpeg" chip (with-input-from-string "chip.jpg" read-string))
     53 (test-define "compressed jpeg" chipc (z3:encode-buffer chip))
     54 (test-define "uncompressed jpeg" chip2 (z3:decode-buffer chipc))
     55 (test/equal "compare uncompressed jpeg to original" (string=? chip chip2) #t)
     56
    5257)
  • release/4/z3/tags/1.45/z3.scm

    r26427 r35034  
    1515(import scheme)
    1616(import chicken)
    17 (import (except foreign foreign-declare))
     17(import foreign)
    1818(require-extension data-structures ports posix bind)
    1919
     
    264264
    265265(bind* #<<EOF
    266 int encode_all(___scheme_pointer zh, ___scheme_pointer str, unsigned int start, unsigned int end,
    267                ___scheme_pointer dest, unsigned int destlen)
     266int encode_all(___scheme_pointer zh, ___scheme_pointer str,
     267               unsigned int start, unsigned int end,
     268               ___scheme_pointer dest, unsigned int dstart,
     269               unsigned int destlen, ___out unsigned int *dend,
     270               ___out unsigned int *startout)
    268271{
    269272  struct z3de_handle *z = (struct z3de_handle *)zh;
    270273  int len = end - start;
    271   char *ptr = dest;
     274  char *ptr = dest + dstart;
    272275  unsigned int taken, given;
     276  int r = 1;
    273277
    274278  do {
    275279    z = z3d_encode(z, str + start, len, &taken, ptr, destlen, &given);
     280
     281    if(destlen == 0 && given == 0 && taken == 0) {
     282        /* destination buffer not sufficient */
     283        r = 0;
     284        break;
     285    }
     286
    276287    start += taken;
    277288    destlen -= given;
     
    280291  } while(z != NULL);
    281292
    282   return (unsigned char *)ptr - (unsigned char *)dest;
     293  *dend = (unsigned char *)ptr - (unsigned char *)dest;
     294  *startout = start;
     295  return r;
    283296}
    284297EOF
     
    286299
    287300(define (z3:encode-buffer str #!optional (start 0) (end (string-length str)) odest)
    288   (let* ((dest (or odest (make-string (+ 4096 (- end start)))))
     301  ;; catch using old style API:
     302  (when odest
     303    (error 'z3:encode-buffer "expected at most 3 arguments but received 4"))
     304  (let* ((dest (make-string (+ 4096 (- end start))))
    289305         (dlen (string-length dest))
    290          (r (encode_all (z3:handle-buffer (z3:encode-init)) str start end dest dlen)) )
    291     (if odest
    292         r
    293         (substring dest 0 r) ) ) )
     306         (z3 (z3:encode-init)))
     307    (let loop ((start start) (to 0) (dest dest) (dlen dlen))
     308      (let-values (((r dend start2)
     309                    (encode_all (z3:handle-buffer z3)
     310                                str start end dest to dlen)))
     311        (if (zero? r)
     312            (let ((new (make-string (+ dlen (quotient dlen 2)))))
     313              (when (positive? dend)
     314                (##core#inline "C_substring_copy" dest new 0 dlen 0))
     315              (loop start2 dend new (string-length new)))
     316            (substring dest 0 dend) ) ) )))
     317
    294318(define (z3:encode-buffer-to-blob str #!optional (start 0) (end (string-length str)))
    295319  (let ((bv (z3:encode-buffer str start end)))
  • release/4/z3/tags/1.45/z3.setup

    r28849 r35034  
    77 'z3
    88 '("z3.so" "z3.import.so")
    9  '((version 1.44)))
     9 '((version 1.45)))
  • release/4/z3/trunk/tests/run.scm

    r15109 r35034  
    5050 (test/equal "comparing results" (string=? BSD-LICENCE (get-output-string dest)) #t)
    5151
     52 (test-define "load jpeg" chip (with-input-from-string "chip.jpg" read-string))
     53 (test-define "compressed jpeg" chipc (z3:encode-buffer chip))
     54 (test-define "uncompressed jpeg" chip2 (z3:decode-buffer chipc))
     55 (test/equal "compare uncompressed jpeg to original" (string=? chip chip2) #t)
     56
    5257)
  • release/4/z3/trunk/z3.scm

    r26427 r35034  
    1515(import scheme)
    1616(import chicken)
    17 (import (except foreign foreign-declare))
     17(import foreign)
    1818(require-extension data-structures ports posix bind)
    1919
     
    264264
    265265(bind* #<<EOF
    266 int encode_all(___scheme_pointer zh, ___scheme_pointer str, unsigned int start, unsigned int end,
    267                ___scheme_pointer dest, unsigned int destlen)
     266int encode_all(___scheme_pointer zh, ___scheme_pointer str,
     267               unsigned int start, unsigned int end,
     268               ___scheme_pointer dest, unsigned int dstart,
     269               unsigned int destlen, ___out unsigned int *dend,
     270               ___out unsigned int *startout)
    268271{
    269272  struct z3de_handle *z = (struct z3de_handle *)zh;
    270273  int len = end - start;
    271   char *ptr = dest;
     274  char *ptr = dest + dstart;
    272275  unsigned int taken, given;
     276  int r = 1;
    273277
    274278  do {
    275279    z = z3d_encode(z, str + start, len, &taken, ptr, destlen, &given);
     280
     281    if(destlen == 0 && given == 0 && taken == 0) {
     282        /* destination buffer not sufficient */
     283        r = 0;
     284        break;
     285    }
     286
    276287    start += taken;
    277288    destlen -= given;
     
    280291  } while(z != NULL);
    281292
    282   return (unsigned char *)ptr - (unsigned char *)dest;
     293  *dend = (unsigned char *)ptr - (unsigned char *)dest;
     294  *startout = start;
     295  return r;
    283296}
    284297EOF
     
    286299
    287300(define (z3:encode-buffer str #!optional (start 0) (end (string-length str)) odest)
    288   (let* ((dest (or odest (make-string (+ 4096 (- end start)))))
     301  ;; catch using old style API:
     302  (when odest
     303    (error 'z3:encode-buffer "expected at most 3 arguments but received 4"))
     304  (let* ((dest (make-string (+ 4096 (- end start))))
    289305         (dlen (string-length dest))
    290          (r (encode_all (z3:handle-buffer (z3:encode-init)) str start end dest dlen)) )
    291     (if odest
    292         r
    293         (substring dest 0 r) ) ) )
     306         (z3 (z3:encode-init)))
     307    (let loop ((start start) (to 0) (dest dest) (dlen dlen))
     308      (let-values (((r dend start2)
     309                    (encode_all (z3:handle-buffer z3)
     310                                str start end dest to dlen)))
     311        (if (zero? r)
     312            (let ((new (make-string (+ dlen (quotient dlen 2)))))
     313              (when (positive? dend)
     314                (##core#inline "C_substring_copy" dest new 0 dlen 0))
     315              (loop start2 dend new (string-length new)))
     316            (substring dest 0 dend) ) ) )))
     317
    294318(define (z3:encode-buffer-to-blob str #!optional (start 0) (end (string-length str)))
    295319  (let ((bv (z3:encode-buffer str start end)))
  • release/4/z3/trunk/z3.setup

    r28849 r35034  
    77 'z3
    88 '("z3.so" "z3.import.so")
    9  '((version 1.44)))
     9 '((version 1.45)))
Note: See TracChangeset for help on using the changeset viewer.