#ifndef _MAKEFS_CD9660_H
#define _MAKEFS_CD9660_H
#include <inttypes.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <limits.h>
#include <sys/queue.h>
#include <sys/param.h>
#include <sys/endian.h>
#include <sys/tree.h>
#include "makefs.h"
#include "iso.h"
#include "iso_rrip.h"
#include "cd9660/cd9660_eltorito.h"
#ifdef DEBUG
#define INODE_WARNX(__x) warnx __x
#else
#define INODE_WARNX(__x)
#endif
#define ISO_VOLUME_DESCRIPTOR_STANDARD_ID "CD001"
#define ISO_VOLUME_DESCRIPTOR_BOOT 0
#define ISO_VOLUME_DESCRIPTOR_PVD 1
#define ISO_VOLUME_DESCRIPTOR_TERMINATOR 255
#define ISO_FILENAME_MAXLENGTH_BEFORE_VERSION 30
#define ISO_FILENAME_MAXLENGTH 38
#define ISO_FLAG_CLEAR 0x00
#define ISO_FLAG_HIDDEN 0x01
#define ISO_FLAG_DIRECTORY 0x02
#define ISO_FLAG_ASSOCIATED 0x04
#define ISO_FLAG_PERMISSIONS 0x08
#define ISO_FLAG_RESERVED5 0x10
#define ISO_FLAG_RESERVED6 0x20
#define ISO_FLAG_FINAL_RECORD 0x40
#define ISO_PATHTABLE_ENTRY_BASESIZE 8
#define ISO_RRIP_DEFAULT_MOVE_DIR_NAME "RR_MOVED"
#define RRIP_DEFAULT_MOVE_DIR_NAME ".rr_moved"
#define CD9660_BLOCKS(__sector_size, __bytes) \
howmany((__bytes), (__sector_size))
#define CD9660_MEM_ALLOC_ERROR(_F) \
err(EXIT_FAILURE, "%s, %s l. %d", _F, __FILE__, __LINE__)
#define CD9660_TYPE_FILE 0x01
#define CD9660_TYPE_DIR 0x02
#define CD9660_TYPE_DOT 0x04
#define CD9660_TYPE_DOTDOT 0x08
#define CD9660_TYPE_VIRTUAL 0x80
#define CD9660_INODE_HASH_SIZE 1024
#define CD9660_SECTOR_SIZE 2048
#define CD9660_END_PADDING 150
typedef struct _iso_directory_record_cd9660 {
u_char length [ISODCL (1, 1)];
u_char ext_attr_length [ISODCL (2, 2)];
u_char extent [ISODCL (3, 10)];
u_char size [ISODCL (11, 18)];
u_char date [ISODCL (19, 25)];
u_char flags [ISODCL (26, 26)];
u_char file_unit_size [ISODCL (27, 27)];
u_char interleave [ISODCL (28, 28)];
u_char volume_sequence_number [ISODCL (29, 32)];
u_char name_len [ISODCL (33, 33)];
char name [ISO_FILENAME_MAXLENGTH];
} iso_directory_record_cd9660;
typedef struct _cd9660node {
u_char type;
struct _cd9660node *parent;
TAILQ_HEAD(cd9660_children_head, _cd9660node) cn_children;
TAILQ_ENTRY(_cd9660node) cn_next_child;
struct _cd9660node *dot_record;
struct _cd9660node *dot_dot_record;
fsnode *node;
struct _iso_directory_record_cd9660 *isoDirRecord;
struct iso_extended_attributes *isoExtAttributes;
uint32_t fileDataSector;
int64_t fileDataLength;
int64_t fileSectorsUsed;
int fileRecordSize;
char o_name [ISO_FILENAME_MAXLENGTH];
struct _cd9660node *rr_real_parent, *rr_relocated;
int64_t susp_entry_size;
int64_t susp_dot_entry_size;
int64_t susp_dot_dot_entry_size;
int64_t susp_entry_ce_start;
int64_t susp_dot_ce_start;
int64_t susp_dot_dot_ce_start;
int64_t susp_entry_ce_length;
int64_t susp_dot_ce_length;
int64_t susp_dot_dot_ce_length;
int64_t su_tail_size;
char *su_tail_data;
int level;
int ptnumber;
struct _cd9660node *ptnext, *ptprev, *ptlast;
TAILQ_HEAD(susp_linked_list, ISO_SUSP_ATTRIBUTES) head;
} cd9660node;
typedef struct _path_table_entry
{
u_char length[ISODCL (1, 1)];
u_char extended_attribute_length[ISODCL (2, 2)];
u_char first_sector[ISODCL (3, 6)];
u_char parent_number[ISODCL (7, 8)];
char name[ISO_FILENAME_MAXLENGTH];
} path_table_entry;
typedef struct _volume_descriptor
{
u_char *volumeDescriptorData;
int64_t sector;
struct _volume_descriptor *next;
} volume_descriptor;
struct inode_map_node {
RB_ENTRY(inode_map_node) entry;
uint64_t key;
uint64_t value;
};
typedef struct _iso9660_disk {
int sectorSize;
struct iso_primary_descriptor primaryDescriptor;
struct iso_supplementary_descriptor supplementaryDescriptor;
volume_descriptor *firstVolumeDescriptor;
cd9660node *rootNode;
int64_t primaryBigEndianTableSector;
int64_t primaryLittleEndianTableSector;
int64_t secondaryBigEndianTableSector;
int64_t secondaryLittleEndianTableSector;
int pathTableLength;
int64_t dataFirstSector;
int64_t totalSectors;
int isoLevel;
int include_padding_areas;
int verbose_level;
int keep_bad_images;
int64_t susp_continuation_area_start_sector;
int64_t susp_continuation_area_size;
int64_t susp_continuation_area_current_free;
int rock_ridge_enabled;
char *rock_ridge_renamed_dir_name;
unsigned rock_ridge_move_count;
cd9660node *rr_moved_dir;
uint64_t rr_inode_next;
RB_HEAD(inode_map_tree, inode_map_node) rr_inode_map;
int chrp_boot;
u_char allow_deep_trees;
u_char allow_start_dot;
u_char allow_max_name;
u_char allow_illegal_chars;
u_char allow_lowercase;
u_char allow_multidot;
u_char omit_trailing_period;
int has_generic_bootimage;
char *generic_bootimage;
int is_bootable;
int64_t boot_catalog_sector;
boot_volume_descriptor *boot_descriptor;
char * boot_image_directory;
TAILQ_HEAD(boot_image_list,cd9660_boot_image) boot_images;
int image_serialno;
LIST_HEAD(boot_catalog_entries,boot_catalog_entry) boot_entries;
} iso9660_disk;
RB_PROTOTYPE(inode_map_tree, inode_map_node, entry, inode_map_node_cmp);
int cd9660_valid_a_chars(const char *);
int cd9660_valid_d_chars(const char *);
void cd9660_uppercase_characters(char *, size_t);
void cd9660_721(uint16_t, unsigned char *);
void cd9660_731(uint32_t, unsigned char *);
void cd9660_722(uint16_t, unsigned char *);
void cd9660_732(uint32_t, unsigned char *);
void cd9660_bothendian_dword(uint32_t dw, unsigned char *);
void cd9660_bothendian_word(uint16_t dw, unsigned char *);
void cd9660_set_date(char *, time_t);
void cd9660_time_8426(unsigned char *, time_t);
void cd9660_time_915(unsigned char *, time_t);
int cd9660_write_generic_bootimage(FILE *);
int cd9660_write_boot(iso9660_disk *, FILE *);
int cd9660_add_boot_disk(iso9660_disk *, const char *);
int cd9660_eltorito_add_boot_option(iso9660_disk *, const char *,
const char *);
int cd9660_setup_boot(iso9660_disk *, int);
int cd9660_setup_boot_volume_descriptor(iso9660_disk *,
volume_descriptor *);
int cd9660_write_image(iso9660_disk *, const char *image);
int cd9660_copy_file(iso9660_disk *, FILE *, off_t, const char *);
void cd9660_compute_full_filename(cd9660node *, char *);
int cd9660_compute_record_size(iso9660_disk *, cd9660node *);
void debug_print_tree(iso9660_disk *, cd9660node *,int);
void debug_print_path_tree(cd9660node *);
void debug_print_volume_descriptor_information(iso9660_disk *);
void debug_dump_to_xml_ptentry(path_table_entry *,int, int);
void debug_dump_to_xml_path_table(FILE *, off_t, int, int);
void debug_dump_to_xml(FILE *);
int debug_get_encoded_number(const unsigned char *, int);
void debug_dump_integer(const char *, const unsigned char *, int);
void debug_dump_string(const char *, const unsigned char *, int);
void debug_dump_directory_record_9_1(unsigned char *);
void debug_dump_to_xml_volume_descriptor(unsigned char *,int);
void cd9660_pad_string_spaces(char *, int);
#endif