Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/paxlib/calib/camap.c
1811 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 2004-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* David Korn <[email protected]> *
19
* *
20
***********************************************************************/
21
#pragma prototyped
22
23
#include <ast.h>
24
25
#define MAPCHARS " PROCESS MAPCHARS"
26
27
static int special[] = { 0, '\r' };
28
29
void*
30
camap_open(void)
31
{
32
return newof(0, char, UCHAR_MAX+1, 0);
33
}
34
35
int
36
camap_init(void* ptr)
37
{
38
memset(ptr, 0, UCHAR_MAX+1);
39
return 0;
40
}
41
42
int
43
camap_write(void* ptr,void *buff, register size_t size)
44
{
45
register char* hit = (char*)ptr;
46
register unsigned char* cp = (unsigned char *)buff;
47
register unsigned char* ep = cp + size;
48
49
while(cp < ep)
50
hit[*cp++] = 1;
51
return 0;
52
}
53
54
static int
55
tr(int in, int out, const char *header, const unsigned char *table)
56
{
57
unsigned char buff0[32*1024], buff1[32*1024], *buff[2];
58
register unsigned char *cp,*cpmax;
59
size_t z;
60
int n[2],odd=1;
61
62
buff[0] = buff0;
63
buff[1] = buff1;
64
if((n[0]=read(in,buff[0],sizeof(buff0)))<=0)
65
return -1;
66
z = strlen(header);
67
if (write(out,header,z) != z)
68
return -1;
69
do
70
{
71
n[odd]=read(in,buff[odd],sizeof(buff0));
72
odd = 1 - odd;
73
cp = buff[odd];
74
for(cpmax= &cp[n[odd]]; cp<cpmax; cp++)
75
*cp = table[*cp];
76
z = n[odd];
77
if (write(out,buff[odd],z) != z)
78
return -1;
79
}
80
while(n[1-odd]>0);
81
return n[1-odd];
82
}
83
84
int
85
camap_done(void* ptr, const char* file, int out)
86
{
87
unsigned char table[256];
88
char header[80];
89
register char* hit = (char*)ptr;
90
int i,j,k,m,n,in= -1,r=0, sep='(';
91
for(i=0; i < sizeof(special)/sizeof(*special); i++)
92
{
93
m = special[i];
94
if(hit[m]==0)
95
continue;
96
if(in < 0)
97
{
98
if((in = open(file, O_RDONLY)) < 0)
99
{
100
free(ptr);
101
return -1;
102
}
103
for(n=0 ; n < 256; n++)
104
table[n] = n;
105
strcpy(header,MAPCHARS);
106
k = sizeof(MAPCHARS)-1;
107
n=1;
108
}
109
for(; n < 256; n++)
110
{
111
for(j=1; j < sizeof(special)/sizeof(*special); j++)
112
if(n==j)
113
continue;
114
if(hit[n]==0)
115
{
116
k+= sfsprintf(&header[k],sizeof(header)-k,"%c%d,%d",sep,m,n);
117
sep = ',';
118
table[m] = n;
119
hit[n] = 1;
120
break;
121
}
122
}
123
}
124
if(in>=0)
125
{
126
strcpy(&header[k],")\n");
127
if(lseek(out, (off_t)0, SEEK_SET)==0)
128
r = tr(in,out,header,table);
129
else
130
r = -1;
131
close(in);
132
}
133
return r;
134
}
135
136
int
137
camap_close(void* ptr)
138
{
139
free(ptr);
140
return 0;
141
}
142
143