CTC++ Coverage Report - Execution Profile    #5/1532

Files Summary | Functions Summary | Execution Profile | Index | No Index
First | Previous | Next | Last


File: init/initramfs.c
Instrumentation mode: function-decision-multicondition
TER: 44 % (223/504)

Start/ End/    
True False - Line Source

  1 #include <linux/init.h>
  2 #include <linux/fs.h>
  3 #include <linux/slab.h>
  4 #include <linux/types.h>
  5 #include <linux/fcntl.h>
  6 #include <linux/delay.h>
  7 #include <linux/string.h>
  8 #include <linux/syscalls.h>
  9 
  10 static __initdata char *message;
 
- 11 static void __init error(char *x)
  12 {
- 13    if (!message)
  14       message = x;
  15 }
  16 
 
330   17 static void __init *malloc(size_t size)
  18 {
330    19    return kmalloc(size, GFP_KERNEL);
  20 }
  21 
 
330 330   22 static void __init free(void *where)
  23 {
  24    kfree(where);
  25 }
  26 
  27 /* link hash */
  28 
  29 static __initdata struct hash {
  30    int ino, minor, major;
  31    struct hash *next;
  32    char *name;
  33 } *head[32];
  34 
 
- 35 static inline int hash(int major, int minor, int ino)
  36 {
  37    unsigned long tmp = ino + minor + (major << 3);
  38    tmp += tmp >> 5;
 - 39    return tmp & 31;
  40 }
  41 
 
- 42 static char __init *find_link(int major, int minor, int ino, char *name)
  43 {
  44    struct hash **p, *q;
- 45    for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
- 46       if ((*p)->ino != ino)
 - 47          continue;
- 48       if ((*p)->minor != minor)
 - 49          continue;
- 50       if ((*p)->major != major)
 - 51          continue;
 - 52       return (*p)->name;
  53    }
  54    q = (struct hash *)malloc(sizeof(struct hash));
- 55    if (!q)
  56       panic("can't allocate link hash entry");
  57    q->ino = ino;
  58    q->minor = minor;
  59    q->major = major;
  60    q->name = name;
  61    q->next = NULL;
  62    *p = q;
 - 63    return NULL;
  64 }
  65 
 
  66 static void __init free_hash(void)
  67 {
  68    struct hash **p, *q;
96   69    for (p = head; p < head + 32; p++) {
96 - 70       while (*p) {
  71          q = *p;
  72          *p = q->next;
  73          free(q);
  74       }
  75    }
  76 }
  77 
  78 /* cpio header parsing */
  79 
  80 static __initdata unsigned long ino, major, minor, nlink;
  81 static __initdata mode_t mode;
  82 static __initdata unsigned long body_len, name_len;
  83 static __initdata uid_t uid;
  84 static __initdata gid_t gid;
  85 static __initdata unsigned rdev;
  86 
 
12 12   87 static void __init parse_header(char *s)
  88 {
  89    unsigned long parsed[12];
  90    char buf[9];
  91    int i;
  92 
  93    buf[8] = '\0';
144 12   94    for (i = 0, s += 6; i < 12; i++, s += 8) {
  95       memcpy(buf, s, 8);
  96       parsed[i] = simple_strtoul(buf, NULL, 16);
  97    }
  98    ino = parsed[0];
  99    mode = parsed[1];
  100    uid = parsed[2];
  101    gid = parsed[3];
  102    nlink = parsed[4];
  103    body_len = parsed[6];
  104    major = parsed[7];
  105    minor = parsed[8];
  106    rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
  107    name_len = parsed[11];
  108 }
  109 
  110 /* FSM */
  111 
  112 static __initdata enum state {
  113    Start,
  114    Collect,
  115    GotHeader,
  116    SkipIt,
  117    GotName,
  118    CopyFile,
  119    GotSymlink,
  120    Reset
  121 } state, next_state;
  122 
  123 static __initdata char *victim;
  124 static __initdata unsigned count;
  125 static __initdata loff_t this_header, next_header;
  126 
  127 static __initdata int dry_run;
  128 
 
