1 /*
2     OW -- One-Wire filesystem
3     version 0.4 7/2/2003
4 
5     Function naming scheme:
6     OW -- Generic call to interaface
7     LI -- LINK commands
8     L1 -- 2480B commands
9     FS -- filesystem commands
10     UT -- utility functions
11 
12     Written 2003 Paul H Alfille
13         Fuse code based on "fusexmp" {GPL} by Miklos Szeredi, mszeredi@inf.bme.hu
14         Serial code based on "xt" {GPL} by David Querbach, www.realtime.bc.ca
15         in turn based on "miniterm" by Sven Goldt, goldt@math.tu.berlin.de
16     GPL license
17     This program is free software; you can redistribute it and/or
18     modify it under the terms of the GNU General Public License
19     as published by the Free Software Foundation; either version 2
20     of the License, or (at your option) any later version.
21 
22     This program is distributed in the hope that it will be useful,
23     but WITHOUT ANY WARRANTY; without even the implied warranty of
24     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25     GNU General Public License for more details.
26 
27     Other portions based on Dallas Semiconductor Public Domain Kit,
28     ---------------------------------------------------------------------------
29     Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
30         Permission is hereby granted, free of charge, to any person obtaining a
31         copy of this software and associated documentation files (the "Software"),
32         to deal in the Software without restriction, including without limitation
33         the rights to use, copy, modify, merge, publish, distribute, sublicense,
34         and/or sell copies of the Software, and to permit persons to whom the
35         Software is furnished to do so, subject to the following conditions:
36         The above copyright notice and this permission notice shall be included
37         in all copies or substantial portions of the Software.
38     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
39     OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
40     MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
41     IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
42     OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
43     ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
44     OTHER DEALINGS IN THE SOFTWARE.
45         Except as contained in this notice, the name of Dallas Semiconductor
46         shall not be used except as stated in the Dallas Semiconductor
47         Branding Policy.
48     ---------------------------------------------------------------------------
49     Implementation:
50     2006 dirblob
51 */
52 
53 #include <config.h>
54 #include "owfs_config.h"
55 #include "ow.h"
56 
57 /*
58     A "charblob" is a structure holding a list of files
59 
60     Most interesting, it allocates memory dynamically.
61 */
62 
CharblobClear(struct charblob * cb)63 void CharblobClear(struct charblob *cb)
64 {
65 	if (cb->blob) {
66 		free(cb->blob);
67 	}
68 	CharblobInit(cb);
69 }
70 
CharblobInit(struct charblob * cb)71 void CharblobInit(struct charblob *cb)
72 {
73 	cb->used = 0;
74 	cb->allocated = 0;
75 	cb->blob = 0;
76 	cb->troubled = 0;
77 }
78 
CharblobPure(struct charblob * cb)79 int CharblobPure(struct charblob *cb)
80 {
81 	return !cb->troubled;
82 }
83 
CharblobAdd(const ASCII * a,size_t s,struct charblob * cb)84 int CharblobAdd(const ASCII * a, size_t s, struct charblob *cb)
85 {
86 	size_t incr = 1024;
87 	if (incr < s)
88 		incr = s;
89 	// make more room? -- blocks of 1k
90 	if (cb->used)
91 		CharblobAddChar(',', cb);	// add a comma
92 	if (cb->used + s > cb->allocated) {
93 		int newalloc = cb->allocated + incr;
94 		ASCII *temp = realloc(cb->blob, newalloc);
95 		if (temp) {
96 			memset(&temp[cb->allocated], 0, incr);	// set the new memory to blank
97 			cb->allocated = newalloc;
98 			cb->blob = temp;
99 		} else {				// allocation failed -- keep old
100 			cb->troubled = 1;
101 			return -ENOMEM;
102 		}
103 	}
104 	memcpy(&cb->blob[cb->used], a, s);
105 	cb->used += s;
106 	return 0;
107 }
108 
CharblobAddChar(ASCII a,struct charblob * cb)109 int CharblobAddChar(ASCII a, struct charblob *cb)
110 {
111 	// make more room? -- blocks of 1k
112 	if (cb->used + 1 > cb->allocated) {
113 		int newalloc = cb->allocated + 1024;
114 		ASCII *temp = realloc(cb->blob, newalloc);
115 		if (temp) {
116 			memset(&temp[cb->allocated], 0, 1024);	// set the new memory to blank
117 			cb->allocated = newalloc;
118 			cb->blob = temp;
119 		} else {				// allocation failed -- keep old
120 			cb->troubled = 1;
121 			return -ENOMEM;
122 		}
123 	}
124 	cb->blob[cb->used] = a;
125 	++cb->used;
126 	return 0;
127 }
128 
CharblobLength(struct charblob * cb)129 size_t CharblobLength( struct charblob * cb )
130 {
131 	return cb->used ;
132 }
133 
CharblobData(struct charblob * cb)134 ASCII * CharblobData(struct charblob * cb)
135 {
136 	return cb->blob ;
137 }
138