Merge pull request #177 from bptato/inlineimages
Kitty inline image & base64 encoding fixes
This commit is contained in:
@@ -2008,32 +2008,25 @@ void (*mySignal(int signal_number, void (*action) (int))) (int) {
|
|||||||
static char Base64Table[] =
|
static char Base64Table[] =
|
||||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||||
|
|
||||||
char *
|
Str
|
||||||
base64_encode(const unsigned char *src, size_t len)
|
base64_encode(const unsigned char *src, size_t len)
|
||||||
{
|
{
|
||||||
unsigned char *w, *at;
|
Str dest;
|
||||||
const unsigned char *in, *endw;
|
const unsigned char *in, *endw;
|
||||||
unsigned long j;
|
unsigned long j;
|
||||||
size_t k;
|
size_t k;
|
||||||
|
|
||||||
|
|
||||||
if (!len)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
k = len;
|
k = len;
|
||||||
if (k % 3)
|
if (k % 3)
|
||||||
k += 3 - (k % 3);
|
k += 3 - (k % 3);
|
||||||
|
|
||||||
k = k / 3 * 4;
|
k = k / 3 * 4;
|
||||||
|
|
||||||
if (k + 1 < len)
|
if (!len || k + 1 < len)
|
||||||
return NULL;
|
return Strnew();
|
||||||
|
|
||||||
w = GC_MALLOC_ATOMIC(k + 1);
|
dest = Strnew_size(k);
|
||||||
if (!w)
|
|
||||||
return NULL;
|
|
||||||
w[k] = 0;
|
|
||||||
|
|
||||||
at = w;
|
|
||||||
in = src;
|
in = src;
|
||||||
|
|
||||||
endw = src + len - 2;
|
endw = src + len - 2;
|
||||||
@@ -2043,30 +2036,28 @@ base64_encode(const unsigned char *src, size_t len)
|
|||||||
j = j << 8 | *in++;
|
j = j << 8 | *in++;
|
||||||
j = j << 8 | *in++;
|
j = j << 8 | *in++;
|
||||||
|
|
||||||
*at++ = Base64Table[(j >> 18) & 0x3f];
|
Strcat_char(dest, Base64Table[(j >> 18) & 0x3f]);
|
||||||
*at++ = Base64Table[(j >> 12) & 0x3f];
|
Strcat_char(dest, Base64Table[(j >> 12) & 0x3f]);
|
||||||
*at++ = Base64Table[(j >> 6) & 0x3f];
|
Strcat_char(dest, Base64Table[(j >> 6) & 0x3f]);
|
||||||
*at++ = Base64Table[j & 0x3f];
|
Strcat_char(dest, Base64Table[j & 0x3f]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (in - src - len) {
|
if (src + len - in) {
|
||||||
if (in - src - len == 1) {
|
j = *in++;
|
||||||
j = *in++;
|
if (src + len - in) {
|
||||||
j = j << 8;
|
|
||||||
j = j << 8;
|
|
||||||
*at++ = Base64Table[(j >> 18) & 0x3f];
|
|
||||||
*at++ = Base64Table[(j >> 12) & 0x3f];
|
|
||||||
*at++ = '=';
|
|
||||||
*at++ = '=';
|
|
||||||
} else {
|
|
||||||
j = *in++;
|
|
||||||
j = j << 8 | *in++;
|
j = j << 8 | *in++;
|
||||||
j = j << 8;
|
j = j << 8;
|
||||||
*at++ = Base64Table[(j >> 18) & 0x3f];
|
Strcat_char(dest, Base64Table[(j >> 18) & 0x3f]);
|
||||||
*at++ = Base64Table[(j >> 12) & 0x3f];
|
Strcat_char(dest, Base64Table[(j >> 12) & 0x3f]);
|
||||||
*at++ = Base64Table[(j >> 6) & 0x3f];
|
Strcat_char(dest, Base64Table[(j >> 6) & 0x3f]);
|
||||||
*at++ = '=';
|
} else {
|
||||||
|
j = j << 8;
|
||||||
|
j = j << 8;
|
||||||
|
Strcat_char(dest, Base64Table[(j >> 18) & 0x3f]);
|
||||||
|
Strcat_char(dest, Base64Table[(j >> 12) & 0x3f]);
|
||||||
|
Strcat_char(dest, '=');
|
||||||
}
|
}
|
||||||
|
Strcat_char(dest, '=');
|
||||||
}
|
}
|
||||||
return (char *)w;
|
return dest;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1179,11 +1179,7 @@ AuthBasicCred(struct http_auth *ha, Str uname, Str pw, ParsedURL *pu,
|
|||||||
Str s = Strdup(uname);
|
Str s = Strdup(uname);
|
||||||
Strcat_char(s, ':');
|
Strcat_char(s, ':');
|
||||||
Strcat(s, pw);
|
Strcat(s, pw);
|
||||||
char *base64 = base64_encode(s->ptr, s->length);
|
return Strnew_m_charp("Basic ", base64_encode(s->ptr, s->length)->ptr, NULL);
|
||||||
if (!base64)
|
|
||||||
return Strnew_charp("Basic ");
|
|
||||||
else
|
|
||||||
return Strnew_m_charp("Basic ", base64, NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef USE_DIGEST_AUTH
|
#ifdef USE_DIGEST_AUTH
|
||||||
|
|||||||
@@ -829,4 +829,4 @@ void srand48(long);
|
|||||||
long lrand48(void);
|
long lrand48(void);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
extern char *base64_encode(const unsigned char *src, size_t len);
|
extern Str base64_encode(const unsigned char *src, size_t len);
|
||||||
|
|||||||
@@ -491,7 +491,7 @@ void
|
|||||||
put_image_iterm2(char *url, int x, int y, int w, int h)
|
put_image_iterm2(char *url, int x, int y, int w, int h)
|
||||||
{
|
{
|
||||||
Str buf;
|
Str buf;
|
||||||
char *base64, *cbuf;
|
char *cbuf;
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
int c, i;
|
int c, i;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
@@ -518,32 +518,25 @@ put_image_iterm2(char *url, int x, int y, int w, int h)
|
|||||||
writestr(buf->ptr);
|
writestr(buf->ptr);
|
||||||
|
|
||||||
cbuf = GC_MALLOC_ATOMIC(3072);
|
cbuf = GC_MALLOC_ATOMIC(3072);
|
||||||
|
if (!cbuf)
|
||||||
|
goto cleanup;
|
||||||
i = 0;
|
i = 0;
|
||||||
while ((c = fgetc(fp)) != EOF) {
|
while ((c = fgetc(fp)) != EOF) {
|
||||||
cbuf[i++] = c;
|
cbuf[i++] = c;
|
||||||
if (i == 3072) {
|
if (i == 3072) {
|
||||||
base64 = base64_encode(cbuf, i);
|
buf = base64_encode(cbuf, i);
|
||||||
if (!base64) {
|
writestr(buf->ptr);
|
||||||
writestr("\a");
|
|
||||||
fclose(fp);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
writestr(base64);
|
|
||||||
i = 0;
|
i = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(fp);
|
|
||||||
|
|
||||||
if (i) {
|
if (i) {
|
||||||
base64 = base64_encode(cbuf, i);
|
buf = base64_encode(cbuf, i);
|
||||||
if (!base64) {
|
writestr(buf->ptr);
|
||||||
writestr("\a");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
writestr(base64);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
fclose(fp);
|
||||||
writestr("\a");
|
writestr("\a");
|
||||||
MOVE(Currentbuf->cursorY,Currentbuf->cursorX);
|
MOVE(Currentbuf->cursorY,Currentbuf->cursorX);
|
||||||
}
|
}
|
||||||
@@ -555,11 +548,11 @@ void
|
|||||||
put_image_kitty(char *url, int x, int y, int w, int h, int sx, int sy, int sw,
|
put_image_kitty(char *url, int x, int y, int w, int h, int sx, int sy, int sw,
|
||||||
int sh, int cols, int rows)
|
int sh, int cols, int rows)
|
||||||
{
|
{
|
||||||
Str buf;
|
Str buf, base64;
|
||||||
char *base64, *cbuf, *type, *tmpf;
|
char *cbuf, *type, *tmpf;
|
||||||
char *argv[4];
|
char *argv[4];
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
int c, i, j, k, t;
|
int c, i, j, m, t, is_anim;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
MySignalHandler(*volatile previntr) (SIGNAL_ARG);
|
MySignalHandler(*volatile previntr) (SIGNAL_ARG);
|
||||||
@@ -573,9 +566,14 @@ put_image_kitty(char *url, int x, int y, int w, int h, int sx, int sy, int sw,
|
|||||||
t = 100; /* always convert to png for now. */
|
t = 100; /* always convert to png for now. */
|
||||||
|
|
||||||
if(!(type && !strcasecmp(type, "image/png"))) {
|
if(!(type && !strcasecmp(type, "image/png"))) {
|
||||||
buf = Strnew();
|
|
||||||
tmpf = Sprintf("%s/%s.png", tmp_dir, mybasename(url))->ptr;
|
tmpf = Sprintf("%s/%s.png", tmp_dir, mybasename(url))->ptr;
|
||||||
|
|
||||||
|
if (!strcasecmp(type, "image/gif")) {
|
||||||
|
is_anim = 1;
|
||||||
|
} else {
|
||||||
|
is_anim = 0;
|
||||||
|
}
|
||||||
|
|
||||||
/* convert only if png doesn't exist yet. */
|
/* convert only if png doesn't exist yet. */
|
||||||
|
|
||||||
if (stat(tmpf, &st)) {
|
if (stat(tmpf, &st)) {
|
||||||
@@ -599,7 +597,13 @@ put_image_kitty(char *url, int x, int y, int w, int h, int sx, int sy, int sw,
|
|||||||
else
|
else
|
||||||
argv[i++] = "convert";
|
argv[i++] = "convert";
|
||||||
|
|
||||||
argv[i++] = url;
|
if (is_anim) {
|
||||||
|
buf = Strnew_charp(url);
|
||||||
|
Strcat_charp(buf, "[0]");
|
||||||
|
argv[i++] = buf->ptr;
|
||||||
|
} else {
|
||||||
|
argv[i++] = url;
|
||||||
|
}
|
||||||
argv[i++] = tmpf;
|
argv[i++] = tmpf;
|
||||||
argv[i++] = NULL;
|
argv[i++] = NULL;
|
||||||
execvp(argv[0],argv);
|
execvp(argv[0],argv);
|
||||||
@@ -626,67 +630,50 @@ put_image_kitty(char *url, int x, int y, int w, int h, int sx, int sy, int sw,
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
MOVE(y, x);
|
MOVE(y, x);
|
||||||
buf = Sprintf("\x1b_Gf=100,s=%d,v=%d,a=T,m=1,x=%d,y=%d,"
|
|
||||||
"w=%d,h=%d,c=%d,r=%d;",
|
|
||||||
w, h, sx, sy, sw, sh, cols, rows);
|
|
||||||
|
|
||||||
|
|
||||||
cbuf = GC_MALLOC_ATOMIC(3072);
|
cbuf = GC_MALLOC_ATOMIC(3072); /* base64-encoded chunks of 4096 bytes */
|
||||||
|
if (!cbuf)
|
||||||
|
goto cleanup;
|
||||||
i = 0;
|
i = 0;
|
||||||
j = buf->length;
|
|
||||||
|
|
||||||
while (buf->length + i / 3 * 4 < 4096 && (c = fgetc(fp)) != EOF) {
|
while (i < 3072 && (c = fgetc(fp)) != EOF)
|
||||||
cbuf[i++] = c;
|
cbuf[i++] = c;
|
||||||
}
|
|
||||||
|
|
||||||
base64 = base64_encode(cbuf, i);
|
base64 = base64_encode(cbuf, i);
|
||||||
if (!base64) {
|
|
||||||
fclose(fp);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c == EOF)
|
if (c == EOF)
|
||||||
buf = Sprintf("\x1b_Gf=100,s=%d,v=%d,a=T,m=0,x=%d,y=%d,"
|
m = 0;
|
||||||
"w=%d,h=%d,c=%d,r=%d;",
|
else
|
||||||
w, h, sx, sy, sw, sh, cols, rows);
|
m = 1;
|
||||||
|
buf = Sprintf("\x1b_Gf=%d,s=%d,v=%d,a=T,m=%d,x=%d,y=%d,w=%d,h=%d,c=%d,r=%d;"
|
||||||
|
"%s\x1b\\", t, w, h, m, sx, sy, sw, sh, cols, rows, base64->ptr);
|
||||||
writestr(buf->ptr);
|
writestr(buf->ptr);
|
||||||
|
|
||||||
buf = Sprintf("%s\x1b\\", base64);
|
if (m) {
|
||||||
writestr(buf->ptr);
|
i = 0;
|
||||||
|
j = 0;
|
||||||
|
while ((c = fgetc(fp)) != EOF) {
|
||||||
|
if (j) {
|
||||||
|
base64 = base64_encode(cbuf, i);
|
||||||
|
buf = Sprintf("\x1b_Gm=1;%s\x1b\\", base64->ptr);
|
||||||
|
writestr(buf->ptr);
|
||||||
|
i = 0;
|
||||||
|
j = 0;
|
||||||
|
}
|
||||||
|
cbuf[i++] = c;
|
||||||
|
if (i == 3072)
|
||||||
|
j = 1;
|
||||||
|
}
|
||||||
|
|
||||||
if (c != EOF) {
|
if (i) {
|
||||||
i = 0;
|
base64 = base64_encode(cbuf, i);
|
||||||
base64 = NULL;
|
buf = Sprintf("\x1b_Gm=0;%s\x1b\\", base64->ptr);
|
||||||
while ((c = fgetc(fp)) != EOF) {
|
writestr(buf->ptr);
|
||||||
if (!i && base64) {
|
}
|
||||||
buf = Sprintf("\x1b_Gm=1;%s\x1b\\", base64);
|
|
||||||
writestr(buf->ptr);
|
|
||||||
}
|
|
||||||
cbuf[i++] = c;
|
|
||||||
if (i == 3072) {
|
|
||||||
base64 = base64_encode(cbuf, i);
|
|
||||||
if (!base64) {
|
|
||||||
fclose(fp);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (i) {
|
|
||||||
base64 = base64_encode(cbuf, i);
|
|
||||||
if (!base64) {
|
|
||||||
fclose(fp);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (base64) {
|
|
||||||
buf = Sprintf("\x1b_Gm=0;%s\x1b\\", base64);
|
|
||||||
writestr(buf->ptr);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
cleanup:
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
MOVE(Currentbuf->cursorY, Currentbuf->cursorX);
|
MOVE(Currentbuf->cursorY, Currentbuf->cursorX);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user