comparison src/shnplug/vario.c @ 1305:51bf0e431e02

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