fix colors.c

This commit is contained in:
Green Sky 2023-11-22 23:04:15 +01:00
parent a72a4d97b3
commit 9da166ea34
No known key found for this signature in database

View File

@ -1,18 +1,21 @@
/* /*
* Copyright (C) 2004-2012 George Yunaev gyunaev@ulduzsoft.com * Copyright (C) 2004-2012 George Yunaev gyunaev@ulduzsoft.com
* *
* This library is free software; you can redistribute it and/or modify it * This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by * under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your * the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version. * option) any later version.
* *
* This library is distributed in the hope that it will be useful, but WITHOUT * This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details. * License for more details.
*/ */
#include <ctype.h> #include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define LIBIRC_COLORPARSER_BOLD (1<<1) #define LIBIRC_COLORPARSER_BOLD (1<<1)
#define LIBIRC_COLORPARSER_UNDERLINE (1<<2) #define LIBIRC_COLORPARSER_UNDERLINE (1<<2)
@ -58,7 +61,7 @@ static inline void libirc_colorparser_addorcat (char ** destline, unsigned int *
} }
static void libirc_colorparser_applymask (unsigned int * mask, static void libirc_colorparser_applymask (unsigned int * mask,
char ** destline, unsigned int * destlen, char ** destline, unsigned int * destlen,
unsigned int bitmask, const char * start, const char * end) unsigned int bitmask, const char * start, const char * end)
{ {
@ -75,7 +78,7 @@ static void libirc_colorparser_applymask (unsigned int * mask,
} }
static void libirc_colorparser_applycolor (unsigned int * mask, static void libirc_colorparser_applycolor (unsigned int * mask,
char ** destline, unsigned int * destlen, char ** destline, unsigned int * destlen,
unsigned int colorid, unsigned int bgcolorid) unsigned int colorid, unsigned int bgcolorid)
{ {
@ -95,7 +98,7 @@ static void libirc_colorparser_applycolor (unsigned int * mask,
} }
static void libirc_colorparser_closetags (unsigned int * mask, static void libirc_colorparser_closetags (unsigned int * mask,
char ** destline, unsigned int * destlen) char ** destline, unsigned int * destlen)
{ {
if ( *mask & LIBIRC_COLORPARSER_BOLD ) if ( *mask & LIBIRC_COLORPARSER_BOLD )
@ -123,11 +126,11 @@ static char * libirc_colorparser_irc2code (const char * source, int strip)
const char *p; const char *p;
int current_bg = 0; int current_bg = 0;
/* /*
* There will be two passes. First pass calculates the total length of * There will be two passes. First pass calculates the total length of
* the destination string. The second pass allocates memory for the string, * the destination string. The second pass allocates memory for the string,
* and fills it. * and fills it.
*/ */
while ( destline == 0 ) // destline will be set after the 2nd pass while ( destline == 0 ) // destline will be set after the 2nd pass
{ {
if ( destlen > 0 ) if ( destlen > 0 )
@ -149,7 +152,7 @@ static char * libirc_colorparser_irc2code (const char * source, int strip)
libirc_colorparser_applymask (&mask, &d, &destlen, LIBIRC_COLORPARSER_BOLD, "[B]", "[/B]"); libirc_colorparser_applymask (&mask, &d, &destlen, LIBIRC_COLORPARSER_BOLD, "[B]", "[/B]");
break; break;
case 0x1F: // underline case 0x1F: // underline
if ( strip ) if ( strip )
continue; continue;
@ -174,7 +177,7 @@ static char * libirc_colorparser_irc2code (const char * source, int strip)
case 0x03: // set color case 0x03: // set color
if ( isdigit (p[1]) ) if ( isdigit (p[1]) )
{ {
// Parse // Parse
int bgcolor = -1, color = p[1] - 0x30; int bgcolor = -1, color = p[1] - 0x30;
p++; p++;
@ -184,7 +187,7 @@ static char * libirc_colorparser_irc2code (const char * source, int strip)
p++; p++;
} }
// If there is a comma, search for the following // If there is a comma, search for the following
// background color // background color
if ( p[1] == ',' && isdigit (p[2]) ) if ( p[1] == ',' && isdigit (p[2]) )
{ {
@ -199,7 +202,7 @@ static char * libirc_colorparser_irc2code (const char * source, int strip)
} }
// Check for range // Check for range
if ( color <= LIBIRC_COLORPARSER_MAXCOLORS if ( color <= LIBIRC_COLORPARSER_MAXCOLORS
&& bgcolor <= LIBIRC_COLORPARSER_MAXCOLORS ) && bgcolor <= LIBIRC_COLORPARSER_MAXCOLORS )
{ {
if ( strip ) if ( strip )
@ -252,11 +255,11 @@ char * irc_color_convert_to_mirc (const char * source)
char * destline = 0, *d = 0; char * destline = 0, *d = 0;
const char *p1, *p2, *cur; const char *p1, *p2, *cur;
/* /*
* There will be two passes. First pass calculates the total length of * There will be two passes. First pass calculates the total length of
* the destination string. The second pass allocates memory for the string, * the destination string. The second pass allocates memory for the string,
* and fills it. * and fills it.
*/ */
while ( destline == 0 ) // destline will be set after the 2nd pass while ( destline == 0 ) // destline will be set after the 2nd pass
{ {
if ( destlen > 0 ) if ( destlen > 0 )
@ -276,7 +279,7 @@ char * irc_color_convert_to_mirc (const char * source)
// Check if the closing bracket is available after p1 // Check if the closing bracket is available after p1
// and the tag length is suitable // and the tag length is suitable
if ( p1[1] != '\0' if ( p1[1] != '\0'
&& (p2 = strchr (p1, ']')) != 0 && (p2 = strchr (p1, ']')) != 0
&& (p2 - p1) > 1 && (p2 - p1) > 1
&& (p2 - p1) < 31 ) && (p2 - p1) < 31 )