Ignore:
Timestamp:
Nov 4, 2007, 3:16:40 AM (16 years ago)
Author:
Bruno Cornec
Message:

Update to busybox 1.7.2

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.5/mindi-busybox/coreutils/uudecode.c

    r821 r1765  
     1/* vi: set sw=4 ts=4: */
    12/*
    23 *  Copyright 2003, Glenn McGrath <bug1@iinet.net.au>
     
    1213
    1314
    14 #include <stdio.h>
    15 #include <errno.h>
    16 #include <getopt.h> /* optind */
    17 #include <string.h>
    18 #include <stdlib.h>
    19 #include "busybox.h"
    20 
    21 static int read_stduu(FILE *src_stream, FILE *dst_stream)
     15#include "libbb.h"
     16
     17static void read_stduu(FILE *src_stream, FILE *dst_stream)
    2218{
    2319    char *line;
    2420
    25     while ((line = bb_get_chomped_line_from_file(src_stream)) != NULL) {
     21    while ((line = xmalloc_getline(src_stream)) != NULL) {
    2622        int length;
    2723        char *line_ptr = line;
    2824
    2925        if (strcmp(line, "end") == 0) {
    30             return(EXIT_SUCCESS);
     26            return;
    3127        }
    3228        length = ((*line_ptr - 0x20) & 0x3f)* 4 / 3;
     
    3733        }
    3834        if (length > 60) {
    39             bb_error_msg_and_die("Line too long");
     35            bb_error_msg_and_die("line too long");
    4036        }
    4137
    4238        line_ptr++;
    43         /* Tolerate an overly long line to acomadate a possible exta '`' */
     39        /* Tolerate an overly long line to accomodate a possible exta '`' */
    4440        if (strlen(line_ptr) < (size_t)length) {
    45             bb_error_msg_and_die("Short file");
     41            bb_error_msg_and_die("short file");
    4642        }
    4743
     
    6864        free(line);
    6965    }
    70     bb_error_msg_and_die("Short file");
     66    bb_error_msg_and_die("short file");
    7167}
    7268
    73 static int read_base64(FILE *src_stream, FILE *dst_stream)
     69static void read_base64(FILE *src_stream, FILE *dst_stream)
    7470{
    75     static const char base64_table[] =
    76         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n";
    77     char term_count = 0;
     71    int term_count = 1;
    7872
    7973    while (1) {
     
    8579            int ch;
    8680
    87             /* Get next _valid_ character */
     81            /* Get next _valid_ character.
     82             * global vector bb_uuenc_tbl_base64[] contains this string:
     83             * "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n"
     84             */
    8885            do {
    8986                ch = fgetc(src_stream);
    9087                if (ch == EOF) {
    91                     bb_error_msg_and_die("Short file");
     88                    bb_error_msg_and_die("short file");
    9289                }
    93             } while ((table_ptr = strchr(base64_table, ch)) == NULL);
    94 
    95             /* Convert encoded charcter to decimal */
    96             ch = table_ptr - base64_table;
     90                table_ptr = strchr(bb_uuenc_tbl_base64, ch);
     91            } while (table_ptr == NULL);
     92
     93            /* Convert encoded character to decimal */
     94            ch = table_ptr - bb_uuenc_tbl_base64;
    9795
    9896            if (*table_ptr == '=') {
    9997                if (term_count == 0) {
    100                     translated[count] = 0;
     98                    translated[count] = '\0';
    10199                    break;
    102100                }
    103101                term_count++;
    104             }
    105             else if (*table_ptr == '\n') {
     102            } else if (*table_ptr == '\n') {
    106103                /* Check for terminating line */
    107104                if (term_count == 5) {
    108                     return(EXIT_SUCCESS);
     105                    return;
    109106                }
    110107                term_count = 1;
     
    118115
    119116        /* Merge 6 bit chars to 8 bit */
    120         fputc(translated[0] << 2 | translated[1] >> 4, dst_stream);
     117        if (count > 1) {
     118            fputc(translated[0] << 2 | translated[1] >> 4, dst_stream);
     119        }
    121120        if (count > 2) {
    122121            fputc(translated[1] << 4 | translated[2] >> 2, dst_stream);
     
    128127}
    129128
     129int uudecode_main(int argc, char **argv);
    130130int uudecode_main(int argc, char **argv)
    131131{
    132     int (*decode_fn_ptr) (FILE * src, FILE * dst);
    133     FILE *src_stream;
     132    FILE *src_stream = stdin;
    134133    char *outname = NULL;
    135134    char *line;
    136     int opt;
    137 
    138     opt = bb_getopt_ulflags(argc, argv, "o:", &outname);
    139 
    140     if (optind == argc) {
    141         src_stream = stdin;
    142     } else if (optind + 1 == argc) {
    143         src_stream = bb_xfopen(argv[optind], "r");
    144     } else {
    145         bb_show_usage();
    146     }
     135
     136    opt_complementary = "?1"; /* 1 argument max */
     137    getopt32(argv, "o:", &outname);
     138    argv += optind;
     139
     140    if (argv[0])
     141        src_stream = xfopen(argv[0], "r");
    147142
    148143    /* Search for the start of the encoding */
    149     while ((line = bb_get_chomped_line_from_file(src_stream)) != NULL) {
    150         char *line_ptr = NULL;
    151 
    152         if (line == NULL) {
    153             break;
    154         } else if (strncmp(line, "begin-base64 ", 13) == 0) {
     144    while ((line = xmalloc_getline(src_stream)) != NULL) {
     145        void (*decode_fn_ptr)(FILE * src, FILE * dst);
     146        char *line_ptr;
     147        FILE *dst_stream;
     148        int mode;
     149
     150        if (strncmp(line, "begin-base64 ", 13) == 0) {
    155151            line_ptr = line + 13;
    156152            decode_fn_ptr = read_base64;
     
    158154            line_ptr = line + 6;
    159155            decode_fn_ptr = read_stduu;
    160         }
    161 
    162         if (line_ptr) {
    163             FILE *dst_stream;
    164             int mode;
    165             int ret;
    166 
    167             mode = strtoul(line_ptr, NULL, 8);
    168             if (outname == NULL) {
    169                 outname = strchr(line_ptr, ' ');
    170                 if ((outname == NULL) || (*outname == '\0')) {
    171                     break;
    172                 }
    173                 outname++;
    174             }
    175             if (strcmp(outname, "-") == 0) {
    176                 dst_stream = stdout;
    177             } else {
    178                 dst_stream = bb_xfopen(outname, "w");
    179                 chmod(outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO));
    180             }
     156        } else {
    181157            free(line);
    182             ret = decode_fn_ptr(src_stream, dst_stream);
    183             bb_fclose_nonstdin(src_stream);
    184             return(ret);
     158            continue;
     159        }
     160
     161        /* begin line found. decode and exit */
     162        mode = strtoul(line_ptr, NULL, 8);
     163        if (outname == NULL) {
     164            outname = strchr(line_ptr, ' ');
     165            if ((outname == NULL) || (*outname == '\0')) {
     166                break;
     167            }
     168            outname++;
     169        }
     170        dst_stream = stdout;
     171        if (NOT_LONE_DASH(outname)) {
     172            dst_stream = xfopen(outname, "w");
     173            chmod(outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO));
    185174        }
    186175        free(line);
     176        decode_fn_ptr(src_stream, dst_stream);
     177        /* fclose_if_not_stdin(src_stream); - redundant */
     178        return EXIT_SUCCESS;
    187179    }
    188     bb_error_msg_and_die("No `begin' line");
     180    bb_error_msg_and_die("no 'begin' line");
    189181}
     182
     183/* Test script.
     184Put this into an empty dir with busybox binary, an run.
     185
     186#!/bin/sh
     187test -x busybox || { echo "No ./busybox?"; exit; }
     188ln -sf busybox uudecode
     189ln -sf busybox uuencode
     190>A_null
     191echo -n A >A
     192echo -n AB >AB
     193echo -n ABC >ABC
     194echo -n ABCD >ABCD
     195echo -n ABCDE >ABCDE
     196echo -n ABCDEF >ABCDEF
     197cat busybox >A_bbox
     198for f in A*; do
     199    echo uuencode $f
     200    ./uuencode    $f <$f >u_$f
     201    ./uuencode -m $f <$f >m_$f
     202done
     203mkdir unpk_u unpk_m 2>/dev/null
     204for f in u_*; do
     205    ./uudecode <$f -o unpk_u/${f:2}
     206    diff -a ${f:2} unpk_u/${f:2} >/dev/null 2>&1
     207    echo uudecode $f: $?
     208done
     209for f in m_*; do
     210    ./uudecode <$f -o unpk_m/${f:2}
     211    diff -a ${f:2} unpk_m/${f:2} >/dev/null 2>&1
     212    echo uudecode $f: $?
     213done
     214*/
Note: See TracChangeset for help on using the changeset viewer.