| 1 | /* $Id: pushback.c,v 1.2 2000/12/14 18:45:35 ghazi Exp $
|
|---|
| 2 |
|
|---|
| 3 | $Log: pushback.c,v $
|
|---|
| 4 | Revision 1.2 2000/12/14 18:45:35 ghazi
|
|---|
| 5 | Warning fixes:
|
|---|
| 6 |
|
|---|
| 7 | * compress.c: Include stdlib.h and compress.h.
|
|---|
| 8 | (rcsid): Delete.
|
|---|
| 9 | (report_str_error): Make static.
|
|---|
| 10 | (ez_inflate_str): Delete unused variable. Add parens in if-stmt.
|
|---|
| 11 | (hrd_inflate_str): Likewise.
|
|---|
| 12 |
|
|---|
| 13 | * compress.h (init_compression, end_compression, init_inflation,
|
|---|
| 14 | end_inflation): Prototype void arguments.
|
|---|
| 15 |
|
|---|
| 16 | * dostime.c (rcsid): Delete.
|
|---|
| 17 |
|
|---|
| 18 | * jargrep.c: Include ctype.h, stdlib.h, zlib.h and compress.h.
|
|---|
| 19 | Make functions static. Cast ctype function argument to `unsigned
|
|---|
| 20 | char'. Add parens in if-stmts. Constify.
|
|---|
| 21 | (Usage): Change into a macro.
|
|---|
| 22 | (jargrep): Remove unused parameter.
|
|---|
| 23 |
|
|---|
| 24 | * jartool.c: Constify. Add parens in if-stmts. Align
|
|---|
| 25 | signed/unsigned char pointers in functions calls using casts.
|
|---|
| 26 | (rcsid): Delete.
|
|---|
| 27 | (list_jar): Fix printf format specifier.
|
|---|
| 28 | (usage): Chop long string into bits. Reformat.
|
|---|
| 29 |
|
|---|
| 30 | * pushback.c (rcsid): Delete.
|
|---|
| 31 |
|
|---|
| 32 | Revision 1.1 2000/12/09 03:08:23 apbianco
|
|---|
| 33 | 2000-12-08 Alexandre Petit-Bianco <apbianco@cygnus.com>
|
|---|
| 34 |
|
|---|
| 35 | * fastjar: Imported.
|
|---|
| 36 |
|
|---|
| 37 | Revision 1.2 2000/08/23 19:42:17 cory
|
|---|
| 38 | Added support for more Unix platforms. The following code has been hacked
|
|---|
| 39 | to work on AIX, Solaris, True 64, and HP-UX.
|
|---|
| 40 | Added bigendian check. Probably works on most big and little endian platforms
|
|---|
| 41 | now.
|
|---|
| 42 |
|
|---|
| 43 | Revision 1.1.1.1 1999/12/06 03:09:13 toast
|
|---|
| 44 | initial checkin..
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 | Revision 1.1 1999/05/10 08:32:37 burnsbr
|
|---|
| 49 | Initial revision
|
|---|
| 50 |
|
|---|
| 51 | */
|
|---|
| 52 |
|
|---|
| 53 | /*
|
|---|
| 54 | pushback.c - code for a pushback buffer to handle file I/O
|
|---|
| 55 | Copyright (C) 1999 Bryan Burns
|
|---|
| 56 |
|
|---|
| 57 | This program is free software; you can redistribute it and/or
|
|---|
| 58 | modify it under the terms of the GNU General Public License
|
|---|
| 59 | as published by the Free Software Foundation; either version 2
|
|---|
| 60 | of the License, or (at your option) any later version.
|
|---|
| 61 |
|
|---|
| 62 | This program is distributed in the hope that it will be useful,
|
|---|
| 63 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 64 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 65 | GNU General Public License for more details.
|
|---|
| 66 |
|
|---|
| 67 | You should have received a copy of the GNU General Public License
|
|---|
| 68 | along with this program; if not, write to the Free Software
|
|---|
| 69 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|---|
| 70 | */
|
|---|
| 71 |
|
|---|
| 72 | #include <unistd.h>
|
|---|
| 73 | #include <string.h>
|
|---|
| 74 | #include <stdio.h>
|
|---|
| 75 |
|
|---|
| 76 | #include "jartool.h"
|
|---|
| 77 | #include "pushback.h"
|
|---|
| 78 |
|
|---|
| 79 | void pb_init(pb_file *pbf, int fd){
|
|---|
| 80 | pbf->fd = fd;
|
|---|
| 81 | pbf->next = pbf->pb_buff;
|
|---|
| 82 | pbf->buff_amt = 0;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | int pb_push(pb_file *pbf, void *buff, int amt){
|
|---|
| 86 | int in_amt;
|
|---|
| 87 | int wrap = 0;
|
|---|
| 88 |
|
|---|
| 89 | #ifdef DEBUG
|
|---|
| 90 | printf("%d bytes being pushed back to the buffer\n", amt);
|
|---|
| 91 | #endif
|
|---|
| 92 |
|
|---|
| 93 | /* determine how much we can take */
|
|---|
| 94 | if((int)(RDSZ - pbf->buff_amt) < amt)
|
|---|
| 95 | in_amt = RDSZ - pbf->buff_amt;
|
|---|
| 96 | else
|
|---|
| 97 | in_amt = amt;
|
|---|
| 98 |
|
|---|
| 99 | if(in_amt == 0)
|
|---|
| 100 | return 0;
|
|---|
| 101 |
|
|---|
| 102 | /* figure out if we need to wrap around, and if so, by how much */
|
|---|
| 103 | if(((pbf->pb_buff + RDSZ) - pbf->next) < in_amt)
|
|---|
| 104 | wrap = in_amt - ((pbf->pb_buff + RDSZ) - pbf->next);
|
|---|
| 105 |
|
|---|
| 106 | /* write everything up til the end of the buffer */
|
|---|
| 107 | memcpy(pbf->next, buff, (in_amt - wrap));
|
|---|
| 108 |
|
|---|
| 109 | /* finish writing what's wrapped around */
|
|---|
| 110 | memcpy(pbf->pb_buff, ((char *)buff + (in_amt - wrap)), wrap);
|
|---|
| 111 |
|
|---|
| 112 | /* update the buff_amt field */
|
|---|
| 113 | pbf->buff_amt += in_amt;
|
|---|
| 114 |
|
|---|
| 115 | #ifdef DEBUG
|
|---|
| 116 | printf("%d bytes we can't accept\n", (amt - in_amt));
|
|---|
| 117 | #endif
|
|---|
| 118 |
|
|---|
| 119 | return in_amt;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 | int pb_read(pb_file *pbf, void *buff, int amt){
|
|---|
| 124 | int out_amt = 0;
|
|---|
| 125 | int wrap = 0;
|
|---|
| 126 | void *bp = buff;
|
|---|
| 127 | int tmp;
|
|---|
| 128 |
|
|---|
| 129 | #ifdef DEBUG
|
|---|
| 130 | printf("%d bytes requested from us\n", amt);
|
|---|
| 131 | #endif
|
|---|
| 132 | while(out_amt < amt){
|
|---|
| 133 | /* if our push-back buffer contains some data */
|
|---|
| 134 | if(pbf->buff_amt > 0){
|
|---|
| 135 |
|
|---|
| 136 | #ifdef DEBUG
|
|---|
| 137 | printf("giving data from buffer\n");
|
|---|
| 138 | #endif
|
|---|
| 139 |
|
|---|
| 140 | /* calculate how much we can actually give the caller */
|
|---|
| 141 | if( (amt - out_amt) < (int)pbf->buff_amt )
|
|---|
| 142 | tmp = (amt - out_amt);
|
|---|
| 143 | else
|
|---|
| 144 | tmp = pbf->buff_amt;
|
|---|
| 145 |
|
|---|
| 146 | /* Determine if we're going to need to wrap around the buffer */
|
|---|
| 147 | if(tmp > ((pbf->pb_buff + RDSZ) - pbf->next))
|
|---|
| 148 | wrap = tmp - ((pbf->pb_buff + RDSZ) - pbf->next);
|
|---|
| 149 |
|
|---|
| 150 | memcpy(bp, pbf->next, (tmp - wrap));
|
|---|
| 151 | bp = &(((char *)bp)[tmp - wrap]);
|
|---|
| 152 |
|
|---|
| 153 | /* If we need to wrap, read from the start of the buffer */
|
|---|
| 154 | if(wrap > 0){
|
|---|
| 155 | memcpy(bp, pbf->pb_buff, wrap);
|
|---|
| 156 | bp = &(((char *)bp)[wrap]);
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | /* update the buff_amt field */
|
|---|
| 160 | pbf->buff_amt -= tmp;
|
|---|
| 161 | pbf->next += tmp;
|
|---|
| 162 |
|
|---|
| 163 | #ifdef DEBUG
|
|---|
| 164 | printf("%d bytes remaining in buffer\n", pbf->buff_amt);
|
|---|
| 165 | #endif
|
|---|
| 166 |
|
|---|
| 167 | /* if the buffer is empty, reset the next header to the front of the
|
|---|
| 168 | buffer so subsequent pushbacks/reads won't have to wrap */
|
|---|
| 169 | if(pbf->buff_amt == 0)
|
|---|
| 170 | pbf->next = pbf->pb_buff;
|
|---|
| 171 |
|
|---|
| 172 | out_amt += tmp;
|
|---|
| 173 |
|
|---|
| 174 | } else {
|
|---|
| 175 | #ifdef DEBUG
|
|---|
| 176 | printf("Reading from file..\n");
|
|---|
| 177 | #endif
|
|---|
| 178 |
|
|---|
| 179 | /* The pushback buffer was empty, so we just need to read from the file */
|
|---|
| 180 | tmp = read(pbf->fd, bp, (amt - out_amt));
|
|---|
| 181 | if(tmp == 0)
|
|---|
| 182 | break;
|
|---|
| 183 | else
|
|---|
| 184 | out_amt += tmp;
|
|---|
| 185 |
|
|---|
| 186 | bp = &(((char *)bp)[tmp]);
|
|---|
| 187 | }
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | #ifdef DEBUG
|
|---|
| 191 | printf("managed to read %d bytes\n", out_amt);
|
|---|
| 192 | #endif
|
|---|
| 193 | return out_amt;
|
|---|
| 194 | }
|
|---|