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