mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-29 02:40:05 +00:00
Add more uses of or_return
This commit is contained in:
@@ -183,9 +183,7 @@ writer_read_from :: proc(b: ^Writer, r: io.Reader) -> (n: i64, err: io.Error) {
|
|||||||
|
|
||||||
for {
|
for {
|
||||||
if writer_available(b) == 0 {
|
if writer_available(b) == 0 {
|
||||||
if ferr := writer_flush(b); ferr != nil {
|
writer_flush(b) or_return;
|
||||||
return n, ferr;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if b.max_consecutive_empty_writes <= 0 {
|
if b.max_consecutive_empty_writes <= 0 {
|
||||||
b.max_consecutive_empty_writes = DEFAULT_MAX_CONSECUTIVE_EMPTY_READS;
|
b.max_consecutive_empty_writes = DEFAULT_MAX_CONSECUTIVE_EMPTY_READS;
|
||||||
|
|||||||
@@ -442,24 +442,22 @@ inflate_from_context :: proc(using ctx: ^compress.Context_Memory_Input, raw := f
|
|||||||
return E_General.Unknown_Compression_Method;
|
return E_General.Unknown_Compression_Method;
|
||||||
}
|
}
|
||||||
|
|
||||||
cinfo := (cmf >> 4) & 0xf;
|
if cinfo := (cmf >> 4) & 0xf; cinfo > 7 {
|
||||||
if cinfo > 7 {
|
|
||||||
return E_ZLIB.Unsupported_Window_Size;
|
return E_ZLIB.Unsupported_Window_Size;
|
||||||
}
|
}
|
||||||
flg, _ := compress.read_u8(ctx);
|
flg, _ := compress.read_u8(ctx);
|
||||||
|
|
||||||
fcheck := flg & 0x1f;
|
fcheck := flg & 0x1f;
|
||||||
fcheck_computed := (cmf << 8 | flg) & 0x1f;
|
fcheck_computed := (cmf << 8 | flg) & 0x1f;
|
||||||
if fcheck != fcheck_computed {
|
if fcheck != fcheck_computed {
|
||||||
return E_General.Checksum_Failed;
|
return E_General.Checksum_Failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
fdict := (flg >> 5) & 1;
|
|
||||||
/*
|
/*
|
||||||
We don't handle built-in dictionaries for now.
|
We don't handle built-in dictionaries for now.
|
||||||
They're application specific and PNG doesn't use them.
|
They're application specific and PNG doesn't use them.
|
||||||
*/
|
*/
|
||||||
if fdict != 0 {
|
if fdict := (flg >> 5) & 1; fdict != 0 {
|
||||||
return E_ZLIB.FDICT_Unsupported;
|
return E_ZLIB.FDICT_Unsupported;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -473,10 +471,7 @@ inflate_from_context :: proc(using ctx: ^compress.Context_Memory_Input, raw := f
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Parse ZLIB stream without header.
|
// Parse ZLIB stream without header.
|
||||||
err = inflate_raw(z=ctx, expected_output_size=expected_output_size);
|
inflate_raw(z=ctx, expected_output_size=expected_output_size) or_return;
|
||||||
if err != nil {
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
if !raw {
|
if !raw {
|
||||||
compress.discard_to_next_byte_lsb(ctx);
|
compress.discard_to_next_byte_lsb(ctx);
|
||||||
@@ -527,23 +522,14 @@ inflate_raw :: proc(z: ^$C, expected_output_size := -1, allocator := context.all
|
|||||||
z_repeat: ^Huffman_Table;
|
z_repeat: ^Huffman_Table;
|
||||||
z_offset: ^Huffman_Table;
|
z_offset: ^Huffman_Table;
|
||||||
codelength_ht: ^Huffman_Table;
|
codelength_ht: ^Huffman_Table;
|
||||||
|
|
||||||
z_repeat, err = allocate_huffman_table(allocator=context.allocator);
|
|
||||||
if err != nil {
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
z_offset, err = allocate_huffman_table(allocator=context.allocator);
|
|
||||||
if err != nil {
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
codelength_ht, err = allocate_huffman_table(allocator=context.allocator);
|
|
||||||
if err != nil {
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
defer free(z_repeat);
|
defer free(z_repeat);
|
||||||
defer free(z_offset);
|
defer free(z_offset);
|
||||||
defer free(codelength_ht);
|
defer free(codelength_ht);
|
||||||
|
|
||||||
|
z_repeat = allocate_huffman_table(allocator=context.allocator) or_return;
|
||||||
|
z_offset = allocate_huffman_table(allocator=context.allocator) or_return;
|
||||||
|
codelength_ht = allocate_huffman_table(allocator=context.allocator) or_return;
|
||||||
|
|
||||||
final := u32(0);
|
final := u32(0);
|
||||||
type := u32(0);
|
type := u32(0);
|
||||||
|
|
||||||
@@ -560,8 +546,8 @@ inflate_raw :: proc(z: ^$C, expected_output_size := -1, allocator := context.all
|
|||||||
// Discard bits until next byte boundary
|
// Discard bits until next byte boundary
|
||||||
compress.discard_to_next_byte_lsb(z);
|
compress.discard_to_next_byte_lsb(z);
|
||||||
|
|
||||||
uncompressed_len := i16(compress.read_bits_lsb(z, 16));
|
uncompressed_len := i16(compress.read_bits_lsb(z, 16));
|
||||||
length_check := i16(compress.read_bits_lsb(z, 16));
|
length_check := i16(compress.read_bits_lsb(z, 16));
|
||||||
|
|
||||||
// fmt.printf("LEN: %v, ~LEN: %v, NLEN: %v, ~NLEN: %v\n", uncompressed_len, ~uncompressed_len, length_check, ~length_check);
|
// fmt.printf("LEN: %v, ~LEN: %v, NLEN: %v, ~NLEN: %v\n", uncompressed_len, ~uncompressed_len, length_check, ~length_check);
|
||||||
|
|
||||||
@@ -586,14 +572,8 @@ inflate_raw :: proc(z: ^$C, expected_output_size := -1, allocator := context.all
|
|||||||
// log.debugf("Err: %v | Final: %v | Type: %v\n", err, final, type);
|
// log.debugf("Err: %v | Final: %v | Type: %v\n", err, final, type);
|
||||||
if type == 1 {
|
if type == 1 {
|
||||||
// Use fixed code lengths.
|
// Use fixed code lengths.
|
||||||
err = build_huffman(z_repeat, Z_FIXED_LENGTH[:]);
|
build_huffman(z_repeat, Z_FIXED_LENGTH[:]) or_return;
|
||||||
if err != nil {
|
build_huffman(z_offset, Z_FIXED_DIST[:]) or_return;
|
||||||
return err;
|
|
||||||
}
|
|
||||||
err = build_huffman(z_offset, Z_FIXED_DIST[:]);
|
|
||||||
if err != nil {
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
lencodes: [286+32+137]u8;
|
lencodes: [286+32+137]u8;
|
||||||
codelength_sizes: [19]u8;
|
codelength_sizes: [19]u8;
|
||||||
@@ -611,19 +591,13 @@ inflate_raw :: proc(z: ^$C, expected_output_size := -1, allocator := context.all
|
|||||||
s := compress.read_bits_lsb(z, 3);
|
s := compress.read_bits_lsb(z, 3);
|
||||||
codelength_sizes[Z_LENGTH_DEZIGZAG[i]] = u8(s);
|
codelength_sizes[Z_LENGTH_DEZIGZAG[i]] = u8(s);
|
||||||
}
|
}
|
||||||
err = build_huffman(codelength_ht, codelength_sizes[:]);
|
build_huffman(codelength_ht, codelength_sizes[:]) or_return;
|
||||||
if err != nil {
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
n = 0;
|
n = 0;
|
||||||
c: u16;
|
c: u16;
|
||||||
|
|
||||||
for n < ntot {
|
for n < ntot {
|
||||||
c, err = decode_huffman(z, codelength_ht);
|
c = decode_huffman(z, codelength_ht) or_return;
|
||||||
if err != nil {
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
if c < 0 || c >= 19 {
|
if c < 0 || c >= 19 {
|
||||||
return E_Deflate.Huffman_Bad_Code_Lengths;
|
return E_Deflate.Huffman_Bad_Code_Lengths;
|
||||||
@@ -664,21 +638,10 @@ inflate_raw :: proc(z: ^$C, expected_output_size := -1, allocator := context.all
|
|||||||
return E_Deflate.Huffman_Bad_Code_Lengths;
|
return E_Deflate.Huffman_Bad_Code_Lengths;
|
||||||
}
|
}
|
||||||
|
|
||||||
err = build_huffman(z_repeat, lencodes[:hlit]);
|
build_huffman(z_repeat, lencodes[:hlit]) or_return;
|
||||||
if err != nil {
|
build_huffman(z_offset, lencodes[hlit:ntot]) or_return;
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
err = build_huffman(z_offset, lencodes[hlit:ntot]);
|
|
||||||
if err != nil {
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
err = parse_huffman_block(z, z_repeat, z_offset);
|
|
||||||
// log.debugf("Err: %v | Final: %v | Type: %v\n", err, final, type);
|
|
||||||
if err != nil {
|
|
||||||
return err;
|
|
||||||
}
|
}
|
||||||
|
parse_huffman_block(z, z_repeat, z_offset) or_return;
|
||||||
}
|
}
|
||||||
if final == 1 {
|
if final == 1 {
|
||||||
break;
|
break;
|
||||||
@@ -698,9 +661,7 @@ inflate_from_byte_array :: proc(input: []u8, buf: ^bytes.Buffer, raw := false, e
|
|||||||
ctx.input_data = input;
|
ctx.input_data = input;
|
||||||
ctx.output = buf;
|
ctx.output = buf;
|
||||||
|
|
||||||
err = inflate_from_context(ctx=&ctx, raw=raw, expected_output_size=expected_output_size);
|
return inflate_from_context(ctx=&ctx, raw=raw, expected_output_size=expected_output_size);
|
||||||
|
|
||||||
return err;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inflate_from_byte_array_raw :: proc(input: []u8, buf: ^bytes.Buffer, raw := false, expected_output_size := -1) -> (err: Error) {
|
inflate_from_byte_array_raw :: proc(input: []u8, buf: ^bytes.Buffer, raw := false, expected_output_size := -1) -> (err: Error) {
|
||||||
@@ -712,4 +673,4 @@ inflate_from_byte_array_raw :: proc(input: []u8, buf: ^bytes.Buffer, raw := fals
|
|||||||
return inflate_raw(z=&ctx, expected_output_size=expected_output_size);
|
return inflate_raw(z=&ctx, expected_output_size=expected_output_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
inflate :: proc{inflate_from_context, inflate_from_byte_array};
|
inflate :: proc{inflate_from_context, inflate_from_byte_array};
|
||||||
@@ -64,21 +64,15 @@ write :: proc(w: ^Writer, record: []string) -> io.Error {
|
|||||||
field := record[field_idx];
|
field := record[field_idx];
|
||||||
|
|
||||||
if field_idx > 0 {
|
if field_idx > 0 {
|
||||||
if _, err := io.write_rune(w.w, w.comma); err != nil {
|
io.write_rune(w.w, w.comma) or_return;
|
||||||
return err;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !field_needs_quoting(w, field) {
|
if !field_needs_quoting(w, field) {
|
||||||
if _, err := io.write_string(w.w, field); err != nil {
|
io.write_string(w.w, field) or_return;
|
||||||
return err;
|
|
||||||
}
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := io.write_byte(w.w, '"'); err != nil {
|
io.write_byte(w.w, '"') or_return;
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
for len(field) > 0 {
|
for len(field) > 0 {
|
||||||
i := strings.index_any(field, CHAR_SET);
|
i := strings.index_any(field, CHAR_SET);
|
||||||
@@ -86,40 +80,28 @@ write :: proc(w: ^Writer, record: []string) -> io.Error {
|
|||||||
i = len(field);
|
i = len(field);
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := io.write_string(w.w, field[:i]); err != nil {
|
io.write_string(w.w, field[:i]) or_return;
|
||||||
return err;
|
|
||||||
}
|
|
||||||
field = field[i:];
|
field = field[i:];
|
||||||
|
|
||||||
if len(field) > 0 {
|
if len(field) > 0 {
|
||||||
switch field[0] {
|
switch field[0] {
|
||||||
case '\r':
|
case '\r':
|
||||||
if !w.use_crlf {
|
if !w.use_crlf {
|
||||||
if err := io.write_byte(w.w, '\r'); err != nil {
|
io.write_byte(w.w, '\r') or_return;
|
||||||
return err;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
case '\n':
|
case '\n':
|
||||||
if w.use_crlf {
|
if w.use_crlf {
|
||||||
if _, err := io.write_string(w.w, "\r\n"); err != nil {
|
io.write_string(w.w, "\r\n") or_return;
|
||||||
return err;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if err := io.write_byte(w.w, '\n'); err != nil {
|
io.write_byte(w.w, '\n') or_return;
|
||||||
return err;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
case '"':
|
case '"':
|
||||||
if _, err := io.write_string(w.w, `""`); err != nil {
|
io.write_string(w.w, `""`) or_return;
|
||||||
return err;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
field = field[1:];
|
field = field[1:];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := io.write_byte(w.w, '"'); err != nil {
|
io.write_byte(w.w, '"') or_return;
|
||||||
return err;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if w.use_crlf {
|
if w.use_crlf {
|
||||||
@@ -132,10 +114,7 @@ write :: proc(w: ^Writer, record: []string) -> io.Error {
|
|||||||
// write_all writes multiple CSV records to w using write, and then flushes (if necessary).
|
// write_all writes multiple CSV records to w using write, and then flushes (if necessary).
|
||||||
write_all :: proc(w: ^Writer, records: [][]string) -> io.Error {
|
write_all :: proc(w: ^Writer, records: [][]string) -> io.Error {
|
||||||
for record in records {
|
for record in records {
|
||||||
err := write(w, record);
|
write(w, record) or_return;
|
||||||
if err != nil {
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return writer_flush(w);
|
return writer_flush(w);
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-30
@@ -372,8 +372,7 @@ load_from_file :: proc(filename: string, options := Options{}, allocator := cont
|
|||||||
defer delete(data);
|
defer delete(data);
|
||||||
|
|
||||||
if ok {
|
if ok {
|
||||||
img, err = load_from_slice(data, options, allocator);
|
return load_from_slice(data, options, allocator);
|
||||||
return;
|
|
||||||
} else {
|
} else {
|
||||||
img = new(Image);
|
img = new(Image);
|
||||||
return img, E_General.File_Not_Found;
|
return img, E_General.File_Not_Found;
|
||||||
@@ -453,10 +452,7 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a
|
|||||||
}
|
}
|
||||||
seen_ihdr = true;
|
seen_ihdr = true;
|
||||||
|
|
||||||
header, err = read_header(ctx);
|
header = read_header(ctx) or_return;
|
||||||
if err != nil {
|
|
||||||
return img, err;
|
|
||||||
}
|
|
||||||
|
|
||||||
if .Paletted in header.color_type {
|
if .Paletted in header.color_type {
|
||||||
// Color type 3
|
// Color type 3
|
||||||
@@ -506,10 +502,7 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a
|
|||||||
return img, E_PNG.PLTE_Encountered_Unexpectedly;
|
return img, E_PNG.PLTE_Encountered_Unexpectedly;
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err = read_chunk(ctx);
|
c = read_chunk(ctx) or_return;
|
||||||
if err != nil {
|
|
||||||
return img, err;
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.header.length % 3 != 0 || c.header.length > 768 {
|
if c.header.length % 3 != 0 || c.header.length > 768 {
|
||||||
return img, E_PNG.PLTE_Invalid_Length;
|
return img, E_PNG.PLTE_Invalid_Length;
|
||||||
@@ -540,10 +533,7 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a
|
|||||||
|
|
||||||
next := ch.type;
|
next := ch.type;
|
||||||
for next == .IDAT {
|
for next == .IDAT {
|
||||||
c, err = read_chunk(ctx);
|
c = read_chunk(ctx) or_return;
|
||||||
if err != nil {
|
|
||||||
return img, err;
|
|
||||||
}
|
|
||||||
|
|
||||||
bytes.buffer_write(&idat_b, c.data);
|
bytes.buffer_write(&idat_b, c.data);
|
||||||
idat_length += c.header.length;
|
idat_length += c.header.length;
|
||||||
@@ -560,19 +550,13 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a
|
|||||||
}
|
}
|
||||||
seen_idat = true;
|
seen_idat = true;
|
||||||
case .IEND:
|
case .IEND:
|
||||||
c, err = read_chunk(ctx);
|
c = read_chunk(ctx) or_return;
|
||||||
if err != nil {
|
|
||||||
return img, err;
|
|
||||||
}
|
|
||||||
seen_iend = true;
|
seen_iend = true;
|
||||||
case .bKGD:
|
case .bKGD:
|
||||||
|
|
||||||
// TODO: Make sure that 16-bit bKGD + tRNS chunks return u16 instead of u16be
|
// TODO: Make sure that 16-bit bKGD + tRNS chunks return u16 instead of u16be
|
||||||
|
|
||||||
c, err = read_chunk(ctx);
|
c = read_chunk(ctx) or_return;
|
||||||
if err != nil {
|
|
||||||
return img, err;
|
|
||||||
}
|
|
||||||
seen_bkgd = true;
|
seen_bkgd = true;
|
||||||
if .return_metadata in options {
|
if .return_metadata in options {
|
||||||
append(&info.chunks, c);
|
append(&info.chunks, c);
|
||||||
@@ -604,10 +588,7 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a
|
|||||||
img.background = [3]u16{u16(col[0]), u16(col[1]), u16(col[2])};
|
img.background = [3]u16{u16(col[0]), u16(col[1]), u16(col[2])};
|
||||||
}
|
}
|
||||||
case .tRNS:
|
case .tRNS:
|
||||||
c, err = read_chunk(ctx);
|
c = read_chunk(ctx) or_return;
|
||||||
if err != nil {
|
|
||||||
return img, err;
|
|
||||||
}
|
|
||||||
|
|
||||||
if .Alpha in info.header.color_type {
|
if .Alpha in info.header.color_type {
|
||||||
return img, E_PNG.TRNS_Encountered_Unexpectedly;
|
return img, E_PNG.TRNS_Encountered_Unexpectedly;
|
||||||
@@ -646,10 +627,8 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a
|
|||||||
return img, E_PNG.PNG_Does_Not_Adhere_to_Spec;
|
return img, E_PNG.PNG_Does_Not_Adhere_to_Spec;
|
||||||
case:
|
case:
|
||||||
// Unhandled type
|
// Unhandled type
|
||||||
c, err = read_chunk(ctx);
|
c = read_chunk(ctx) or_return;
|
||||||
if err != nil {
|
|
||||||
return img, err;
|
|
||||||
}
|
|
||||||
if .return_metadata in options {
|
if .return_metadata in options {
|
||||||
// NOTE: Chunk cata is currently allocated on the temp allocator.
|
// NOTE: Chunk cata is currently allocated on the temp allocator.
|
||||||
append(&info.chunks, c);
|
append(&info.chunks, c);
|
||||||
|
|||||||
@@ -298,10 +298,7 @@ _glob :: proc(dir, pattern: string, matches: ^[dynamic]string) -> (m: [dynamic]s
|
|||||||
|
|
||||||
for fi in fis {
|
for fi in fis {
|
||||||
n := fi.name;
|
n := fi.name;
|
||||||
matched, err := match(pattern, n);
|
matched := match(pattern, n) or_return;
|
||||||
if err != nil {
|
|
||||||
return m, err;
|
|
||||||
}
|
|
||||||
if matched {
|
if matched {
|
||||||
append(&m, join(dir, n));
|
append(&m, join(dir, n));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ _Recursive_Mutex :: struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_recursive_mutex_lock :: proc(m: ^Recursive_Mutex) {
|
_recursive_mutex_lock :: proc(m: ^Recursive_Mutex) {
|
||||||
tid := runtime.current_thread_id();
|
tid := _current_thread_id();
|
||||||
if tid != m.impl.owner {
|
if tid != m.impl.owner {
|
||||||
mutex_lock(&m.impl.mutex);
|
mutex_lock(&m.impl.mutex);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user