michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef MAR_H__ michael@0: #define MAR_H__ michael@0: michael@0: #include "mozilla/Assertions.h" michael@0: #include michael@0: michael@0: #ifdef __cplusplus michael@0: extern "C" { michael@0: #endif michael@0: michael@0: /* We have a MAX_SIGNATURES limit so that an invalid MAR will never michael@0: * waste too much of either updater's or signmar's time. michael@0: * It is also used at various places internally and will affect memory usage. michael@0: * If you want to increase this value above 9 then you need to adjust parsing michael@0: * code in tool/mar.c. michael@0: */ michael@0: #define MAX_SIGNATURES 8 michael@0: #ifdef __cplusplus michael@0: static_assert(MAX_SIGNATURES <= 9, "too many signatures"); michael@0: #else michael@0: MOZ_STATIC_ASSERT(MAX_SIGNATURES <= 9, "too many signatures"); michael@0: #endif michael@0: michael@0: struct ProductInformationBlock { michael@0: const char *MARChannelID; michael@0: const char *productVersion; michael@0: }; michael@0: michael@0: /** michael@0: * The MAR item data structure. michael@0: */ michael@0: typedef struct MarItem_ { michael@0: struct MarItem_ *next; /* private field */ michael@0: uint32_t offset; /* offset into archive */ michael@0: uint32_t length; /* length of data in bytes */ michael@0: uint32_t flags; /* contains file mode bits */ michael@0: char name[1]; /* file path */ michael@0: } MarItem; michael@0: michael@0: #define TABLESIZE 256 michael@0: michael@0: struct MarFile_ { michael@0: FILE *fp; michael@0: MarItem *item_table[TABLESIZE]; michael@0: }; michael@0: michael@0: typedef struct MarFile_ MarFile; michael@0: michael@0: /** michael@0: * Signature of callback function passed to mar_enum_items. michael@0: * @param mar The MAR file being visited. michael@0: * @param item The MAR item being visited. michael@0: * @param data The data parameter passed by the caller of mar_enum_items. michael@0: * @return A non-zero value to stop enumerating. michael@0: */ michael@0: typedef int (* MarItemCallback)(MarFile *mar, const MarItem *item, void *data); michael@0: michael@0: /** michael@0: * Open a MAR file for reading. michael@0: * @param path Specifies the path to the MAR file to open. This path must michael@0: * be compatible with fopen. michael@0: * @return NULL if an error occurs. michael@0: */ michael@0: MarFile *mar_open(const char *path); michael@0: michael@0: #ifdef XP_WIN michael@0: MarFile *mar_wopen(const wchar_t *path); michael@0: #endif michael@0: michael@0: /** michael@0: * Close a MAR file that was opened using mar_open. michael@0: * @param mar The MarFile object to close. michael@0: */ michael@0: void mar_close(MarFile *mar); michael@0: michael@0: /** michael@0: * Find an item in the MAR file by name. michael@0: * @param mar The MarFile object to query. michael@0: * @param item The name of the item to query. michael@0: * @return A const reference to a MAR item or NULL if not found. michael@0: */ michael@0: const MarItem *mar_find_item(MarFile *mar, const char *item); michael@0: michael@0: /** michael@0: * Enumerate all MAR items via callback function. michael@0: * @param mar The MAR file to enumerate. michael@0: * @param callback The function to call for each MAR item. michael@0: * @param data A caller specified value that is passed along to the michael@0: * callback function. michael@0: * @return 0 if the enumeration ran to completion. Otherwise, any michael@0: * non-zero return value from the callback is returned. michael@0: */ michael@0: int mar_enum_items(MarFile *mar, MarItemCallback callback, void *data); michael@0: michael@0: /** michael@0: * Read from MAR item at given offset up to bufsize bytes. michael@0: * @param mar The MAR file to read. michael@0: * @param item The MAR item to read. michael@0: * @param offset The byte offset relative to the start of the item. michael@0: * @param buf A pointer to a buffer to copy the data into. michael@0: * @param bufsize The length of the buffer to copy the data into. michael@0: * @return The number of bytes written or a negative value if an michael@0: * error occurs. michael@0: */ michael@0: int mar_read(MarFile *mar, const MarItem *item, int offset, char *buf, michael@0: int bufsize); michael@0: michael@0: /** michael@0: * Create a MAR file from a set of files. michael@0: * @param dest The path to the file to create. This path must be michael@0: * compatible with fopen. michael@0: * @param numfiles The number of files to store in the archive. michael@0: * @param files The list of null-terminated file paths. Each file michael@0: * path must be compatible with fopen. michael@0: * @param infoBlock The information to store in the product information block. michael@0: * @return A non-zero value if an error occurs. michael@0: */ michael@0: int mar_create(const char *dest, michael@0: int numfiles, michael@0: char **files, michael@0: struct ProductInformationBlock *infoBlock); michael@0: michael@0: /** michael@0: * Extract a MAR file to the current working directory. michael@0: * @param path The path to the MAR file to extract. This path must be michael@0: * compatible with fopen. michael@0: * @return A non-zero value if an error occurs. michael@0: */ michael@0: int mar_extract(const char *path); michael@0: michael@0: /** michael@0: * Verifies a MAR file by verifying each signature with the corresponding michael@0: * certificate. That is, the first signature will be verified using the first michael@0: * certificate given, the second signature will be verified using the second michael@0: * certificate given, etc. The signature count must exactly match the number of michael@0: * certificates given, and all signature verifications must succeed. michael@0: * We do not check that the certificate was issued by any trusted authority. michael@0: * We assume it to be self-signed. We do not check whether the certificate michael@0: * is valid for this usage. michael@0: * michael@0: * @param mar The already opened MAR file. michael@0: * @param certData Pointer to the first element in an array of certificate michael@0: * file data. michael@0: * @param certDataSizes Pointer to the first element in an array for size of michael@0: * the cert data. michael@0: * @param certCount The number of elements in certData and certDataSizes michael@0: * @return 0 on success michael@0: * a negative number if there was an error michael@0: * a positive number if the signature does not verify michael@0: */ michael@0: #ifdef XP_WIN michael@0: int mar_verify_signaturesW(MarFile *mar, michael@0: const uint8_t * const *certData, michael@0: const uint32_t *certDataSizes, michael@0: uint32_t certCount); michael@0: #endif michael@0: michael@0: /** michael@0: * Reads the product info block from the MAR file's additional block section. michael@0: * The caller is responsible for freeing the fields in infoBlock michael@0: * if the return is successful. michael@0: * michael@0: * @param infoBlock Out parameter for where to store the result to michael@0: * @return 0 on success, -1 on failure michael@0: */ michael@0: int michael@0: mar_read_product_info_block(MarFile *mar, michael@0: struct ProductInformationBlock *infoBlock); michael@0: michael@0: #ifdef __cplusplus michael@0: } michael@0: #endif michael@0: michael@0: #endif /* MAR_H__ */