aboutsummaryrefslogtreecommitdiffstats
path: root/test/monniaux/uzlib/src/nasm/tinfzlib.nas
blob: 1f9519eb9d666aba1aaaa5cdcdfa232170f393c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
;;
;; tinfzlib  -  tiny zlib uncompress
;;
;; Copyright (c) 2003 by Joergen Ibsen / Jibz
;; All Rights Reserved
;;
;; http://www.ibsensoftware.com/
;;
;; This software is provided 'as-is', without any express
;; or implied warranty.  In no event will the authors be
;; held liable for any damages arising from the use of
;; this software.
;;
;; Permission is granted to anyone to use this software
;; for any purpose, including commercial applications,
;; and to alter it and redistribute it freely, subject to
;; the following restrictions:
;;
;; 1. The origin of this software must not be
;;    misrepresented; you must not claim that you
;;    wrote the original software. If you use this
;;    software in a product, an acknowledgment in
;;    the product documentation would be appreciated
;;    but is not required.
;;
;; 2. Altered source versions must be plainly marked
;;    as such, and must not be misrepresented as
;;    being the original software.
;;
;; 3. This notice may not be removed or altered from
;;    any source distribution.
;;

TINF_OK         equ  0
TINF_DATA_ERROR equ (-3)

cpu 386

bits 32

%include "nasmlcm.inc"

section lcmtext

lcmglobal tinf_zlib_uncompress,16

lcmexport tinf_zlib_uncompress,16

lcmextern tinf_uncompress,16
lcmextern tinf_adler32,8

; =============================================================

lcmlabel tinf_zlib_uncompress,16
    ; tinf_zlib_uncompress(void *dest,
    ;                      unsigned int *destLen,
    ;                      const void *source,
    ;                      unsigned int sourceLen)

    .slen$ equ 2*4 + 4 + 12
    .src$  equ 2*4 + 4 + 8
    .dlen$ equ 2*4 + 4 + 4
    .dst$  equ 2*4 + 4

    push   esi
    push   ebx

    mov    esi, [esp + .src$]  ; esi -> source

    ; -- get header bytes --

    movzx  eax, word [esi]    ; al = cmf, ah = flg,

    ; -- check format --

    ; check method is deflate
    ; if ((cmf & 0x0f) != 8) return TINF_DATA_ERROR;
    mov    cl, 0x0f
    and    cl, al
    cmp    cl, 8
    jne    short .return_error

    ; check window size is valid
    ; if ((cmf >> 4) > 7) return TINF_DATA_ERROR;
    mov    ch, al
    shr    ch, 4
    cmp    ch, cl             ; cl = 8 from above
    jae    short .return_error

    ; check there is no preset dictionary
    ; if (flg & 0x20) return TINF_DATA_ERROR;
    test   ah, 0x20
    jnz    short .return_error

    ; check checksum
    ; if ((256*cmf + flg) % 31) return TINF_DATA_ERROR;
    xchg   al, ah
    xor    edx, edx
    lea    ebx, [edx + 31]
    div    ebx
    test   edx, edx
    jnz    short .return_error

    ; -- get adler32 checksum --

    mov    ecx, [esp + .slen$] ; ecx =  sourceLen
    mov    ebx, [esi + ecx - 4]

  %ifdef BSWAP_OK
    bswap  ebx
  %else ; BSWAP_OK
    xchg   bl, bh
    rol    ebx, 16
    xchg   bl, bh
  %endif ; BSWAP_OK

    ; -- inflate --

    ; res = tinf_uncompress(dst, destLen, src + 2, sourceLen - 6);
    lea    eax, [ecx - 6]
    push   eax
    lea    eax, [esi + 2]
    push   eax
    push   dword [esp + 8 + .dlen$]
    push   dword [esp + 12 + .dst$]
    call   tinf_uncompress
    add    esp, byte 16

    ; if (res != TINF_OK) return TINF_DATA_ERROR;
    test   eax, eax
    jnz    short .return_error

    ; -- check adler32 checksum --

    ; if (a32 != tinf_adler32(dst, *destLen)) return TINF_DATA_ERROR;
    mov    eax, [esp + .dlen$];
    push   dword [eax]
    push   dword [esp + 4 + .dst$]
    call   tinf_adler32
    add    esp, byte 8

    sub    eax, ebx
    jz     short .return_eax

  .return_error:
    mov    eax, TINF_DATA_ERROR

  .return_eax:
    pop    ebx
    pop    esi

    lcmret 16

; =============================================================

%ifdef _OBJ_
  section lcmdata
%endif

; =============================================================