aboutsummaryrefslogtreecommitdiff
path: root/coreutils/uuencode.c
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2001-07-29 05:47:33 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2001-07-29 05:47:33 +0000
commiteba260587ad880535f8beee9402e05a11735c7a5 (patch)
tree73bc45829ee21f3dc9ff99b7b6884095f5fee6c9 /coreutils/uuencode.c
parent4e664ac51d974091097aaa436d72105e3446d90a (diff)
downloadbusybox-w32-eba260587ad880535f8beee9402e05a11735c7a5.tar.gz
busybox-w32-eba260587ad880535f8beee9402e05a11735c7a5.tar.bz2
busybox-w32-eba260587ad880535f8beee9402e05a11735c7a5.zip
Fix broken logic when wraping encoded data into rows, also simplifies it a bit
Diffstat (limited to 'coreutils/uuencode.c')
-rw-r--r--coreutils/uuencode.c34
1 files changed, 13 insertions, 21 deletions
diff --git a/coreutils/uuencode.c b/coreutils/uuencode.c
index 68d6f19a7..1789aefbb 100644
--- a/coreutils/uuencode.c
+++ b/coreutils/uuencode.c
@@ -56,7 +56,7 @@ static char tbl_std[64] = {
56 * buffer of at least 1+BASE64_LENGTH(length) bytes. 56 * buffer of at least 1+BASE64_LENGTH(length) bytes.
57 * where BASE64_LENGTH(len) = (4 * ((LENGTH + 2) / 3)) 57 * where BASE64_LENGTH(len) = (4 * ((LENGTH + 2) / 3))
58 */ 58 */
59static void base64_encode (const char *s, const char *store, const int length, const char *tbl) 59static void uuencode (const char *s, const char *store, const int length, const char *tbl)
60{ 60{
61 int i; 61 int i;
62 unsigned char *p = (unsigned char *)store; 62 unsigned char *p = (unsigned char *)store;
@@ -82,7 +82,7 @@ static void base64_encode (const char *s, const char *store, const int length, c
82 82
83int uuencode_main(int argc, char **argv) 83int uuencode_main(int argc, char **argv)
84{ 84{
85 const int src_buf_size = 600; // This *MUST* be a multiple of 3 85 const int src_buf_size = 60; // This *MUST* be a multiple of 3
86 const int dst_buf_size = 4 * ((src_buf_size + 2) / 3); 86 const int dst_buf_size = 4 * ((src_buf_size + 2) / 3);
87 RESERVE_BB_BUFFER(src_buf, src_buf_size + 1); 87 RESERVE_BB_BUFFER(src_buf, src_buf_size + 1);
88 RESERVE_BB_BUFFER(dst_buf, dst_buf_size + 1); 88 RESERVE_BB_BUFFER(dst_buf, dst_buf_size + 1);
@@ -93,7 +93,7 @@ int uuencode_main(int argc, char **argv)
93 mode_t mode; 93 mode_t mode;
94 int opt; 94 int opt;
95 int column = 0; 95 int column = 0;
96 int write_size; 96 int write_size = 0;
97 int remaining; 97 int remaining;
98 int buffer_offset = 0; 98 int buffer_offset = 0;
99 99
@@ -127,7 +127,7 @@ int uuencode_main(int argc, char **argv)
127 127
128 while ((size = fread(src_buf, 1, src_buf_size, src_stream)) > 0) { 128 while ((size = fread(src_buf, 1, src_buf_size, src_stream)) > 0) {
129 /* Encode the buffer we just read in */ 129 /* Encode the buffer we just read in */
130 base64_encode(src_buf, dst_buf, size, tbl); 130 uuencode(src_buf, dst_buf, size, tbl);
131 131
132 /* Write the buffer to stdout, wrapping at 60 chars. 132 /* Write the buffer to stdout, wrapping at 60 chars.
133 * This looks overly complex, but it gets tricky as 133 * This looks overly complex, but it gets tricky as
@@ -140,19 +140,20 @@ int uuencode_main(int argc, char **argv)
140 /* Initialise values for the new buffer */ 140 /* Initialise values for the new buffer */
141 remaining = 4 * ((size + 2) / 3); 141 remaining = 4 * ((size + 2) / 3);
142 buffer_offset = 0; 142 buffer_offset = 0;
143 if (remaining > (60 - column)) {
144 write_size = 60 - column;
145 }
146 else if (remaining < 60) {
147 write_size = remaining;
148 } else {
149 write_size = 60;
150 }
151 143
152 /* Write the buffer to stdout, wrapping at 60 chars 144 /* Write the buffer to stdout, wrapping at 60 chars
153 * starting from the column the last buffer ran out 145 * starting from the column the last buffer ran out
154 */ 146 */
155 do { 147 do {
148 if (remaining > (60 - column)) {
149 write_size = 60 - column;
150 }
151 else if (remaining < 60) {
152 write_size = remaining;
153 } else {
154 write_size = 60;
155 }
156
156 /* Setup a new row if required */ 157 /* Setup a new row if required */
157 if (column == 0) { 158 if (column == 0) {
158 putchar('\n'); 159 putchar('\n');
@@ -171,15 +172,6 @@ int uuencode_main(int argc, char **argv)
171 if (column % 60 == 0) { 172 if (column % 60 == 0) {
172 column = 0; 173 column = 0;
173 } 174 }
174
175 /* working next amount to write */
176 write_size = (60 - column) % 60;
177 if (write_size < remaining) {
178 write_size = remaining;
179 }
180 if (write_size == 0) {
181 write_size = 60;
182 }
183 } while (remaining > 0); 175 } while (remaining > 0);
184 } 176 }
185 printf(tbl == tbl_std ? "\n`\nend\n" : "\n====\n"); 177 printf(tbl == tbl_std ? "\n`\nend\n" : "\n====\n");