My current knowledge:
If you are trying to write text files in vbscript / asp land you have two options.
the Scripting.FileSystemObject
the ADODB.Stream object
Scripting.FileSystemObject does not support utf8. It will write in Ascii, or Unicode (and waste two bytes for most of your characters)
ADODB.Stream does not support appending (afaik). I can't figure out how to make the ADODB.Stream object actually open a file and write to it when I call stream.Write. There is a SaveToFile function, but it outputs the entire stream.
If you need to write 1GB files, you would have to fill the whole 1GB into the stream before you could write it out.
Is there a special trick to get the ADODB.Stream object to link to a file on disk? I tried :
stream.Open "URL=file:///c:\test.txt"
but that gave an error.
In this scenario, I would probably create a COM component that takes a string, and runs it through WideCharToMultiByte to convert it to UTF-8.
In case you really want to stay within VBScript, I just hacked up a very quick and very dirty UTF-8 conversion...
Function Utf8(ByVal c)
Dim b1, b2, b3
If c < 128 Then
Utf8 = chr(c)
ElseIf c < 2048 Then
b1 = c Mod 64
b2 = (c - b1) / 64
Utf8 = chr(&hc0 + b2) & chr(&h80 + b1)
ElseIf c < 65536 Then
b1 = c Mod 64
b2 = ((c - b1) / 64) Mod 64
b3 = (c - b1 - (64 * b2)) / 4096
Utf8 = chr(&he0 + b3) & chr(&h80 + b2) & chr(&h80 + b1)
End If
End Function
Just open a file with Scripting.FileSystemObject, using system default encoding. Then pass every character of the string you want to write through this function.
NOTE that the function expects a Unicode code point, so be sure to use AscW() instead of plain Asc():
For i = 1 to Len(s)
file.Write Utf8(AscW(Mid(s, i, 1)))
Next
Related
I have been using DOSbox and ANSI with some success, but I want to detect ANSI installed.
Ralf Brown description for detecting ANSI is:
--------V-2F1A00-----------------------------
INT 2F - DOS 4.0+ ANSI.SYS - INSTALLATION CHECK
AX = 1A00h
Return: AL = FFh if installed
Notes: AVATAR.SYS also responds to this call
documented for DOS 5+, but undocumented for DOS 4.x
And my code to detect ANSI is:
' detect ansi
InregsX.AX = &H1A00
CALL InterruptX(&H2F, InregsX, OutregsX)
PRINT "AX="; HEX$(OutregsX.AX)
IF (OutregsX.AX AND &HFF) = &HFF THEN
Ansi.Installed = -1
ELSE
Ansi.Installed = 0
END IF
IF Ansi.Installed THEN
PRINT "Ansi installed."
ELSE
PRINT "Ansi not installed."
END IF
which always displays "Ansi not installed."
is there some other way to detect ANSI??
I think you might resolve making the try to use ANSI codes.
The code below implements the function ansiOn% that returns 1 if an ansi string is written on the screen (used as console: see the code) as expected, otherwise returns 0.
The method that the function implements is to clear the screen and write a string that contains: the first char different from A$, the backward ANSI sequence for 1 char - CSI 1 D - and the content of A$. After the string is written, if the upper left char on the screen is A$ then ANSI is enabled.
DECLARE FUNCTION ansiOn% ()
ansi% = ansiOn%
CLS
PRINT "ANSI is";
IF ansi% = 0 THEN
PRINT "n't";
END IF
PRINT " enabled."
FUNCTION ansiOn%
CLS : A$ = "C"
OPEN "CON" FOR OUTPUT AS #1
PRINT #1, CHR$(ASC(A$) - 1); CHR$(27); "[1D"; A$;
CLOSE #1
IF CHR$(SCREEN(1, 1)) = A$ THEN
ansiOn% = 1
ELSE
ansiOn% = 0
END IF
END FUNCTION
I tried this code using QB45 in a DOS 6.22 VM and it works.
I wrote this code to detect ANSI:
Cls
Const A$ = "alpha"
Const B$ = "beta"
Open "CONS:" For Output As #1
Print #1, A$
Z$ = Chr$(27) + "[2J" ' ansi cls
Print #1, Z$; B$;
Close #1
For T = 1 To Len(B$)
T$ = T$ + Chr$(Screen(1, T))
Next
If T$ = B$ Then Print "ANSI detected."
I'm new to LUA but figured out that gsub is a global substitution function and tonumber is a converter function. What I don't understand is how the two functions are used together to produce an encoded string.
I've already tried reading parts of PIL (Programming in Lua) and the reference manual but still, am a bit confused.
local L0_0, L1_1
function L0_0(A0_2)
return (A0_2:gsub("..", function(A0_3)
return string.char((tonumber(A0_3, 16) + 256 - 13 + 255999744) % 256)
end))
end
encodes = L0_0
L0_0 = gg
L0_0 = L0_0.toast
L1_1 = "__loading__\226\128\166"
L0_0(L1_1)
L0_0 = encodes
L1_1 = --"The Encoded String"
L0_0 = L0_0(L1_1)
L1_1 = load
L1_1 = L1_1(L0_0)
pcall(L1_1)
I removed the encoded string where I put the comment because of how long it was. If needed I can upload the encoded string as well.
gsub is being used to get 2 digit sections of A0_2. This means the string A0_3 is a 2 digit hexadecimal number but it is not in a number format so we cannot preform math on the value. A0_3 being a hex number can be inferred based on how tonubmer is used.
tonumber from Lua 5.1 Reference Manual:
Tries to convert its argument to a number. If the argument is already a number or a string convertible to a number, then tonumber returns this number; otherwise, it returns nil.
An optional argument specifies the base to interpret the numeral. The base may be any integer between 2 and 36, inclusive. In bases above 10, the letter 'A' (in either upper or lower case) represents 10, 'B' represents 11, and so forth, with 'Z' representing 35. In base 10 (the default), the number can have a decimal part, as well as an optional exponent part (see §2.1). In other bases, only unsigned integers are accepted.
So tonumber(A0_3, 16) means we are expecting for A0_3 to be a base 16 number (hexadecimal).
Once we have the number value of A0_3 we do some math and finally convert it to a character.
function L0_0(A0_2)
return (A0_2:gsub("..", function(A0_3)
return string.char((tonumber(A0_3, 16) + 256 - 13 + 255999744) % 256)
end))
end
This block of code takes a string of hex digits and converts them into chars. tonumber is being used to allow for the manipulation of the values.
Here is an example of how this works with Hello World:
local str = "Hello World"
local hex_str = ''
for i = 1, #str do
hex_string = hex_string .. string.format("%x", str:byte(i,i))
end
function L0_0(A0_2)
return (A0_2:gsub("..", function(A0_3)
return string.char((tonumber(A0_3, 16) + 256 - 13 + 255999744) % 256)
end))
end
local encoded = L0_0(hex_str)
print(encoded)
Output
;X__bJbe_W
And taking it back to the orginal string:
function decode(A0_2)
return (A0_2:gsub("..", function(A0_3)
return string.char((tonumber(A0_3, 16) + 13) % 256)
end))
end
hex_string = ''
for i = 1, #encoded do
hex_string = hex_string .. string.format("%x", encoded:byte(i,i))
end
print(decode(hex_string))
I'm new to Lua when I began to use OpenResty, I want to output a image and it's x,y coordinate together as one binary sequence to the clients, looks like: x_int32_bits y_int32_bits image_raw_data. At the client, I know the first 32 bits is x, the second 32 is y, and the others are image raw data. I met some questions:
How to convert number to 32 binary bits in Lua?
How to merge two 32 bits to one 64 bits sequence?
How to insert 64 bits to front of image raw data? And how to be fastest?
file:read("*a") got string type result, is the result ASCII sequence or like "000001110000001..." string?
What I'm thinking is like below, I don't know how to convert 32bits to string format same as file:read("*a") result.
#EgorSkriptunoff thank you, you opened a window for me. I wrote some new code, would you take a look, and I have another question, is the string merge method .. inefficient and expensive? Specially when one of the string is very large. Is there an alternative way to merge the bytes string?
NEW CODE UNDER #EgorSkriptunoff 's GUIDANCE
function _M.number_to_int32_bytes(num)
return ffi.string(ffi.new("int32_t[1]", num), 4)
end
local x, y = unpack(v)
local file, err = io.open(image_path, "rb")
if nil ~= file then
local image_raw_data = file:read("*a")
if nil == image_raw_data then
ngx.log(ngx.ERR, "read file error:", err)
else
-- Is the .. method inefficient and expensive? Because the image raw data maybe large,
-- so will .. copy all the data to a new string? Is there an alternative way to merge the bytes string?
output = utils.number_to_int32_bytes(x) .. utils.number_to_int32_bytes(y) .. image_raw_data
ngx.print(output)
ngx.flush(true)
end
file:close()
end
OLD CODE:
function table_merge(t1, t2)
for k,v in ipairs(t2) do
table.insert(t1, v)
end
return t1
end
function numToBits(num, bits)
-- returns a table of bits
local t={} -- will contain the bits
for b=bits,1,-1 do
rest=math.fmod(num,2)
t[b]=rest
num=(num-rest)/2
end
if num==0 then return t else return {'Not enough bits to represent this number'} end
end
-- Need to insert x,y as 32bits respectively to front of image binary sequence
function output()
local x = 1, y = 3
local file, err = io.open("/storage/images/1.png", "rb")
if nil ~= file then
local d = file:read("*a") ------- type(d) is string, why?
if nil == d then
ngx.log(ngx.ERR, "read file error:", err)
else
-- WHAT WAY I'M THINKING -----------------
-- Convert x, y to binary table, then merge them to one binary table
data = table_merge(numToBits(x, 32), numToBits(y, 32))
-- Convert data from binary table to string
data = convert_binary_table_to_string(data) -- HOW TO DO THAT? --------
-- Insert x,y data to front of image data, is data .. d ineffective?
data = data .. d
-------------------------------------------
ngx.print(data)
ngx.flush(true)
end
file:close()
end
end
Intro
I got a string original, which was encoded (using the procedure below), then encrypted with rsa and then decoded again, so I'm left with a ciphertext s.
To get back to the original plaintext I'd encode s, then decrypt and then decode again.
Encoding
Each character in s gets encoded (using the function x) like this:
x(A)=0, x(B)=1, ..., x(Z)=25
Then the message, with k amount of characters, gets encoded (using the function y) like this:
encoded_msg = y(s) = x(s0)*260 + x(s1)*261 + x(s2)*262 + ... x(sk)*26k-1
The problem
Now, if i do this for original="ABCD" for example, that would lead to
y(x(original)) = 0 + 1*26 + 2*676 + 3*17576 = 54106.
(encrypt → decrypt → 54106)
decode ?
My question is: If all I got are the functions x and y and a result 54106, how do I decode back to "ABCD"?
So I'm developing a web application to more easily be able distribute color palettes created with Photoshop in a *.ACO or *.ASE format to colleagues who doesn't have those programs. I've come quite a long way setting up the basics, but now I'm totally stuck for the sole reason that I can't figure out how the swatch files are structured.
This is what I get when I open the *.ASE file in a text editor:
ASEF & S w a t c h 1 RGB & S w a t c h 2 RGB ?
Œ ?€ ?€ & S w a t c h 3 RGB ?oïð?)̪>mí & S w a t c h 1 RGB & S w a t c h 2 RGB ?
Œ ?€ ?€ & S w a t c h 3 RGB ?oïð?)̪>mí & S w a t c h 1 RGB & S w a t c h 2 RGB ?
Œ ?€ ?€ & S w a t c h 3 RGB ?oïð?)̪>mí & S w a t c h 1 RGB & S w a t c h 2 RGB ?
Œ ?€ ?€ & S w a t c h 3 RGB ?oïð?)̪>mí
and when I open it in NP++ it looks like this:
I was hoping (and naively expecting) that the format would be in some comprehensible XML structure, but it's clearly not..
I've tried researching the subject and found these sources:
http://www.nomodes.com/aco.html
http://www.selapa.net/swatches/colors/fileformats.php
http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/PhotoshopFileFormats.htm#50577411_31265
But to be honest it feels to complicated for me to be able to wrap my head around.. If anyone with better knowledge about file coding formats or color coding formats has any input for me I would greatly appreciate it!
The files are available here for download if you want to have a look at them:
https://www.dropbox.com/sh/9vo2h7ophpfc201/p7saMtxi_k
What you are seeing is the ASCII representation of the binary file. In the link you shared previously you will see that the file format is binary. So that lets take that as the starting point. Now open the file and read it in like so,
$handle = fopen("temp.aco", "rb");
while (!feof($handle))
{
$data = fread($handle, 2);
echo bin2hex($data)."<br/>";
}
This opens the .aco file and reads until the end of file is reached. By using fread and setting the second parameter to 2 you read in 2 bytes worth of data from the file. You will then see output like so,
0001 0008 0000 fafa e2e2 dbdb 0000
Now looking at your other link you will see that the first number 0001 represents the version number 0008 represents the number of colours in the file (in my case 8) then you have the colour type which will typically be 0000 (RGB) 0001 (HSB) 0002 (CMYK) see the colour conversion table for the rest.
Colours are made up of either 3 or 4 words so sometimes you can ignore the last word it will be zero'd. So lets see an example of this,
0000-(RGB Type) fafa-(Red represented by 0..65535 range) e2e2-(Green represented by 0..65535 range) dbdb-(Blue represented by 0..65535 range) 0000-(no data needed in this positioned so zero'd)
By converting the words read to an unsigned int and following the conversion table you will get the appropriate rgb values. Here is my code for parsing the various types.
function colorInColorSpace($colorSpace, $w, $x, $y, $z){
// RGB
if($colorSpace==0){
$r = $w/256;
$g = $x/256;
$b = $y/256;
//z component not used in rgb format
print $colorSpace." ".$r." ".$g." ".$b."<br/>";
}
//HSB
else if($colorSpace==1){
$h = $w/182.04;
$s = $x/655.35;
$b = $y/655.35;
print $colorSpace." ".$h." ".$s." ".$b."<br/>";
}
//CYMK
else if($colorSpace==2){
$c = 100 - ($w/655.35);
$m = 100 - ($x/655.35);
$y = 100 - ($y/655.35);
$k = 100 - ($z/655.35);
print $colorSpace." ".$c." ".$m." ".$y." ".$k."<br/>";
}
//Lab
else if($colorSpace==7){
// print $colorSpace." ".bin2hex($w[0])." ".bin2hex($w[1])."<br/>";
}
//Grayscale
else if($colorSpace==8){
$greyscale = $w/39.0625;
print $colorSpace." ".$greyscale."<br/>";
}
//Wide CYMK
else if($colorSpace==9){
$c = $w/100;
$m = $x/100;
$y = $y/100;
$k = $z/100;
print $colorSpace." ".$c." ".$m." ".$y." ".$k."<br/>";
}
}
Hope this helps.
Just for reference to others, I faced a similar problem and found this C program that reads ACO files and convert them into html. http://www.hping.org/aco2html/
It's a great tool, but as I wanted to modify it and made it better, I decided to port it to JavaScript. Here's my code on Github https://github.com/websemantics/Color-Palette-Toolkit
There's also a live demo
http://websemantics.github.io/Color-Palette-Toolkit/
Stumbled on this post while I was searching for a way to decode Adobe's .ASE swatch format. I'm not sure if it will help you or not with .ACO, but I found a guy that wrote a class for extracting colors from the .ASE format:
Adobe Swatch Exchange Reader/Decoder!broken link