132 132   129 static inline void eat(unsigned n)
  130 {
  131    victim += n;
  132    this_header += n;
  133    count -= n;
  134 }
  135 
  136 #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
  137 
  138 static __initdata char *collected;
  139 static __initdata int remains;
  140 static __initdata char *collect;
  141 
 
24 24   142 static void __init read_into(char *buf, unsigned size, enum state next)
  143 {
24 - 144    if (count >= size) {
  145       collected = victim;
  146       eat(size);
  147       state = next;
    148    } else {
  149       collect = collected = buf;
  150       remains = size;
  151       next_state = next;
  152       state = Collect;
  153    }
  154 }
  155 
  156 static __initdata char *header_buf, *symlink_buf, *name_buf;
  157 
 
12   158 static int __init do_start(void)
  159 {
  160    read_into(header_buf, 110, GotHeader);
12    161    return 0;
  162 }
  163 
 
- 164 static int __init do_collect(void)
  165 {
  166    unsigned n = remains;
- 167    if (count < n)
  168       n = count;
  169    memcpy(collect, victim, n);
  170    eat(n);
  171    collect += n;
- 172    if ((remains -= n) != 0)
 - 173       return 1;
  174    state = next_state;
 - 175    return 0;
  176 }
  177 
 
12   178 static int __init do_header(void)
  179 {
12 - 180    if (memcmp(collected, "070701", 6)) {
  181       error("no cpio magic");
 - 182       return 1;
  183    }
  184    parse_header(collected);
  185    next_header = this_header + N_ALIGN(name_len) + body_len;
  186    next_header = (next_header + 3) & ~3;
12 - 187    if (dry_run) {
  188       read_into(name_buf, N_ALIGN(name_len), GotName);
 - 189       return 0;
  190    }
  191    state = SkipIt;
12 - 192    if (name_len <= 0 || name_len > PATH_MAX)
 - 192   T || _
 - 192   F || T
 12   192   F || F
 - 193       return 0;
12 - 194    if (S_ISLNK(mode)) {
- 195       if (body_len > PATH_MAX)
 - 196          return 0;
  197       collect = collected = symlink_buf;
  198       remains = N_ALIGN(name_len) + body_len;
  199       next_state = GotSymlink;
  200       state = Collect;
 - 201       return 0;
  202    }
12 - 203    if (S_ISREG(mode) || !body_len)
 - 203   (T) || _
12    203   (F) || T
 - 203   (F) || F
  204       read_into(name_buf, N_ALIGN(name_len), GotName);
12    205    return 0;
  206 }
  207 
 
12   208 static int __init do_skip(void)
  209 {
12 - 210    if (this_header + count < next_header) {
  211       eat(count);
 - 212       return 1;
    213    } else {
  214       eat(next_header - this_header);
  215       state = next_state;
12    216       return 0;
  217    }
  218 }
  219 
 
