1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/modules/libmar/src/mar.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,180 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef MAR_H__ 1.11 +#define MAR_H__ 1.12 + 1.13 +#include "mozilla/Assertions.h" 1.14 +#include <stdint.h> 1.15 + 1.16 +#ifdef __cplusplus 1.17 +extern "C" { 1.18 +#endif 1.19 + 1.20 +/* We have a MAX_SIGNATURES limit so that an invalid MAR will never 1.21 + * waste too much of either updater's or signmar's time. 1.22 + * It is also used at various places internally and will affect memory usage. 1.23 + * If you want to increase this value above 9 then you need to adjust parsing 1.24 + * code in tool/mar.c. 1.25 +*/ 1.26 +#define MAX_SIGNATURES 8 1.27 +#ifdef __cplusplus 1.28 +static_assert(MAX_SIGNATURES <= 9, "too many signatures"); 1.29 +#else 1.30 +MOZ_STATIC_ASSERT(MAX_SIGNATURES <= 9, "too many signatures"); 1.31 +#endif 1.32 + 1.33 +struct ProductInformationBlock { 1.34 + const char *MARChannelID; 1.35 + const char *productVersion; 1.36 +}; 1.37 + 1.38 +/** 1.39 + * The MAR item data structure. 1.40 + */ 1.41 +typedef struct MarItem_ { 1.42 + struct MarItem_ *next; /* private field */ 1.43 + uint32_t offset; /* offset into archive */ 1.44 + uint32_t length; /* length of data in bytes */ 1.45 + uint32_t flags; /* contains file mode bits */ 1.46 + char name[1]; /* file path */ 1.47 +} MarItem; 1.48 + 1.49 +#define TABLESIZE 256 1.50 + 1.51 +struct MarFile_ { 1.52 + FILE *fp; 1.53 + MarItem *item_table[TABLESIZE]; 1.54 +}; 1.55 + 1.56 +typedef struct MarFile_ MarFile; 1.57 + 1.58 +/** 1.59 + * Signature of callback function passed to mar_enum_items. 1.60 + * @param mar The MAR file being visited. 1.61 + * @param item The MAR item being visited. 1.62 + * @param data The data parameter passed by the caller of mar_enum_items. 1.63 + * @return A non-zero value to stop enumerating. 1.64 + */ 1.65 +typedef int (* MarItemCallback)(MarFile *mar, const MarItem *item, void *data); 1.66 + 1.67 +/** 1.68 + * Open a MAR file for reading. 1.69 + * @param path Specifies the path to the MAR file to open. This path must 1.70 + * be compatible with fopen. 1.71 + * @return NULL if an error occurs. 1.72 + */ 1.73 +MarFile *mar_open(const char *path); 1.74 + 1.75 +#ifdef XP_WIN 1.76 +MarFile *mar_wopen(const wchar_t *path); 1.77 +#endif 1.78 + 1.79 +/** 1.80 + * Close a MAR file that was opened using mar_open. 1.81 + * @param mar The MarFile object to close. 1.82 + */ 1.83 +void mar_close(MarFile *mar); 1.84 + 1.85 +/** 1.86 + * Find an item in the MAR file by name. 1.87 + * @param mar The MarFile object to query. 1.88 + * @param item The name of the item to query. 1.89 + * @return A const reference to a MAR item or NULL if not found. 1.90 + */ 1.91 +const MarItem *mar_find_item(MarFile *mar, const char *item); 1.92 + 1.93 +/** 1.94 + * Enumerate all MAR items via callback function. 1.95 + * @param mar The MAR file to enumerate. 1.96 + * @param callback The function to call for each MAR item. 1.97 + * @param data A caller specified value that is passed along to the 1.98 + * callback function. 1.99 + * @return 0 if the enumeration ran to completion. Otherwise, any 1.100 + * non-zero return value from the callback is returned. 1.101 + */ 1.102 +int mar_enum_items(MarFile *mar, MarItemCallback callback, void *data); 1.103 + 1.104 +/** 1.105 + * Read from MAR item at given offset up to bufsize bytes. 1.106 + * @param mar The MAR file to read. 1.107 + * @param item The MAR item to read. 1.108 + * @param offset The byte offset relative to the start of the item. 1.109 + * @param buf A pointer to a buffer to copy the data into. 1.110 + * @param bufsize The length of the buffer to copy the data into. 1.111 + * @return The number of bytes written or a negative value if an 1.112 + * error occurs. 1.113 + */ 1.114 +int mar_read(MarFile *mar, const MarItem *item, int offset, char *buf, 1.115 + int bufsize); 1.116 + 1.117 +/** 1.118 + * Create a MAR file from a set of files. 1.119 + * @param dest The path to the file to create. This path must be 1.120 + * compatible with fopen. 1.121 + * @param numfiles The number of files to store in the archive. 1.122 + * @param files The list of null-terminated file paths. Each file 1.123 + * path must be compatible with fopen. 1.124 + * @param infoBlock The information to store in the product information block. 1.125 + * @return A non-zero value if an error occurs. 1.126 + */ 1.127 +int mar_create(const char *dest, 1.128 + int numfiles, 1.129 + char **files, 1.130 + struct ProductInformationBlock *infoBlock); 1.131 + 1.132 +/** 1.133 + * Extract a MAR file to the current working directory. 1.134 + * @param path The path to the MAR file to extract. This path must be 1.135 + * compatible with fopen. 1.136 + * @return A non-zero value if an error occurs. 1.137 + */ 1.138 +int mar_extract(const char *path); 1.139 + 1.140 +/** 1.141 + * Verifies a MAR file by verifying each signature with the corresponding 1.142 + * certificate. That is, the first signature will be verified using the first 1.143 + * certificate given, the second signature will be verified using the second 1.144 + * certificate given, etc. The signature count must exactly match the number of 1.145 + * certificates given, and all signature verifications must succeed. 1.146 + * We do not check that the certificate was issued by any trusted authority. 1.147 + * We assume it to be self-signed. We do not check whether the certificate 1.148 + * is valid for this usage. 1.149 + * 1.150 + * @param mar The already opened MAR file. 1.151 + * @param certData Pointer to the first element in an array of certificate 1.152 + * file data. 1.153 + * @param certDataSizes Pointer to the first element in an array for size of 1.154 + * the cert data. 1.155 + * @param certCount The number of elements in certData and certDataSizes 1.156 + * @return 0 on success 1.157 + * a negative number if there was an error 1.158 + * a positive number if the signature does not verify 1.159 + */ 1.160 +#ifdef XP_WIN 1.161 +int mar_verify_signaturesW(MarFile *mar, 1.162 + const uint8_t * const *certData, 1.163 + const uint32_t *certDataSizes, 1.164 + uint32_t certCount); 1.165 +#endif 1.166 + 1.167 +/** 1.168 + * Reads the product info block from the MAR file's additional block section. 1.169 + * The caller is responsible for freeing the fields in infoBlock 1.170 + * if the return is successful. 1.171 + * 1.172 + * @param infoBlock Out parameter for where to store the result to 1.173 + * @return 0 on success, -1 on failure 1.174 +*/ 1.175 +int 1.176 +mar_read_product_info_block(MarFile *mar, 1.177 + struct ProductInformationBlock *infoBlock); 1.178 + 1.179 +#ifdef __cplusplus 1.180 +} 1.181 +#endif 1.182 + 1.183 +#endif /* MAR_H__ */