2012-06-07 01:41:21 +00:00
|
|
|
/*
|
|
|
|
* ascii85 - Ascii85 encode/decode data and print to standard output
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Remy Oukaour
|
2017-01-15 22:36:19 +00:00
|
|
|
* Updated by pancake in 2017
|
2012-06-07 01:41:21 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
2012-10-19 22:31:18 +00:00
|
|
|
#include <r_types.h>
|
2012-06-07 01:41:21 +00:00
|
|
|
|
2012-10-19 22:31:18 +00:00
|
|
|
static int getc_nospace(FILE *f) {
|
2012-06-07 01:41:21 +00:00
|
|
|
int c;
|
2017-01-15 22:36:19 +00:00
|
|
|
while (isspace (c = getc (f)));
|
2012-06-07 01:41:21 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2012-10-19 22:31:18 +00:00
|
|
|
static void putc_wrap(char c, int wrap, int *len) {
|
2012-06-07 01:41:21 +00:00
|
|
|
if (wrap && *len >= wrap) {
|
2017-01-15 22:36:19 +00:00
|
|
|
putchar ('\n');
|
2012-06-07 01:41:21 +00:00
|
|
|
*len = 0;
|
|
|
|
}
|
|
|
|
putchar(c);
|
|
|
|
(*len)++;
|
|
|
|
}
|
|
|
|
|
2012-10-19 22:31:18 +00:00
|
|
|
static void encode_tuple(unsigned long tuple, int count, int wrap, int *plen, int y_abbr) {
|
2012-06-07 01:41:21 +00:00
|
|
|
int i, lim;
|
|
|
|
char out[5];
|
|
|
|
if (tuple == 0 && count == 4) {
|
|
|
|
putc_wrap('z', wrap, plen);
|
|
|
|
}
|
|
|
|
else if (tuple == 0x20202020 && count == 4 && y_abbr) {
|
|
|
|
putc_wrap('y', wrap, plen);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (i = 0; i < 5; i++) {
|
|
|
|
out[i] = tuple % 85 + '!';
|
|
|
|
tuple /= 85;
|
|
|
|
}
|
|
|
|
lim = 4 - count;
|
|
|
|
for (i = 4; i >= lim; i--) {
|
|
|
|
putc_wrap(out[i], wrap, plen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-19 22:31:18 +00:00
|
|
|
R_API void r_base85_decode_tuple(unsigned long tuple, int count) {
|
2012-06-07 01:41:21 +00:00
|
|
|
int i;
|
|
|
|
for (i = 1; i < count; i++) {
|
|
|
|
putchar(tuple >> ((4 - i) * 8));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-19 22:31:18 +00:00
|
|
|
R_API void r_base85_encode(FILE *fp, int delims, int wrap, int y_abbr) {
|
2012-06-07 01:41:21 +00:00
|
|
|
int c, count = 0, len = 0;
|
|
|
|
unsigned long tuple = 0;
|
|
|
|
if (delims) {
|
2017-01-15 22:36:19 +00:00
|
|
|
putc_wrap ('<', wrap, &len);
|
|
|
|
putc_wrap ('~', wrap, &len);
|
2012-06-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
c = getc(fp);
|
|
|
|
if (c != EOF) {
|
|
|
|
tuple |= c << ((3 - count++) * 8);
|
2017-01-15 22:36:19 +00:00
|
|
|
if (count < 4) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if (count == 0) {
|
|
|
|
break;
|
2012-06-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
encode_tuple(tuple, count, wrap, &len, y_abbr);
|
2017-01-15 22:36:19 +00:00
|
|
|
if (c == EOF) {
|
|
|
|
break;
|
|
|
|
}
|
2012-06-07 01:41:21 +00:00
|
|
|
tuple = 0;
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
if (delims) {
|
2017-01-15 22:36:19 +00:00
|
|
|
putc_wrap ('~', wrap, &len);
|
|
|
|
putc_wrap ('>', wrap, &len);
|
2012-06-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-15 22:36:19 +00:00
|
|
|
R_API bool r_base85_decode(FILE *fp, int delims, int ignore_garbage) {
|
2012-06-07 01:41:21 +00:00
|
|
|
int c, count = 0, end = 0;
|
|
|
|
unsigned long tuple = 0, pows[] = {85*85*85*85, 85*85*85, 85*85, 85, 1};
|
|
|
|
while (delims) {
|
2017-01-15 22:36:19 +00:00
|
|
|
c = getc_nospace (fp);
|
2012-06-07 01:41:21 +00:00
|
|
|
if (c == '<') {
|
2017-01-15 22:36:19 +00:00
|
|
|
c = getc_nospace (fp);
|
|
|
|
if (c == '~') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ungetc (c, fp);
|
|
|
|
} else if (c == EOF) {
|
|
|
|
eprint ("ascii85: missing <~");
|
|
|
|
return false;
|
2012-06-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
c = getc_nospace(fp);
|
|
|
|
if (c == 'z' && count == 0) {
|
2012-10-19 22:31:18 +00:00
|
|
|
r_base85_decode_tuple (0, 5);
|
2012-06-07 01:41:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c == 'y' && count == 0) {
|
2012-10-19 22:31:18 +00:00
|
|
|
r_base85_decode_tuple (0x20202020, 5);
|
2012-06-07 01:41:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c == '~' && delims) {
|
2012-10-19 22:31:18 +00:00
|
|
|
c = getc_nospace (fp);
|
2012-06-07 01:41:21 +00:00
|
|
|
if (c != '>') {
|
2017-01-15 22:36:19 +00:00
|
|
|
eprint ("ascii85: ~ without >\n");
|
|
|
|
return false;
|
2012-06-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
c = EOF;
|
|
|
|
end = 1;
|
|
|
|
}
|
|
|
|
if (c == EOF) {
|
|
|
|
if (delims && !end) {
|
2017-01-15 22:36:19 +00:00
|
|
|
eprint ("ascii85: missing ~>");
|
|
|
|
return false;
|
2012-06-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
if (count > 0) {
|
|
|
|
tuple += pows[count-1];
|
2012-10-19 22:31:18 +00:00
|
|
|
r_base85_decode_tuple(tuple, count);
|
2012-06-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (c < '!' || c > 'u') {
|
2017-01-15 22:36:19 +00:00
|
|
|
if (ignore_garbage) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
eprintf ("ascii85: invalid character '%c'\n", c);
|
|
|
|
return false;
|
2012-06-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
tuple += (c - '!') * pows[count++];
|
|
|
|
if (count == 5) {
|
2012-10-19 22:31:18 +00:00
|
|
|
r_base85_decode_tuple(tuple, count);
|
2012-06-07 01:41:21 +00:00
|
|
|
tuple = 0;
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
}
|
2017-01-15 22:36:19 +00:00
|
|
|
return true;
|
2012-06-07 01:41:21 +00:00
|
|
|
}
|