Back to index

lightning-sunbird  0.9+nobinonly
mar_read.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* vim:set ts=2 sw=2 sts=2 et cindent: */
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla Archive code.
00017  *
00018  * The Initial Developer of the Original Code is Google Inc.
00019  * Portions created by the Initial Developer are Copyright (C) 2005
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *  Darin Fisher <darin@meer.net>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include <sys/types.h>
00040 #include <fcntl.h>
00041 #include <stdlib.h>
00042 #include <stdio.h>
00043 #include <string.h>
00044 #include "mar.h"
00045 #include "mar_private.h"
00046 
00047 #ifdef XP_WIN
00048 #include <winsock2.h>
00049 #else
00050 #include <netinet/in.h>
00051 #endif
00052 
00053 #define TABLESIZE 256
00054 
00055 struct MarFile_ {
00056   FILE *fp;
00057   MarItem *item_table[TABLESIZE];
00058 };
00059 
00060 /* this is the same hash algorithm used by nsZipArchive.cpp */
00061 static PRUint32 mar_hash_name(const char *name) {
00062   PRUint32 val = 0;
00063   unsigned char* c;
00064 
00065   for (c = (unsigned char *) name; *c; ++c)
00066     val = val*37 + *c;
00067 
00068   return val % TABLESIZE;
00069 }
00070 
00071 static int mar_insert_item(MarFile *mar, const char *name, int namelen,
00072                            PRUint32 offset, PRUint32 length, PRUint32 flags) {
00073   MarItem *item, *root;
00074   PRUint32 hash;
00075   
00076   item = (MarItem *) malloc(sizeof(MarItem) + namelen);
00077   if (!item)
00078     return -1;
00079   item->next = NULL;
00080   item->offset = offset;
00081   item->length = length;
00082   item->flags = flags;
00083   memcpy(item->name, name, namelen + 1);
00084 
00085   hash = mar_hash_name(name);
00086 
00087   root = mar->item_table[hash];
00088   if (!root) {
00089     mar->item_table[hash] = item;
00090   } else {
00091     /* append item */
00092     while (root->next)
00093       root = root->next;
00094     root->next = item;
00095   }
00096   return 0;
00097 }
00098 
00099 static int mar_consume_index(MarFile *mar, char **buf, const char *buf_end) {
00100   /*
00101    * Each item has the following structure:
00102    *   PRUint32 offset      (network byte order)
00103    *   PRUint32 length      (network byte order)
00104    *   PRUint32 flags       (network byte order)
00105    *   char     name[N]     (where N >= 1)
00106    *   char     null_byte;
00107    */
00108   PRUint32 offset;
00109   PRUint32 length;
00110   PRUint32 flags;
00111   const char *name;
00112   int namelen;
00113 
00114   if ((buf_end - *buf) < (int)(3*sizeof(PRUint32) + 2))
00115     return -1;
00116 
00117   memcpy(&offset, *buf, sizeof(offset));
00118   *buf += sizeof(offset);
00119 
00120   memcpy(&length, *buf, sizeof(length));
00121   *buf += sizeof(length);
00122 
00123   memcpy(&flags, *buf, sizeof(flags));
00124   *buf += sizeof(flags);
00125 
00126   offset = ntohl(offset);
00127   length = ntohl(length);
00128   flags = ntohl(flags);
00129 
00130   name = *buf;
00131   /* find namelen; must take care not to read beyond buf_end */
00132   while (**buf) {
00133     if (*buf == buf_end)
00134       return -1;
00135     ++(*buf);
00136   }
00137   namelen = (*buf - name);
00138   /* consume null byte */
00139   if (*buf == buf_end)
00140     return -1;
00141   ++(*buf);
00142 
00143   return mar_insert_item(mar, name, namelen, offset, length, flags);
00144 }
00145 
00146 static int mar_read_index(MarFile *mar) {
00147   char id[MAR_ID_SIZE], *buf, *bufptr, *bufend;
00148   PRUint32 offset_to_index, size_of_index;
00149 
00150   /* verify MAR ID */
00151   if (fread(id, MAR_ID_SIZE, 1, mar->fp) != 1)
00152     return -1;
00153   if (memcmp(id, MAR_ID, MAR_ID_SIZE) != 0)
00154     return -1;
00155 
00156   if (fread(&offset_to_index, sizeof(PRUint32), 1, mar->fp) != 1)
00157     return -1;
00158   offset_to_index = ntohl(offset_to_index);
00159 
00160   if (fseek(mar->fp, offset_to_index, SEEK_SET))
00161     return -1;
00162   if (fread(&size_of_index, sizeof(PRUint32), 1, mar->fp) != 1)
00163     return -1;
00164   size_of_index = ntohl(size_of_index);
00165 
00166   buf = (char *) malloc(size_of_index);
00167   if (!buf)
00168     return -1;
00169   if (fread(buf, size_of_index, 1, mar->fp) != 1) {
00170     free(buf);
00171     return -1;
00172   }
00173 
00174   bufptr = buf;
00175   bufend = buf + size_of_index;
00176   while (bufptr < bufend && mar_consume_index(mar, &bufptr, bufend) == 0);
00177 
00178   free(buf);
00179   return (bufptr == bufend) ? 0 : -1;
00180 }
00181 
00182 MarFile *mar_open(const char *path) {
00183   MarFile *mar;
00184   FILE *fp;
00185 
00186   fp = fopen(path, "rb");
00187   if (!fp)
00188     return NULL;
00189 
00190   mar = (MarFile *) malloc(sizeof(*mar));
00191   if (!mar) {
00192     fclose(fp);
00193     return NULL;
00194   }
00195 
00196   mar->fp = fp;
00197   memset(mar->item_table, 0, sizeof(mar->item_table));
00198   if (mar_read_index(mar)) {
00199     mar_close(mar);
00200     return NULL;
00201   }
00202 
00203   return mar;
00204 }
00205 
00206 void mar_close(MarFile *mar) {
00207   MarItem *item;
00208   int i;
00209 
00210   fclose(mar->fp);
00211 
00212   for (i = 0; i < TABLESIZE; ++i) {
00213     item = mar->item_table[i];
00214     while (item) {
00215       MarItem *temp = item;
00216       item = item->next;
00217       free(temp);
00218     }
00219   }
00220 
00221   free(mar);
00222 }
00223 
00224 const MarItem *mar_find_item(MarFile *mar, const char *name) {
00225   PRUint32 hash;
00226   const MarItem *item;
00227 
00228   hash = mar_hash_name(name);
00229 
00230   item = mar->item_table[hash];
00231   while (item && strcmp(item->name, name) != 0)
00232     item = item->next;
00233 
00234   return item;
00235 }
00236 
00237 int mar_enum_items(MarFile *mar, MarItemCallback callback, void *closure) {
00238   MarItem *item;
00239   int i;
00240 
00241   for (i = 0; i < TABLESIZE; ++i) {
00242     item = mar->item_table[i];
00243     while (item) {
00244       int rv = callback(mar, item, closure);
00245       if (rv)
00246         return rv;
00247       item = item->next;
00248     }
00249   }
00250 
00251   return 0;
00252 }
00253 
00254 int mar_read(MarFile *mar, const MarItem *item, int offset, char *buf,
00255              int bufsize) {
00256   int nr;
00257 
00258   if (offset == (int) item->length)
00259     return 0;
00260   if (offset > (int) item->length)
00261     return -1;
00262 
00263   nr = item->length - offset;
00264   if (nr > bufsize)
00265     nr = bufsize;
00266 
00267   if (fseek(mar->fp, item->offset + offset, SEEK_SET))
00268     return -1;
00269 
00270   return fread(buf, 1, nr, mar->fp);
00271 }