blob: 1214ba69dbef684888db31ea0c751e5743bd0425 [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
Wanlong Gaofed96412012-10-24 10:10:29 +080020 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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
Wanlong Gaodf5b3b42012-12-25 15:25:35 +080040#include <stdlib.h>
whrb973f2b2000-05-05 19:34:50 +000041#endif
42
43static char Errmsg[80];
44
Wanlong Gaodf5b3b42012-12-25 15:25:35 +080045int dataasciigen(char *listofchars, char *buffer, int bsize, int offset)
whrb973f2b2000-05-05 19:34:50 +000046{
Wanlong Gao354ebb42012-12-07 10:10:04 +080047 int cnt;
48 int total;
Wanlong Gaodf5b3b42012-12-25 15:25:35 +080049 int ind;
Wanlong Gao354ebb42012-12-07 10:10:04 +080050 char *chr;
51 int chars_size;
52 char *charlist;
whrb973f2b2000-05-05 19:34:50 +000053
Wanlong Gao354ebb42012-12-07 10:10:04 +080054 chr = buffer;
55 total = offset + bsize;
whrb973f2b2000-05-05 19:34:50 +000056
Garrett Cooper903910d2010-11-23 09:27:44 -080057 if (listofchars == NULL) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080058 charlist = CHARS;
59 chars_size = CHARS_SIZE;
60 } else {
61 charlist = listofchars;
62 chars_size = strlen(listofchars);
whrb973f2b2000-05-05 19:34:50 +000063 }
64
Wanlong Gao354ebb42012-12-07 10:10:04 +080065 for (cnt = offset; cnt < total; cnt++) {
66 ind = cnt % chars_size;
67 *chr++ = charlist[ind];
whrb973f2b2000-05-05 19:34:50 +000068 }
69
70 return bsize;
Wanlong Gaodf5b3b42012-12-25 15:25:35 +080071}
whrb973f2b2000-05-05 19:34:50 +000072
Wanlong Gaodf5b3b42012-12-25 15:25:35 +080073int dataasciichk(char *listofchars, char *buffer, int bsize,
74 int offset, char **errmsg)
whrb973f2b2000-05-05 19:34:50 +000075{
Wanlong Gao354ebb42012-12-07 10:10:04 +080076 int cnt;
77 int total;
Wanlong Gaodf5b3b42012-12-25 15:25:35 +080078 int ind;
Wanlong Gao354ebb42012-12-07 10:10:04 +080079 char *chr;
80 int chars_size;
81 char *charlist;
whrb973f2b2000-05-05 19:34:50 +000082
Wanlong Gao354ebb42012-12-07 10:10:04 +080083 chr = buffer;
84 total = offset + bsize;
whrb973f2b2000-05-05 19:34:50 +000085
Garrett Cooper903910d2010-11-23 09:27:44 -080086 if (listofchars == NULL) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080087 charlist = CHARS;
88 chars_size = CHARS_SIZE;
89 } else {
90 charlist = listofchars;
91 chars_size = strlen(listofchars);
whrb973f2b2000-05-05 19:34:50 +000092 }
93
Wanlong Gaodf5b3b42012-12-25 15:25:35 +080094 if (errmsg != NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +080095 *errmsg = Errmsg;
whrb973f2b2000-05-05 19:34:50 +000096
Wanlong Gao354ebb42012-12-07 10:10:04 +080097 for (cnt = offset; cnt < total; chr++, cnt++) {
98 ind = cnt % chars_size;
99 if (*chr != charlist[ind]) {
100 sprintf(Errmsg,
101 "data mismatch at offset %d, exp:%#o, act:%#o",
102 cnt, charlist[ind], *chr);
103 return cnt;
104 }
whrb973f2b2000-05-05 19:34:50 +0000105 }
106
107 sprintf(Errmsg, "all %d bytes match desired pattern", bsize);
Wanlong Gaodf5b3b42012-12-25 15:25:35 +0800108 return -1;
109}
whrb973f2b2000-05-05 19:34:50 +0000110
111#if UNIT_TEST
112
Wanlong Gaodf5b3b42012-12-25 15:25:35 +0800113int main(int ac, char **ag)
whrb973f2b2000-05-05 19:34:50 +0000114{
115
Wanlong Gao354ebb42012-12-07 10:10:04 +0800116 int size = 1023;
117 char *buffer;
118 int ret;
119 char *errmsg;
whrb973f2b2000-05-05 19:34:50 +0000120
Wanlong Gaodf5b3b42012-12-25 15:25:35 +0800121 buffer = malloc(size);
122 if (buffer == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800123 perror("malloc");
124 exit(2);
125 }
whrb973f2b2000-05-05 19:34:50 +0000126
Wanlong Gao354ebb42012-12-07 10:10:04 +0800127 dataasciigen(NULL, buffer, size, 0);
128 printf("dataasciigen(NULL, buffer, %d, 0)\n", size);
whrb973f2b2000-05-05 19:34:50 +0000129
Wanlong Gao354ebb42012-12-07 10:10:04 +0800130 ret = dataasciichk(NULL, buffer, size, 0, &errmsg);
131 printf("dataasciichk(NULL, buffer, %d, 0, &errmsg) returned %d %s\n",
132 size, ret, errmsg);
whrb973f2b2000-05-05 19:34:50 +0000133
Wanlong Gao354ebb42012-12-07 10:10:04 +0800134 if (ret == -1)
135 printf("\tPASS return value is -1 as expected\n");
136 else
137 printf("\tFAIL return value is %d, expected -1\n", ret);
whrb973f2b2000-05-05 19:34:50 +0000138
Wanlong Gao354ebb42012-12-07 10:10:04 +0800139 ret = dataasciichk(NULL, &buffer[1], size - 1, 1, &errmsg);
Wanlong Gaodf5b3b42012-12-25 15:25:35 +0800140 printf("dataasciichk(NULL, &buffer[1], %d, 1, &errmsg) returned %d %s\n",
141 size - 1, ret, errmsg);
whrb973f2b2000-05-05 19:34:50 +0000142
Wanlong Gao354ebb42012-12-07 10:10:04 +0800143 if (ret == -1)
144 printf("\tPASS return value is -1 as expected\n");
145 else
146 printf("\tFAIL return value is %d, expected -1\n", ret);
whrb973f2b2000-05-05 19:34:50 +0000147
Wanlong Gao354ebb42012-12-07 10:10:04 +0800148 buffer[25] = 0x0;
149 printf("changing char 25\n");
whrb973f2b2000-05-05 19:34:50 +0000150
Wanlong Gao354ebb42012-12-07 10:10:04 +0800151 ret = dataasciichk(NULL, &buffer[1], size - 1, 1, &errmsg);
Wanlong Gaodf5b3b42012-12-25 15:25:35 +0800152 printf("dataasciichk(NULL, &buffer[1], %d, 1, &errmsg) returned %d %s\n",
153 size - 1, ret, errmsg);
whrb973f2b2000-05-05 19:34:50 +0000154
Wanlong Gao354ebb42012-12-07 10:10:04 +0800155 if (ret == 25)
156 printf("\tPASS return value is 25 as expected\n");
157 else
158 printf("\tFAIL return value is %d, expected 25\n", ret);
whrb973f2b2000-05-05 19:34:50 +0000159
Wanlong Gao354ebb42012-12-07 10:10:04 +0800160 dataasciigen("this is a test of the my string", buffer, size, 0);
Wanlong Gaodf5b3b42012-12-25 15:25:35 +0800161 printf("dataasciigen(\"this is a test of the my string\", buffer, %d, 0)\n",
162 size);
whrb973f2b2000-05-05 19:34:50 +0000163
Wanlong Gaodf5b3b42012-12-25 15:25:35 +0800164 ret = dataasciichk("this is a test of the my string",
165 buffer, size, 0, &errmsg);
166 printf("dataasciichk(\"this is a test of the my string\", buffer, %d, 0, &errmsg) returned %d %s\n",
167 size, ret, errmsg);
whrb973f2b2000-05-05 19:34:50 +0000168
Wanlong Gao354ebb42012-12-07 10:10:04 +0800169 if (ret == -1)
170 printf("\tPASS return value is -1 as expected\n");
171 else
172 printf("\tFAIL return value is %d, expected -1\n", ret);
whrb973f2b2000-05-05 19:34:50 +0000173
Wanlong Gao354ebb42012-12-07 10:10:04 +0800174 ret =
175 dataasciichk("this is a test of the my string", &buffer[1],
176 size - 1, 1, &errmsg);
Wanlong Gaodf5b3b42012-12-25 15:25:35 +0800177 printf("dataasciichk(\"this is a test of the my string\", &buffer[1], %d, 1, &errmsg) returned %d %s\n",
178 size - 1, ret, errmsg);
whrb973f2b2000-05-05 19:34:50 +0000179
Wanlong Gao354ebb42012-12-07 10:10:04 +0800180 if (ret == -1)
181 printf("\tPASS return value is -1 as expected\n");
182 else
183 printf("\tFAIL return value is %d, expected -1\n", ret);
whrb973f2b2000-05-05 19:34:50 +0000184
Wanlong Gao354ebb42012-12-07 10:10:04 +0800185 buffer[25] = 0x0;
186 printf("changing char 25\n");
whrb973f2b2000-05-05 19:34:50 +0000187
Wanlong Gaodf5b3b42012-12-25 15:25:35 +0800188 ret = dataasciichk("this is a test of the my string", &buffer[1],
189 size - 1, 1, &errmsg);
190 printf("dataasciichk(\"this is a test of the my string\", &buffer[1], %d, 1, &errmsg) returned %d %s\n",
191 size - 1, ret, errmsg);
whrb973f2b2000-05-05 19:34:50 +0000192
Wanlong Gao354ebb42012-12-07 10:10:04 +0800193 if (ret == 25)
194 printf("\tPASS return value is 25 as expected\n");
195 else
196 printf("\tFAIL return value is %d, expected 25\n", ret);
whrb973f2b2000-05-05 19:34:50 +0000197
Wanlong Gao354ebb42012-12-07 10:10:04 +0800198 exit(0);
whrb973f2b2000-05-05 19:34:50 +0000199}
200
201#endif