1 /*	$Id: pascal.c,v 1.2 2006/12/28 18:30:15 toad32767 Exp $ */
2 
3 /*-
4  * Copyright (c) 2006 Marco Trillo
5  *
6  * Permission is hereby granted, free of charge, to any
7  * person obtaining a copy of this software and associated
8  * documentation files (the "Software"), to deal in the
9  * Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the
12  * Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice
16  * shall be included in all copies or substantial portions of
17  * the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
20  * KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
21  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22  * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
23  * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28 
29 #define LIBAIFF 1
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <libaiff/libaiff.h>
34 #include "private.h"
35 
36 /*
37  * PASCAL-style strings I/O
38  */
39 
40 /*
41  * return num. of bytes to skip the PASCAL string
42  */
43 int
PASCALInGetLength(FILE * fd)44 PASCALInGetLength (FILE * fd)
45 {
46 	int count;
47 
48 	if ((count = fgetc(fd)) < 0)
49 		return (-1);
50 	return (count + !(count & 1));
51 }
52 
53 /*
54  * read and return the PASCAL string (allocate the needed
55  * memory). Update 'length' to be the total num. of bytes read
56  */
57 char *
PASCALInRead(FILE * fd,int * length)58 PASCALInRead (FILE * fd, int * length)
59 {
60 	int c, l, r, fr;
61 	char *str;
62 
63 	if ((c = fgetc(fd)) < 0) {
64 		*length = -1;
65 		return (NULL);
66 	}
67 	r = 1;
68 	l = c + !(c & 1); /* pad */
69 	if ((str = malloc(c + 1)) == NULL) {
70 		goto ret;
71 	}
72 	if ((fr = (int) fread(str, 1, l, fd)) != l) {
73 		free(str);
74 		str = NULL;
75 		r += fr;
76 		goto ret;
77 	}
78 	r += l;
79 	str[c] = 0; /* NUL terminator */
80 
81 ret:
82 	*length = r;
83 	return (str);
84 }
85 
86 /*
87  * return the num. of bytes needed to write
88  * 'str' as a PASCAL string, but don't write
89  * anything
90  */
91 int
PASCALOutGetLength(char * str)92 PASCALOutGetLength (char * str)
93 {
94 	int l = strlen(str);
95 	l = MIN(l, 0xFF);
96 	return (1 + l + !(l & 1));
97 }
98 
99 /*
100  * write 'str' to file 'fd' as a PASCAL string
101  * and return the num. of bytes written
102  */
103 int
PASCALOutWrite(FILE * fd,char * str)104 PASCALOutWrite (FILE * fd, char * str)
105 {
106 	int l = strlen(str), w;
107 
108 	l = MIN(l, 0xFF);
109 	if (fputc(l, fd) < 0) {
110 		return (-1);
111 	}
112 	w = 1;
113 	l += !(l & 1); /* pad */
114 	/*
115 	 * The C-string NUL terminator will serve
116 	 * as a pad byte if necessary
117 	 */
118 	w += (int) fwrite(str, 1, l, fd);
119 
120 	return (w);
121 }
122 
123 
124