1 /*	$NetBSD: iso9660_rrip.c,v 1.14 2014/05/30 13:14:47 martin Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
7  * Perez-Rathke and Ram Vedam.  All rights reserved.
8  *
9  * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
10  * Alan Perez-Rathke and Ram Vedam.
11  *
12  * Redistribution and use in source and binary forms, with or
13  * without modification, are permitted provided that the following
14  * conditions are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above
18  *    copyright notice, this list of conditions and the following
19  *    disclaimer in the documentation and/or other materials provided
20  *    with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
23  * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
27  * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30  * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
34  * OF SUCH DAMAGE.
35  */
36 /* This will hold all the function definitions
37  * defined in iso9660_rrip.h
38  */
39 
40 #include <sys/cdefs.h>
41 #include <sys/queue.h>
42 #include <sys/types.h>
43 #include <stdio.h>
44 
45 #include "makefs.h"
46 #include "cd9660.h"
47 #include "iso9660_rrip.h"
48 #include <util.h>
49 
50 static void cd9660_rrip_initialize_inode(cd9660node *);
51 static int cd9660_susp_handle_continuation(iso9660_disk *, cd9660node *);
52 static int cd9660_susp_handle_continuation_common(iso9660_disk *, cd9660node *,
53     int);
54 
55 int
cd9660_susp_initialize(iso9660_disk * diskStructure,cd9660node * node,cd9660node * parent,cd9660node * grandparent)56 cd9660_susp_initialize(iso9660_disk *diskStructure, cd9660node *node,
57     cd9660node *parent, cd9660node *grandparent)
58 {
59 	cd9660node *cn;
60 	int r;
61 
62 	/* Make sure the node is not NULL. If it is, there are major problems */
63 	assert(node != NULL);
64 
65 	if (!(node->type & CD9660_TYPE_DOT) &&
66 	    !(node->type & CD9660_TYPE_DOTDOT))
67 		TAILQ_INIT(&(node->head));
68 	if (node->dot_record != 0)
69 		TAILQ_INIT(&(node->dot_record->head));
70 	if (node->dot_dot_record != 0)
71 		TAILQ_INIT(&(node->dot_dot_record->head));
72 
73 	 /* SUSP specific entries here */
74 	if ((r = cd9660_susp_initialize_node(diskStructure, node)) < 0)
75 		return r;
76 
77 	/* currently called cd9660node_rrip_init_links */
78 	r = cd9660_rrip_initialize_node(diskStructure, node, parent, grandparent);
79 	if (r < 0)
80 		return r;
81 
82 	/*
83 	 * See if we need a CE record, and set all of the
84 	 * associated counters.
85 	 *
86 	 * This should be called after all extensions. After
87 	 * this is called, no new records should be added.
88 	 */
89 	if ((r = cd9660_susp_handle_continuation(diskStructure, node)) < 0)
90 		return r;
91 
92 	/* Recurse on children. */
93 	TAILQ_FOREACH(cn, &node->cn_children, cn_next_child) {
94 		if ((r = cd9660_susp_initialize(diskStructure, cn, node, parent)) < 0)
95 			return 0;
96 	}
97 	return 1;
98 }
99 
100 int
cd9660_susp_finalize(iso9660_disk * diskStructure,cd9660node * node)101 cd9660_susp_finalize(iso9660_disk *diskStructure, cd9660node *node)
102 {
103 	cd9660node *temp;
104 	int r;
105 
106 	assert(node != NULL);
107 
108 	if (node == diskStructure->rootNode)
109 		diskStructure->susp_continuation_area_current_free = 0;
110 
111 	if ((r = cd9660_susp_finalize_node(diskStructure, node)) < 0)
112 		return r;
113 	if ((r = cd9660_rrip_finalize_node(node)) < 0)
114 		return r;
115 
116 	TAILQ_FOREACH(temp, &node->cn_children, cn_next_child) {
117 		if ((r = cd9660_susp_finalize(diskStructure, temp)) < 0)
118 			return r;
119 	}
120 	return 1;
121 }
122 
123 /*
124  * If we really wanted to speed things up, we could have some sort of
125  * lookup table on the SUSP entry type that calls a functor. Or, we could
126  * combine the functions. These functions are kept separate to allow
127  * easier addition of other extensions.
128 
129  * For the sake of simplicity and clarity, we won't be doing that for now.
130  */
131 
132 /*
133  * SUSP needs to update the following types:
134  * CE (continuation area)
135  */
136 int
cd9660_susp_finalize_node(iso9660_disk * diskStructure,cd9660node * node)137 cd9660_susp_finalize_node(iso9660_disk *diskStructure, cd9660node *node)
138 {
139 	struct ISO_SUSP_ATTRIBUTES *t;
140 
141 	/* Handle CE counters */
142 	if (node->susp_entry_ce_length > 0) {
143 		node->susp_entry_ce_start =
144 		    diskStructure->susp_continuation_area_current_free;
145 		diskStructure->susp_continuation_area_current_free +=
146 		    node->susp_entry_ce_length;
147 	}
148 
149 	TAILQ_FOREACH(t, &node->head, rr_ll) {
150 		if (t->susp_type != SUSP_TYPE_SUSP ||
151 		    t->entry_type != SUSP_ENTRY_SUSP_CE)
152 			continue;
153 		cd9660_bothendian_dword(
154 			diskStructure->
155 			  susp_continuation_area_start_sector,
156 			t->attr.su_entry.CE.ca_sector);
157 
158 		cd9660_bothendian_dword(
159 			diskStructure->
160 			  susp_continuation_area_start_sector,
161 			t->attr.su_entry.CE.ca_sector);
162 		cd9660_bothendian_dword(node->susp_entry_ce_start,
163 			t->attr.su_entry.CE.offset);
164 		cd9660_bothendian_dword(node->susp_entry_ce_length,
165 			t->attr.su_entry.CE.length);
166 	}
167 	return 0;
168 }
169 
170 int
cd9660_rrip_finalize_node(cd9660node * node)171 cd9660_rrip_finalize_node(cd9660node *node)
172 {
173 	struct ISO_SUSP_ATTRIBUTES *t;
174 
175 	TAILQ_FOREACH(t, &node->head, rr_ll) {
176 		if (t->susp_type != SUSP_TYPE_RRIP)
177 			continue;
178 		switch (t->entry_type) {
179 		case SUSP_ENTRY_RRIP_CL:
180 			/* Look at rr_relocated*/
181 			if (node->rr_relocated == NULL)
182 				return -1;
183 			cd9660_bothendian_dword(
184 				node->rr_relocated->fileDataSector,
185 				(unsigned char *)
186 				    t->attr.rr_entry.CL.dir_loc);
187 			break;
188 		case SUSP_ENTRY_RRIP_PL:
189 			/* Look at rr_real_parent */
190 			if (node->parent == NULL ||
191 			    node->parent->rr_real_parent == NULL)
192 				return -1;
193 			cd9660_bothendian_dword(
194 				node->parent->rr_real_parent->fileDataSector,
195 				(unsigned char *)
196 				    t->attr.rr_entry.PL.dir_loc);
197 			break;
198 		}
199 	}
200 	return 0;
201 }
202 
203 static int
cd9660_susp_handle_continuation_common(iso9660_disk * diskStructure,cd9660node * node,int space)204 cd9660_susp_handle_continuation_common(iso9660_disk *diskStructure,
205     cd9660node *node, int space)
206 {
207 	int ca_used, susp_used, susp_used_pre_ce, working;
208 	struct ISO_SUSP_ATTRIBUTES *temp, *pre_ce, *last, *CE, *ST;
209 
210 	pre_ce = last = NULL;
211 	working = 254 - space;
212 	if (node->su_tail_size > 0)
213 		/* Allow 4 bytes for "ST" record. */
214 		working -= node->su_tail_size + 4;
215 	/* printf("There are %i bytes to work with\n",working); */
216 
217 	susp_used_pre_ce = susp_used = 0;
218 	ca_used = 0;
219 	TAILQ_FOREACH(temp, &node->head, rr_ll) {
220 		if (working < 0)
221 			break;
222 		/*
223 		 * printf("SUSP Entry found, length is %i\n",
224 		 * CD9660_SUSP_ENTRY_SIZE(temp));
225 		 */
226 		working -= CD9660_SUSP_ENTRY_SIZE(temp);
227 		if (working >= 0) {
228 			last = temp;
229 			susp_used += CD9660_SUSP_ENTRY_SIZE(temp);
230 		}
231 		if (working >= 28) {
232 			/*
233 			 * Remember the last entry after which we
234 			 * could insert a "CE" entry.
235 			 */
236 			pre_ce = last;
237 			susp_used_pre_ce = susp_used;
238 		}
239 	}
240 
241 	/* A CE entry is needed */
242 	if (working <= 0) {
243 		CE = cd9660node_susp_create_node(SUSP_TYPE_SUSP,
244 			SUSP_ENTRY_SUSP_CE, "CE", SUSP_LOC_ENTRY);
245 		cd9660_susp_ce(CE, node);
246 		/* This will automatically insert at the appropriate location */
247 		if (pre_ce != NULL)
248 			TAILQ_INSERT_AFTER(&node->head, pre_ce, CE, rr_ll);
249 		else
250 			TAILQ_INSERT_HEAD(&node->head, CE, rr_ll);
251 		last = CE;
252 		susp_used = susp_used_pre_ce + 28;
253 		/* Count how much CA data is necessary */
254 		for (temp = TAILQ_NEXT(last, rr_ll); temp != NULL;
255 		     temp = TAILQ_NEXT(temp, rr_ll)) {
256 			ca_used += CD9660_SUSP_ENTRY_SIZE(temp);
257 		}
258 	}
259 
260 	/* An ST entry is needed */
261 	if (node->su_tail_size > 0) {
262 		ST = cd9660node_susp_create_node(SUSP_TYPE_SUSP,
263 		    SUSP_ENTRY_SUSP_ST, "ST", SUSP_LOC_ENTRY);
264 		cd9660_susp_st(ST, node);
265 		if (last != NULL)
266 			TAILQ_INSERT_AFTER(&node->head, last, ST, rr_ll);
267 		else
268 			TAILQ_INSERT_HEAD(&node->head, ST, rr_ll);
269 		last = ST;
270 		susp_used += 4;
271 	}
272 	if (last != NULL)
273 		last->last_in_suf = 1;
274 
275 	node->susp_entry_size = susp_used;
276 	node->susp_entry_ce_length = ca_used;
277 
278 	diskStructure->susp_continuation_area_size += ca_used;
279 	return 1;
280 }
281 
282 /* See if a continuation entry is needed for each of the different types */
283 static int
cd9660_susp_handle_continuation(iso9660_disk * diskStructure,cd9660node * node)284 cd9660_susp_handle_continuation(iso9660_disk *diskStructure, cd9660node *node)
285 {
286 	assert (node != NULL);
287 
288 	/* Entry */
289 	if (cd9660_susp_handle_continuation_common(diskStructure,
290 		node,(int)(node->isoDirRecord->length[0])) < 0)
291 		return 0;
292 
293 	return 1;
294 }
295 
296 int
cd9660_susp_initialize_node(iso9660_disk * diskStructure,cd9660node * node)297 cd9660_susp_initialize_node(iso9660_disk *diskStructure, cd9660node *node)
298 {
299 	struct ISO_SUSP_ATTRIBUTES *temp;
300 
301 	/*
302 	 * Requirements/notes:
303 	 * CE: is added for us where needed
304 	 * ST: not sure if it is even required, but if so, should be
305 	 *     handled by the CE code
306 	 * PD: isn't needed (though might be added for testing)
307 	 * SP: is stored ONLY on the . record of the root directory
308 	 * ES: not sure
309 	 */
310 
311 	/* Check for root directory, add SP and ER if needed. */
312 	if (node->type & CD9660_TYPE_DOT) {
313 		if (node->parent == diskStructure->rootNode) {
314 			temp = cd9660node_susp_create_node(SUSP_TYPE_SUSP,
315 				SUSP_ENTRY_SUSP_SP, "SP", SUSP_LOC_DOT);
316 			cd9660_susp_sp(temp, node);
317 
318 			/* Should be first entry. */
319 			TAILQ_INSERT_HEAD(&node->head, temp, rr_ll);
320 		}
321 	}
322 	return 1;
323 }
324 
325 static void
cd9660_rrip_initialize_inode(cd9660node * node)326 cd9660_rrip_initialize_inode(cd9660node *node)
327 {
328 	struct ISO_SUSP_ATTRIBUTES *attr;
329 
330 	/*
331 	 * Inode dependent values - this may change,
332 	 * but for now virtual files and directories do
333 	 * not have an inode structure
334 	 */
335 
336 	if ((node->node != NULL) && (node->node->inode != NULL)) {
337 		/* PX - POSIX attributes */
338 		attr = cd9660node_susp_create_node(SUSP_TYPE_RRIP,
339 			SUSP_ENTRY_RRIP_PX, "PX", SUSP_LOC_ENTRY);
340 		cd9660node_rrip_px(attr, node->node);
341 
342 		TAILQ_INSERT_TAIL(&node->head, attr, rr_ll);
343 
344 		/* TF - timestamp */
345 		attr = cd9660node_susp_create_node(SUSP_TYPE_RRIP,
346 			SUSP_ENTRY_RRIP_TF, "TF", SUSP_LOC_ENTRY);
347 		cd9660node_rrip_tf(attr, node->node);
348 		TAILQ_INSERT_TAIL(&node->head, attr, rr_ll);
349 
350 		/* SL - Symbolic link */
351 		/* ?????????? Dan - why is this here? */
352 		if (TAILQ_EMPTY(&node->cn_children) &&
353 		    node->node->inode != NULL &&
354 		    S_ISLNK(node->node->inode->st.st_mode))
355 			cd9660_createSL(node);
356 
357 		/* PN - device number */
358 		if (node->node->inode != NULL &&
359 		    ((S_ISCHR(node->node->inode->st.st_mode) ||
360 		     S_ISBLK(node->node->inode->st.st_mode)))) {
361 			attr =
362 			    cd9660node_susp_create_node(SUSP_TYPE_RRIP,
363 				SUSP_ENTRY_RRIP_PN, "PN",
364 				SUSP_LOC_ENTRY);
365 			cd9660node_rrip_pn(attr, node->node);
366 			TAILQ_INSERT_TAIL(&node->head, attr, rr_ll);
367 		}
368 	}
369 }
370 
371 int
cd9660_rrip_initialize_node(iso9660_disk * diskStructure,cd9660node * node,cd9660node * parent,cd9660node * grandparent)372 cd9660_rrip_initialize_node(iso9660_disk *diskStructure, cd9660node *node,
373     cd9660node *parent, cd9660node *grandparent)
374 {
375 	struct ISO_SUSP_ATTRIBUTES *current = NULL;
376 
377 	assert(node != NULL);
378 
379 	if (node->type & CD9660_TYPE_DOT) {
380 		/*
381 		 * Handle ER - should be the only entry to appear on
382 		 * a "." record
383 		 */
384 		if (node->parent == diskStructure->rootNode) {
385 			cd9660_susp_ER(node, 1, SUSP_RRIP_ER_EXT_ID,
386 				SUSP_RRIP_ER_EXT_DES, SUSP_RRIP_ER_EXT_SRC);
387 		}
388 		if (parent != NULL && parent->node != NULL &&
389 		    parent->node->inode != NULL) {
390 			/* PX - POSIX attributes */
391 			current = cd9660node_susp_create_node(SUSP_TYPE_RRIP,
392 				SUSP_ENTRY_RRIP_PX, "PX", SUSP_LOC_ENTRY);
393 			cd9660node_rrip_px(current, parent->node);
394 			TAILQ_INSERT_TAIL(&node->head, current, rr_ll);
395 
396 			/* TF - timestamp */
397 			current = cd9660node_susp_create_node(SUSP_TYPE_RRIP,
398 				SUSP_ENTRY_RRIP_TF, "TF", SUSP_LOC_ENTRY);
399 			cd9660node_rrip_tf(current, parent->node);
400 			TAILQ_INSERT_TAIL(&node->head, current, rr_ll);
401 		}
402 	} else if (node->type & CD9660_TYPE_DOTDOT) {
403 		if (grandparent != NULL && grandparent->node != NULL &&
404 		    grandparent->node->inode != NULL) {
405 			/* PX - POSIX attributes */
406 			current = cd9660node_susp_create_node(SUSP_TYPE_RRIP,
407 				SUSP_ENTRY_RRIP_PX, "PX", SUSP_LOC_ENTRY);
408 			cd9660node_rrip_px(current, grandparent->node);
409 			TAILQ_INSERT_TAIL(&node->head, current, rr_ll);
410 
411 			/* TF - timestamp */
412 			current = cd9660node_susp_create_node(SUSP_TYPE_RRIP,
413 				SUSP_ENTRY_RRIP_TF, "TF", SUSP_LOC_ENTRY);
414 			cd9660node_rrip_tf(current, grandparent->node);
415 			TAILQ_INSERT_TAIL(&node->head, current, rr_ll);
416 		}
417 		/* Handle PL */
418 		if (parent != NULL && parent->rr_real_parent != NULL) {
419 			current = cd9660node_susp_create_node(SUSP_TYPE_RRIP,
420 			    SUSP_ENTRY_RRIP_PL, "PL", SUSP_LOC_DOTDOT);
421 			cd9660_rrip_PL(current,node);
422 			TAILQ_INSERT_TAIL(&node->head, current, rr_ll);
423 		}
424 	} else {
425 		cd9660_rrip_initialize_inode(node);
426 
427 		if (node == diskStructure->rr_moved_dir) {
428 			cd9660_rrip_add_NM(node, RRIP_DEFAULT_MOVE_DIR_NAME);
429 		} else if (node->node != NULL) {
430 			cd9660_rrip_NM(node);
431 		}
432 
433 		/* Rock ridge directory relocation code here. */
434 
435 		/* First handle the CL for the placeholder file. */
436 		if (node->rr_relocated != NULL) {
437 			current = cd9660node_susp_create_node(SUSP_TYPE_RRIP,
438 				SUSP_ENTRY_RRIP_CL, "CL", SUSP_LOC_ENTRY);
439 			cd9660_rrip_CL(current, node);
440 			TAILQ_INSERT_TAIL(&node->head, current, rr_ll);
441 		}
442 
443 		/* Handle RE*/
444 		if (node->rr_real_parent != NULL) {
445 			current = cd9660node_susp_create_node(SUSP_TYPE_RRIP,
446 				SUSP_ENTRY_RRIP_RE, "RE", SUSP_LOC_ENTRY);
447 			cd9660_rrip_RE(current,node);
448 			TAILQ_INSERT_TAIL(&node->head, current, rr_ll);
449 		}
450 	}
451 	return 1;
452 }
453 
454 struct ISO_SUSP_ATTRIBUTES*
cd9660node_susp_create_node(int susp_type,int entry_type,const char * type_id,int write_loc)455 cd9660node_susp_create_node(int susp_type, int entry_type, const char *type_id,
456 			    int write_loc)
457 {
458 	struct ISO_SUSP_ATTRIBUTES* temp;
459 
460 	temp = emalloc(sizeof(*temp));
461 	temp->susp_type = susp_type;
462 	temp->entry_type = entry_type;
463 	temp->last_in_suf = 0;
464 	/* Phase this out */
465 	temp->type_of[0] = type_id[0];
466 	temp->type_of[1] = type_id[1];
467 	temp->write_location = write_loc;
468 
469 	/*
470 	 * Since the first four bytes is common, lets go ahead and
471 	 * set the type identifier, since we are passing that to this
472 	 * function anyhow.
473 	 */
474 	temp->attr.su_entry.SP.h.type[0] = type_id[0];
475 	temp->attr.su_entry.SP.h.type[1] = type_id[1];
476 	return temp;
477 }
478 
479 int
cd9660_rrip_PL(struct ISO_SUSP_ATTRIBUTES * p,cd9660node * node __unused)480 cd9660_rrip_PL(struct ISO_SUSP_ATTRIBUTES* p, cd9660node *node __unused)
481 {
482 	p->attr.rr_entry.PL.h.length[0] = 12;
483 	p->attr.rr_entry.PL.h.version[0] = 1;
484 	return 1;
485 }
486 
487 int
cd9660_rrip_CL(struct ISO_SUSP_ATTRIBUTES * p,cd9660node * node __unused)488 cd9660_rrip_CL(struct ISO_SUSP_ATTRIBUTES *p, cd9660node *node __unused)
489 {
490 	p->attr.rr_entry.CL.h.length[0] = 12;
491 	p->attr.rr_entry.CL.h.version[0] = 1;
492 	return 1;
493 }
494 
495 int
cd9660_rrip_RE(struct ISO_SUSP_ATTRIBUTES * p,cd9660node * node __unused)496 cd9660_rrip_RE(struct ISO_SUSP_ATTRIBUTES *p, cd9660node *node __unused)
497 {
498 	p->attr.rr_entry.RE.h.length[0] = 4;
499 	p->attr.rr_entry.RE.h.version[0] = 1;
500 	return 1;
501 }
502 
503 void
cd9660_createSL(cd9660node * node)504 cd9660_createSL(cd9660node *node)
505 {
506 	struct ISO_SUSP_ATTRIBUTES* current;
507 	int path_count, dir_count, done, i, j, dir_copied;
508 	char temp_cr[255];
509 	char temp_sl[255]; /* used in copying continuation entry*/
510 	char* sl_ptr;
511 
512 	sl_ptr = node->node->symlink;
513 
514 	done = 0;
515 	path_count = 0;
516 	dir_count = 0;
517 	dir_copied = 0;
518 	current = cd9660node_susp_create_node(SUSP_TYPE_RRIP,
519 	    SUSP_ENTRY_RRIP_SL, "SL", SUSP_LOC_ENTRY);
520 
521 	current->attr.rr_entry.SL.h.version[0] = 1;
522 	current->attr.rr_entry.SL.flags[0] = SL_FLAGS_NONE;
523 
524 	if (*sl_ptr == '/') {
525 		temp_cr[0] = SL_FLAGS_ROOT;
526 		temp_cr[1] = 0;
527 		memcpy(current->attr.rr_entry.SL.component + path_count,
528 		    temp_cr, 2);
529 		path_count += 2;
530 		sl_ptr++;
531 	}
532 
533 	for (i = 0; i < (dir_count + 2); i++)
534 		temp_cr[i] = '\0';
535 
536 	while (!done) {
537 		while ((*sl_ptr != '/') && (*sl_ptr != '\0')) {
538 			dir_copied = 1;
539 			if (*sl_ptr == '.') {
540 				if ((*(sl_ptr + 1) == '/') || (*(sl_ptr + 1)
541 				     == '\0')) {
542 					temp_cr[0] = SL_FLAGS_CURRENT;
543 					sl_ptr++;
544 				} else if(*(sl_ptr + 1) == '.') {
545 					if ((*(sl_ptr + 2) == '/') ||
546 					    (*(sl_ptr + 2) == '\0')) {
547 						temp_cr[0] = SL_FLAGS_PARENT;
548 						sl_ptr += 2;
549 					}
550 				} else {
551 					temp_cr[dir_count+2] = *sl_ptr;
552 					sl_ptr++;
553 					dir_count++;
554 				}
555 			} else {
556 				temp_cr[dir_count + 2] = *sl_ptr;
557 				sl_ptr++;
558 				dir_count++;
559 			}
560 		}
561 
562 		if ((path_count + dir_count) >= 249) {
563 			current->attr.rr_entry.SL.flags[0] |= SL_FLAGS_CONTINUE;
564 
565 			j = 0;
566 
567 			if (path_count <= 249) {
568 				while(j != (249 - path_count)) {
569 					temp_sl[j] = temp_cr[j];
570 					j++;
571 				}
572 				temp_sl[0] = SL_FLAGS_CONTINUE;
573 				temp_sl[1] = j - 2;
574 				memcpy(
575 				    current->attr.rr_entry.SL.component +
576 					path_count,
577 				    temp_sl, j);
578 			}
579 
580 			path_count += j;
581 			current->attr.rr_entry.SL.h.length[0] = path_count + 5;
582 			TAILQ_INSERT_TAIL(&node->head, current, rr_ll);
583 			current= cd9660node_susp_create_node(SUSP_TYPE_RRIP,
584 			       SUSP_ENTRY_RRIP_SL, "SL", SUSP_LOC_ENTRY);
585 			current->attr.rr_entry.SL.h.version[0] = 1;
586 			current->attr.rr_entry.SL.flags[0] = SL_FLAGS_NONE;
587 
588 			path_count = 0;
589 
590 			if (dir_count > 2) {
591 				while (j != dir_count + 2) {
592 					current->attr.rr_entry.SL.component[
593 					    path_count + 2] = temp_cr[j];
594 					j++;
595 					path_count++;
596 				}
597 				current->attr.rr_entry.SL.component[1]
598 				    = path_count;
599 				path_count+= 2;
600 			} else {
601 				while(j != dir_count) {
602 					current->attr.rr_entry.SL.component[
603 					    path_count+2] = temp_cr[j];
604 					j++;
605 					path_count++;
606 				}
607 			}
608 		} else {
609 			if (dir_copied == 1) {
610 				temp_cr[1] = dir_count;
611 				memcpy(current->attr.rr_entry.SL.component +
612 					path_count,
613 				    temp_cr, dir_count + 2);
614 				path_count += dir_count + 2;
615 			}
616 		}
617 
618 		if (*sl_ptr == '\0') {
619 			done = 1;
620 			current->attr.rr_entry.SL.h.length[0] = path_count + 5;
621 			TAILQ_INSERT_TAIL(&node->head, current, rr_ll);
622 		} else {
623 			sl_ptr++;
624 			dir_count = 0;
625 			dir_copied = 0;
626 			for(i = 0; i < 255; i++) {
627 				temp_cr[i] = '\0';
628 			}
629 		}
630 	}
631 }
632 
633 int
cd9660node_rrip_px(struct ISO_SUSP_ATTRIBUTES * v,fsnode * pxinfo)634 cd9660node_rrip_px(struct ISO_SUSP_ATTRIBUTES *v, fsnode *pxinfo)
635 {
636 	v->attr.rr_entry.PX.h.length[0] = 44;
637 	v->attr.rr_entry.PX.h.version[0] = 1;
638 	cd9660_bothendian_dword(pxinfo->inode->st.st_mode,
639 	    v->attr.rr_entry.PX.mode);
640 	cd9660_bothendian_dword(pxinfo->inode->st.st_nlink,
641 	    v->attr.rr_entry.PX.links);
642 	cd9660_bothendian_dword(pxinfo->inode->st.st_uid,
643 	    v->attr.rr_entry.PX.uid);
644 	cd9660_bothendian_dword(pxinfo->inode->st.st_gid,
645 	    v->attr.rr_entry.PX.gid);
646 	cd9660_bothendian_dword(pxinfo->inode->st.st_ino,
647 	    v->attr.rr_entry.PX.serial);
648 
649 	return 1;
650 }
651 
652 int
cd9660node_rrip_pn(struct ISO_SUSP_ATTRIBUTES * pn_field,fsnode * fnode)653 cd9660node_rrip_pn(struct ISO_SUSP_ATTRIBUTES *pn_field, fsnode *fnode)
654 {
655 	pn_field->attr.rr_entry.PN.h.length[0] = 20;
656 	pn_field->attr.rr_entry.PN.h.version[0] = 1;
657 
658 	if (sizeof (fnode->inode->st.st_rdev) > 4)
659 		cd9660_bothendian_dword(
660 		    (uint64_t)fnode->inode->st.st_rdev >> 32,
661 		    pn_field->attr.rr_entry.PN.high);
662 	else
663 		cd9660_bothendian_dword(0, pn_field->attr.rr_entry.PN.high);
664 
665 	cd9660_bothendian_dword(fnode->inode->st.st_rdev & 0xffffffff,
666 		pn_field->attr.rr_entry.PN.low);
667 	return 1;
668 }
669 
670 #if 0
671 int
672 cd9660node_rrip_nm(struct ISO_SUSP_ATTRIBUTES *p, cd9660node *file_node)
673 {
674 	int nm_length = strlen(file_node->isoDirRecord->name) + 5;
675         p->attr.rr_entry.NM.h.type[0] = 'N';
676 	p->attr.rr_entry.NM.h.type[1] = 'M';
677 	sprintf(p->attr.rr_entry.NM.altname, "%s", file_node->isoDirRecord->name);
678 	p->attr.rr_entry.NM.h.length[0] = (unsigned char)nm_length;
679 	p->attr.rr_entry.NM.h.version[0] = (unsigned char)1;
680 	p->attr.rr_entry.NM.flags[0] = (unsigned char) NM_PARENT;
681 	return 1;
682 }
683 #endif
684 
685 int
cd9660node_rrip_tf(struct ISO_SUSP_ATTRIBUTES * p,fsnode * _node)686 cd9660node_rrip_tf(struct ISO_SUSP_ATTRIBUTES *p, fsnode *_node)
687 {
688 	p->attr.rr_entry.TF.flags[0] = TF_MODIFY | TF_ACCESS | TF_ATTRIBUTES;
689 	p->attr.rr_entry.TF.h.length[0] = 5;
690 	p->attr.rr_entry.TF.h.version[0] = 1;
691 
692 	/*
693 	 * Need to add creation time, backup time,
694 	 * expiration time, and effective time.
695 	 */
696 
697 	cd9660_time_915(p->attr.rr_entry.TF.timestamp,
698 		_node->inode->st.st_mtime);
699 	p->attr.rr_entry.TF.h.length[0] += 7;
700 
701 	cd9660_time_915(p->attr.rr_entry.TF.timestamp + 7,
702 		_node->inode->st.st_atime);
703 	p->attr.rr_entry.TF.h.length[0] += 7;
704 
705 	cd9660_time_915(p->attr.rr_entry.TF.timestamp + 14,
706 		_node->inode->st.st_ctime);
707 	p->attr.rr_entry.TF.h.length[0] += 7;
708 	return 1;
709 }
710 
711 int
cd9660_susp_sp(struct ISO_SUSP_ATTRIBUTES * p,cd9660node * spinfo __unused)712 cd9660_susp_sp(struct ISO_SUSP_ATTRIBUTES *p, cd9660node *spinfo __unused)
713 {
714 	p->attr.su_entry.SP.h.length[0] = 7;
715 	p->attr.su_entry.SP.h.version[0] = 1;
716 	p->attr.su_entry.SP.check[0] = 0xBE;
717 	p->attr.su_entry.SP.check[1] = 0xEF;
718 	p->attr.su_entry.SP.len_skp[0] = 0;
719 	return 1;
720 }
721 
722 int
cd9660_susp_st(struct ISO_SUSP_ATTRIBUTES * p,cd9660node * stinfo __unused)723 cd9660_susp_st(struct ISO_SUSP_ATTRIBUTES *p, cd9660node *stinfo __unused)
724 {
725 	p->attr.su_entry.ST.h.type[0] = 'S';
726 	p->attr.su_entry.ST.h.type[1] = 'T';
727 	p->attr.su_entry.ST.h.length[0] = 4;
728 	p->attr.su_entry.ST.h.version[0] = 1;
729 	return 1;
730 }
731 
732 int
cd9660_susp_ce(struct ISO_SUSP_ATTRIBUTES * p,cd9660node * spinfo __unused)733 cd9660_susp_ce(struct ISO_SUSP_ATTRIBUTES *p, cd9660node *spinfo __unused)
734 {
735 	p->attr.su_entry.CE.h.length[0] = 28;
736 	p->attr.su_entry.CE.h.version[0] = 1;
737 	/* Other attributes dont matter right now, will be updated later */
738 	return 1;
739 }
740 
741 int
cd9660_susp_pd(struct ISO_SUSP_ATTRIBUTES * p __unused,int length __unused)742 cd9660_susp_pd(struct ISO_SUSP_ATTRIBUTES *p __unused, int length __unused)
743 {
744 	return 1;
745 }
746 
747 void
cd9660_rrip_add_NM(cd9660node * node,const char * name)748 cd9660_rrip_add_NM(cd9660node *node, const char *name)
749 {
750 	int working,len;
751 	const char *p;
752 	struct ISO_SUSP_ATTRIBUTES *r;
753 
754 	/*
755 	 * Each NM record has 254 bytes to work with. This means that
756 	 * the name data itself only has 249 bytes to work with. So, a
757 	 * name with 251 characters would require two nm records.
758 	 */
759 	p = name;
760 	working = 1;
761 	while (working) {
762 		r = cd9660node_susp_create_node(SUSP_TYPE_RRIP,
763 		    SUSP_ENTRY_RRIP_NM, "NM", SUSP_LOC_ENTRY);
764 		r->attr.rr_entry.NM.h.version[0] = 1;
765 		r->attr.rr_entry.NM.flags[0] = RRIP_NM_FLAGS_NONE;
766 		len = strlen(p);
767 
768 		if (len > 249) {
769 			len = 249;
770 			r->attr.rr_entry.NM.flags[0] = RRIP_NM_FLAGS_CONTINUE;
771 		} else {
772 			working = 0;
773 		}
774 		memcpy(r->attr.rr_entry.NM.altname, p, len);
775 		r->attr.rr_entry.NM.h.length[0] = 5 + len;
776 
777 		TAILQ_INSERT_TAIL(&node->head, r, rr_ll);
778 
779 		p += len;
780 	}
781 }
782 
783 void
cd9660_rrip_NM(cd9660node * node)784 cd9660_rrip_NM(cd9660node *node)
785 {
786 	cd9660_rrip_add_NM(node, node->node->name);
787 }
788 
789 struct ISO_SUSP_ATTRIBUTES*
cd9660_susp_ER(cd9660node * node,u_char ext_version,const char * ext_id,const char * ext_des,const char * ext_src)790 cd9660_susp_ER(cd9660node *node,
791 	       u_char ext_version, const char* ext_id, const char* ext_des,
792 	       const char* ext_src)
793 {
794 	int l;
795 	struct ISO_SUSP_ATTRIBUTES *r;
796 
797 	r = cd9660node_susp_create_node(SUSP_TYPE_SUSP,
798 			SUSP_ENTRY_SUSP_ER, "ER", SUSP_LOC_DOT);
799 
800 	/* Fixed data is 8 bytes */
801 	r->attr.su_entry.ER.h.length[0] = 8;
802 	r->attr.su_entry.ER.h.version[0] = 1;
803 
804 	r->attr.su_entry.ER.len_id[0] = (u_char)strlen(ext_id);
805 	r->attr.su_entry.ER.len_des[0] = (u_char)strlen(ext_des);
806 	r->attr.su_entry.ER.len_src[0] = (u_char)strlen(ext_src);
807 
808 	l = r->attr.su_entry.ER.len_id[0] +
809 		r->attr.su_entry.ER.len_src[0] +
810 		r->attr.su_entry.ER.len_des[0];
811 
812 	/* Everything must fit. */
813 	assert(l + r->attr.su_entry.ER.h.length[0] <= 254);
814 
815 	r->attr.su_entry.ER.h.length[0] += (u_char)l;
816 
817 
818 	r->attr.su_entry.ER.ext_ver[0] = ext_version;
819 	memcpy(r->attr.su_entry.ER.ext_data, ext_id,
820 		(int)r->attr.su_entry.ER.len_id[0]);
821 	l = (int) r->attr.su_entry.ER.len_id[0];
822 	memcpy(r->attr.su_entry.ER.ext_data + l,ext_des,
823 		(int)r->attr.su_entry.ER.len_des[0]);
824 
825 	l += (int)r->attr.su_entry.ER.len_des[0];
826 	memcpy(r->attr.su_entry.ER.ext_data + l,ext_src,
827 		(int)r->attr.su_entry.ER.len_src[0]);
828 
829 	TAILQ_INSERT_TAIL(&node->head, r, rr_ll);
830 	return r;
831 }
832 
833 struct ISO_SUSP_ATTRIBUTES*
cd9660_susp_ES(struct ISO_SUSP_ATTRIBUTES * last __unused,cd9660node * node __unused)834 cd9660_susp_ES(struct ISO_SUSP_ATTRIBUTES *last __unused, cd9660node *node __unused)
835 {
836 	return NULL;
837 }
838