Skip to content

Commit be5d2dc

Browse files
authored
Improving the logic of encoding detection (#3752)
1 parent e714ddc commit be5d2dc

File tree

4 files changed

+26
-22
lines changed

4 files changed

+26
-22
lines changed

Tasks/AzureResourceGroupDeployment/Strings/resources.resjson/en-US/resources.resjson

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,5 @@
160160
"loc.messages.TimeoutWhileWaiting": "Timed out while waiting",
161161
"loc.messages.InvalidTemplateLocation": "The template location supplied is invalid. Task only supports 'Linked artifact' or 'URL of the file'",
162162
"loc.messages.EncodingNotSupported": "Encoding of the file '%s' is '%s' which is not supported. Supported encodings are ['utf-8', 'utf-16le']",
163-
"loc.messages.CouldNotDetectEncoding": "Could not detect encoding of file '%s'",
164-
"loc.messages.ShortFileBufferError": "Short file buffer error on file '%s'"
163+
"loc.messages.CouldNotDetectEncoding": "Could not detect encoding of file '%s'"
165164
}

Tasks/AzureResourceGroupDeployment/operations/FileEncoding.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ function detectFileEncodingWithBOM(fileName: string, buffer: Buffer) {
2020
type = 'utf-8';
2121
}
2222
else if (buffer.slice(0, 4).equals(new Buffer([255, 254, 0, 0]))) {
23-
type = 'UTF-32LE';
23+
type = 'utf-32le';
2424
}
2525
else if (buffer.slice(0, 2).equals(new Buffer([254, 255]))) {
26-
type = 'UTF-16BE';
26+
type = 'utf-16be';
2727
}
2828
else if (buffer.slice(0, 2).equals(new Buffer([255, 254]))) {
2929
type = 'utf-16le';
3030
}
3131
else if (buffer.slice(0, 4).equals(new Buffer([0, 0, 254, 255]))) {
32-
type = 'UTF-32BE';
32+
type = 'utf-32be';
3333
}
3434
else {
3535
tl.debug('Unable to detect File encoding using BOM');
@@ -40,41 +40,48 @@ function detectFileEncodingWithBOM(fileName: string, buffer: Buffer) {
4040

4141
function detectFileEncodingWithoutBOM(fileName: string, buffer: Buffer) {
4242
tl.debug('Detecting file encoding without BOM');
43+
if (buffer.length < 4) {
44+
tl.debug('Short file buffer error on file ' + fileName + '. length: ' + buffer.length);
45+
}
46+
4347
var typeCode = 0;
4448
var type: string;
45-
for (var index = 0; index < 4; index++) {
49+
var codeForUtf8 = 0
50+
for (var index = 0; index < 4 && index < buffer.length; index++) {
4651
typeCode = typeCode << 1;
4752
typeCode = typeCode | (buffer[index] > 0 ? 1 : 0);
53+
codeForUtf8 = codeForUtf8 << 1;
54+
codeForUtf8++;
4855
}
4956
switch (typeCode) {
5057
case 1:
51-
type = 'UTF-32BE';
58+
type = 'utf-32be';
5259
break;
5360
case 5:
54-
type = 'UTF-16BE';
61+
type = 'utf-16be';
5562
break;
5663
case 8:
57-
type = 'UTF-32LE';
64+
type = 'utf-32le';
5865
break;
5966
case 10:
6067
type = 'utf-16le';
6168
break;
62-
case 15:
63-
type = 'utf-8';
64-
break;
6569
default:
66-
return null;
70+
if (codeForUtf8 == typeCode) {
71+
type = 'utf-8';
72+
}
73+
else {
74+
return null;
75+
}
6776
}
6877
return new FileEncoding(type, false);
6978
}
7079
export function detectFileEncoding(fileName: string, buffer: Buffer): FileEncoding {
71-
if (buffer.length < 4) {
72-
tl.debug(tl.loc('ShortFileBufferError', fileName))
73-
throw Error(tl.loc("CouldNotDetectEncoding", fileName));
74-
}
80+
7581
var fileEncoding: FileEncoding = detectFileEncodingWithBOM(fileName, buffer);
76-
if (fileEncoding == null)
82+
if (fileEncoding == null) {
7783
fileEncoding = detectFileEncodingWithoutBOM(fileName, buffer);
84+
}
7885

7986
if (fileEncoding == null) {
8087
throw new Error(tl.loc("CouldNotDetectEncoding", fileName));

Tasks/AzureResourceGroupDeployment/task.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,6 @@
369369
"TimeoutWhileWaiting": "Timed out while waiting",
370370
"InvalidTemplateLocation": "The template location supplied is invalid. Task only supports 'Linked artifact' or 'URL of the file'",
371371
"EncodingNotSupported": "Encoding of the file '%s' is '%s' which is not supported. Supported encodings are ['utf-8', 'utf-16le']",
372-
"CouldNotDetectEncoding": "Could not detect encoding of file '%s'",
373-
"ShortFileBufferError": "Short file buffer error on file '%s'"
372+
"CouldNotDetectEncoding": "Could not detect encoding of file '%s'"
374373
}
375374
}

Tasks/AzureResourceGroupDeployment/task.loc.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,6 @@
369369
"TimeoutWhileWaiting": "ms-resource:loc.messages.TimeoutWhileWaiting",
370370
"InvalidTemplateLocation": "ms-resource:loc.messages.InvalidTemplateLocation",
371371
"EncodingNotSupported": "ms-resource:loc.messages.EncodingNotSupported",
372-
"CouldNotDetectEncoding": "ms-resource:loc.messages.CouldNotDetectEncoding",
373-
"ShortFileBufferError": "ms-resource:loc.messages.ShortFileBufferError"
372+
"CouldNotDetectEncoding": "ms-resource:loc.messages.CouldNotDetectEncoding"
374373
}
375374
}

0 commit comments

Comments
 (0)