12   220 static int __init do_reset(void)
  221 {
96 12   222    while(count && *victim == '\0')
96    222   T && T
   222   T && F
   222   F && _
  223       eat(1);
12 - 224    if (count && (this_header & 3))
 - 224   T && (T)
   224   T && (F)
   224   F && (_)
  225       error("broken padding");
12    226    return 1;
  227 }
  228 
 
  229 static int __init maybe_link(void)
  230 {
- 231    if (nlink >= 2) {
  232       char *old = find_link(major, minor, ino, collected);
- 233       if (old)
    234          return (sys_link(old, collected) < 0) ? -1 : 1;
- 234     ternary-?: ( sys_link ( old , collected ) ..
 - 234     return ( sys_link ( old , collected ) < 0 ..
  235    }
   236    return 0;
  237 }
  238 
  239 static __initdata int wfd;
  240 
 
12   241 static int __init do_name(void)
  242 {
  243    state = SkipIt;
  244    next_state = Reset;
  245    if (strcmp(collected, "TRAILER!!!") == 0) {
  246       free_hash();
   247       return 0;
  248    }
- 249    if (dry_run)
 - 250       return 0;
- 251    if (S_ISREG(mode)) {
- 252       if (maybe_link() >= 0) {
  253          wfd = sys_open(collected, O_WRONLY|O_CREAT, mode);
- 254          if (wfd >= 0) {
  255             sys_fchown(wfd, uid, gid);
  256             sys_fchmod(wfd, mode);
  257             state = CopyFile;
  258          }
  259       }
  260    } else if (S_ISDIR(mode)) {
  261       sys_mkdir(collected, mode);
  262       sys_chown(collected, uid, gid);
  263       sys_chmod(collected, mode);
  264    } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
- 265          S_ISFIFO(mode) || S_ISSOCK(mode)) {
 - 265   (T) || (_) || (_) || (_)
   265   (F) || (T) || (_) || (_)
 - 265   (F) || (F) || (T) || (_)
 - 265   (F) || (F) || (F) || (T)
 - 265   (F) || (F) || (F) || (F)
- 266       if (maybe_link() == 0) {
  267          sys_mknod(collected, mode, rdev);
  268          sys_chown(collected, uid, gid);
  269          sys_chmod(collected, mode);
  270       }
  271    }
   272    return 0;
  273 }
  274 
 
- 275 static int __init do_copy(void)
  276 {
- 277    if (count >= body_len) {
  278       sys_write(wfd, victim, body_len);
  279       sys_close(wfd);
  280       eat(body_len);
  281       state = SkipIt;
 - 282       return 0;
    283    } else {
  284       sys_write(wfd, victim, count);
  285       body_len -= count;
  286       eat(count);
 - 287       return 1;
  288    }
  289 }
  290 
 
- 291 static int __init do_symlink(void)
  292 {
  293    collected[N_ALIGN(name_len) + body_len] = '\0';
  294    sys_symlink(collected + N_ALIGN(name_len), collected);
  295    sys_lchown(collected, uid, gid);
  296    state = SkipIt;
  297    next_state = Reset;
 - 298    return 0;
  299 }
  300 
  301 static __initdata int (*actions[])(void) = {
  302    [Start]      = do_start,
  303    [Collect]   = do_collect,
  304    [GotHeader]   = do_header,
  305    [SkipIt]   = do_skip,
  306    [GotName]   = do_name,
  307    [CopyFile]   = do_copy,
  308    [GotSymlink]   = do_symlink,
  309    [Reset]      = do_reset,
  310 };
  311 
 
12   312 static int __init write_buffer(char *buf, unsigned len)
  313 {
  314    count = len;
  315    victim = buf;
  316 
48 12   317    while (!actions[state]())
  318       ;
12    319    return len - count;
  320 }
  321 
 
  322 static void __init flush_buffer(char *buf, unsigned len)
  323 {
  324    int written;
- 325    if (message)
 - 326       return;
  327    while ((written = write_buffer(buf, len)) < len && !message) {
   327   T && T
 - 327   T && F
   327   F && _
  328       char c = buf[written];
- 329       if (c == '0') {
  330          buf += written;
  331          len -= written;
  332          state = Start;
- 333       } else if (c == 0) {
  334          buf += written;
  335          len -= written;
  336          state = Reset;
    337       } else
#line 267 "init/../lib/inflate.c"
  1 #define DEBG(x)
  2 #define DEBG1(x)
  3 /* inflate.c -- Not copyrighted 1992 by Mark Adler
  4    version c10p1, 10 January 1993 */
  5 
  6 /* 
  7  * Adapted for booting Linux by Hannu Savolainen 1993
  8  * based on gzip-1.0.3 
  9  *
  10  * Nicolas Pitre <nico@cam.org>, 1999/04/14 :
  11  *   Little mods for all variable to reside either into rodata or bss segments
  12  *   by marking constant variables with 'const' and initializing all the others
  13  *   at run-time only.  This allows for the kernel uncompressor to run
  14  *   directly from Flash or ROM memory on embedded systems.
  15  */
  16 
  17 /*
  18    Inflate deflated (PKZIP's method 8 compressed) data.  The compression
  19    method searches for as much of the current string of bytes (up to a
  20    length of 258) in the previous 32 K bytes.  If it doesn't find any
  21    matches (of at least length 3), it codes the next byte.  Otherwise, it
  22    codes the length of the matched string and its distance backwards from
  23    the current position.  There is a single Huffman code that codes both
  24    single bytes (called "literals") and match lengths.  A second Huffman
  25    code codes the distance information, which follows a length code.  Each
  26    length or distance code actually represents a base value and a number
  27    of "extra" (sometimes zero) bits to get to add to the base value.  At
  28    the end of each deflated block is a special end-of-block (EOB) literal/
  29    length code.  The decoding process is basically: get a literal/length
  30    code; if EOB then done; if a literal, emit the decoded byte; if a
  31    length then get the distance and emit the referred-to bytes from the
  32    sliding window of previously emitted data.
  33 
  34    There are (currently) three kinds of inflate blocks: stored, fixed, and
  35    dynamic.  The compressor deals with some chunk of data at a time, and
  36    decides which method to use on a chunk-by-chunk basis.  A chunk might
  37    typically be 32 K or 64 K.  If the chunk is incompressible, then the
  38    "stored" method is used.  In this case, the bytes are simply stored as
  39    is, eight bits per byte, with none of the above coding.  The bytes are
  40    preceded by a count, since there is no longer an EOB code.
  41 
  42    If the data is compressible, then either the fixed or dynamic methods
  43    are used.  In the dynamic method, the compressed data is preceded by
  44    an encoding of the literal/length and distance Huffman codes that are
  45    to be used to decode this block.  The representation is itself Huffman
  46    coded, and so is preceded by a description of that code.  These code
  47    descriptions take up a little space, and so for small blocks, there is
  48    a predefined set of codes, called the fixed codes.  The fixed method is
  49    used if the block codes up smaller that way (usually for quite small
  50    chunks), otherwise the dynamic method is used.  In the latter case, the
  51    codes are customized to the probabilities in the current block, and so
  52    can code it much better than the pre-determined fixed codes.
  53  
  54    The Huffman codes themselves are decoded using a multi-level table
  55    lookup, in order to maximize the speed of decoding plus the speed of
  56    building the decoding tables.  See the comments below that precede the
  57    lbits and dbits tuning parameters.
  58  */
  59 
  60 
  61 /*
  62    Notes beyond the 1.93a appnote.txt:
  63 
  64    1. Distance pointers never point before the beginning of the output
  65       stream.
  66    2. Distance pointers can point back across blocks, up to 32k away.
  67    3. There is an implied maximum of 7 bits for the bit length table and
  68       15 bits for the actual data.
  69    4. If only one code exists, then it is encoded using one bit.  (Zero
  70       would be more efficient, but perhaps a little confusing.)  If two
  71       codes exist, they are coded using one bit each (0 and 1).
  72    5. There is no way of sending zero distance codes--a dummy must be
  73       sent if there are none.  (History: a pre 2.0 version of PKZIP would
  74       store blocks with no distance codes, but this was discovered to be
  75       too harsh a criterion.)  Valid only for 1.93a.  2.04c does allow
  76       zero distance codes, which is sent as one code of zero bits in
  77       length.
  78    6. There are up to 286 literal/length codes.  Code 256 represents the
  79       end-of-block.  Note however that the static length tree defines
  80       288 codes just to fill out the Huffman codes.  Codes 286 and 287
  81       cannot be used though, since there is no length base or extra bits
  82       defined for them.  Similarly, there are up to 30 distance codes.
  83       However, static trees define 32 codes (all 5 bits) to fill out the
  84       Huffman codes, but the last two had better not show up in the data.
  85    7. Unzip can check dynamic Huffman blocks for complete code sets.
  86       The exception is that a single code would not be complete (see #4).
  87    8. The five bits following the block type is really the number of
  88       literal codes sent minus 257.
  89    9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
  90       (1+6+6).  Therefore, to output three times the length, you output
  91       three codes (1+1+1), whereas to output four times the same length,
  92       you only need two codes (1+3).  Hmm.
  93   10. In the tree reconstruction algorithm, Code = Code + Increment
  94       only if BitLength(i) is not zero.  (Pretty obvious.)
  95   11. Correction: 4 Bits: # of Bit Length codes - 4     (4 - 19)
  96   12. Note: length code 284 can represent 227-258, but length code 285
  97       really is 258.  The last length deserves its own, short code
  98       since it gets used a lot in very redundant files.  The length
  99       258 is special since 258 - 3 (the min match length) is 255.
  100   13. The literal/length and distance code bit lengths are read as a
  101       single stream of lengths.  It is possible (and advantageous) for
  102       a repeat code (16, 17, or 18) to go across the boundary between
  103       the two sets of lengths.
  104  */
  105 #include <linux/compiler.h>
  106 
  107 #ifdef RCSID
  108 static char rcsid[] = "#Id: inflate.c,v 0.14 1993/06/10 13:27:04 jloup Exp #";
  109 #endif
  110 
  111 #ifndef STATIC
  112 
  113 #if defined(STDC_HEADERS) || defined(HAVE_STDLIB_H)
  114 #  include <sys/types.h>
  115 #  include <stdlib.h>
  116 #endif
  117 
  118 #include "gzip.h"
  119 #define STATIC
  120 #endif /* !STATIC */
  121 
  122 #ifndef INIT
  123 #define INIT
  124 #endif
  125    
  126 #define slide window
  127 
  128 /* Huffman code lookup table entry--this entry is four bytes for machines
  129    that have 16-bit pointers (e.g. PC's in the small or medium model).
  130    Valid extra bits are 0..13.  e == 15 is EOB (end of block), e == 16
  131    means that v is a literal, 16 < e < 32 means that v is a pointer to
  132    the next table, which codes e - 16 bits, and lastly e == 99 indicates
  133    an unused code.  If a code with e == 99 is looked up, this implies an
  134    error in the data. */
  135 struct huft {
  136   uch e;                /* number of extra bits or operation */
  137   uch b;                /* number of bits in this code or subcode */
  138   union {
  139     ush n;              /* literal, length base, or distance base */
  140     struct huft *t;     /* pointer to next level of table */
  141   } v;
  142 };
  143 
  144 
  145 /* Function prototypes */
  146 STATIC int INIT huft_build OF((unsigned *, unsigned, unsigned, 
  147       const ush *, const ush *, struct huft **, int *));
  148 STATIC int INIT huft_free OF((struct huft *));
  149 STATIC int INIT inflate_codes OF((struct huft *, struct huft *, int, int));
  150 STATIC int INIT inflate_stored OF((void));
  151 STATIC int INIT inflate_fixed OF((void));
  152 STATIC int INIT inflate_dynamic OF((void));
  153 STATIC int INIT inflate_block OF((int *));
  154 STATIC int INIT inflate OF((void));
  155 
  156 
  157 /* The inflate algorithm uses a sliding 32 K byte window on the uncompressed
  158    stream to find repeated byte strings.  This is implemented here as a
  159    circular buffer.  The index is updated simply by incrementing and then
  160    ANDing with 0x7fff (32K-1). */
  161 /* It is left to other modules to supply the 32 K area.  It is assumed
  162    to be usable as if it were declared "uch slide[32768];" or as just
  163    "uch *slide;" and then malloc'ed in the latter case.  The definition
  164    must be in unzip.h, included above. */
  165 /* unsigned wp;             current position in slide */
  166 #define wp outcnt
  167 #define flush_output(w) (wp=(w),flush_window())
  168 
  169 /* Tables for deflate from PKZIP's appnote.txt. */
  170 static const unsigned border[] = {    /* Order of the bit length code lengths */
  171         16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  172 static const ush cplens[] = {         /* Copy lengths for literal codes 257..285 */
  173         3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  174         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
  175         /* note: see note #13 above about the 258 in this list. */
  176 static const ush cplext[] = {         /* Extra bits for literal codes 257..285 */
  177         0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,