1305
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
1 /******************************************************************************
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
2 * *
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
3 * Copyright (C) 1992-1995 Tony Robinson *
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
4 * *
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
5 * See the file doc/LICENSE.shorten for conditions on distribution and usage *
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
6 * *
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
7 ******************************************************************************/
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
8
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
9 /*
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
10 * $Id: vario.c,v 1.10 2004/05/04 02:26:36 jason Exp $
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
11 */
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
12
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
13 #include <math.h>
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
14 #include <stdio.h>
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
15 #include <stdlib.h>
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
16 #include "shorten.h"
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
17
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
18 #define MASKTABSIZE 33
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
19 ulong masktab[MASKTABSIZE];
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
20
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
21 void mkmasktab() {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
22 int i;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
23 ulong val = 0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
24
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
25 masktab[0] = val;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
26 for(i = 1; i < MASKTABSIZE; i++) {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
27 val <<= 1;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
28 val |= 1;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
29 masktab[i] = val;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
30 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
31 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
32
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
33 void var_get_init(shn_file *this_shn)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
34 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
35 mkmasktab();
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
36
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
37 this_shn->decode_state->getbuf = (uchar*) pmalloc((ulong) BUFSIZ,this_shn);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
38 this_shn->decode_state->getbufp = this_shn->decode_state->getbuf;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
39 this_shn->decode_state->nbyteget = 0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
40 this_shn->decode_state->gbuffer = 0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
41 this_shn->decode_state->nbitget = 0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
42 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
43
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
44 ulong word_get(shn_file *this_shn)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
45 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
46 ulong buffer;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
47 int bytes;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
48
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
49 if(this_shn->decode_state->nbyteget < 4)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
50 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
51 this_shn->vars.last_file_position = this_shn->vars.bytes_read;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
52
|
1978
|
53 bytes = aud_vfs_fread((uchar*) this_shn->decode_state->getbuf, 1, BUFSIZ, this_shn->vars.fd);
|
1305
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
54 this_shn->decode_state->nbyteget += bytes;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
55
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
56 if(this_shn->decode_state->nbyteget < 4) {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
57 shn_error_fatal(this_shn,"Premature EOF on compressed stream -\npossible corrupt or truncated file");
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
58 return (ulong)0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
59 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
60
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
61 this_shn->vars.bytes_read += bytes;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
62
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
63 this_shn->decode_state->getbufp = this_shn->decode_state->getbuf;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
64 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
65
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
66 buffer = (((slong) (this_shn->decode_state->getbufp[0])) << 24) | (((slong) (this_shn->decode_state->getbufp[1])) << 16) |
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
67 (((slong) (this_shn->decode_state->getbufp[2])) << 8) | ((slong) (this_shn->decode_state->getbufp[3]));
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
68
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
69 this_shn->decode_state->getbufp += 4;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
70 this_shn->decode_state->nbyteget -= 4;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
71
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
72 return(buffer);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
73 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
74
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
75 slong uvar_get(int nbin,shn_file *this_shn)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
76 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
77 slong result;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
78
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
79 if (this_shn->vars.reading_function_code) {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
80 this_shn->vars.last_file_position_no_really = this_shn->vars.last_file_position;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
81 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
82
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
83 if(this_shn->decode_state->nbitget == 0)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
84 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
85 this_shn->decode_state->gbuffer = word_get(this_shn);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
86 if (this_shn->vars.fatal_error)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
87 return (ulong)0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
88 this_shn->decode_state->nbitget = 32;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
89 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
90
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
91 for(result = 0; !(this_shn->decode_state->gbuffer & (1L << --(this_shn->decode_state->nbitget))); result++)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
92 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
93 if(this_shn->decode_state->nbitget == 0)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
94 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
95 this_shn->decode_state->gbuffer = word_get(this_shn);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
96 if (this_shn->vars.fatal_error)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
97 return (ulong)0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
98 this_shn->decode_state->nbitget = 32;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
99 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
100 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
101
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
102 while(nbin != 0)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
103 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
104 if(this_shn->decode_state->nbitget >= nbin)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
105 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
106 result = (result << nbin) | ((this_shn->decode_state->gbuffer >> (this_shn->decode_state->nbitget-nbin)) &masktab[nbin]);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
107 this_shn->decode_state->nbitget -= nbin;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
108 nbin = 0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
109 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
110 else
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
111 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
112 result = (result << this_shn->decode_state->nbitget) | (this_shn->decode_state->gbuffer & masktab[this_shn->decode_state->nbitget]);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
113 this_shn->decode_state->gbuffer = word_get(this_shn);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
114 if (this_shn->vars.fatal_error)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
115 return (ulong)0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
116 nbin -= this_shn->decode_state->nbitget;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
117 this_shn->decode_state->nbitget = 32;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
118 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
119 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
120
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
121 return(result);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
122 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
123
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
124 ulong ulong_get(shn_file *this_shn)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
125 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
126 unsigned int nbit = uvar_get(ULONGSIZE,this_shn);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
127 if (this_shn->vars.fatal_error)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
128 return (ulong)0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
129 return(uvar_get(nbit,this_shn));
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
130 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
131
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
132 slong var_get(int nbin,shn_file *this_shn)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
133 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
134 ulong uvar = uvar_get(nbin + 1,this_shn);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
135 if (this_shn->vars.fatal_error)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
136 return (slong)0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
137
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
138 if(uvar & 1) return((slong) ~(uvar >> 1));
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
139 else return((slong) (uvar >> 1));
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
140 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
141
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
142 void var_get_quit(shn_file *this_shn)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
143 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
144 free((void *) this_shn->decode_state->getbuf);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
145 this_shn->decode_state->getbuf = NULL;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
146 }
|