blob: 775eb48de33b38a627cadce9b2d789a7d65b2f90 [file] [log] [blame]
whrb973f2b2000-05-05 19:34:50 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
vapier45a8ba02009-07-20 10:59:32 +00003 *
whrb973f2b2000-05-05 19:34:50 +00004 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
vapier45a8ba02009-07-20 10:59:32 +00007 *
whrb973f2b2000-05-05 19:34:50 +00008 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
vapier45a8ba02009-07-20 10:59:32 +000011 *
whrb973f2b2000-05-05 19:34:50 +000012 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
vapier45a8ba02009-07-20 10:59:32 +000018 *
whrb973f2b2000-05-05 19:34:50 +000019 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
vapier45a8ba02009-07-20 10:59:32 +000022 *
whrb973f2b2000-05-05 19:34:50 +000023 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
vapier45a8ba02009-07-20 10:59:32 +000025 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
whrb973f2b2000-05-05 19:34:50 +000030 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 */
32#include <stdio.h>
33#include <string.h>
34#include "dataascii.h"
35
36#define CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghjiklmnopqrstuvwxyz\n"
37#define CHARS_SIZE sizeof(CHARS)
38
39#ifdef UNIT_TEST
40#include <stdlib.h> /* malloc */
41#endif
42
43static char Errmsg[80];
44
45int
46dataasciigen(listofchars, buffer, bsize, offset)
47char *listofchars; /* a null terminated list of characters */
48char *buffer;
49int bsize;
50int offset;
51{
52 int cnt;
53 int total;
54 int ind; /* index into CHARS array */
55 char *chr;
56 int chars_size;
57 char *charlist;
58
59 chr=buffer;
60 total=offset+bsize;
61
62 if ( listofchars == NULL ) {
63 charlist=CHARS;
64 chars_size=CHARS_SIZE;
65 }
66 else {
67 charlist=listofchars;
68 chars_size=strlen(listofchars);
69 }
70
71 for(cnt=offset; cnt<total; cnt++) {
72 ind=cnt%chars_size;
73 *chr++=charlist[ind];
74 }
75
76 return bsize;
77
78} /* end of dataasciigen */
79
80int
81dataasciichk(listofchars, buffer, bsize, offset, errmsg)
82char *listofchars; /* a null terminated list of characters */
83char *buffer;
84int bsize;
85int offset;
86char **errmsg;
87{
88 int cnt;
89 int total;
90 int ind; /* index into CHARS array */
91 char *chr;
92 int chars_size;
93 char *charlist;
94
95 chr=buffer;
96 total=offset+bsize;
97
98 if ( listofchars == NULL ) {
99 charlist=CHARS;
100 chars_size=CHARS_SIZE;
101 }
102 else {
103 charlist=listofchars;
104 chars_size=strlen(listofchars);
105 }
106
107 if ( errmsg != NULL ) {
108 *errmsg = Errmsg;
109 }
110
111 for(cnt=offset; cnt<total; chr++, cnt++) {
112 ind=cnt%chars_size;
113 if ( *chr != charlist[ind] ) {
114 sprintf(Errmsg,
115 "data mismatch at offset %d, exp:%#o, act:%#o", cnt,
116 charlist[ind], *chr);
117 return cnt;
118 }
119 }
120
121 sprintf(Errmsg, "all %d bytes match desired pattern", bsize);
122 return -1; /* buffer is ok */
123
124} /* end of dataasciichk */
125
126
127#if UNIT_TEST
128
129/***********************************************************************
130 * main for doing unit testing
131 ***********************************************************************/
132int
133main(ac, ag)
134int ac;
135char **ag;
136{
137
138int size=1023;
139char *buffer;
140int ret;
141char *errmsg;
142
143 if ((buffer=(char *)malloc(size)) == NULL ) {
144 perror("malloc");
145 exit(2);
146 }
147
148 dataasciigen(NULL, buffer, size, 0);
149 printf("dataasciigen(NULL, buffer, %d, 0)\n", size);
150
151 ret=dataasciichk(NULL, buffer, size, 0, &errmsg);
152 printf("dataasciichk(NULL, buffer, %d, 0, &errmsg) returned %d %s\n",
153 size, ret, errmsg);
154
155 if ( ret == -1 )
156 printf("\tPASS return value is -1 as expected\n");
157 else
158 printf("\tFAIL return value is %d, expected -1\n", ret);
159
160 ret=dataasciichk(NULL, &buffer[1], size-1, 1, &errmsg);
161 printf("dataasciichk(NULL, &buffer[1], %d, 1, &errmsg) returned %d %s\n",
162 size-1, ret, errmsg);
163
164 if ( ret == -1 )
165 printf("\tPASS return value is -1 as expected\n");
166 else
167 printf("\tFAIL return value is %d, expected -1\n", ret);
168
169 buffer[25]= 0x0;
170 printf("changing char 25\n");
171
172 ret=dataasciichk(NULL, &buffer[1], size-1, 1, &errmsg);
173 printf("dataasciichk(NULL, &buffer[1], %d, 1, &errmsg) returned %d %s\n",
174 size-1, ret, errmsg);
175
176 if ( ret == 25 )
177 printf("\tPASS return value is 25 as expected\n");
178 else
179 printf("\tFAIL return value is %d, expected 25\n", ret);
180
181 dataasciigen("this is a test of the my string" , buffer, size, 0);
182 printf("dataasciigen(\"this is a test of the my string\", buffer, %d, 0)\n", size);
183
184 ret=dataasciichk("this is a test of the my string", buffer, size, 0, &errmsg);
185 printf("dataasciichk(\"this is a test of the my string\", buffer, %d, 0, &errmsg) returned %d %s\n",
186 size, ret, errmsg);
187
188 if ( ret == -1 )
189 printf("\tPASS return value is -1 as expected\n");
190 else
191 printf("\tFAIL return value is %d, expected -1\n", ret);
192
193 ret=dataasciichk("this is a test of the my string", &buffer[1], size-1, 1, &errmsg);
194 printf("dataasciichk(\"this is a test of the my string\", &buffer[1], %d, 1, &errmsg) returned %d %s\n",
195 size-1, ret, errmsg);
196
197 if ( ret == -1 )
198 printf("\tPASS return value is -1 as expected\n");
199 else
200 printf("\tFAIL return value is %d, expected -1\n", ret);
201
202 buffer[25]= 0x0;
203 printf("changing char 25\n");
204
205 ret=dataasciichk("this is a test of the my string", &buffer[1], size-1, 1, &errmsg);
206 printf("dataasciichk(\"this is a test of the my string\", &buffer[1], %d, 1, &errmsg) returned %d %s\n",
207 size-1, ret, errmsg);
208
209 if ( ret == 25 )
210 printf("\tPASS return value is 25 as expected\n");
211 else
212 printf("\tFAIL return value is %d, expected 25\n", ret);
213
214 exit(0);
215}
216
217#endif
218