iconv.obj: rollback to a working revision 5098 with minor additions. Temporary regression is possible.

git-svn-id: svn://kolibrios.org@9307 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
turbocat 2021-11-24 21:42:58 +00:00
parent a6e0b135b3
commit 63b9f66dc7
2 changed files with 91 additions and 224 deletions

View File

@ -1,2 +1,2 @@
if tup.getconfig("NO_GCC") ~= "" then return end
tup.rule("iconv.c", "kos32-gcc -c -I../../../../contrib/sdk/sources/newlib/libc/include/ -fno-ident -Os -c -o %o %f " .. tup.getconfig("KPACK_CMD"), "iconv.obj")
tup.rule("iconv.c", "kos32-gcc -Os -fno-ident -fno-leading-underscore -fno-pie -c -o %o %f && kos32-strip %o --strip-unneeded " .. tup.getconfig("KPACK_CMD"), "iconv.obj")

View File

@ -1,6 +1,5 @@
#include <string.h>
#include <stdio.h>
#include <errno.h>
typedef unsigned int size_t;
#define NULL ((void*)0)
typedef int conv_t;
typedef unsigned int ucs4_t;
@ -25,245 +24,113 @@ typedef int iconv_t;
#define KOI8_RU 3
#define ISO8859_5 4
#define UTF_8 5
#define KOI8_R 6
#define ISO8859_1 7
int strcmp (const char* a, const char* b) {
return (*a && *b && (*a == *b)) ? ((*(a+1) || *(b+1)) ? (strcmp(a+1, b+1)) : (0)) : ((*a > *b) ? (1) : (-1));
}
#include "cp866.h"
#include "cp1251.h"
#include "cp1252.h"
#include "koi8_r.h"
#include "koi8_ru.h"
#include "iso8859_1.h"
#include "iso8859_5.h"
#include "utf8.h"
int encoding(const char *someencoding) {
char *what = strdup(someencoding);
/* Ignore //TRANSLIT or //IGNORE for now. */
int i;
for(i = 0; i < strlen(what); i++) {
if(what[i] == '/') {
what[i] = '\0';
break;
}
}
if (!strcasecmp(what,"CP866")) return CP866;
if (!strcasecmp(what,"CP1251")) return CP1251;
if (!strcasecmp(what,"windows-1251")) return CP1251;
if (!strcasecmp(what,"CP1252")) return CP1252;
if (!strcasecmp(what,"windows-1252")) return CP1252;
if (!strcasecmp(what,"KOI8-R")) return KOI8_R;
if (!strcasecmp(what,"KOI8-RU")) return KOI8_RU;
if (!strcasecmp(what,"ISO8859-1")) return ISO8859_1;
if (!strcasecmp(what,"ISO8859-5")) return ISO8859_5;
if (!strcasecmp(what,"UTF-8")) return UTF_8;
return -1;
int encoding(const char *what) {
if (!strcmp(what,"CP866")) return CP866;
if (!strcmp(what,"CP1251")) return CP1251;
if (!strcmp(what,"CP1252")) return CP1252;
if (!strcmp(what,"KOI8-RU")) return KOI8_RU;
if (!strcmp(what,"ISO8859-5")) return ISO8859_5;
if (!strcmp(what,"UTF-8")) return UTF_8;
return -1;
}
iconv_t iconv_open(const char *tocode, const char *fromcode) {
int to, from;
if ((to=encoding(tocode))==-1) return -1;
if ((from=encoding(fromcode))==-1) return -1;
to=to<<16&0xFFFF0000;
from=from&0xFFFF;
return to+from;
int to, from;
if ((to=encoding(tocode))==-1) return -1;
if ((from=encoding(fromcode))==-1) return -1;
to=to<<16&0xFFFF0000;
from=from&0xFFFF;
return to+from;
}
int iconv_close(iconv_t icd)
{
return 0;
}
size_t iconv(iconv_t cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft) {
int n, to, from, count1,count2;
int pwc, converted,written;
int (*mbtowc)(conv_t, ucs4_t*, const unsigned char*, int);
int (*wctomb)(conv_t, unsigned char*, ucs4_t, int);
size_t iconv(iconv_t cd, const char **inbuf, size_t *inbytesleft,
char **outbuf, size_t *outbytesleft)
{
int n, to, from;
size_t count1,count2;
unsigned int pwc;
int converted,written;
int (*mbtowc)(conv_t, ucs4_t *, const unsigned char *, int);
int (*wctomb)(conv_t, unsigned char *, ucs4_t, int);
char *str;
str=*outbuf;
to=cd>>16;
from=cd&0xFFFF;
from=cd>>16;
to=cd&0xFFFF;
switch (from)
{
case CP866: mbtowc=cp866_mbtowc; break;
case CP1251: mbtowc=cp1251_mbtowc; break;
case CP1252: mbtowc=cp1252_mbtowc; break;
case ISO8859_1: mbtowc=iso8859_1_mbtowc; break;
case ISO8859_5: mbtowc=iso8859_5_mbtowc; break;
case KOI8_R: mbtowc=koi8_r_mbtowc; break;
case KOI8_RU: mbtowc=koi8_ru_mbtowc; break;
case UTF_8: mbtowc=utf8_mbtowc; break;
default: return (size_t)-1;
}
switch (to)
{
case CP866: wctomb=cp866_wctomb; break;
case CP1251: wctomb=cp1251_wctomb; break;
case CP1252: wctomb=cp1252_wctomb; break;
case ISO8859_1: wctomb=iso8859_1_wctomb; break;
case ISO8859_5: wctomb=iso8859_5_wctomb; break;
case KOI8_R: wctomb=koi8_r_wctomb; break;
case KOI8_RU: wctomb=koi8_ru_wctomb; break;
case UTF_8: wctomb=utf8_wctomb; break;
default: return (size_t)-1;
}
count1=0;
count2=0;
/* Convert input multibyte char to wide character by using calls to mbtowc */
/* Convert wide character to multibyte by calls to wctomb */
/* Handle errors as we go on converting to be as standard compliant as possible */
while(count1 < *inbytesleft) {
unsigned char mbholder[] = { 0,0,0,0,0,0 };
int numbytes = (mbtowc)(0, &pwc,((*inbuf)+count1), *inbytesleft - count1);
if(numbytes < 0) {
/* errno = EILSEQ if invalid multibyte sequence encountered in input */
/* errno = EINVAL if input ends in the middle of a multibyte sequence */
switch(numbytes) {
case RET_TOOFEW(0):
errno = EINVAL;
break;
case RET_ILSEQ:
errno = EILSEQ;
break;
}
*inbytesleft -= count1;
*outbytesleft -= count2;
*inbuf += count1;
*outbuf += count2;
return (size_t) -1;
}
/* Convert from wide to multibyte storing result in mbholder and num converted in numbytes2 */
/* Pass the minimum amount of space we have, one from mbholder and one from remaining in outbuf */
int minspace = sizeof(mbholder) <= (*outbytesleft - count2) ? sizeof(mbholder) : (*outbytesleft - count2);
int numbytes2 = (wctomb)(0, &mbholder[0], pwc, minspace);
if(numbytes2 < 0) {
switch(numbytes2) {
case RET_ILUNI:
errno = EILSEQ;
break;
case RET_TOOSMALL:
errno = E2BIG;
break;
}
*inbytesleft -= count1;
*outbytesleft -= count2;
*inbuf += count1;
*outbuf += count2;
return (size_t) -1;
}
int i;
for(i = 0; i < numbytes2; i++) {
*(*outbuf + count2 + i) = mbholder[i];
}
count1+=numbytes;
count2+=numbytes2;
switch (from)
{
case CP866: mbtowc=cp866_mbtowc; break;
case CP1251: mbtowc=cp1251_mbtowc; break;
case CP1252: mbtowc=cp1252_mbtowc; break;
case ISO8859_5: mbtowc=iso8859_5_mbtowc; break;
case KOI8_RU: mbtowc=koi8_ru_mbtowc; break;
case UTF_8: mbtowc=utf8_mbtowc; break;
default: return -2;
}
/* Successfully converted everything, update the variables and return number of bytes converted */
*inbytesleft -= count1;
*outbytesleft -= count2;
*inbuf += count1;
*outbuf += count2;
switch (to)
{
case CP866: wctomb=cp866_wctomb; break;
case CP1251: wctomb=cp1251_wctomb; break;
case CP1252: wctomb=cp1252_wctomb; break;
case ISO8859_5: wctomb=iso8859_5_wctomb; break;
case KOI8_RU: wctomb=koi8_ru_wctomb; break;
case UTF_8: wctomb=utf8_wctomb; break;
default: return -3;
}
return count1;
count1=0;
count2=0;
while ( *inbytesleft>0 && *outbytesleft>1)
{
n=1;
do {
//converted= (utf8_mbtowc)(0,&pwc,((*inbuf)+count1),n);
// printf("%d\n",n);
converted = (mbtowc)(0,&pwc,((*inbuf)+count1),n);
n++;
} while (converted==RET_TOOFEW(0));
if (converted<0) return -10;
//written= (cp866_wctomb)(0,str+count2,pwc,1);
written = (wctomb)(0,str+count2,pwc,1);
if (written<0) written=0;//return -11;
//printf("Conv:%d Wri:%d In:%d Out:%d UTF:%x UCS:%x 866:%s\n",converted, written, *inbytesleft,*outbytesleft,*((*inbuf)+count1),pwc, str);
(*inbytesleft)-=converted;
(*outbytesleft)-=written;
count1+=converted;
count2+=written;
}
*(str+count2)='\0';
if (*inbytesleft>0 && *outbytesleft==0) return -12;
return 0;
}
/* int main() */
/* { */
/* char *s;// ="вертолет"; */
/* char *z; */
/* //unsigned int pwc; */
/* iconv_t cd; */
/* size_t in, out; */
typedef struct {
char *name;
void *func;
} export_t;
/* FILE *infile; */
/* char *fname = "file3.txt"; */
/* size_t testmax = 100; */
/* size_t test = 0; */
/* infile = fopen(fname,"r"); */
/* fseek(infile, 0, SEEK_END); */
/* size_t file_size = ftell(infile); */
/* rewind(infile); */
/* char *buffer = (char*)malloc(file_size * sizeof(char)); */
/* if (buffer == NULL) */
/* { */
/* fclose(infile); */
/* printf("Error allocating %d bytes.\n", file_size * sizeof(char)); */
/* return -1; */
/* } */
/* size_t bytes_read = fread(buffer, sizeof(char), file_size, infile); */
/* if (bytes_read != file_size) */
/* { */
/* /\* printf("Have read only %d bytes of %d.\n", bytes_read, file_size); *\/ */
/* free(buffer); */
/* fclose(infile); */
/* return -1; */
/* } */
/* /\* in=strlen(buffer); *\/ */
/* in = bytes_read; */
/* z=malloc(in+12000); */
/* out=in-1000; */
/* cd=iconv_open("UTF-8","UTF-8"); */
/* // printf("%x\n",cd); */
/* int t; */
/* char *zor = z; */
/* /\* for(t = 0; t < 27400; t++) *\/ */
/* /\* printf("0x%x,", buffer[t]); *\/ */
/* t=iconv(cd, &buffer, &in, &z, &out); */
/* /\* printf("\nResult after iconv(): %d", t); *\/ */
/* /\* for(t = 0; t < 24259; t++) *\/ */
/* /\* printf("%c", zor[t]); *\/ */
/* //for (;s<s+strlen(s);s++) {cp866_mbtowc (0, &pwc, s, 1);printf("%c=%u\n",*s,pwc);} */
/* } */
/* typedef struct */
/* { */
/* char *name; */
/* void *f; */
/* } export_t; */
/* char szStart[] = "START"; */
/* char szVersion[] = "version"; */
/* char sziconv_open[] = "iconv_open"; */
/* char sziconv[] = "iconv"; */
/* export_t EXPORTS[] __asm__("EXPORTS") = */
/* { */
/* { szStart, (void*)0x0 }, */
/* { szVersion, (void*)0x00010001 }, */
/* { sziconv_open, iconv_open }, */
/* { sziconv, iconv }, */
/* { NULL, NULL }, */
/* }; */
export_t EXPORTS[] = {
{"START", (void*)0x0},
{"version", (void*)0x00010001},
{"iconv_open", iconv_open},
{"iconv", iconv},
NULL
};